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()
61 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
62 ThreadContext
*tc
= threadContexts
[i
];
64 // initialize CPU, including PC
65 TheISA::initCPU(tc
, tc
->readCpuId());
71 TimingSimpleCPU::CpuPort::recvAtomic(PacketPtr pkt
)
73 panic("TimingSimpleCPU doesn't expect recvAtomic callback!");
78 TimingSimpleCPU::CpuPort::recvFunctional(PacketPtr pkt
)
80 //No internal storage to update, jusst return
85 TimingSimpleCPU::CpuPort::recvStatusChange(Status status
)
87 if (status
== RangeChange
) {
88 if (!snoopRangeSent
) {
89 snoopRangeSent
= true;
90 sendStatusChange(Port::RangeChange
);
95 panic("TimingSimpleCPU doesn't expect recvStatusChange callback!");
100 TimingSimpleCPU::CpuPort::TickEvent::schedule(PacketPtr _pkt
, Tick t
)
106 TimingSimpleCPU::TimingSimpleCPU(Params
*p
)
107 : BaseSimpleCPU(p
), icachePort(this, p
->clock
), dcachePort(this, p
->clock
)
111 icachePort
.snoopRangeSent
= false;
112 dcachePort
.snoopRangeSent
= false;
114 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 // Delete the old event if it existed.
166 if (fetchEvent
->scheduled())
167 fetchEvent
->deschedule();
172 fetchEvent
= new FetchEvent(this, nextCycle());
175 changeState(SimObject::Running
);
179 TimingSimpleCPU::switchOut()
181 assert(status() == Running
|| status() == Idle
);
182 _status
= SwitchedOut
;
183 numCycles
+= tickToCycles(curTick
- previousTick
);
185 // If we've been scheduled to resume but are then told to switch out,
186 // we'll need to cancel it.
187 if (fetchEvent
&& fetchEvent
->scheduled())
188 fetchEvent
->deschedule();
193 TimingSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
195 BaseCPU::takeOverFrom(oldCPU
, &icachePort
, &dcachePort
);
197 // if any of this CPU's ThreadContexts are active, mark the CPU as
198 // running and schedule its tick event.
199 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
200 ThreadContext
*tc
= threadContexts
[i
];
201 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
207 if (_status
!= Running
) {
210 assert(threadContexts
.size() == 1);
211 cpuId
= tc
->readCpuId();
212 previousTick
= curTick
;
217 TimingSimpleCPU::activateContext(int thread_num
, int delay
)
219 DPRINTF(SimpleCPU
, "ActivateContext %d (%d cycles)\n", thread_num
, delay
);
221 assert(thread_num
== 0);
224 assert(_status
== Idle
);
229 // kick things off by initiating the fetch of the next instruction
230 fetchEvent
= new FetchEvent(this, nextCycle(curTick
+ ticks(delay
)));
235 TimingSimpleCPU::suspendContext(int thread_num
)
237 DPRINTF(SimpleCPU
, "SuspendContext %d\n", thread_num
);
239 assert(thread_num
== 0);
242 assert(_status
== Running
);
244 // just change status to Idle... if status != Running,
245 // completeInst() will not initiate fetch of next instruction.
254 TimingSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
257 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
258 cpuId
, /* thread ID */ 0);
261 traceData
->setAddr(req
->getVaddr());
264 // translate to physical address
265 Fault fault
= thread
->translateDataReadReq(req
);
267 // Now do the access.
268 if (fault
== NoFault
) {
272 MemCmd::LoadLockedReq
: MemCmd::ReadReq
),
274 pkt
->dataDynamic
<T
>(new T
);
276 if (req
->isMmapedIpr()) {
278 delay
= TheISA::handleIprRead(thread
->getTC(), pkt
);
279 new IprEvent(pkt
, this, nextCycle(curTick
+ delay
));
280 _status
= DcacheWaitResponse
;
282 } else if (!dcachePort
.sendTiming(pkt
)) {
283 _status
= DcacheRetry
;
286 _status
= DcacheWaitResponse
;
287 // memory system takes ownership of packet
291 // This will need a new way to tell if it has a dcache attached.
292 if (req
->isUncacheable())
293 recordEvent("Uncached Read");
302 TimingSimpleCPU::translateDataReadAddr(Addr vaddr
, Addr
&paddr
,
303 int size
, unsigned flags
)
306 new Request(0, vaddr
, size
, flags
, thread
->readPC(), cpuId
, 0);
309 traceData
->setAddr(vaddr
);
312 Fault fault
= thread
->translateDataWriteReq(req
);
314 if (fault
== NoFault
)
315 paddr
= req
->getPaddr();
321 #ifndef DOXYGEN_SHOULD_SKIP_THIS
325 TimingSimpleCPU::read(Addr addr
, Twin64_t
&data
, unsigned flags
);
329 TimingSimpleCPU::read(Addr addr
, Twin32_t
&data
, unsigned flags
);
333 TimingSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
337 TimingSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
341 TimingSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
345 TimingSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
347 #endif //DOXYGEN_SHOULD_SKIP_THIS
351 TimingSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
353 return read(addr
, *(uint64_t*)&data
, flags
);
358 TimingSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
360 return read(addr
, *(uint32_t*)&data
, flags
);
366 TimingSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
368 return read(addr
, (uint32_t&)data
, flags
);
374 TimingSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
377 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
378 cpuId
, /* thread ID */ 0);
381 traceData
->setAddr(req
->getVaddr());
384 // translate to physical address
385 Fault fault
= thread
->translateDataWriteReq(req
);
387 // Now do the access.
388 if (fault
== NoFault
) {
389 MemCmd cmd
= MemCmd::WriteReq
; // default
390 bool do_access
= true; // flag to suppress cache access
392 if (req
->isLocked()) {
393 cmd
= MemCmd::StoreCondReq
;
394 do_access
= TheISA::handleLockedWrite(thread
, req
);
395 } else if (req
->isSwap()) {
396 cmd
= MemCmd::SwapReq
;
397 if (req
->isCondSwap()) {
399 req
->setExtraData(*res
);
403 // Note: need to allocate dcache_pkt even if do_access is
404 // false, as it's used unconditionally to call completeAcc().
405 assert(dcache_pkt
== NULL
);
406 dcache_pkt
= new Packet(req
, cmd
, Packet::Broadcast
);
407 dcache_pkt
->allocate();
408 dcache_pkt
->set(data
);
411 if (req
->isMmapedIpr()) {
413 dcache_pkt
->set(htog(data
));
414 delay
= TheISA::handleIprWrite(thread
->getTC(), dcache_pkt
);
415 new IprEvent(dcache_pkt
, this, nextCycle(curTick
+ delay
));
416 _status
= DcacheWaitResponse
;
418 } else if (!dcachePort
.sendTiming(dcache_pkt
)) {
419 _status
= DcacheRetry
;
421 _status
= DcacheWaitResponse
;
422 // memory system takes ownership of packet
426 // This will need a new way to tell if it's hooked up to a cache or not.
427 if (req
->isUncacheable())
428 recordEvent("Uncached Write");
434 // If the write needs to have a fault on the access, consider calling
435 // changeStatus() and changing it to "bad addr write" or something.
440 TimingSimpleCPU::translateDataWriteAddr(Addr vaddr
, Addr
&paddr
,
441 int size
, unsigned flags
)
444 new Request(0, vaddr
, size
, flags
, thread
->readPC(), cpuId
, 0);
447 traceData
->setAddr(vaddr
);
450 Fault fault
= thread
->translateDataWriteReq(req
);
452 if (fault
== NoFault
)
453 paddr
= req
->getPaddr();
460 #ifndef DOXYGEN_SHOULD_SKIP_THIS
463 TimingSimpleCPU::write(Twin32_t data
, Addr addr
,
464 unsigned flags
, uint64_t *res
);
468 TimingSimpleCPU::write(Twin64_t data
, Addr addr
,
469 unsigned flags
, uint64_t *res
);
473 TimingSimpleCPU::write(uint64_t data
, Addr addr
,
474 unsigned flags
, uint64_t *res
);
478 TimingSimpleCPU::write(uint32_t data
, Addr addr
,
479 unsigned flags
, uint64_t *res
);
483 TimingSimpleCPU::write(uint16_t data
, Addr addr
,
484 unsigned flags
, uint64_t *res
);
488 TimingSimpleCPU::write(uint8_t data
, Addr addr
,
489 unsigned flags
, uint64_t *res
);
491 #endif //DOXYGEN_SHOULD_SKIP_THIS
495 TimingSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
497 return write(*(uint64_t*)&data
, addr
, flags
, res
);
502 TimingSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
504 return write(*(uint32_t*)&data
, addr
, flags
, res
);
510 TimingSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
512 return write((uint32_t)data
, addr
, flags
, res
);
517 TimingSimpleCPU::fetch()
519 DPRINTF(SimpleCPU
, "Fetch\n");
521 if (!curStaticInst
|| !curStaticInst
->isDelayedCommit())
522 checkForInterrupts();
524 Request
*ifetch_req
= new Request();
525 ifetch_req
->setThreadContext(cpuId
, /* thread ID */ 0);
526 Fault fault
= setupFetchRequest(ifetch_req
);
528 ifetch_pkt
= new Packet(ifetch_req
, MemCmd::ReadReq
, Packet::Broadcast
);
529 ifetch_pkt
->dataStatic(&inst
);
531 if (fault
== NoFault
) {
532 if (!icachePort
.sendTiming(ifetch_pkt
)) {
533 // Need to wait for retry
534 _status
= IcacheRetry
;
536 // Need to wait for cache to respond
537 _status
= IcacheWaitResponse
;
538 // ownership of packet transferred to memory system
544 // fetch fault: advance directly to next instruction (fault handler)
548 numCycles
+= tickToCycles(curTick
- previousTick
);
549 previousTick
= curTick
;
554 TimingSimpleCPU::advanceInst(Fault fault
)
558 if (_status
== Running
) {
559 // kick off fetch of next instruction... callback from icache
560 // response will cause that instruction to be executed,
561 // keeping the CPU running.
568 TimingSimpleCPU::completeIfetch(PacketPtr pkt
)
570 DPRINTF(SimpleCPU
, "Complete ICache Fetch\n");
572 // received a response from the icache: execute the received
574 assert(!pkt
->isError());
575 assert(_status
== IcacheWaitResponse
);
579 numCycles
+= tickToCycles(curTick
- previousTick
);
580 previousTick
= curTick
;
582 if (getState() == SimObject::Draining
) {
591 if (curStaticInst
->isMemRef() && !curStaticInst
->isDataPrefetch()) {
592 // load or store: just send to dcache
593 Fault fault
= curStaticInst
->initiateAcc(this, traceData
);
594 if (_status
!= Running
) {
595 // instruction will complete in dcache response callback
596 assert(_status
== DcacheWaitResponse
|| _status
== DcacheRetry
);
597 assert(fault
== NoFault
);
599 if (fault
== NoFault
) {
600 // early fail on store conditional: complete now
601 assert(dcache_pkt
!= NULL
);
602 fault
= curStaticInst
->completeAcc(dcache_pkt
, this,
604 delete dcache_pkt
->req
;
608 // keep an instruction count
609 if (fault
== NoFault
)
611 } else if (traceData
) {
612 // If there was a fault, we shouldn't trace this instruction.
618 // @todo remove me after debugging with legion done
619 if (curStaticInst
&& (!curStaticInst
->isMicroop() ||
620 curStaticInst
->isFirstMicroop()))
625 // non-memory instruction: execute completely now
626 Fault fault
= curStaticInst
->execute(this, traceData
);
628 // keep an instruction count
629 if (fault
== NoFault
)
631 else if (traceData
) {
632 // If there was a fault, we shouldn't trace this instruction.
638 // @todo remove me after debugging with legion done
639 if (curStaticInst
&& (!curStaticInst
->isMicroop() ||
640 curStaticInst
->isFirstMicroop()))
650 TimingSimpleCPU::IcachePort::ITickEvent::process()
652 cpu
->completeIfetch(pkt
);
656 TimingSimpleCPU::IcachePort::recvTiming(PacketPtr pkt
)
658 if (pkt
->isResponse() && !pkt
->wasNacked()) {
659 // delay processing of returned data until next CPU clock edge
660 Tick next_tick
= cpu
->nextCycle(curTick
);
662 if (next_tick
== curTick
)
663 cpu
->completeIfetch(pkt
);
665 tickEvent
.schedule(pkt
, next_tick
);
669 else if (pkt
->wasNacked()) {
670 assert(cpu
->_status
== IcacheWaitResponse
);
672 if (!sendTiming(pkt
)) {
673 cpu
->_status
= IcacheRetry
;
674 cpu
->ifetch_pkt
= pkt
;
677 //Snooping a Coherence Request, do nothing
682 TimingSimpleCPU::IcachePort::recvRetry()
684 // we shouldn't get a retry unless we have a packet that we're
685 // waiting to transmit
686 assert(cpu
->ifetch_pkt
!= NULL
);
687 assert(cpu
->_status
== IcacheRetry
);
688 PacketPtr tmp
= cpu
->ifetch_pkt
;
689 if (sendTiming(tmp
)) {
690 cpu
->_status
= IcacheWaitResponse
;
691 cpu
->ifetch_pkt
= NULL
;
696 TimingSimpleCPU::completeDataAccess(PacketPtr pkt
)
698 // received a response from the dcache: complete the load or store
700 assert(!pkt
->isError());
701 assert(_status
== DcacheWaitResponse
);
704 numCycles
+= tickToCycles(curTick
- previousTick
);
705 previousTick
= curTick
;
707 Fault fault
= curStaticInst
->completeAcc(pkt
, this, traceData
);
709 // keep an instruction count
710 if (fault
== NoFault
)
712 else if (traceData
) {
713 // If there was a fault, we shouldn't trace this instruction.
718 if (pkt
->isRead() && pkt
->isLocked()) {
719 TheISA::handleLockedRead(thread
, pkt
->req
);
727 if (getState() == SimObject::Draining
) {
739 TimingSimpleCPU::completeDrain()
741 DPRINTF(Config
, "Done draining\n");
742 changeState(SimObject::Drained
);
743 drainEvent
->process();
747 TimingSimpleCPU::DcachePort::setPeer(Port
*port
)
752 // Update the ThreadContext's memory ports (Functional/Virtual
754 cpu
->tcBase()->connectMemPorts();
759 TimingSimpleCPU::DcachePort::recvTiming(PacketPtr pkt
)
761 if (pkt
->isResponse() && !pkt
->wasNacked()) {
762 // delay processing of returned data until next CPU clock edge
763 Tick next_tick
= cpu
->nextCycle(curTick
);
765 if (next_tick
== curTick
)
766 cpu
->completeDataAccess(pkt
);
768 tickEvent
.schedule(pkt
, next_tick
);
772 else if (pkt
->wasNacked()) {
773 assert(cpu
->_status
== DcacheWaitResponse
);
775 if (!sendTiming(pkt
)) {
776 cpu
->_status
= DcacheRetry
;
777 cpu
->dcache_pkt
= pkt
;
780 //Snooping a Coherence Request, do nothing
785 TimingSimpleCPU::DcachePort::DTickEvent::process()
787 cpu
->completeDataAccess(pkt
);
791 TimingSimpleCPU::DcachePort::recvRetry()
793 // we shouldn't get a retry unless we have a packet that we're
794 // waiting to transmit
795 assert(cpu
->dcache_pkt
!= NULL
);
796 assert(cpu
->_status
== DcacheRetry
);
797 PacketPtr tmp
= cpu
->dcache_pkt
;
798 if (sendTiming(tmp
)) {
799 cpu
->_status
= DcacheWaitResponse
;
800 // memory system takes ownership of packet
801 cpu
->dcache_pkt
= NULL
;
805 TimingSimpleCPU::IprEvent::IprEvent(Packet
*_pkt
, TimingSimpleCPU
*_cpu
, Tick t
)
806 : Event(&mainEventQueue
), pkt(_pkt
), cpu(_cpu
)
812 TimingSimpleCPU::IprEvent::process()
814 cpu
->completeDataAccess(pkt
);
818 TimingSimpleCPU::IprEvent::description()
820 return "Timing Simple CPU Delay IPR event";
824 ////////////////////////////////////////////////////////////////////////
826 // TimingSimpleCPU Simulation Object
829 TimingSimpleCPUParams::create()
831 TimingSimpleCPU::Params
*params
= new TimingSimpleCPU::Params();
833 params
->numberOfThreads
= 1;
834 params
->max_insts_any_thread
= max_insts_any_thread
;
835 params
->max_insts_all_threads
= max_insts_all_threads
;
836 params
->max_loads_any_thread
= max_loads_any_thread
;
837 params
->max_loads_all_threads
= max_loads_all_threads
;
838 params
->progress_interval
= progress_interval
;
839 params
->deferRegistration
= defer_registration
;
840 params
->clock
= clock
;
841 params
->phase
= phase
;
842 params
->functionTrace
= function_trace
;
843 params
->functionTraceStart
= function_trace_start
;
844 params
->system
= system
;
845 params
->cpu_id
= cpu_id
;
846 params
->tracer
= tracer
;
851 params
->profile
= profile
;
852 params
->do_quiesce
= do_quiesce
;
853 params
->do_checkpoint_insts
= do_checkpoint_insts
;
854 params
->do_statistics_insts
= do_statistics_insts
;
856 if (workload
.size() != 1)
857 panic("only one workload allowed");
858 params
->process
= workload
[0];
861 TimingSimpleCPU
*cpu
= new TimingSimpleCPU(params
);