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 "base/bigint.hh"
34 #include "cpu/exetrace.hh"
35 #include "cpu/simple/timing.hh"
36 #include "mem/packet.hh"
37 #include "mem/packet_access.hh"
38 #include "sim/builder.hh"
39 #include "sim/system.hh"
42 using namespace TheISA
;
45 TimingSimpleCPU::getPort(const std::string
&if_name
, int idx
)
47 if (if_name
== "dcache_port")
49 else if (if_name
== "icache_port")
52 panic("No Such Port\n");
56 TimingSimpleCPU::init()
60 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
61 ThreadContext
*tc
= threadContexts
[i
];
63 // initialize CPU, including PC
64 TheISA::initCPU(tc
, tc
->readCpuId());
70 TimingSimpleCPU::CpuPort::recvAtomic(PacketPtr pkt
)
72 panic("TimingSimpleCPU doesn't expect recvAtomic callback!");
77 TimingSimpleCPU::CpuPort::recvFunctional(PacketPtr pkt
)
79 //No internal storage to update, jusst return
84 TimingSimpleCPU::CpuPort::recvStatusChange(Status status
)
86 if (status
== RangeChange
) {
87 if (!snoopRangeSent
) {
88 snoopRangeSent
= true;
89 sendStatusChange(Port::RangeChange
);
94 panic("TimingSimpleCPU doesn't expect recvStatusChange callback!");
99 TimingSimpleCPU::CpuPort::TickEvent::schedule(PacketPtr _pkt
, Tick t
)
105 TimingSimpleCPU::TimingSimpleCPU(Params
*p
)
106 : 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() == System::Timing
);
163 // Delete the old event if it existed.
165 if (fetchEvent
->scheduled())
166 fetchEvent
->deschedule();
172 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
173 fetchEvent
->schedule(nextCycle());
176 changeState(SimObject::Running
);
177 previousTick
= curTick
;
181 TimingSimpleCPU::switchOut()
183 assert(status() == Running
|| status() == Idle
);
184 _status
= SwitchedOut
;
185 numCycles
+= curTick
- previousTick
;
187 // If we've been scheduled to resume but are then told to switch out,
188 // we'll need to cancel it.
189 if (fetchEvent
&& fetchEvent
->scheduled())
190 fetchEvent
->deschedule();
195 TimingSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
197 BaseCPU::takeOverFrom(oldCPU
, &icachePort
, &dcachePort
);
199 // if any of this CPU's ThreadContexts are active, mark the CPU as
200 // running and schedule its tick event.
201 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
202 ThreadContext
*tc
= threadContexts
[i
];
203 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
209 if (_status
!= Running
) {
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
228 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
229 fetchEvent
->schedule(nextCycle(curTick
+ cycles(delay
)));
234 TimingSimpleCPU::suspendContext(int thread_num
)
236 assert(thread_num
== 0);
239 assert(_status
== Running
);
241 // just change status to Idle... if status != Running,
242 // completeInst() will not initiate fetch of next instruction.
251 TimingSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
254 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
255 cpu_id
, /* thread ID */ 0);
258 traceData
->setAddr(req
->getVaddr());
261 // translate to physical address
262 Fault fault
= thread
->translateDataReadReq(req
);
264 // Now do the access.
265 if (fault
== NoFault
) {
267 new Packet(req
, MemCmd::ReadReq
, Packet::Broadcast
);
268 pkt
->dataDynamic
<T
>(new T
);
270 if (!dcachePort
.sendTiming(pkt
)) {
271 _status
= DcacheRetry
;
274 _status
= DcacheWaitResponse
;
275 // memory system takes ownership of packet
279 // This will need a new way to tell if it has a dcache attached.
280 if (req
->isUncacheable())
281 recordEvent("Uncached Read");
289 #ifndef DOXYGEN_SHOULD_SKIP_THIS
293 TimingSimpleCPU::read(Addr addr
, Twin64_t
&data
, unsigned flags
);
297 TimingSimpleCPU::read(Addr addr
, Twin32_t
&data
, unsigned flags
);
301 TimingSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
305 TimingSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
309 TimingSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
313 TimingSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
315 #endif //DOXYGEN_SHOULD_SKIP_THIS
319 TimingSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
321 return read(addr
, *(uint64_t*)&data
, flags
);
326 TimingSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
328 return read(addr
, *(uint32_t*)&data
, flags
);
334 TimingSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
336 return read(addr
, (uint32_t&)data
, flags
);
342 TimingSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
345 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
346 cpu_id
, /* thread ID */ 0);
349 traceData
->setAddr(req
->getVaddr());
352 // translate to physical address
353 Fault fault
= thread
->translateDataWriteReq(req
);
355 // Now do the access.
356 if (fault
== NoFault
) {
357 assert(dcache_pkt
== NULL
);
359 dcache_pkt
= new Packet(req
, MemCmd::SwapReq
, Packet::Broadcast
);
361 dcache_pkt
= new Packet(req
, MemCmd::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
);
370 if (req
->isCondSwap()) {
372 req
->setExtraData(*res
);
376 if (!dcachePort
.sendTiming(dcache_pkt
)) {
377 _status
= DcacheRetry
;
379 _status
= DcacheWaitResponse
;
380 // 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");
392 // If the write needs to have a fault on the access, consider calling
393 // changeStatus() and changing it to "bad addr write" or something.
398 #ifndef DOXYGEN_SHOULD_SKIP_THIS
401 TimingSimpleCPU::write(Twin32_t data
, Addr addr
,
402 unsigned flags
, uint64_t *res
);
406 TimingSimpleCPU::write(Twin64_t data
, Addr addr
,
407 unsigned flags
, uint64_t *res
);
411 TimingSimpleCPU::write(uint64_t data
, Addr addr
,
412 unsigned flags
, uint64_t *res
);
416 TimingSimpleCPU::write(uint32_t data
, Addr addr
,
417 unsigned flags
, uint64_t *res
);
421 TimingSimpleCPU::write(uint16_t data
, Addr addr
,
422 unsigned flags
, uint64_t *res
);
426 TimingSimpleCPU::write(uint8_t data
, Addr addr
,
427 unsigned flags
, uint64_t *res
);
429 #endif //DOXYGEN_SHOULD_SKIP_THIS
433 TimingSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
435 return write(*(uint64_t*)&data
, addr
, flags
, res
);
440 TimingSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
442 return write(*(uint32_t*)&data
, addr
, flags
, res
);
448 TimingSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
450 return write((uint32_t)data
, addr
, flags
, res
);
455 TimingSimpleCPU::fetch()
457 if (!curStaticInst
|| !curStaticInst
->isDelayedCommit())
458 checkForInterrupts();
460 Request
*ifetch_req
= new Request();
461 ifetch_req
->setThreadContext(cpu_id
, /* thread ID */ 0);
462 Fault fault
= setupFetchRequest(ifetch_req
);
464 ifetch_pkt
= new Packet(ifetch_req
, MemCmd::ReadReq
, Packet::Broadcast
);
465 ifetch_pkt
->dataStatic(&inst
);
467 if (fault
== NoFault
) {
468 if (!icachePort
.sendTiming(ifetch_pkt
)) {
469 // Need to wait for retry
470 _status
= IcacheRetry
;
472 // Need to wait for cache to respond
473 _status
= IcacheWaitResponse
;
474 // ownership of packet transferred to memory system
480 // fetch fault: advance directly to next instruction (fault handler)
484 numCycles
+= curTick
- previousTick
;
485 previousTick
= curTick
;
490 TimingSimpleCPU::advanceInst(Fault fault
)
494 if (_status
== Running
) {
495 // kick off fetch of next instruction... callback from icache
496 // response will cause that instruction to be executed,
497 // keeping the CPU running.
504 TimingSimpleCPU::completeIfetch(PacketPtr pkt
)
506 // received a response from the icache: execute the received
508 assert(pkt
->result
== Packet::Success
);
509 assert(_status
== IcacheWaitResponse
);
513 numCycles
+= curTick
- previousTick
;
514 previousTick
= curTick
;
516 if (getState() == SimObject::Draining
) {
525 if (curStaticInst
->isMemRef() && !curStaticInst
->isDataPrefetch()) {
526 // load or store: just send to dcache
527 Fault fault
= curStaticInst
->initiateAcc(this, traceData
);
528 if (_status
!= Running
) {
529 // instruction will complete in dcache response callback
530 assert(_status
== DcacheWaitResponse
|| _status
== DcacheRetry
);
531 assert(fault
== NoFault
);
533 if (fault
== NoFault
) {
534 // early fail on store conditional: complete now
535 assert(dcache_pkt
!= NULL
);
536 fault
= curStaticInst
->completeAcc(dcache_pkt
, this,
538 delete dcache_pkt
->req
;
546 // non-memory instruction: execute completely now
547 Fault fault
= curStaticInst
->execute(this, traceData
);
557 TimingSimpleCPU::IcachePort::ITickEvent::process()
559 cpu
->completeIfetch(pkt
);
563 TimingSimpleCPU::IcachePort::recvTiming(PacketPtr pkt
)
565 if (pkt
->isResponse()) {
566 // delay processing of returned data until next CPU clock edge
567 Tick mem_time
= pkt
->req
->getTime();
568 Tick next_tick
= cpu
->nextCycle(mem_time
);
570 if (next_tick
== curTick
)
571 cpu
->completeIfetch(pkt
);
573 tickEvent
.schedule(pkt
, next_tick
);
578 //Snooping a Coherence Request, do nothing
584 TimingSimpleCPU::IcachePort::recvRetry()
586 // we shouldn't get a retry unless we have a packet that we're
587 // waiting to transmit
588 assert(cpu
->ifetch_pkt
!= NULL
);
589 assert(cpu
->_status
== IcacheRetry
);
590 PacketPtr tmp
= cpu
->ifetch_pkt
;
591 if (sendTiming(tmp
)) {
592 cpu
->_status
= IcacheWaitResponse
;
593 cpu
->ifetch_pkt
= NULL
;
598 TimingSimpleCPU::completeDataAccess(PacketPtr pkt
)
600 // received a response from the dcache: complete the load or store
602 assert(pkt
->result
== Packet::Success
);
603 assert(_status
== DcacheWaitResponse
);
606 numCycles
+= curTick
- previousTick
;
607 previousTick
= curTick
;
609 Fault fault
= curStaticInst
->completeAcc(pkt
, this, traceData
);
611 if (pkt
->isRead() && pkt
->req
->isLocked()) {
612 TheISA::handleLockedRead(thread
, pkt
->req
);
620 if (getState() == SimObject::Draining
) {
632 TimingSimpleCPU::completeDrain()
634 DPRINTF(Config
, "Done draining\n");
635 changeState(SimObject::Drained
);
636 drainEvent
->process();
640 TimingSimpleCPU::DcachePort::setPeer(Port
*port
)
645 // Update the ThreadContext's memory ports (Functional/Virtual
647 cpu
->tcBase()->connectMemPorts();
652 TimingSimpleCPU::DcachePort::recvTiming(PacketPtr pkt
)
654 if (pkt
->isResponse()) {
655 // delay processing of returned data until next CPU clock edge
656 Tick mem_time
= pkt
->req
->getTime();
657 Tick next_tick
= cpu
->nextCycle(mem_time
);
659 if (next_tick
== curTick
)
660 cpu
->completeDataAccess(pkt
);
662 tickEvent
.schedule(pkt
, next_tick
);
667 //Snooping a coherence req, do nothing
673 TimingSimpleCPU::DcachePort::DTickEvent::process()
675 cpu
->completeDataAccess(pkt
);
679 TimingSimpleCPU::DcachePort::recvRetry()
681 // we shouldn't get a retry unless we have a packet that we're
682 // waiting to transmit
683 assert(cpu
->dcache_pkt
!= NULL
);
684 assert(cpu
->_status
== DcacheRetry
);
685 PacketPtr tmp
= cpu
->dcache_pkt
;
686 if (sendTiming(tmp
)) {
687 cpu
->_status
= DcacheWaitResponse
;
688 // memory system takes ownership of packet
689 cpu
->dcache_pkt
= NULL
;
694 ////////////////////////////////////////////////////////////////////////
696 // TimingSimpleCPU Simulation Object
698 BEGIN_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
700 Param
<Counter
> max_insts_any_thread
;
701 Param
<Counter
> max_insts_all_threads
;
702 Param
<Counter
> max_loads_any_thread
;
703 Param
<Counter
> max_loads_all_threads
;
704 Param
<Tick
> progress_interval
;
705 SimObjectParam
<System
*> system
;
709 SimObjectParam
<TheISA::ITB
*> itb
;
710 SimObjectParam
<TheISA::DTB
*> dtb
;
713 Param
<bool> do_quiesce
;
714 Param
<bool> do_checkpoint_insts
;
715 Param
<bool> do_statistics_insts
;
717 SimObjectParam
<Process
*> workload
;
718 #endif // FULL_SYSTEM
723 Param
<bool> defer_registration
;
725 Param
<bool> function_trace
;
726 Param
<Tick
> function_trace_start
;
727 Param
<bool> simulate_stalls
;
729 END_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
731 BEGIN_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
733 INIT_PARAM(max_insts_any_thread
,
734 "terminate when any thread reaches this inst count"),
735 INIT_PARAM(max_insts_all_threads
,
736 "terminate when all threads have reached this inst count"),
737 INIT_PARAM(max_loads_any_thread
,
738 "terminate when any thread reaches this load count"),
739 INIT_PARAM(max_loads_all_threads
,
740 "terminate when all threads have reached this load count"),
741 INIT_PARAM(progress_interval
, "Progress interval"),
742 INIT_PARAM(system
, "system object"),
743 INIT_PARAM(cpu_id
, "processor ID"),
746 INIT_PARAM(itb
, "Instruction TLB"),
747 INIT_PARAM(dtb
, "Data TLB"),
748 INIT_PARAM(profile
, ""),
749 INIT_PARAM(do_quiesce
, ""),
750 INIT_PARAM(do_checkpoint_insts
, ""),
751 INIT_PARAM(do_statistics_insts
, ""),
753 INIT_PARAM(workload
, "processes to run"),
754 #endif // FULL_SYSTEM
756 INIT_PARAM(clock
, "clock speed"),
757 INIT_PARAM_DFLT(phase
, "clock phase", 0),
758 INIT_PARAM(defer_registration
, "defer system registration (for sampling)"),
759 INIT_PARAM(width
, "cpu width"),
760 INIT_PARAM(function_trace
, "Enable function trace"),
761 INIT_PARAM(function_trace_start
, "Cycle to start function trace"),
762 INIT_PARAM(simulate_stalls
, "Simulate cache stall cycles")
764 END_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
767 CREATE_SIM_OBJECT(TimingSimpleCPU
)
769 TimingSimpleCPU::Params
*params
= new TimingSimpleCPU::Params();
770 params
->name
= getInstanceName();
771 params
->numberOfThreads
= 1;
772 params
->max_insts_any_thread
= max_insts_any_thread
;
773 params
->max_insts_all_threads
= max_insts_all_threads
;
774 params
->max_loads_any_thread
= max_loads_any_thread
;
775 params
->max_loads_all_threads
= max_loads_all_threads
;
776 params
->progress_interval
= progress_interval
;
777 params
->deferRegistration
= defer_registration
;
778 params
->clock
= clock
;
779 params
->phase
= phase
;
780 params
->functionTrace
= function_trace
;
781 params
->functionTraceStart
= function_trace_start
;
782 params
->system
= system
;
783 params
->cpu_id
= cpu_id
;
788 params
->profile
= profile
;
789 params
->do_quiesce
= do_quiesce
;
790 params
->do_checkpoint_insts
= do_checkpoint_insts
;
791 params
->do_statistics_insts
= do_statistics_insts
;
793 params
->process
= workload
;
796 TimingSimpleCPU
*cpu
= new TimingSimpleCPU(params
);
800 REGISTER_SIM_OBJECT("TimingSimpleCPU", TimingSimpleCPU
)