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/utility.hh"
33 #include "cpu/exetrace.hh"
34 #include "cpu/simple/timing.hh"
35 #include "mem/packet.hh"
36 #include "mem/packet_access.hh"
37 #include "sim/builder.hh"
38 #include "sim/system.hh"
41 using namespace TheISA
;
44 TimingSimpleCPU::getPort(const std::string
&if_name
, int idx
)
46 if (if_name
== "dcache_port")
48 else if (if_name
== "icache_port")
51 panic("No Such Port\n");
55 TimingSimpleCPU::init()
59 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
60 ThreadContext
*tc
= threadContexts
[i
];
62 // initialize CPU, including PC
63 TheISA::initCPU(tc
, tc
->readCpuId());
69 TimingSimpleCPU::CpuPort::recvAtomic(PacketPtr pkt
)
71 panic("TimingSimpleCPU doesn't expect recvAtomic callback!");
76 TimingSimpleCPU::CpuPort::recvFunctional(PacketPtr pkt
)
78 //No internal storage to update, jusst return
83 TimingSimpleCPU::CpuPort::recvStatusChange(Status status
)
85 if (status
== RangeChange
) {
86 if (!snoopRangeSent
) {
87 snoopRangeSent
= true;
88 sendStatusChange(Port::RangeChange
);
93 panic("TimingSimpleCPU doesn't expect recvStatusChange callback!");
98 TimingSimpleCPU::CpuPort::TickEvent::schedule(PacketPtr _pkt
, Tick t
)
104 TimingSimpleCPU::TimingSimpleCPU(Params
*p
)
105 : BaseSimpleCPU(p
), icachePort(this, p
->clock
), dcachePort(this, p
->clock
),
110 icachePort
.snoopRangeSent
= false;
111 dcachePort
.snoopRangeSent
= false;
113 ifetch_pkt
= dcache_pkt
= NULL
;
117 changeState(SimObject::Running
);
121 TimingSimpleCPU::~TimingSimpleCPU()
126 TimingSimpleCPU::serialize(ostream
&os
)
128 SimObject::State so_state
= SimObject::getState();
129 SERIALIZE_ENUM(so_state
);
130 BaseSimpleCPU::serialize(os
);
134 TimingSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
136 SimObject::State so_state
;
137 UNSERIALIZE_ENUM(so_state
);
138 BaseSimpleCPU::unserialize(cp
, section
);
142 TimingSimpleCPU::drain(Event
*drain_event
)
144 // TimingSimpleCPU is ready to drain if it's not waiting for
145 // an access to complete.
146 if (status() == Idle
|| status() == Running
|| status() == SwitchedOut
) {
147 changeState(SimObject::Drained
);
150 changeState(SimObject::Draining
);
151 drainEvent
= drain_event
;
157 TimingSimpleCPU::resume()
159 if (_status
!= SwitchedOut
&& _status
!= Idle
) {
160 assert(system
->getMemoryMode() == System::Timing
);
162 // Delete the old event if it existed.
164 if (fetchEvent
->scheduled())
165 fetchEvent
->deschedule();
171 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
172 fetchEvent
->schedule(nextCycle());
175 changeState(SimObject::Running
);
176 previousTick
= curTick
;
180 TimingSimpleCPU::switchOut()
182 assert(status() == Running
|| status() == Idle
);
183 _status
= SwitchedOut
;
184 numCycles
+= curTick
- previousTick
;
186 // If we've been scheduled to resume but are then told to switch out,
187 // we'll need to cancel it.
188 if (fetchEvent
&& fetchEvent
->scheduled())
189 fetchEvent
->deschedule();
194 TimingSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
196 BaseCPU::takeOverFrom(oldCPU
);
198 // if any of this CPU's ThreadContexts are active, mark the CPU as
199 // running and schedule its tick event.
200 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
201 ThreadContext
*tc
= threadContexts
[i
];
202 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
208 if (_status
!= Running
) {
213 if (icachePort
.getPeer() == NULL
) {
214 peer
= oldCPU
->getPort("icache_port")->getPeer();
215 icachePort
.setPeer(peer
);
217 peer
= icachePort
.getPeer();
219 peer
->setPeer(&icachePort
);
221 if (dcachePort
.getPeer() == NULL
) {
222 peer
= oldCPU
->getPort("dcache_port")->getPeer();
223 dcachePort
.setPeer(peer
);
225 peer
= dcachePort
.getPeer();
227 peer
->setPeer(&dcachePort
);
232 TimingSimpleCPU::activateContext(int thread_num
, int delay
)
234 assert(thread_num
== 0);
237 assert(_status
== Idle
);
241 // kick things off by initiating the fetch of the next instruction
243 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
244 fetchEvent
->schedule(nextCycle(curTick
+ cycles(delay
)));
249 TimingSimpleCPU::suspendContext(int thread_num
)
251 assert(thread_num
== 0);
254 assert(_status
== Running
);
256 // just change status to Idle... if status != Running,
257 // completeInst() will not initiate fetch of next instruction.
266 TimingSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
269 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
270 cpu_id
, /* thread ID */ 0);
273 traceData
->setAddr(req
->getVaddr());
276 // translate to physical address
277 Fault fault
= thread
->translateDataReadReq(req
);
279 // Now do the access.
280 if (fault
== NoFault
) {
282 new Packet(req
, Packet::ReadReq
, Packet::Broadcast
);
283 pkt
->dataDynamic
<T
>(new T
);
285 if (!dcachePort
.sendTiming(pkt
)) {
286 _status
= DcacheRetry
;
289 _status
= DcacheWaitResponse
;
290 // memory system takes ownership of packet
297 // This will need a new way to tell if it has a dcache attached.
298 if (req
->isUncacheable())
299 recordEvent("Uncached Read");
304 #ifndef DOXYGEN_SHOULD_SKIP_THIS
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 cpu_id
, /* thread ID */ 0);
355 // translate to physical address
356 Fault fault
= thread
->translateDataWriteReq(req
);
358 // Now do the access.
359 if (fault
== NoFault
) {
360 assert(dcache_pkt
== NULL
);
361 dcache_pkt
= new Packet(req
, Packet::WriteReq
, Packet::Broadcast
);
362 dcache_pkt
->allocate();
363 dcache_pkt
->set(data
);
365 bool do_access
= true; // flag to suppress cache access
367 if (req
->isLocked()) {
368 do_access
= TheISA::handleLockedWrite(thread
, req
);
372 if (!dcachePort
.sendTiming(dcache_pkt
)) {
373 _status
= DcacheRetry
;
375 _status
= DcacheWaitResponse
;
376 // memory system takes ownership of packet
384 // This will need a new way to tell if it's hooked up to a cache or not.
385 if (req
->isUncacheable())
386 recordEvent("Uncached Write");
388 // If the write needs to have a fault on the access, consider calling
389 // changeStatus() and changing it to "bad addr write" or something.
394 #ifndef DOXYGEN_SHOULD_SKIP_THIS
397 TimingSimpleCPU::write(uint64_t data
, Addr addr
,
398 unsigned flags
, uint64_t *res
);
402 TimingSimpleCPU::write(uint32_t data
, Addr addr
,
403 unsigned flags
, uint64_t *res
);
407 TimingSimpleCPU::write(uint16_t data
, Addr addr
,
408 unsigned flags
, uint64_t *res
);
412 TimingSimpleCPU::write(uint8_t data
, Addr addr
,
413 unsigned flags
, uint64_t *res
);
415 #endif //DOXYGEN_SHOULD_SKIP_THIS
419 TimingSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
421 return write(*(uint64_t*)&data
, addr
, flags
, res
);
426 TimingSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
428 return write(*(uint32_t*)&data
, addr
, flags
, res
);
434 TimingSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
436 return write((uint32_t)data
, addr
, flags
, res
);
441 TimingSimpleCPU::fetch()
443 if (!curStaticInst
|| !curStaticInst
->isDelayedCommit())
444 checkForInterrupts();
446 Request
*ifetch_req
= new Request();
447 ifetch_req
->setThreadContext(cpu_id
, /* thread ID */ 0);
448 Fault fault
= setupFetchRequest(ifetch_req
);
450 ifetch_pkt
= new Packet(ifetch_req
, Packet::ReadReq
, Packet::Broadcast
);
451 ifetch_pkt
->dataStatic(&inst
);
453 if (fault
== NoFault
) {
454 if (!icachePort
.sendTiming(ifetch_pkt
)) {
455 // Need to wait for retry
456 _status
= IcacheRetry
;
458 // Need to wait for cache to respond
459 _status
= IcacheWaitResponse
;
460 // ownership of packet transferred to memory system
466 // fetch fault: advance directly to next instruction (fault handler)
470 numCycles
+= curTick
- previousTick
;
471 previousTick
= curTick
;
476 TimingSimpleCPU::advanceInst(Fault fault
)
480 if (_status
== Running
) {
481 // kick off fetch of next instruction... callback from icache
482 // response will cause that instruction to be executed,
483 // keeping the CPU running.
490 TimingSimpleCPU::completeIfetch(PacketPtr pkt
)
492 // received a response from the icache: execute the received
494 assert(pkt
->result
== Packet::Success
);
495 assert(_status
== IcacheWaitResponse
);
499 numCycles
+= curTick
- previousTick
;
500 previousTick
= curTick
;
502 if (getState() == SimObject::Draining
) {
511 if (curStaticInst
->isMemRef() && !curStaticInst
->isDataPrefetch()) {
512 // load or store: just send to dcache
513 Fault fault
= curStaticInst
->initiateAcc(this, traceData
);
514 if (_status
!= Running
) {
515 // instruction will complete in dcache response callback
516 assert(_status
== DcacheWaitResponse
|| _status
== DcacheRetry
);
517 assert(fault
== NoFault
);
519 if (fault
== NoFault
) {
520 // early fail on store conditional: complete now
521 assert(dcache_pkt
!= NULL
);
522 fault
= curStaticInst
->completeAcc(dcache_pkt
, this,
524 delete dcache_pkt
->req
;
532 // non-memory instruction: execute completely now
533 Fault fault
= curStaticInst
->execute(this, traceData
);
543 TimingSimpleCPU::IcachePort::ITickEvent::process()
545 cpu
->completeIfetch(pkt
);
549 TimingSimpleCPU::IcachePort::recvTiming(PacketPtr pkt
)
551 if (pkt
->isResponse()) {
552 // delay processing of returned data until next CPU clock edge
553 Tick mem_time
= pkt
->req
->getTime();
554 Tick next_tick
= cpu
->nextCycle(mem_time
);
556 if (next_tick
== curTick
)
557 cpu
->completeIfetch(pkt
);
559 tickEvent
.schedule(pkt
, next_tick
);
564 //Snooping a Coherence Request, do nothing
570 TimingSimpleCPU::IcachePort::recvRetry()
572 // we shouldn't get a retry unless we have a packet that we're
573 // waiting to transmit
574 assert(cpu
->ifetch_pkt
!= NULL
);
575 assert(cpu
->_status
== IcacheRetry
);
576 PacketPtr tmp
= cpu
->ifetch_pkt
;
577 if (sendTiming(tmp
)) {
578 cpu
->_status
= IcacheWaitResponse
;
579 cpu
->ifetch_pkt
= NULL
;
584 TimingSimpleCPU::completeDataAccess(PacketPtr pkt
)
586 // received a response from the dcache: complete the load or store
588 assert(pkt
->result
== Packet::Success
);
589 assert(_status
== DcacheWaitResponse
);
592 numCycles
+= curTick
- previousTick
;
593 previousTick
= curTick
;
595 Fault fault
= curStaticInst
->completeAcc(pkt
, this, traceData
);
597 if (pkt
->isRead() && pkt
->req
->isLocked()) {
598 TheISA::handleLockedRead(thread
, pkt
->req
);
606 if (getState() == SimObject::Draining
) {
618 TimingSimpleCPU::completeDrain()
620 DPRINTF(Config
, "Done draining\n");
621 changeState(SimObject::Drained
);
622 drainEvent
->process();
626 TimingSimpleCPU::DcachePort::recvTiming(PacketPtr pkt
)
628 if (pkt
->isResponse()) {
629 // delay processing of returned data until next CPU clock edge
630 Tick mem_time
= pkt
->req
->getTime();
631 Tick next_tick
= cpu
->nextCycle(mem_time
);
633 if (next_tick
== curTick
)
634 cpu
->completeDataAccess(pkt
);
636 tickEvent
.schedule(pkt
, next_tick
);
641 //Snooping a coherence req, do nothing
647 TimingSimpleCPU::DcachePort::DTickEvent::process()
649 cpu
->completeDataAccess(pkt
);
653 TimingSimpleCPU::DcachePort::recvRetry()
655 // we shouldn't get a retry unless we have a packet that we're
656 // waiting to transmit
657 assert(cpu
->dcache_pkt
!= NULL
);
658 assert(cpu
->_status
== DcacheRetry
);
659 PacketPtr tmp
= cpu
->dcache_pkt
;
660 if (sendTiming(tmp
)) {
661 cpu
->_status
= DcacheWaitResponse
;
662 // memory system takes ownership of packet
663 cpu
->dcache_pkt
= NULL
;
668 ////////////////////////////////////////////////////////////////////////
670 // TimingSimpleCPU Simulation Object
672 BEGIN_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
674 Param
<Counter
> max_insts_any_thread
;
675 Param
<Counter
> max_insts_all_threads
;
676 Param
<Counter
> max_loads_any_thread
;
677 Param
<Counter
> max_loads_all_threads
;
678 Param
<Tick
> progress_interval
;
679 SimObjectParam
<System
*> system
;
683 SimObjectParam
<TheISA::ITB
*> itb
;
684 SimObjectParam
<TheISA::DTB
*> dtb
;
687 Param
<bool> do_quiesce
;
688 Param
<bool> do_checkpoint_insts
;
689 Param
<bool> do_statistics_insts
;
691 SimObjectParam
<Process
*> workload
;
692 #endif // FULL_SYSTEM
697 Param
<bool> defer_registration
;
699 Param
<bool> function_trace
;
700 Param
<Tick
> function_trace_start
;
701 Param
<bool> simulate_stalls
;
703 END_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
705 BEGIN_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
707 INIT_PARAM(max_insts_any_thread
,
708 "terminate when any thread reaches this inst count"),
709 INIT_PARAM(max_insts_all_threads
,
710 "terminate when all threads have reached this inst count"),
711 INIT_PARAM(max_loads_any_thread
,
712 "terminate when any thread reaches this load count"),
713 INIT_PARAM(max_loads_all_threads
,
714 "terminate when all threads have reached this load count"),
715 INIT_PARAM(progress_interval
, "Progress interval"),
716 INIT_PARAM(system
, "system object"),
717 INIT_PARAM(cpu_id
, "processor ID"),
720 INIT_PARAM(itb
, "Instruction TLB"),
721 INIT_PARAM(dtb
, "Data TLB"),
722 INIT_PARAM(profile
, ""),
723 INIT_PARAM(do_quiesce
, ""),
724 INIT_PARAM(do_checkpoint_insts
, ""),
725 INIT_PARAM(do_statistics_insts
, ""),
727 INIT_PARAM(workload
, "processes to run"),
728 #endif // FULL_SYSTEM
730 INIT_PARAM(clock
, "clock speed"),
731 INIT_PARAM_DFLT(phase
, "clock phase", 0),
732 INIT_PARAM(defer_registration
, "defer system registration (for sampling)"),
733 INIT_PARAM(width
, "cpu width"),
734 INIT_PARAM(function_trace
, "Enable function trace"),
735 INIT_PARAM(function_trace_start
, "Cycle to start function trace"),
736 INIT_PARAM(simulate_stalls
, "Simulate cache stall cycles")
738 END_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
741 CREATE_SIM_OBJECT(TimingSimpleCPU
)
743 TimingSimpleCPU::Params
*params
= new TimingSimpleCPU::Params();
744 params
->name
= getInstanceName();
745 params
->numberOfThreads
= 1;
746 params
->max_insts_any_thread
= max_insts_any_thread
;
747 params
->max_insts_all_threads
= max_insts_all_threads
;
748 params
->max_loads_any_thread
= max_loads_any_thread
;
749 params
->max_loads_all_threads
= max_loads_all_threads
;
750 params
->progress_interval
= progress_interval
;
751 params
->deferRegistration
= defer_registration
;
752 params
->clock
= clock
;
753 params
->phase
= phase
;
754 params
->functionTrace
= function_trace
;
755 params
->functionTraceStart
= function_trace_start
;
756 params
->system
= system
;
757 params
->cpu_id
= cpu_id
;
762 params
->profile
= profile
;
763 params
->do_quiesce
= do_quiesce
;
764 params
->do_checkpoint_insts
= do_checkpoint_insts
;
765 params
->do_statistics_insts
= do_statistics_insts
;
767 params
->process
= workload
;
770 TimingSimpleCPU
*cpu
= new TimingSimpleCPU(params
);
774 REGISTER_SIM_OBJECT("TimingSimpleCPU", TimingSimpleCPU
)