RubyPort.cc (11025:4872dbdea907) RubyPort.cc (11111:6da33e720481)
1/*
2 * Copyright (c) 2012-2013 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) 2009 Advanced Micro Devices, Inc.
15 * Copyright (c) 2011 Mark D. Hill and David A. Wood
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#include "cpu/testers/rubytest/RubyTester.hh"
43#include "debug/Config.hh"
44#include "debug/Drain.hh"
45#include "debug/Ruby.hh"
46#include "mem/protocol/AccessPermission.hh"
47#include "mem/ruby/slicc_interface/AbstractController.hh"
48#include "mem/ruby/system/RubyPort.hh"
49#include "mem/simple_mem.hh"
50#include "sim/full_system.hh"
51#include "sim/system.hh"
52
53RubyPort::RubyPort(const Params *p)
54 : MemObject(p), m_ruby_system(p->ruby_system), m_version(p->version),
55 m_controller(NULL), m_mandatory_q_ptr(NULL),
56 m_usingRubyTester(p->using_ruby_tester), system(p->system),
57 pioMasterPort(csprintf("%s.pio-master-port", name()), this),
58 pioSlavePort(csprintf("%s.pio-slave-port", name()), this),
59 memMasterPort(csprintf("%s.mem-master-port", name()), this),
60 memSlavePort(csprintf("%s-mem-slave-port", name()), this,
61 p->ruby_system->getAccessBackingStore(), -1),
62 gotAddrRanges(p->port_master_connection_count)
63{
64 assert(m_version != -1);
65
66 // create the slave ports based on the number of connected ports
67 for (size_t i = 0; i < p->port_slave_connection_count; ++i) {
68 slave_ports.push_back(new MemSlavePort(csprintf("%s.slave%d", name(),
69 i), this, p->ruby_system->getAccessBackingStore(), i));
70 }
71
72 // create the master ports based on the number of connected ports
73 for (size_t i = 0; i < p->port_master_connection_count; ++i) {
74 master_ports.push_back(new PioMasterPort(csprintf("%s.master%d",
75 name(), i), this));
76 }
77}
78
79void
80RubyPort::init()
81{
82 assert(m_controller != NULL);
83 m_mandatory_q_ptr = m_controller->getMandatoryQueue();
1/*
2 * Copyright (c) 2012-2013 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) 2009 Advanced Micro Devices, Inc.
15 * Copyright (c) 2011 Mark D. Hill and David A. Wood
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#include "cpu/testers/rubytest/RubyTester.hh"
43#include "debug/Config.hh"
44#include "debug/Drain.hh"
45#include "debug/Ruby.hh"
46#include "mem/protocol/AccessPermission.hh"
47#include "mem/ruby/slicc_interface/AbstractController.hh"
48#include "mem/ruby/system/RubyPort.hh"
49#include "mem/simple_mem.hh"
50#include "sim/full_system.hh"
51#include "sim/system.hh"
52
53RubyPort::RubyPort(const Params *p)
54 : MemObject(p), m_ruby_system(p->ruby_system), m_version(p->version),
55 m_controller(NULL), m_mandatory_q_ptr(NULL),
56 m_usingRubyTester(p->using_ruby_tester), system(p->system),
57 pioMasterPort(csprintf("%s.pio-master-port", name()), this),
58 pioSlavePort(csprintf("%s.pio-slave-port", name()), this),
59 memMasterPort(csprintf("%s.mem-master-port", name()), this),
60 memSlavePort(csprintf("%s-mem-slave-port", name()), this,
61 p->ruby_system->getAccessBackingStore(), -1),
62 gotAddrRanges(p->port_master_connection_count)
63{
64 assert(m_version != -1);
65
66 // create the slave ports based on the number of connected ports
67 for (size_t i = 0; i < p->port_slave_connection_count; ++i) {
68 slave_ports.push_back(new MemSlavePort(csprintf("%s.slave%d", name(),
69 i), this, p->ruby_system->getAccessBackingStore(), i));
70 }
71
72 // create the master ports based on the number of connected ports
73 for (size_t i = 0; i < p->port_master_connection_count; ++i) {
74 master_ports.push_back(new PioMasterPort(csprintf("%s.master%d",
75 name(), i), this));
76 }
77}
78
79void
80RubyPort::init()
81{
82 assert(m_controller != NULL);
83 m_mandatory_q_ptr = m_controller->getMandatoryQueue();
84 m_mandatory_q_ptr->setSender(this);
85}
86
87BaseMasterPort &
88RubyPort::getMasterPort(const std::string &if_name, PortID idx)
89{
90 if (if_name == "mem_master_port") {
91 return memMasterPort;
92 }
93
94 if (if_name == "pio_master_port") {
95 return pioMasterPort;
96 }
97
98 // used by the x86 CPUs to connect the interrupt PIO and interrupt slave
99 // port
100 if (if_name != "master") {
101 // pass it along to our super class
102 return MemObject::getMasterPort(if_name, idx);
103 } else {
104 if (idx >= static_cast<PortID>(master_ports.size())) {
105 panic("RubyPort::getMasterPort: unknown index %d\n", idx);
106 }
107
108 return *master_ports[idx];
109 }
110}
111
112BaseSlavePort &
113RubyPort::getSlavePort(const std::string &if_name, PortID idx)
114{
115 if (if_name == "mem_slave_port") {
116 return memSlavePort;
117 }
118
119 if (if_name == "pio_slave_port")
120 return pioSlavePort;
121
122 // used by the CPUs to connect the caches to the interconnect, and
123 // for the x86 case also the interrupt master
124 if (if_name != "slave") {
125 // pass it along to our super class
126 return MemObject::getSlavePort(if_name, idx);
127 } else {
128 if (idx >= static_cast<PortID>(slave_ports.size())) {
129 panic("RubyPort::getSlavePort: unknown index %d\n", idx);
130 }
131
132 return *slave_ports[idx];
133 }
134}
135
136RubyPort::PioMasterPort::PioMasterPort(const std::string &_name,
137 RubyPort *_port)
138 : QueuedMasterPort(_name, _port, reqQueue, snoopRespQueue),
139 reqQueue(*_port, *this), snoopRespQueue(*_port, *this)
140{
141 DPRINTF(RubyPort, "Created master pioport on sequencer %s\n", _name);
142}
143
144RubyPort::PioSlavePort::PioSlavePort(const std::string &_name,
145 RubyPort *_port)
146 : QueuedSlavePort(_name, _port, queue), queue(*_port, *this)
147{
148 DPRINTF(RubyPort, "Created slave pioport on sequencer %s\n", _name);
149}
150
151RubyPort::MemMasterPort::MemMasterPort(const std::string &_name,
152 RubyPort *_port)
153 : QueuedMasterPort(_name, _port, reqQueue, snoopRespQueue),
154 reqQueue(*_port, *this), snoopRespQueue(*_port, *this)
155{
156 DPRINTF(RubyPort, "Created master memport on ruby sequencer %s\n", _name);
157}
158
159RubyPort::MemSlavePort::MemSlavePort(const std::string &_name, RubyPort *_port,
160 bool _access_backing_store, PortID id)
161 : QueuedSlavePort(_name, _port, queue, id), queue(*_port, *this),
162 access_backing_store(_access_backing_store)
163{
164 DPRINTF(RubyPort, "Created slave memport on ruby sequencer %s\n", _name);
165}
166
167bool
168RubyPort::PioMasterPort::recvTimingResp(PacketPtr pkt)
169{
170 RubyPort *rp = static_cast<RubyPort *>(&owner);
171 DPRINTF(RubyPort, "Response for address: 0x%#x\n", pkt->getAddr());
172
173 // send next cycle
174 rp->pioSlavePort.schedTimingResp(
175 pkt, curTick() + rp->m_ruby_system->clockPeriod());
176 return true;
177}
178
179bool RubyPort::MemMasterPort::recvTimingResp(PacketPtr pkt)
180{
181 // got a response from a device
182 assert(pkt->isResponse());
183
184 // First we must retrieve the request port from the sender State
185 RubyPort::SenderState *senderState =
186 safe_cast<RubyPort::SenderState *>(pkt->popSenderState());
187 MemSlavePort *port = senderState->port;
188 assert(port != NULL);
189 delete senderState;
190
191 // In FS mode, ruby memory will receive pio responses from devices
192 // and it must forward these responses back to the particular CPU.
193 DPRINTF(RubyPort, "Pio response for address %#x, going to %s\n",
194 pkt->getAddr(), port->name());
195
196 // attempt to send the response in the next cycle
197 RubyPort *rp = static_cast<RubyPort *>(&owner);
198 port->schedTimingResp(pkt, curTick() + rp->m_ruby_system->clockPeriod());
199
200 return true;
201}
202
203bool
204RubyPort::PioSlavePort::recvTimingReq(PacketPtr pkt)
205{
206 RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
207
208 for (size_t i = 0; i < ruby_port->master_ports.size(); ++i) {
209 AddrRangeList l = ruby_port->master_ports[i]->getAddrRanges();
210 for (auto it = l.begin(); it != l.end(); ++it) {
211 if (it->contains(pkt->getAddr())) {
212 // generally it is not safe to assume success here as
213 // the port could be blocked
214 bool M5_VAR_USED success =
215 ruby_port->master_ports[i]->sendTimingReq(pkt);
216 assert(success);
217 return true;
218 }
219 }
220 }
221 panic("Should never reach here!\n");
222}
223
224bool
225RubyPort::MemSlavePort::recvTimingReq(PacketPtr pkt)
226{
227 DPRINTF(RubyPort, "Timing request for address %#x on port %d\n",
228 pkt->getAddr(), id);
229 RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
230
231 if (pkt->memInhibitAsserted())
232 panic("RubyPort should never see an inhibited request\n");
233
234 // Check for pio requests and directly send them to the dedicated
235 // pio port.
236 if (!isPhysMemAddress(pkt->getAddr())) {
237 assert(ruby_port->memMasterPort.isConnected());
238 DPRINTF(RubyPort, "Request address %#x assumed to be a pio address\n",
239 pkt->getAddr());
240
241 // Save the port in the sender state object to be used later to
242 // route the response
243 pkt->pushSenderState(new SenderState(this));
244
245 // send next cycle
246 RubySystem *rs = ruby_port->m_ruby_system;
247 ruby_port->memMasterPort.schedTimingReq(pkt,
248 curTick() + rs->clockPeriod());
249 return true;
250 }
251
252 assert(getOffset(pkt->getAddr()) + pkt->getSize() <=
253 RubySystem::getBlockSizeBytes());
254
255 // Submit the ruby request
256 RequestStatus requestStatus = ruby_port->makeRequest(pkt);
257
258 // If the request successfully issued then we should return true.
259 // Otherwise, we need to tell the port to retry at a later point
260 // and return false.
261 if (requestStatus == RequestStatus_Issued) {
262 // Save the port in the sender state object to be used later to
263 // route the response
264 pkt->pushSenderState(new SenderState(this));
265
266 DPRINTF(RubyPort, "Request %s 0x%x issued\n", pkt->cmdString(),
267 pkt->getAddr());
268 return true;
269 }
270
271 //
272 // Unless one is using the ruby tester, record the stalled M5 port for
273 // later retry when the sequencer becomes free.
274 //
275 if (!ruby_port->m_usingRubyTester) {
276 ruby_port->addToRetryList(this);
277 }
278
279 DPRINTF(RubyPort, "Request for address %#x did not issued because %s\n",
280 pkt->getAddr(), RequestStatus_to_string(requestStatus));
281
282 return false;
283}
284
285void
286RubyPort::MemSlavePort::recvFunctional(PacketPtr pkt)
287{
288 DPRINTF(RubyPort, "Functional access for address: %#x\n", pkt->getAddr());
289
290 RubyPort *rp M5_VAR_USED = static_cast<RubyPort *>(&owner);
291 RubySystem *rs = rp->m_ruby_system;
292
293 // Check for pio requests and directly send them to the dedicated
294 // pio port.
295 if (!isPhysMemAddress(pkt->getAddr())) {
296 assert(rp->memMasterPort.isConnected());
297 DPRINTF(RubyPort, "Pio Request for address: 0x%#x\n", pkt->getAddr());
298 panic("RubyPort::PioMasterPort::recvFunctional() not implemented!\n");
299 }
300
301 assert(pkt->getAddr() + pkt->getSize() <=
302 makeLineAddress(pkt->getAddr()) + RubySystem::getBlockSizeBytes());
303
304 if (access_backing_store) {
305 // The attached physmem contains the official version of data.
306 // The following command performs the real functional access.
307 // This line should be removed once Ruby supplies the official version
308 // of data.
309 rs->getPhysMem()->functionalAccess(pkt);
310 } else {
311 bool accessSucceeded = false;
312 bool needsResponse = pkt->needsResponse();
313
314 // Do the functional access on ruby memory
315 if (pkt->isRead()) {
316 accessSucceeded = rs->functionalRead(pkt);
317 } else if (pkt->isWrite()) {
318 accessSucceeded = rs->functionalWrite(pkt);
319 } else {
320 panic("Unsupported functional command %s\n", pkt->cmdString());
321 }
322
323 // Unless the requester explicitly said otherwise, generate an error if
324 // the functional request failed
325 if (!accessSucceeded && !pkt->suppressFuncError()) {
326 fatal("Ruby functional %s failed for address %#x\n",
327 pkt->isWrite() ? "write" : "read", pkt->getAddr());
328 }
329
330 // turn packet around to go back to requester if response expected
331 if (needsResponse) {
332 pkt->setFunctionalResponseStatus(accessSucceeded);
333 }
334
335 DPRINTF(RubyPort, "Functional access %s!\n",
336 accessSucceeded ? "successful":"failed");
337 }
338}
339
340void
341RubyPort::ruby_hit_callback(PacketPtr pkt)
342{
343 DPRINTF(RubyPort, "Hit callback for %s 0x%x\n", pkt->cmdString(),
344 pkt->getAddr());
345
346 // The packet was destined for memory and has not yet been turned
347 // into a response
348 assert(system->isMemAddr(pkt->getAddr()));
349 assert(pkt->isRequest());
350
351 // First we must retrieve the request port from the sender State
352 RubyPort::SenderState *senderState =
353 safe_cast<RubyPort::SenderState *>(pkt->popSenderState());
354 MemSlavePort *port = senderState->port;
355 assert(port != NULL);
356 delete senderState;
357
358 port->hitCallback(pkt);
359
360 //
361 // If we had to stall the MemSlavePorts, wake them up because the sequencer
362 // likely has free resources now.
363 //
364 if (!retryList.empty()) {
365 //
366 // Record the current list of ports to retry on a temporary list before
367 // calling sendRetry on those ports. sendRetry will cause an
368 // immediate retry, which may result in the ports being put back on the
369 // list. Therefore we want to clear the retryList before calling
370 // sendRetry.
371 //
372 std::vector<MemSlavePort *> curRetryList(retryList);
373
374 retryList.clear();
375
376 for (auto i = curRetryList.begin(); i != curRetryList.end(); ++i) {
377 DPRINTF(RubyPort,
378 "Sequencer may now be free. SendRetry to port %s\n",
379 (*i)->name());
380 (*i)->sendRetryReq();
381 }
382 }
383
384 testDrainComplete();
385}
386
387void
388RubyPort::testDrainComplete()
389{
390 //If we weren't able to drain before, we might be able to now.
391 if (drainState() == DrainState::Draining) {
392 unsigned int drainCount = outstandingCount();
393 DPRINTF(Drain, "Drain count: %u\n", drainCount);
394 if (drainCount == 0) {
395 DPRINTF(Drain, "RubyPort done draining, signaling drain done\n");
396 signalDrainDone();
397 }
398 }
399}
400
401DrainState
402RubyPort::drain()
403{
404 if (isDeadlockEventScheduled()) {
405 descheduleDeadlockEvent();
406 }
407
408 //
409 // If the RubyPort is not empty, then it needs to clear all outstanding
410 // requests before it should call signalDrainDone()
411 //
412 DPRINTF(Config, "outstanding count %d\n", outstandingCount());
413 if (outstandingCount() > 0) {
414 DPRINTF(Drain, "RubyPort not drained\n");
415 return DrainState::Draining;
416 } else {
417 return DrainState::Drained;
418 }
419}
420
421void
422RubyPort::MemSlavePort::hitCallback(PacketPtr pkt)
423{
424 bool needsResponse = pkt->needsResponse();
425
426 // Unless specified at configuraiton, all responses except failed SC
427 // and Flush operations access M5 physical memory.
428 bool accessPhysMem = access_backing_store;
429
430 if (pkt->isLLSC()) {
431 if (pkt->isWrite()) {
432 if (pkt->req->getExtraData() != 0) {
433 //
434 // Successful SC packets convert to normal writes
435 //
436 pkt->convertScToWrite();
437 } else {
438 //
439 // Failed SC packets don't access physical memory and thus
440 // the RubyPort itself must convert it to a response.
441 //
442 accessPhysMem = false;
443 }
444 } else {
445 //
446 // All LL packets convert to normal loads so that M5 PhysMem does
447 // not lock the blocks.
448 //
449 pkt->convertLlToRead();
450 }
451 }
452
453 // Flush requests don't access physical memory
454 if (pkt->isFlush()) {
455 accessPhysMem = false;
456 }
457
458 DPRINTF(RubyPort, "Hit callback needs response %d\n", needsResponse);
459
460 RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
461 RubySystem *rs = ruby_port->m_ruby_system;
462 if (accessPhysMem) {
463 rs->getPhysMem()->access(pkt);
464 } else if (needsResponse) {
465 pkt->makeResponse();
466 }
467
468 // turn packet around to go back to requester if response expected
469 if (needsResponse) {
470 DPRINTF(RubyPort, "Sending packet back over port\n");
471 // Send a response in the same cycle. There is no need to delay the
472 // response because the response latency is already incurred in the
473 // Ruby protocol.
474 schedTimingResp(pkt, curTick());
475 } else {
476 delete pkt;
477 }
478
479 DPRINTF(RubyPort, "Hit callback done!\n");
480}
481
482AddrRangeList
483RubyPort::PioSlavePort::getAddrRanges() const
484{
485 // at the moment the assumption is that the master does not care
486 AddrRangeList ranges;
487 RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
488
489 for (size_t i = 0; i < ruby_port->master_ports.size(); ++i) {
490 ranges.splice(ranges.begin(),
491 ruby_port->master_ports[i]->getAddrRanges());
492 }
493 for (const auto M5_VAR_USED &r : ranges)
494 DPRINTF(RubyPort, "%s\n", r.to_string());
495 return ranges;
496}
497
498bool
499RubyPort::MemSlavePort::isPhysMemAddress(Addr addr) const
500{
501 RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
502 return ruby_port->system->isMemAddr(addr);
503}
504
505void
506RubyPort::ruby_eviction_callback(Addr address)
507{
508 DPRINTF(RubyPort, "Sending invalidations.\n");
509 // This request is deleted in the stack-allocated packet destructor
510 // when this function exits
511 // TODO: should this really be using funcMasterId?
512 RequestPtr req = new Request(address, 0, 0, Request::funcMasterId);
513 // Use a single packet to signal all snooping ports of the invalidation.
514 // This assumes that snooping ports do NOT modify the packet/request
515 Packet pkt(req, MemCmd::InvalidateReq);
516 for (CpuPortIter p = slave_ports.begin(); p != slave_ports.end(); ++p) {
517 // check if the connected master port is snooping
518 if ((*p)->isSnooping()) {
519 // send as a snoop request
520 (*p)->sendTimingSnoopReq(&pkt);
521 }
522 }
523}
524
525void
526RubyPort::PioMasterPort::recvRangeChange()
527{
528 RubyPort &r = static_cast<RubyPort &>(owner);
529 r.gotAddrRanges--;
530 if (r.gotAddrRanges == 0 && FullSystem) {
531 r.pioSlavePort.sendRangeChange();
532 }
533}
84}
85
86BaseMasterPort &
87RubyPort::getMasterPort(const std::string &if_name, PortID idx)
88{
89 if (if_name == "mem_master_port") {
90 return memMasterPort;
91 }
92
93 if (if_name == "pio_master_port") {
94 return pioMasterPort;
95 }
96
97 // used by the x86 CPUs to connect the interrupt PIO and interrupt slave
98 // port
99 if (if_name != "master") {
100 // pass it along to our super class
101 return MemObject::getMasterPort(if_name, idx);
102 } else {
103 if (idx >= static_cast<PortID>(master_ports.size())) {
104 panic("RubyPort::getMasterPort: unknown index %d\n", idx);
105 }
106
107 return *master_ports[idx];
108 }
109}
110
111BaseSlavePort &
112RubyPort::getSlavePort(const std::string &if_name, PortID idx)
113{
114 if (if_name == "mem_slave_port") {
115 return memSlavePort;
116 }
117
118 if (if_name == "pio_slave_port")
119 return pioSlavePort;
120
121 // used by the CPUs to connect the caches to the interconnect, and
122 // for the x86 case also the interrupt master
123 if (if_name != "slave") {
124 // pass it along to our super class
125 return MemObject::getSlavePort(if_name, idx);
126 } else {
127 if (idx >= static_cast<PortID>(slave_ports.size())) {
128 panic("RubyPort::getSlavePort: unknown index %d\n", idx);
129 }
130
131 return *slave_ports[idx];
132 }
133}
134
135RubyPort::PioMasterPort::PioMasterPort(const std::string &_name,
136 RubyPort *_port)
137 : QueuedMasterPort(_name, _port, reqQueue, snoopRespQueue),
138 reqQueue(*_port, *this), snoopRespQueue(*_port, *this)
139{
140 DPRINTF(RubyPort, "Created master pioport on sequencer %s\n", _name);
141}
142
143RubyPort::PioSlavePort::PioSlavePort(const std::string &_name,
144 RubyPort *_port)
145 : QueuedSlavePort(_name, _port, queue), queue(*_port, *this)
146{
147 DPRINTF(RubyPort, "Created slave pioport on sequencer %s\n", _name);
148}
149
150RubyPort::MemMasterPort::MemMasterPort(const std::string &_name,
151 RubyPort *_port)
152 : QueuedMasterPort(_name, _port, reqQueue, snoopRespQueue),
153 reqQueue(*_port, *this), snoopRespQueue(*_port, *this)
154{
155 DPRINTF(RubyPort, "Created master memport on ruby sequencer %s\n", _name);
156}
157
158RubyPort::MemSlavePort::MemSlavePort(const std::string &_name, RubyPort *_port,
159 bool _access_backing_store, PortID id)
160 : QueuedSlavePort(_name, _port, queue, id), queue(*_port, *this),
161 access_backing_store(_access_backing_store)
162{
163 DPRINTF(RubyPort, "Created slave memport on ruby sequencer %s\n", _name);
164}
165
166bool
167RubyPort::PioMasterPort::recvTimingResp(PacketPtr pkt)
168{
169 RubyPort *rp = static_cast<RubyPort *>(&owner);
170 DPRINTF(RubyPort, "Response for address: 0x%#x\n", pkt->getAddr());
171
172 // send next cycle
173 rp->pioSlavePort.schedTimingResp(
174 pkt, curTick() + rp->m_ruby_system->clockPeriod());
175 return true;
176}
177
178bool RubyPort::MemMasterPort::recvTimingResp(PacketPtr pkt)
179{
180 // got a response from a device
181 assert(pkt->isResponse());
182
183 // First we must retrieve the request port from the sender State
184 RubyPort::SenderState *senderState =
185 safe_cast<RubyPort::SenderState *>(pkt->popSenderState());
186 MemSlavePort *port = senderState->port;
187 assert(port != NULL);
188 delete senderState;
189
190 // In FS mode, ruby memory will receive pio responses from devices
191 // and it must forward these responses back to the particular CPU.
192 DPRINTF(RubyPort, "Pio response for address %#x, going to %s\n",
193 pkt->getAddr(), port->name());
194
195 // attempt to send the response in the next cycle
196 RubyPort *rp = static_cast<RubyPort *>(&owner);
197 port->schedTimingResp(pkt, curTick() + rp->m_ruby_system->clockPeriod());
198
199 return true;
200}
201
202bool
203RubyPort::PioSlavePort::recvTimingReq(PacketPtr pkt)
204{
205 RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
206
207 for (size_t i = 0; i < ruby_port->master_ports.size(); ++i) {
208 AddrRangeList l = ruby_port->master_ports[i]->getAddrRanges();
209 for (auto it = l.begin(); it != l.end(); ++it) {
210 if (it->contains(pkt->getAddr())) {
211 // generally it is not safe to assume success here as
212 // the port could be blocked
213 bool M5_VAR_USED success =
214 ruby_port->master_ports[i]->sendTimingReq(pkt);
215 assert(success);
216 return true;
217 }
218 }
219 }
220 panic("Should never reach here!\n");
221}
222
223bool
224RubyPort::MemSlavePort::recvTimingReq(PacketPtr pkt)
225{
226 DPRINTF(RubyPort, "Timing request for address %#x on port %d\n",
227 pkt->getAddr(), id);
228 RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
229
230 if (pkt->memInhibitAsserted())
231 panic("RubyPort should never see an inhibited request\n");
232
233 // Check for pio requests and directly send them to the dedicated
234 // pio port.
235 if (!isPhysMemAddress(pkt->getAddr())) {
236 assert(ruby_port->memMasterPort.isConnected());
237 DPRINTF(RubyPort, "Request address %#x assumed to be a pio address\n",
238 pkt->getAddr());
239
240 // Save the port in the sender state object to be used later to
241 // route the response
242 pkt->pushSenderState(new SenderState(this));
243
244 // send next cycle
245 RubySystem *rs = ruby_port->m_ruby_system;
246 ruby_port->memMasterPort.schedTimingReq(pkt,
247 curTick() + rs->clockPeriod());
248 return true;
249 }
250
251 assert(getOffset(pkt->getAddr()) + pkt->getSize() <=
252 RubySystem::getBlockSizeBytes());
253
254 // Submit the ruby request
255 RequestStatus requestStatus = ruby_port->makeRequest(pkt);
256
257 // If the request successfully issued then we should return true.
258 // Otherwise, we need to tell the port to retry at a later point
259 // and return false.
260 if (requestStatus == RequestStatus_Issued) {
261 // Save the port in the sender state object to be used later to
262 // route the response
263 pkt->pushSenderState(new SenderState(this));
264
265 DPRINTF(RubyPort, "Request %s 0x%x issued\n", pkt->cmdString(),
266 pkt->getAddr());
267 return true;
268 }
269
270 //
271 // Unless one is using the ruby tester, record the stalled M5 port for
272 // later retry when the sequencer becomes free.
273 //
274 if (!ruby_port->m_usingRubyTester) {
275 ruby_port->addToRetryList(this);
276 }
277
278 DPRINTF(RubyPort, "Request for address %#x did not issued because %s\n",
279 pkt->getAddr(), RequestStatus_to_string(requestStatus));
280
281 return false;
282}
283
284void
285RubyPort::MemSlavePort::recvFunctional(PacketPtr pkt)
286{
287 DPRINTF(RubyPort, "Functional access for address: %#x\n", pkt->getAddr());
288
289 RubyPort *rp M5_VAR_USED = static_cast<RubyPort *>(&owner);
290 RubySystem *rs = rp->m_ruby_system;
291
292 // Check for pio requests and directly send them to the dedicated
293 // pio port.
294 if (!isPhysMemAddress(pkt->getAddr())) {
295 assert(rp->memMasterPort.isConnected());
296 DPRINTF(RubyPort, "Pio Request for address: 0x%#x\n", pkt->getAddr());
297 panic("RubyPort::PioMasterPort::recvFunctional() not implemented!\n");
298 }
299
300 assert(pkt->getAddr() + pkt->getSize() <=
301 makeLineAddress(pkt->getAddr()) + RubySystem::getBlockSizeBytes());
302
303 if (access_backing_store) {
304 // The attached physmem contains the official version of data.
305 // The following command performs the real functional access.
306 // This line should be removed once Ruby supplies the official version
307 // of data.
308 rs->getPhysMem()->functionalAccess(pkt);
309 } else {
310 bool accessSucceeded = false;
311 bool needsResponse = pkt->needsResponse();
312
313 // Do the functional access on ruby memory
314 if (pkt->isRead()) {
315 accessSucceeded = rs->functionalRead(pkt);
316 } else if (pkt->isWrite()) {
317 accessSucceeded = rs->functionalWrite(pkt);
318 } else {
319 panic("Unsupported functional command %s\n", pkt->cmdString());
320 }
321
322 // Unless the requester explicitly said otherwise, generate an error if
323 // the functional request failed
324 if (!accessSucceeded && !pkt->suppressFuncError()) {
325 fatal("Ruby functional %s failed for address %#x\n",
326 pkt->isWrite() ? "write" : "read", pkt->getAddr());
327 }
328
329 // turn packet around to go back to requester if response expected
330 if (needsResponse) {
331 pkt->setFunctionalResponseStatus(accessSucceeded);
332 }
333
334 DPRINTF(RubyPort, "Functional access %s!\n",
335 accessSucceeded ? "successful":"failed");
336 }
337}
338
339void
340RubyPort::ruby_hit_callback(PacketPtr pkt)
341{
342 DPRINTF(RubyPort, "Hit callback for %s 0x%x\n", pkt->cmdString(),
343 pkt->getAddr());
344
345 // The packet was destined for memory and has not yet been turned
346 // into a response
347 assert(system->isMemAddr(pkt->getAddr()));
348 assert(pkt->isRequest());
349
350 // First we must retrieve the request port from the sender State
351 RubyPort::SenderState *senderState =
352 safe_cast<RubyPort::SenderState *>(pkt->popSenderState());
353 MemSlavePort *port = senderState->port;
354 assert(port != NULL);
355 delete senderState;
356
357 port->hitCallback(pkt);
358
359 //
360 // If we had to stall the MemSlavePorts, wake them up because the sequencer
361 // likely has free resources now.
362 //
363 if (!retryList.empty()) {
364 //
365 // Record the current list of ports to retry on a temporary list before
366 // calling sendRetry on those ports. sendRetry will cause an
367 // immediate retry, which may result in the ports being put back on the
368 // list. Therefore we want to clear the retryList before calling
369 // sendRetry.
370 //
371 std::vector<MemSlavePort *> curRetryList(retryList);
372
373 retryList.clear();
374
375 for (auto i = curRetryList.begin(); i != curRetryList.end(); ++i) {
376 DPRINTF(RubyPort,
377 "Sequencer may now be free. SendRetry to port %s\n",
378 (*i)->name());
379 (*i)->sendRetryReq();
380 }
381 }
382
383 testDrainComplete();
384}
385
386void
387RubyPort::testDrainComplete()
388{
389 //If we weren't able to drain before, we might be able to now.
390 if (drainState() == DrainState::Draining) {
391 unsigned int drainCount = outstandingCount();
392 DPRINTF(Drain, "Drain count: %u\n", drainCount);
393 if (drainCount == 0) {
394 DPRINTF(Drain, "RubyPort done draining, signaling drain done\n");
395 signalDrainDone();
396 }
397 }
398}
399
400DrainState
401RubyPort::drain()
402{
403 if (isDeadlockEventScheduled()) {
404 descheduleDeadlockEvent();
405 }
406
407 //
408 // If the RubyPort is not empty, then it needs to clear all outstanding
409 // requests before it should call signalDrainDone()
410 //
411 DPRINTF(Config, "outstanding count %d\n", outstandingCount());
412 if (outstandingCount() > 0) {
413 DPRINTF(Drain, "RubyPort not drained\n");
414 return DrainState::Draining;
415 } else {
416 return DrainState::Drained;
417 }
418}
419
420void
421RubyPort::MemSlavePort::hitCallback(PacketPtr pkt)
422{
423 bool needsResponse = pkt->needsResponse();
424
425 // Unless specified at configuraiton, all responses except failed SC
426 // and Flush operations access M5 physical memory.
427 bool accessPhysMem = access_backing_store;
428
429 if (pkt->isLLSC()) {
430 if (pkt->isWrite()) {
431 if (pkt->req->getExtraData() != 0) {
432 //
433 // Successful SC packets convert to normal writes
434 //
435 pkt->convertScToWrite();
436 } else {
437 //
438 // Failed SC packets don't access physical memory and thus
439 // the RubyPort itself must convert it to a response.
440 //
441 accessPhysMem = false;
442 }
443 } else {
444 //
445 // All LL packets convert to normal loads so that M5 PhysMem does
446 // not lock the blocks.
447 //
448 pkt->convertLlToRead();
449 }
450 }
451
452 // Flush requests don't access physical memory
453 if (pkt->isFlush()) {
454 accessPhysMem = false;
455 }
456
457 DPRINTF(RubyPort, "Hit callback needs response %d\n", needsResponse);
458
459 RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
460 RubySystem *rs = ruby_port->m_ruby_system;
461 if (accessPhysMem) {
462 rs->getPhysMem()->access(pkt);
463 } else if (needsResponse) {
464 pkt->makeResponse();
465 }
466
467 // turn packet around to go back to requester if response expected
468 if (needsResponse) {
469 DPRINTF(RubyPort, "Sending packet back over port\n");
470 // Send a response in the same cycle. There is no need to delay the
471 // response because the response latency is already incurred in the
472 // Ruby protocol.
473 schedTimingResp(pkt, curTick());
474 } else {
475 delete pkt;
476 }
477
478 DPRINTF(RubyPort, "Hit callback done!\n");
479}
480
481AddrRangeList
482RubyPort::PioSlavePort::getAddrRanges() const
483{
484 // at the moment the assumption is that the master does not care
485 AddrRangeList ranges;
486 RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
487
488 for (size_t i = 0; i < ruby_port->master_ports.size(); ++i) {
489 ranges.splice(ranges.begin(),
490 ruby_port->master_ports[i]->getAddrRanges());
491 }
492 for (const auto M5_VAR_USED &r : ranges)
493 DPRINTF(RubyPort, "%s\n", r.to_string());
494 return ranges;
495}
496
497bool
498RubyPort::MemSlavePort::isPhysMemAddress(Addr addr) const
499{
500 RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
501 return ruby_port->system->isMemAddr(addr);
502}
503
504void
505RubyPort::ruby_eviction_callback(Addr address)
506{
507 DPRINTF(RubyPort, "Sending invalidations.\n");
508 // This request is deleted in the stack-allocated packet destructor
509 // when this function exits
510 // TODO: should this really be using funcMasterId?
511 RequestPtr req = new Request(address, 0, 0, Request::funcMasterId);
512 // Use a single packet to signal all snooping ports of the invalidation.
513 // This assumes that snooping ports do NOT modify the packet/request
514 Packet pkt(req, MemCmd::InvalidateReq);
515 for (CpuPortIter p = slave_ports.begin(); p != slave_ports.end(); ++p) {
516 // check if the connected master port is snooping
517 if ((*p)->isSnooping()) {
518 // send as a snoop request
519 (*p)->sendTimingSnoopReq(&pkt);
520 }
521 }
522}
523
524void
525RubyPort::PioMasterPort::recvRangeChange()
526{
527 RubyPort &r = static_cast<RubyPort &>(owner);
528 r.gotAddrRanges--;
529 if (r.gotAddrRanges == 0 && FullSystem) {
530 r.pioSlavePort.sendRangeChange();
531 }
532}