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 if (_status
!= SwitchedOut
&& _status
!= Idle
) {
161 assert(system
->getMemoryMode() == Enums::timing
);
163 // Delete the old event if it existed.
165 if (fetchEvent
->scheduled())
166 fetchEvent
->deschedule();
171 fetchEvent
= new FetchEvent(this, nextCycle());
174 changeState(SimObject::Running
);
178 TimingSimpleCPU::switchOut()
180 assert(status() == Running
|| status() == Idle
);
181 _status
= SwitchedOut
;
182 numCycles
+= tickToCycles(curTick
- previousTick
);
184 // If we've been scheduled to resume but are then told to switch out,
185 // we'll need to cancel it.
186 if (fetchEvent
&& fetchEvent
->scheduled())
187 fetchEvent
->deschedule();
192 TimingSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
194 BaseCPU::takeOverFrom(oldCPU
, &icachePort
, &dcachePort
);
196 // if any of this CPU's ThreadContexts are active, mark the CPU as
197 // running and schedule its tick event.
198 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
199 ThreadContext
*tc
= threadContexts
[i
];
200 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
206 if (_status
!= Running
) {
209 assert(threadContexts
.size() == 1);
210 cpuId
= tc
->readCpuId();
211 previousTick
= curTick
;
216 TimingSimpleCPU::activateContext(int thread_num
, int delay
)
218 assert(thread_num
== 0);
221 assert(_status
== Idle
);
226 // kick things off by initiating the fetch of the next instruction
227 fetchEvent
= new FetchEvent(this, nextCycle(curTick
+ ticks(delay
)));
232 TimingSimpleCPU::suspendContext(int 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");
296 #ifndef DOXYGEN_SHOULD_SKIP_THIS
300 TimingSimpleCPU::read(Addr addr
, Twin64_t
&data
, unsigned flags
);
304 TimingSimpleCPU::read(Addr addr
, Twin32_t
&data
, unsigned flags
);
308 TimingSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
312 TimingSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
316 TimingSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
320 TimingSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
322 #endif //DOXYGEN_SHOULD_SKIP_THIS
326 TimingSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
328 return read(addr
, *(uint64_t*)&data
, flags
);
333 TimingSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
335 return read(addr
, *(uint32_t*)&data
, flags
);
341 TimingSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
343 return read(addr
, (uint32_t&)data
, flags
);
349 TimingSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
352 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
353 cpuId
, /* thread ID */ 0);
356 traceData
->setAddr(req
->getVaddr());
359 // translate to physical address
360 Fault fault
= thread
->translateDataWriteReq(req
);
362 // Now do the access.
363 if (fault
== NoFault
) {
364 MemCmd cmd
= MemCmd::WriteReq
; // default
365 bool do_access
= true; // flag to suppress cache access
367 if (req
->isLocked()) {
368 cmd
= MemCmd::StoreCondReq
;
369 do_access
= TheISA::handleLockedWrite(thread
, req
);
370 } else if (req
->isSwap()) {
371 cmd
= MemCmd::SwapReq
;
372 if (req
->isCondSwap()) {
374 req
->setExtraData(*res
);
378 // Note: need to allocate dcache_pkt even if do_access is
379 // false, as it's used unconditionally to call completeAcc().
380 assert(dcache_pkt
== NULL
);
381 dcache_pkt
= new Packet(req
, cmd
, Packet::Broadcast
);
382 dcache_pkt
->allocate();
383 dcache_pkt
->set(data
);
386 if (req
->isMmapedIpr()) {
388 dcache_pkt
->set(htog(data
));
389 delay
= TheISA::handleIprWrite(thread
->getTC(), dcache_pkt
);
390 new IprEvent(dcache_pkt
, this, nextCycle(curTick
+ delay
));
391 _status
= DcacheWaitResponse
;
393 } else if (!dcachePort
.sendTiming(dcache_pkt
)) {
394 _status
= DcacheRetry
;
396 _status
= DcacheWaitResponse
;
397 // memory system takes ownership of packet
401 // This will need a new way to tell if it's hooked up to a cache or not.
402 if (req
->isUncacheable())
403 recordEvent("Uncached Write");
409 // If the write needs to have a fault on the access, consider calling
410 // changeStatus() and changing it to "bad addr write" or something.
415 #ifndef DOXYGEN_SHOULD_SKIP_THIS
418 TimingSimpleCPU::write(Twin32_t data
, Addr addr
,
419 unsigned flags
, uint64_t *res
);
423 TimingSimpleCPU::write(Twin64_t data
, Addr addr
,
424 unsigned flags
, uint64_t *res
);
428 TimingSimpleCPU::write(uint64_t data
, Addr addr
,
429 unsigned flags
, uint64_t *res
);
433 TimingSimpleCPU::write(uint32_t data
, Addr addr
,
434 unsigned flags
, uint64_t *res
);
438 TimingSimpleCPU::write(uint16_t data
, Addr addr
,
439 unsigned flags
, uint64_t *res
);
443 TimingSimpleCPU::write(uint8_t data
, Addr addr
,
444 unsigned flags
, uint64_t *res
);
446 #endif //DOXYGEN_SHOULD_SKIP_THIS
450 TimingSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
452 return write(*(uint64_t*)&data
, addr
, flags
, res
);
457 TimingSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
459 return write(*(uint32_t*)&data
, addr
, flags
, res
);
465 TimingSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
467 return write((uint32_t)data
, addr
, flags
, res
);
472 TimingSimpleCPU::fetch()
474 if (!curStaticInst
|| !curStaticInst
->isDelayedCommit())
475 checkForInterrupts();
477 Request
*ifetch_req
= new Request();
478 ifetch_req
->setThreadContext(cpuId
, /* thread ID */ 0);
479 Fault fault
= setupFetchRequest(ifetch_req
);
481 ifetch_pkt
= new Packet(ifetch_req
, MemCmd::ReadReq
, Packet::Broadcast
);
482 ifetch_pkt
->dataStatic(&inst
);
484 if (fault
== NoFault
) {
485 if (!icachePort
.sendTiming(ifetch_pkt
)) {
486 // Need to wait for retry
487 _status
= IcacheRetry
;
489 // Need to wait for cache to respond
490 _status
= IcacheWaitResponse
;
491 // ownership of packet transferred to memory system
497 // fetch fault: advance directly to next instruction (fault handler)
501 numCycles
+= tickToCycles(curTick
- previousTick
);
502 previousTick
= curTick
;
507 TimingSimpleCPU::advanceInst(Fault fault
)
511 if (_status
== Running
) {
512 // kick off fetch of next instruction... callback from icache
513 // response will cause that instruction to be executed,
514 // keeping the CPU running.
521 TimingSimpleCPU::completeIfetch(PacketPtr pkt
)
523 // received a response from the icache: execute the received
525 assert(!pkt
->isError());
526 assert(_status
== IcacheWaitResponse
);
530 numCycles
+= tickToCycles(curTick
- previousTick
);
531 previousTick
= curTick
;
533 if (getState() == SimObject::Draining
) {
542 if (curStaticInst
->isMemRef() && !curStaticInst
->isDataPrefetch()) {
543 // load or store: just send to dcache
544 Fault fault
= curStaticInst
->initiateAcc(this, traceData
);
545 if (_status
!= Running
) {
546 // instruction will complete in dcache response callback
547 assert(_status
== DcacheWaitResponse
|| _status
== DcacheRetry
);
548 assert(fault
== NoFault
);
550 if (fault
== NoFault
) {
551 // early fail on store conditional: complete now
552 assert(dcache_pkt
!= NULL
);
553 fault
= curStaticInst
->completeAcc(dcache_pkt
, this,
555 delete dcache_pkt
->req
;
559 // keep an instruction count
560 if (fault
== NoFault
)
562 } else if (traceData
) {
563 // If there was a fault, we shouldn't trace this instruction.
569 // @todo remove me after debugging with legion done
570 if (curStaticInst
&& (!curStaticInst
->isMicroop() ||
571 curStaticInst
->isFirstMicroop()))
576 // non-memory instruction: execute completely now
577 Fault fault
= curStaticInst
->execute(this, traceData
);
579 // keep an instruction count
580 if (fault
== NoFault
)
582 else if (traceData
) {
583 // If there was a fault, we shouldn't trace this instruction.
589 // @todo remove me after debugging with legion done
590 if (curStaticInst
&& (!curStaticInst
->isMicroop() ||
591 curStaticInst
->isFirstMicroop()))
601 TimingSimpleCPU::IcachePort::ITickEvent::process()
603 cpu
->completeIfetch(pkt
);
607 TimingSimpleCPU::IcachePort::recvTiming(PacketPtr pkt
)
609 if (pkt
->isResponse() && !pkt
->wasNacked()) {
610 // delay processing of returned data until next CPU clock edge
611 Tick next_tick
= cpu
->nextCycle(curTick
);
613 if (next_tick
== curTick
)
614 cpu
->completeIfetch(pkt
);
616 tickEvent
.schedule(pkt
, next_tick
);
620 else if (pkt
->wasNacked()) {
621 assert(cpu
->_status
== IcacheWaitResponse
);
623 if (!sendTiming(pkt
)) {
624 cpu
->_status
= IcacheRetry
;
625 cpu
->ifetch_pkt
= pkt
;
628 //Snooping a Coherence Request, do nothing
633 TimingSimpleCPU::IcachePort::recvRetry()
635 // we shouldn't get a retry unless we have a packet that we're
636 // waiting to transmit
637 assert(cpu
->ifetch_pkt
!= NULL
);
638 assert(cpu
->_status
== IcacheRetry
);
639 PacketPtr tmp
= cpu
->ifetch_pkt
;
640 if (sendTiming(tmp
)) {
641 cpu
->_status
= IcacheWaitResponse
;
642 cpu
->ifetch_pkt
= NULL
;
647 TimingSimpleCPU::completeDataAccess(PacketPtr pkt
)
649 // received a response from the dcache: complete the load or store
651 assert(!pkt
->isError());
652 assert(_status
== DcacheWaitResponse
);
655 numCycles
+= tickToCycles(curTick
- previousTick
);
656 previousTick
= curTick
;
658 Fault fault
= curStaticInst
->completeAcc(pkt
, this, traceData
);
660 // keep an instruction count
661 if (fault
== NoFault
)
663 else if (traceData
) {
664 // If there was a fault, we shouldn't trace this instruction.
669 if (pkt
->isRead() && pkt
->isLocked()) {
670 TheISA::handleLockedRead(thread
, pkt
->req
);
678 if (getState() == SimObject::Draining
) {
690 TimingSimpleCPU::completeDrain()
692 DPRINTF(Config
, "Done draining\n");
693 changeState(SimObject::Drained
);
694 drainEvent
->process();
698 TimingSimpleCPU::DcachePort::setPeer(Port
*port
)
703 // Update the ThreadContext's memory ports (Functional/Virtual
705 cpu
->tcBase()->connectMemPorts();
710 TimingSimpleCPU::DcachePort::recvTiming(PacketPtr pkt
)
712 if (pkt
->isResponse() && !pkt
->wasNacked()) {
713 // delay processing of returned data until next CPU clock edge
714 Tick next_tick
= cpu
->nextCycle(curTick
);
716 if (next_tick
== curTick
)
717 cpu
->completeDataAccess(pkt
);
719 tickEvent
.schedule(pkt
, next_tick
);
723 else if (pkt
->wasNacked()) {
724 assert(cpu
->_status
== DcacheWaitResponse
);
726 if (!sendTiming(pkt
)) {
727 cpu
->_status
= DcacheRetry
;
728 cpu
->dcache_pkt
= pkt
;
731 //Snooping a Coherence Request, do nothing
736 TimingSimpleCPU::DcachePort::DTickEvent::process()
738 cpu
->completeDataAccess(pkt
);
742 TimingSimpleCPU::DcachePort::recvRetry()
744 // we shouldn't get a retry unless we have a packet that we're
745 // waiting to transmit
746 assert(cpu
->dcache_pkt
!= NULL
);
747 assert(cpu
->_status
== DcacheRetry
);
748 PacketPtr tmp
= cpu
->dcache_pkt
;
749 if (sendTiming(tmp
)) {
750 cpu
->_status
= DcacheWaitResponse
;
751 // memory system takes ownership of packet
752 cpu
->dcache_pkt
= NULL
;
756 TimingSimpleCPU::IprEvent::IprEvent(Packet
*_pkt
, TimingSimpleCPU
*_cpu
, Tick t
)
757 : Event(&mainEventQueue
), pkt(_pkt
), cpu(_cpu
)
763 TimingSimpleCPU::IprEvent::process()
765 cpu
->completeDataAccess(pkt
);
769 TimingSimpleCPU::IprEvent::description()
771 return "Timing Simple CPU Delay IPR event";
775 ////////////////////////////////////////////////////////////////////////
777 // TimingSimpleCPU Simulation Object
780 TimingSimpleCPUParams::create()
782 TimingSimpleCPU::Params
*params
= new TimingSimpleCPU::Params();
784 params
->numberOfThreads
= 1;
785 params
->max_insts_any_thread
= max_insts_any_thread
;
786 params
->max_insts_all_threads
= max_insts_all_threads
;
787 params
->max_loads_any_thread
= max_loads_any_thread
;
788 params
->max_loads_all_threads
= max_loads_all_threads
;
789 params
->progress_interval
= progress_interval
;
790 params
->deferRegistration
= defer_registration
;
791 params
->clock
= clock
;
792 params
->phase
= phase
;
793 params
->functionTrace
= function_trace
;
794 params
->functionTraceStart
= function_trace_start
;
795 params
->system
= system
;
796 params
->cpu_id
= cpu_id
;
797 params
->tracer
= tracer
;
802 params
->profile
= profile
;
803 params
->do_quiesce
= do_quiesce
;
804 params
->do_checkpoint_insts
= do_checkpoint_insts
;
805 params
->do_statistics_insts
= do_statistics_insts
;
807 if (workload
.size() != 1)
808 panic("only one workload allowed");
809 params
->process
= workload
[0];
812 TimingSimpleCPU
*cpu
= new TimingSimpleCPU(params
);