2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Steve Reinhardt
31 #include "arch/locked_mem.hh"
32 #include "arch/mmaped_ipr.hh"
33 #include "arch/utility.hh"
34 #include "base/bigint.hh"
35 #include "cpu/exetrace.hh"
36 #include "cpu/simple/timing.hh"
37 #include "mem/packet.hh"
38 #include "mem/packet_access.hh"
39 #include "params/TimingSimpleCPU.hh"
40 #include "sim/system.hh"
43 using namespace TheISA
;
46 TimingSimpleCPU::getPort(const std::string
&if_name
, int idx
)
48 if (if_name
== "dcache_port")
50 else if (if_name
== "icache_port")
53 panic("No Such Port\n");
57 TimingSimpleCPU::init()
60 cpuId
= tc
->readCpuId();
62 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
63 ThreadContext
*tc
= threadContexts
[i
];
65 // initialize CPU, including PC
66 TheISA::initCPU(tc
, cpuId
);
72 TimingSimpleCPU::CpuPort::recvAtomic(PacketPtr pkt
)
74 panic("TimingSimpleCPU doesn't expect recvAtomic callback!");
79 TimingSimpleCPU::CpuPort::recvFunctional(PacketPtr pkt
)
81 //No internal storage to update, jusst return
86 TimingSimpleCPU::CpuPort::recvStatusChange(Status status
)
88 if (status
== RangeChange
) {
89 if (!snoopRangeSent
) {
90 snoopRangeSent
= true;
91 sendStatusChange(Port::RangeChange
);
96 panic("TimingSimpleCPU doesn't expect recvStatusChange callback!");
101 TimingSimpleCPU::CpuPort::TickEvent::schedule(PacketPtr _pkt
, Tick t
)
104 cpu
->schedule(this, t
);
107 TimingSimpleCPU::TimingSimpleCPU(TimingSimpleCPUParams
*p
)
108 : BaseSimpleCPU(p
), icachePort(this, p
->clock
), dcachePort(this, p
->clock
), fetchEvent(this)
112 icachePort
.snoopRangeSent
= false;
113 dcachePort
.snoopRangeSent
= false;
115 ifetch_pkt
= dcache_pkt
= NULL
;
118 changeState(SimObject::Running
);
122 TimingSimpleCPU::~TimingSimpleCPU()
127 TimingSimpleCPU::serialize(ostream
&os
)
129 SimObject::State so_state
= SimObject::getState();
130 SERIALIZE_ENUM(so_state
);
131 BaseSimpleCPU::serialize(os
);
135 TimingSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
137 SimObject::State so_state
;
138 UNSERIALIZE_ENUM(so_state
);
139 BaseSimpleCPU::unserialize(cp
, section
);
143 TimingSimpleCPU::drain(Event
*drain_event
)
145 // TimingSimpleCPU is ready to drain if it's not waiting for
146 // an access to complete.
147 if (_status
== Idle
|| _status
== Running
|| _status
== SwitchedOut
) {
148 changeState(SimObject::Drained
);
151 changeState(SimObject::Draining
);
152 drainEvent
= drain_event
;
158 TimingSimpleCPU::resume()
160 DPRINTF(SimpleCPU
, "Resume\n");
161 if (_status
!= SwitchedOut
&& _status
!= Idle
) {
162 assert(system
->getMemoryMode() == Enums::timing
);
164 if (fetchEvent
.scheduled())
165 deschedule(fetchEvent
);
167 schedule(fetchEvent
, nextCycle());
170 changeState(SimObject::Running
);
174 TimingSimpleCPU::switchOut()
176 assert(_status
== Running
|| _status
== Idle
);
177 _status
= SwitchedOut
;
178 numCycles
+= tickToCycles(curTick
- previousTick
);
180 // If we've been scheduled to resume but are then told to switch out,
181 // we'll need to cancel it.
182 if (fetchEvent
.scheduled())
183 deschedule(fetchEvent
);
188 TimingSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
190 BaseCPU::takeOverFrom(oldCPU
, &icachePort
, &dcachePort
);
192 // if any of this CPU's ThreadContexts are active, mark the CPU as
193 // running and schedule its tick event.
194 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
195 ThreadContext
*tc
= threadContexts
[i
];
196 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
202 if (_status
!= Running
) {
205 assert(threadContexts
.size() == 1);
206 cpuId
= tc
->readCpuId();
207 previousTick
= curTick
;
212 TimingSimpleCPU::activateContext(int thread_num
, int delay
)
214 DPRINTF(SimpleCPU
, "ActivateContext %d (%d cycles)\n", thread_num
, delay
);
216 assert(thread_num
== 0);
219 assert(_status
== Idle
);
224 // kick things off by initiating the fetch of the next instruction
225 schedule(fetchEvent
, nextCycle(curTick
+ ticks(delay
)));
230 TimingSimpleCPU::suspendContext(int thread_num
)
232 DPRINTF(SimpleCPU
, "SuspendContext %d\n", thread_num
);
234 assert(thread_num
== 0);
237 assert(_status
== Running
);
239 // just change status to Idle... if status != Running,
240 // completeInst() will not initiate fetch of next instruction.
249 TimingSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
252 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
253 cpuId
, /* thread ID */ 0);
256 traceData
->setAddr(req
->getVaddr());
259 // translate to physical address
260 Fault fault
= thread
->translateDataReadReq(req
);
262 // Now do the access.
263 if (fault
== NoFault
) {
267 MemCmd::LoadLockedReq
: MemCmd::ReadReq
),
269 pkt
->dataDynamic
<T
>(new T
);
271 if (req
->isMmapedIpr()) {
273 delay
= TheISA::handleIprRead(thread
->getTC(), pkt
);
274 new IprEvent(pkt
, this, nextCycle(curTick
+ delay
));
275 _status
= DcacheWaitResponse
;
277 } else if (!dcachePort
.sendTiming(pkt
)) {
278 _status
= DcacheRetry
;
281 _status
= DcacheWaitResponse
;
282 // memory system takes ownership of packet
286 // This will need a new way to tell if it has a dcache attached.
287 if (req
->isUncacheable())
288 recordEvent("Uncached Read");
294 traceData
->setData(data
);
300 TimingSimpleCPU::translateDataReadAddr(Addr vaddr
, Addr
&paddr
,
301 int size
, unsigned flags
)
304 new Request(0, vaddr
, size
, flags
, thread
->readPC(), cpuId
, 0);
307 traceData
->setAddr(vaddr
);
310 Fault fault
= thread
->translateDataWriteReq(req
);
312 if (fault
== NoFault
)
313 paddr
= req
->getPaddr();
319 #ifndef DOXYGEN_SHOULD_SKIP_THIS
323 TimingSimpleCPU::read(Addr addr
, Twin64_t
&data
, unsigned flags
);
327 TimingSimpleCPU::read(Addr addr
, Twin32_t
&data
, unsigned flags
);
331 TimingSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
335 TimingSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
339 TimingSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
343 TimingSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
345 #endif //DOXYGEN_SHOULD_SKIP_THIS
349 TimingSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
351 return read(addr
, *(uint64_t*)&data
, flags
);
356 TimingSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
358 return read(addr
, *(uint32_t*)&data
, flags
);
364 TimingSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
366 return read(addr
, (uint32_t&)data
, flags
);
372 TimingSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
375 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
376 cpuId
, /* thread ID */ 0);
379 traceData
->setAddr(req
->getVaddr());
382 // translate to physical address
383 Fault fault
= thread
->translateDataWriteReq(req
);
385 // Now do the access.
386 if (fault
== NoFault
) {
387 MemCmd cmd
= MemCmd::WriteReq
; // default
388 bool do_access
= true; // flag to suppress cache access
390 if (req
->isLocked()) {
391 cmd
= MemCmd::StoreCondReq
;
392 do_access
= TheISA::handleLockedWrite(thread
, req
);
393 } else if (req
->isSwap()) {
394 cmd
= MemCmd::SwapReq
;
395 if (req
->isCondSwap()) {
397 req
->setExtraData(*res
);
401 // Note: need to allocate dcache_pkt even if do_access is
402 // false, as it's used unconditionally to call completeAcc().
403 assert(dcache_pkt
== NULL
);
404 dcache_pkt
= new Packet(req
, cmd
, Packet::Broadcast
);
405 dcache_pkt
->allocate();
406 dcache_pkt
->set(data
);
409 if (req
->isMmapedIpr()) {
411 dcache_pkt
->set(htog(data
));
412 delay
= TheISA::handleIprWrite(thread
->getTC(), dcache_pkt
);
413 new IprEvent(dcache_pkt
, this, nextCycle(curTick
+ delay
));
414 _status
= DcacheWaitResponse
;
416 } else if (!dcachePort
.sendTiming(dcache_pkt
)) {
417 _status
= DcacheRetry
;
419 _status
= DcacheWaitResponse
;
420 // memory system takes ownership of packet
424 // This will need a new way to tell if it's hooked up to a cache or not.
425 if (req
->isUncacheable())
426 recordEvent("Uncached Write");
432 traceData
->setData(data
);
435 // If the write needs to have a fault on the access, consider calling
436 // changeStatus() and changing it to "bad addr write" or something.
441 TimingSimpleCPU::translateDataWriteAddr(Addr vaddr
, Addr
&paddr
,
442 int size
, unsigned flags
)
445 new Request(0, vaddr
, size
, flags
, thread
->readPC(), cpuId
, 0);
448 traceData
->setAddr(vaddr
);
451 Fault fault
= thread
->translateDataWriteReq(req
);
453 if (fault
== NoFault
)
454 paddr
= req
->getPaddr();
461 #ifndef DOXYGEN_SHOULD_SKIP_THIS
464 TimingSimpleCPU::write(Twin32_t data
, Addr addr
,
465 unsigned flags
, uint64_t *res
);
469 TimingSimpleCPU::write(Twin64_t data
, Addr addr
,
470 unsigned flags
, uint64_t *res
);
474 TimingSimpleCPU::write(uint64_t data
, Addr addr
,
475 unsigned flags
, uint64_t *res
);
479 TimingSimpleCPU::write(uint32_t data
, Addr addr
,
480 unsigned flags
, uint64_t *res
);
484 TimingSimpleCPU::write(uint16_t data
, Addr addr
,
485 unsigned flags
, uint64_t *res
);
489 TimingSimpleCPU::write(uint8_t data
, Addr addr
,
490 unsigned flags
, uint64_t *res
);
492 #endif //DOXYGEN_SHOULD_SKIP_THIS
496 TimingSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
498 return write(*(uint64_t*)&data
, addr
, flags
, res
);
503 TimingSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
505 return write(*(uint32_t*)&data
, addr
, flags
, res
);
511 TimingSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
513 return write((uint32_t)data
, addr
, flags
, res
);
518 TimingSimpleCPU::fetch()
520 DPRINTF(SimpleCPU
, "Fetch\n");
522 if (!curStaticInst
|| !curStaticInst
->isDelayedCommit())
523 checkForInterrupts();
527 bool fromRom
= isRomMicroPC(thread
->readMicroPC());
530 Request
*ifetch_req
= new Request();
531 ifetch_req
->setThreadContext(cpuId
, /* thread ID */ 0);
532 Fault fault
= setupFetchRequest(ifetch_req
);
534 ifetch_pkt
= new Packet(ifetch_req
, MemCmd::ReadReq
, Packet::Broadcast
);
535 ifetch_pkt
->dataStatic(&inst
);
537 if (fault
== NoFault
) {
538 if (!icachePort
.sendTiming(ifetch_pkt
)) {
539 // Need to wait for retry
540 _status
= IcacheRetry
;
542 // Need to wait for cache to respond
543 _status
= IcacheWaitResponse
;
544 // ownership of packet transferred to memory system
550 // fetch fault: advance directly to next instruction (fault handler)
554 _status
= IcacheWaitResponse
;
555 completeIfetch(NULL
);
558 numCycles
+= tickToCycles(curTick
- previousTick
);
559 previousTick
= curTick
;
564 TimingSimpleCPU::advanceInst(Fault fault
)
568 if (_status
== Running
) {
569 // kick off fetch of next instruction... callback from icache
570 // response will cause that instruction to be executed,
571 // keeping the CPU running.
578 TimingSimpleCPU::completeIfetch(PacketPtr pkt
)
580 DPRINTF(SimpleCPU
, "Complete ICache Fetch\n");
582 // received a response from the icache: execute the received
585 assert(!pkt
|| !pkt
->isError());
586 assert(_status
== IcacheWaitResponse
);
590 numCycles
+= tickToCycles(curTick
- previousTick
);
591 previousTick
= curTick
;
593 if (getState() == SimObject::Draining
) {
604 if (curStaticInst
->isMemRef() && !curStaticInst
->isDataPrefetch()) {
605 // load or store: just send to dcache
606 Fault fault
= curStaticInst
->initiateAcc(this, traceData
);
607 if (_status
!= Running
) {
608 // instruction will complete in dcache response callback
609 assert(_status
== DcacheWaitResponse
|| _status
== DcacheRetry
);
610 assert(fault
== NoFault
);
612 if (fault
== NoFault
) {
613 // Note that ARM can have NULL packets if the instruction gets
614 // squashed due to predication
615 // early fail on store conditional: complete now
616 assert(dcache_pkt
!= NULL
|| THE_ISA
== ARM_ISA
);
618 fault
= curStaticInst
->completeAcc(dcache_pkt
, this,
620 if (dcache_pkt
!= NULL
)
622 delete dcache_pkt
->req
;
627 // keep an instruction count
628 if (fault
== NoFault
)
630 } else if (traceData
) {
631 // If there was a fault, we shouldn't trace this instruction.
637 // @todo remove me after debugging with legion done
638 if (curStaticInst
&& (!curStaticInst
->isMicroop() ||
639 curStaticInst
->isFirstMicroop()))
644 // non-memory instruction: execute completely now
645 Fault fault
= curStaticInst
->execute(this, traceData
);
647 // keep an instruction count
648 if (fault
== NoFault
)
650 else if (traceData
) {
651 // If there was a fault, we shouldn't trace this instruction.
657 // @todo remove me after debugging with legion done
658 if (curStaticInst
&& (!curStaticInst
->isMicroop() ||
659 curStaticInst
->isFirstMicroop()))
671 TimingSimpleCPU::IcachePort::ITickEvent::process()
673 cpu
->completeIfetch(pkt
);
677 TimingSimpleCPU::IcachePort::recvTiming(PacketPtr pkt
)
679 if (pkt
->isResponse() && !pkt
->wasNacked()) {
680 // delay processing of returned data until next CPU clock edge
681 Tick next_tick
= cpu
->nextCycle(curTick
);
683 if (next_tick
== curTick
)
684 cpu
->completeIfetch(pkt
);
686 tickEvent
.schedule(pkt
, next_tick
);
690 else if (pkt
->wasNacked()) {
691 assert(cpu
->_status
== IcacheWaitResponse
);
693 if (!sendTiming(pkt
)) {
694 cpu
->_status
= IcacheRetry
;
695 cpu
->ifetch_pkt
= pkt
;
698 //Snooping a Coherence Request, do nothing
703 TimingSimpleCPU::IcachePort::recvRetry()
705 // we shouldn't get a retry unless we have a packet that we're
706 // waiting to transmit
707 assert(cpu
->ifetch_pkt
!= NULL
);
708 assert(cpu
->_status
== IcacheRetry
);
709 PacketPtr tmp
= cpu
->ifetch_pkt
;
710 if (sendTiming(tmp
)) {
711 cpu
->_status
= IcacheWaitResponse
;
712 cpu
->ifetch_pkt
= NULL
;
717 TimingSimpleCPU::completeDataAccess(PacketPtr pkt
)
719 // received a response from the dcache: complete the load or store
721 assert(!pkt
->isError());
722 assert(_status
== DcacheWaitResponse
);
725 numCycles
+= tickToCycles(curTick
- previousTick
);
726 previousTick
= curTick
;
728 Fault fault
= curStaticInst
->completeAcc(pkt
, this, traceData
);
730 // keep an instruction count
731 if (fault
== NoFault
)
733 else if (traceData
) {
734 // If there was a fault, we shouldn't trace this instruction.
739 // the locked flag may be cleared on the response packet, so check
740 // pkt->req and not pkt to see if it was a load-locked
741 if (pkt
->isRead() && pkt
->req
->isLocked()) {
742 TheISA::handleLockedRead(thread
, pkt
->req
);
750 if (getState() == SimObject::Draining
) {
762 TimingSimpleCPU::completeDrain()
764 DPRINTF(Config
, "Done draining\n");
765 changeState(SimObject::Drained
);
766 drainEvent
->process();
770 TimingSimpleCPU::DcachePort::setPeer(Port
*port
)
775 // Update the ThreadContext's memory ports (Functional/Virtual
777 cpu
->tcBase()->connectMemPorts(cpu
->tcBase());
782 TimingSimpleCPU::DcachePort::recvTiming(PacketPtr pkt
)
784 if (pkt
->isResponse() && !pkt
->wasNacked()) {
785 // delay processing of returned data until next CPU clock edge
786 Tick next_tick
= cpu
->nextCycle(curTick
);
788 if (next_tick
== curTick
)
789 cpu
->completeDataAccess(pkt
);
791 tickEvent
.schedule(pkt
, next_tick
);
795 else if (pkt
->wasNacked()) {
796 assert(cpu
->_status
== DcacheWaitResponse
);
798 if (!sendTiming(pkt
)) {
799 cpu
->_status
= DcacheRetry
;
800 cpu
->dcache_pkt
= pkt
;
803 //Snooping a Coherence Request, do nothing
808 TimingSimpleCPU::DcachePort::DTickEvent::process()
810 cpu
->completeDataAccess(pkt
);
814 TimingSimpleCPU::DcachePort::recvRetry()
816 // we shouldn't get a retry unless we have a packet that we're
817 // waiting to transmit
818 assert(cpu
->dcache_pkt
!= NULL
);
819 assert(cpu
->_status
== DcacheRetry
);
820 PacketPtr tmp
= cpu
->dcache_pkt
;
821 if (sendTiming(tmp
)) {
822 cpu
->_status
= DcacheWaitResponse
;
823 // memory system takes ownership of packet
824 cpu
->dcache_pkt
= NULL
;
828 TimingSimpleCPU::IprEvent::IprEvent(Packet
*_pkt
, TimingSimpleCPU
*_cpu
,
830 : pkt(_pkt
), cpu(_cpu
)
832 cpu
->schedule(this, t
);
836 TimingSimpleCPU::IprEvent::process()
838 cpu
->completeDataAccess(pkt
);
842 TimingSimpleCPU::IprEvent::description() const
844 return "Timing Simple CPU Delay IPR event";
849 TimingSimpleCPU::printAddr(Addr a
)
851 dcachePort
.printAddr(a
);
855 ////////////////////////////////////////////////////////////////////////
857 // TimingSimpleCPU Simulation Object
860 TimingSimpleCPUParams::create()
864 if (workload
.size() != 1)
865 panic("only one workload allowed");
867 return new TimingSimpleCPU(this);