2 * Copyright (c) 2012 ARM Limited
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.
14 * Copyright (c) 2009 Advanced Micro Devices, Inc.
15 * Copyright (c) 2011 Mark D. Hill and David A. Wood
16 * All rights reserved.
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.
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.
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 "sim/system.hh"
51 RubyPort::RubyPort(const Params
*p
)
52 : MemObject(p
), m_version(p
->version
), m_controller(NULL
),
53 m_mandatory_q_ptr(NULL
),
54 pio_port(csprintf("%s-pio-port", name()), this),
55 m_usingRubyTester(p
->using_ruby_tester
), m_request_cnt(0),
56 drainEvent(NULL
), ruby_system(p
->ruby_system
), system(p
->system
),
57 waitingOnSequencer(false), access_phys_mem(p
->access_phys_mem
)
59 assert(m_version
!= -1);
61 // create the slave ports based on the number of connected ports
62 for (size_t i
= 0; i
< p
->port_slave_connection_count
; ++i
) {
63 slave_ports
.push_back(new M5Port(csprintf("%s-slave%d", name(), i
),
64 this, ruby_system
, access_phys_mem
));
67 // create the master ports based on the number of connected ports
68 for (size_t i
= 0; i
< p
->port_master_connection_count
; ++i
) {
69 master_ports
.push_back(new PioPort(csprintf("%s-master%d", name(), i
),
77 assert(m_controller
!= NULL
);
78 m_mandatory_q_ptr
= m_controller
->getMandatoryQueue();
82 RubyPort::getMasterPort(const std::string
&if_name
, int idx
)
84 if (if_name
== "pio_port") {
88 // used by the x86 CPUs to connect the interrupt PIO and interrupt slave
90 if (if_name
!= "master") {
91 // pass it along to our super class
92 return MemObject::getMasterPort(if_name
, idx
);
94 if (idx
>= static_cast<int>(master_ports
.size())) {
95 panic("RubyPort::getMasterPort: unknown index %d\n", idx
);
98 return *master_ports
[idx
];
103 RubyPort::getSlavePort(const std::string
&if_name
, int idx
)
105 // used by the CPUs to connect the caches to the interconnect, and
106 // for the x86 case also the interrupt master
107 if (if_name
!= "slave") {
108 // pass it along to our super class
109 return MemObject::getSlavePort(if_name
, idx
);
111 if (idx
>= static_cast<int>(slave_ports
.size())) {
112 panic("RubyPort::getSlavePort: unknown index %d\n", idx
);
115 return *slave_ports
[idx
];
119 RubyPort::PioPort::PioPort(const std::string
&_name
,
121 : QueuedMasterPort(_name
, _port
, queue
), queue(*_port
, *this),
124 DPRINTF(RubyPort
, "creating master port on ruby sequencer %s\n", _name
);
127 RubyPort::M5Port::M5Port(const std::string
&_name
, RubyPort
*_port
,
128 RubySystem
*_system
, bool _access_phys_mem
)
129 : QueuedSlavePort(_name
, _port
, queue
), queue(*_port
, *this),
130 ruby_port(_port
), ruby_system(_system
),
131 _onRetryList(false), access_phys_mem(_access_phys_mem
)
133 DPRINTF(RubyPort
, "creating slave port on ruby sequencer %s\n", _name
);
137 RubyPort::M5Port::recvAtomic(PacketPtr pkt
)
139 panic("RubyPort::M5Port::recvAtomic() not implemented!\n");
145 RubyPort::PioPort::recvTimingResp(PacketPtr pkt
)
147 // In FS mode, ruby memory will receive pio responses from devices
148 // and it must forward these responses back to the particular CPU.
149 DPRINTF(RubyPort
, "Pio response for address %#x\n", pkt
->getAddr());
151 // First we must retrieve the request port from the sender State
152 RubyPort::SenderState
*senderState
=
153 safe_cast
<RubyPort::SenderState
*>(pkt
->senderState
);
154 M5Port
*port
= senderState
->port
;
155 assert(port
!= NULL
);
157 // pop the sender state from the packet
158 pkt
->senderState
= senderState
->saved
;
161 port
->sendTimingResp(pkt
);
167 RubyPort::M5Port::recvTimingReq(PacketPtr pkt
)
170 "Timing access caught for address %#x\n", pkt
->getAddr());
172 //dsm: based on SimpleTimingPort::recvTimingReq(pkt);
174 // The received packets should only be M5 requests, which should never
175 // get nacked. There used to be code to hanldle nacks here, but
176 // I'm pretty sure it didn't work correctly with the drain code,
177 // so that would need to be fixed if we ever added it back.
179 if (pkt
->memInhibitAsserted()) {
180 warn("memInhibitAsserted???");
181 // snooper will supply based on copy of packet
182 // still target's responsibility to delete packet
187 // Save the port in the sender state object to be used later to
188 // route the response
189 pkt
->senderState
= new SenderState(this, pkt
->senderState
);
191 // Check for pio requests and directly send them to the dedicated
193 if (!isPhysMemAddress(pkt
->getAddr())) {
194 assert(ruby_port
->pio_port
.isConnected());
196 "Request for address 0x%#x is assumed to be a pio request\n",
200 ruby_port
->pio_port
.schedTimingReq(pkt
,
201 curTick() + g_system_ptr
->clockPeriod());
205 assert(Address(pkt
->getAddr()).getOffset() + pkt
->getSize() <=
206 RubySystem::getBlockSizeBytes());
208 // Submit the ruby request
209 RequestStatus requestStatus
= ruby_port
->makeRequest(pkt
);
211 // If the request successfully issued then we should return true.
212 // Otherwise, we need to delete the senderStatus we just created and return
214 if (requestStatus
== RequestStatus_Issued
) {
215 DPRINTF(RubyPort
, "Request %#x issued\n", pkt
->getAddr());
220 // Unless one is using the ruby tester, record the stalled M5 port for
221 // later retry when the sequencer becomes free.
223 if (!ruby_port
->m_usingRubyTester
) {
224 ruby_port
->addToRetryList(this);
228 "Request for address %#x did not issue because %s\n",
229 pkt
->getAddr(), RequestStatus_to_string(requestStatus
));
231 SenderState
* senderState
= safe_cast
<SenderState
*>(pkt
->senderState
);
232 pkt
->senderState
= senderState
->saved
;
238 RubyPort::M5Port::recvFunctional(PacketPtr pkt
)
240 DPRINTF(RubyPort
, "Functional access caught for address %#x\n",
243 // Check for pio requests and directly send them to the dedicated
245 if (!isPhysMemAddress(pkt
->getAddr())) {
246 assert(ruby_port
->pio_port
.isConnected());
247 DPRINTF(RubyPort
, "Request for address 0x%#x is a pio request\n",
249 panic("RubyPort::PioPort::recvFunctional() not implemented!\n");
252 assert(pkt
->getAddr() + pkt
->getSize() <=
253 line_address(Address(pkt
->getAddr())).getAddress() +
254 RubySystem::getBlockSizeBytes());
256 bool accessSucceeded
= false;
257 bool needsResponse
= pkt
->needsResponse();
259 // Do the functional access on ruby memory
261 accessSucceeded
= ruby_system
->functionalRead(pkt
);
262 } else if (pkt
->isWrite()) {
263 accessSucceeded
= ruby_system
->functionalWrite(pkt
);
265 panic("RubyPort: unsupported functional command %s\n",
269 // Unless the requester explicitly said otherwise, generate an error if
270 // the functional request failed
271 if (!accessSucceeded
&& !pkt
->suppressFuncError()) {
272 fatal("Ruby functional %s failed for address %#x\n",
273 pkt
->isWrite() ? "write" : "read", pkt
->getAddr());
276 if (access_phys_mem
) {
277 // The attached physmem contains the official version of data.
278 // The following command performs the real functional access.
279 // This line should be removed once Ruby supplies the official version
281 ruby_port
->system
->getPhysMem().functionalAccess(pkt
);
284 // turn packet around to go back to requester if response expected
286 pkt
->setFunctionalResponseStatus(accessSucceeded
);
288 // @todo There should not be a reverse call since the response is
289 // communicated through the packet pointer
290 // DPRINTF(RubyPort, "Sending packet back over port\n");
291 // sendFunctional(pkt);
293 DPRINTF(RubyPort
, "Functional access %s!\n",
294 accessSucceeded
? "successful":"failed");
298 RubyPort::ruby_hit_callback(PacketPtr pkt
)
300 // Retrieve the request port from the sender State
301 RubyPort::SenderState
*senderState
=
302 safe_cast
<RubyPort::SenderState
*>(pkt
->senderState
);
303 M5Port
*port
= senderState
->port
;
304 assert(port
!= NULL
);
306 // pop the sender state from the packet
307 pkt
->senderState
= senderState
->saved
;
310 port
->hitCallback(pkt
);
313 // If we had to stall the M5Ports, wake them up because the sequencer
314 // likely has free resources now.
316 if (waitingOnSequencer
) {
318 // Record the current list of ports to retry on a temporary list before
319 // calling sendRetry on those ports. sendRetry will cause an
320 // immediate retry, which may result in the ports being put back on the
321 // list. Therefore we want to clear the retryList before calling
324 std::list
<M5Port
*> curRetryList(retryList
);
327 waitingOnSequencer
= false;
329 for (std::list
<M5Port
*>::iterator i
= curRetryList
.begin();
330 i
!= curRetryList
.end(); ++i
) {
332 "Sequencer may now be free. SendRetry to port %s\n",
334 (*i
)->onRetryList(false);
343 RubyPort::testDrainComplete()
345 //If we weren't able to drain before, we might be able to now.
346 if (drainEvent
!= NULL
) {
347 unsigned int drainCount
= outstandingCount();
348 DPRINTF(Drain
, "Drain count: %u\n", drainCount
);
349 if (drainCount
== 0) {
350 DPRINTF(Drain
, "RubyPort done draining, processing drain event\n");
351 drainEvent
->process();
352 // Clear the drain event once we're done with it.
359 RubyPort::getChildDrainCount(Event
*de
)
363 if (pio_port
.isConnected()) {
364 count
+= pio_port
.drain(de
);
365 DPRINTF(Config
, "count after pio check %d\n", count
);
368 for (CpuPortIter p
= slave_ports
.begin(); p
!= slave_ports
.end(); ++p
) {
369 count
+= (*p
)->drain(de
);
370 DPRINTF(Config
, "count after slave port check %d\n", count
);
373 for (std::vector
<PioPort
*>::iterator p
= master_ports
.begin();
374 p
!= master_ports
.end(); ++p
) {
375 count
+= (*p
)->drain(de
);
376 DPRINTF(Config
, "count after master port check %d\n", count
);
379 DPRINTF(Config
, "final count %d\n", count
);
385 RubyPort::drain(Event
*de
)
387 if (isDeadlockEventScheduled()) {
388 descheduleDeadlockEvent();
392 // If the RubyPort is not empty, then it needs to clear all outstanding
393 // requests before it should call drainEvent->process()
395 DPRINTF(Config
, "outstanding count %d\n", outstandingCount());
396 bool need_drain
= outstandingCount() > 0;
399 // Also, get the number of child ports that will also need to clear
400 // their buffered requests before they call drainEvent->process()
402 unsigned int child_drain_count
= getChildDrainCount(de
);
408 DPRINTF(Drain
, "RubyPort not drained\n");
409 changeState(SimObject::Draining
);
410 return child_drain_count
+ 1;
414 changeState(SimObject::Drained
);
415 return child_drain_count
;
419 RubyPort::M5Port::hitCallback(PacketPtr pkt
)
421 bool needsResponse
= pkt
->needsResponse();
424 // Unless specified at configuraiton, all responses except failed SC
425 // and Flush operations access M5 physical memory.
427 bool accessPhysMem
= access_phys_mem
;
430 if (pkt
->isWrite()) {
431 if (pkt
->req
->getExtraData() != 0) {
433 // Successful SC packets convert to normal writes
435 pkt
->convertScToWrite();
438 // Failed SC packets don't access physical memory and thus
439 // the RubyPort itself must convert it to a response.
441 accessPhysMem
= false;
445 // All LL packets convert to normal loads so that M5 PhysMem does
446 // not lock the blocks.
448 pkt
->convertLlToRead();
453 // Flush requests don't access physical memory
455 if (pkt
->isFlush()) {
456 accessPhysMem
= false;
459 DPRINTF(RubyPort
, "Hit callback needs response %d\n", needsResponse
);
462 ruby_port
->system
->getPhysMem().access(pkt
);
463 } else if (needsResponse
) {
467 // turn packet around to go back to requester if response expected
469 DPRINTF(RubyPort
, "Sending packet back over port\n");
471 schedTimingResp(pkt
, curTick() + g_system_ptr
->clockPeriod());
475 DPRINTF(RubyPort
, "Hit callback done!\n");
479 RubyPort::M5Port::getAddrRanges() const
481 // at the moment the assumption is that the master does not care
482 AddrRangeList ranges
;
487 RubyPort::M5Port::isPhysMemAddress(Addr addr
)
489 return ruby_port
->system
->isMemAddr(addr
);
493 RubyPort::M5Port::deviceBlockSize() const
495 return (unsigned) RubySystem::getBlockSizeBytes();
499 RubyPort::ruby_eviction_callback(const Address
& address
)
501 DPRINTF(RubyPort
, "Sending invalidations.\n");
502 // should this really be using funcMasterId?
503 Request
req(address
.getAddress(), 0, 0, Request::funcMasterId
);
504 for (CpuPortIter p
= slave_ports
.begin(); p
!= slave_ports
.end(); ++p
) {
505 // check if the connected master port is snooping
506 if ((*p
)->isSnooping()) {
507 Packet
*pkt
= new Packet(&req
, MemCmd::InvalidationReq
);
508 // send as a snoop request
509 (*p
)->sendTimingSnoopReq(pkt
);