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
)
88 panic("TimingSimpleCPU doesn't expect recvStatusChange callback!");
93 TimingSimpleCPU::CpuPort::TickEvent::schedule(PacketPtr _pkt
, Tick t
)
99 TimingSimpleCPU::TimingSimpleCPU(Params
*p
)
100 : BaseSimpleCPU(p
), icachePort(this, p
->clock
), dcachePort(this, p
->clock
),
104 ifetch_pkt
= dcache_pkt
= NULL
;
108 changeState(SimObject::Running
);
112 TimingSimpleCPU::~TimingSimpleCPU()
117 TimingSimpleCPU::serialize(ostream
&os
)
119 SimObject::State so_state
= SimObject::getState();
120 SERIALIZE_ENUM(so_state
);
121 BaseSimpleCPU::serialize(os
);
125 TimingSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
127 SimObject::State so_state
;
128 UNSERIALIZE_ENUM(so_state
);
129 BaseSimpleCPU::unserialize(cp
, section
);
133 TimingSimpleCPU::drain(Event
*drain_event
)
135 // TimingSimpleCPU is ready to drain if it's not waiting for
136 // an access to complete.
137 if (status() == Idle
|| status() == Running
|| status() == SwitchedOut
) {
138 changeState(SimObject::Drained
);
141 changeState(SimObject::Draining
);
142 drainEvent
= drain_event
;
148 TimingSimpleCPU::resume()
150 if (_status
!= SwitchedOut
&& _status
!= Idle
) {
151 assert(system
->getMemoryMode() == System::Timing
);
153 // Delete the old event if it existed.
155 if (fetchEvent
->scheduled())
156 fetchEvent
->deschedule();
162 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
163 fetchEvent
->schedule(curTick
);
166 changeState(SimObject::Running
);
167 previousTick
= curTick
;
171 TimingSimpleCPU::switchOut()
173 assert(status() == Running
|| status() == Idle
);
174 _status
= SwitchedOut
;
175 numCycles
+= curTick
- previousTick
;
177 // If we've been scheduled to resume but are then told to switch out,
178 // we'll need to cancel it.
179 if (fetchEvent
&& fetchEvent
->scheduled())
180 fetchEvent
->deschedule();
185 TimingSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
187 BaseCPU::takeOverFrom(oldCPU
);
189 // if any of this CPU's ThreadContexts are active, mark the CPU as
190 // running and schedule its tick event.
191 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
192 ThreadContext
*tc
= threadContexts
[i
];
193 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
199 if (_status
!= Running
) {
204 if (icachePort
.getPeer() == NULL
) {
205 peer
= oldCPU
->getPort("icache_port")->getPeer();
206 icachePort
.setPeer(peer
);
208 peer
= icachePort
.getPeer();
210 peer
->setPeer(&icachePort
);
212 if (dcachePort
.getPeer() == NULL
) {
213 peer
= oldCPU
->getPort("dcache_port")->getPeer();
214 dcachePort
.setPeer(peer
);
216 peer
= dcachePort
.getPeer();
218 peer
->setPeer(&dcachePort
);
223 TimingSimpleCPU::activateContext(int thread_num
, int delay
)
225 assert(thread_num
== 0);
228 assert(_status
== Idle
);
232 // kick things off by initiating the fetch of the next instruction
234 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
235 fetchEvent
->schedule(curTick
+ cycles(delay
));
240 TimingSimpleCPU::suspendContext(int thread_num
)
242 assert(thread_num
== 0);
245 assert(_status
== Running
);
247 // just change status to Idle... if status != Running,
248 // completeInst() will not initiate fetch of next instruction.
257 TimingSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
260 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
261 cpu_id
, /* thread ID */ 0);
264 traceData
->setAddr(req
->getVaddr());
267 // translate to physical address
268 Fault fault
= thread
->translateDataReadReq(req
);
270 // Now do the access.
271 if (fault
== NoFault
) {
273 new Packet(req
, Packet::ReadReq
, Packet::Broadcast
);
274 pkt
->dataDynamic
<T
>(new T
);
276 if (!dcachePort
.sendTiming(pkt
)) {
277 _status
= DcacheRetry
;
280 _status
= DcacheWaitResponse
;
281 // 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");
293 #ifndef DOXYGEN_SHOULD_SKIP_THIS
297 TimingSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
301 TimingSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
305 TimingSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
309 TimingSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
311 #endif //DOXYGEN_SHOULD_SKIP_THIS
315 TimingSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
317 return read(addr
, *(uint64_t*)&data
, flags
);
322 TimingSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
324 return read(addr
, *(uint32_t*)&data
, flags
);
330 TimingSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
332 return read(addr
, (uint32_t&)data
, flags
);
338 TimingSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
341 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
342 cpu_id
, /* thread ID */ 0);
344 // translate to physical address
345 Fault fault
= thread
->translateDataWriteReq(req
);
347 // Now do the access.
348 if (fault
== NoFault
) {
349 assert(dcache_pkt
== NULL
);
350 dcache_pkt
= new Packet(req
, Packet::WriteReq
, Packet::Broadcast
);
351 dcache_pkt
->allocate();
352 dcache_pkt
->set(data
);
354 bool do_access
= true; // flag to suppress cache access
356 if (req
->isLocked()) {
357 do_access
= TheISA::handleLockedWrite(thread
, req
);
361 if (!dcachePort
.sendTiming(dcache_pkt
)) {
362 _status
= DcacheRetry
;
364 _status
= DcacheWaitResponse
;
365 // memory system takes ownership of packet
371 // This will need a new way to tell if it's hooked up to a cache or not.
372 if (req
->isUncacheable())
373 recordEvent("Uncached Write");
375 // If the write needs to have a fault on the access, consider calling
376 // changeStatus() and changing it to "bad addr write" or something.
381 #ifndef DOXYGEN_SHOULD_SKIP_THIS
384 TimingSimpleCPU::write(uint64_t data
, Addr addr
,
385 unsigned flags
, uint64_t *res
);
389 TimingSimpleCPU::write(uint32_t data
, Addr addr
,
390 unsigned flags
, uint64_t *res
);
394 TimingSimpleCPU::write(uint16_t data
, Addr addr
,
395 unsigned flags
, uint64_t *res
);
399 TimingSimpleCPU::write(uint8_t data
, Addr addr
,
400 unsigned flags
, uint64_t *res
);
402 #endif //DOXYGEN_SHOULD_SKIP_THIS
406 TimingSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
408 return write(*(uint64_t*)&data
, addr
, flags
, res
);
413 TimingSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
415 return write(*(uint32_t*)&data
, addr
, flags
, res
);
421 TimingSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
423 return write((uint32_t)data
, addr
, flags
, res
);
428 TimingSimpleCPU::fetch()
430 if (!curStaticInst
|| !curStaticInst
->isDelayedCommit())
431 checkForInterrupts();
433 Request
*ifetch_req
= new Request();
434 ifetch_req
->setThreadContext(cpu_id
, /* thread ID */ 0);
435 Fault fault
= setupFetchRequest(ifetch_req
);
437 ifetch_pkt
= new Packet(ifetch_req
, Packet::ReadReq
, Packet::Broadcast
);
438 ifetch_pkt
->dataStatic(&inst
);
440 if (fault
== NoFault
) {
441 if (!icachePort
.sendTiming(ifetch_pkt
)) {
442 // Need to wait for retry
443 _status
= IcacheRetry
;
445 // Need to wait for cache to respond
446 _status
= IcacheWaitResponse
;
447 // ownership of packet transferred to memory system
451 // fetch fault: advance directly to next instruction (fault handler)
455 numCycles
+= curTick
- previousTick
;
456 previousTick
= curTick
;
461 TimingSimpleCPU::advanceInst(Fault fault
)
465 if (_status
== Running
) {
466 // kick off fetch of next instruction... callback from icache
467 // response will cause that instruction to be executed,
468 // keeping the CPU running.
475 TimingSimpleCPU::completeIfetch(PacketPtr pkt
)
477 // received a response from the icache: execute the received
479 assert(pkt
->result
== Packet::Success
);
480 assert(_status
== IcacheWaitResponse
);
487 numCycles
+= curTick
- previousTick
;
488 previousTick
= curTick
;
490 if (getState() == SimObject::Draining
) {
496 if (curStaticInst
->isMemRef() && !curStaticInst
->isDataPrefetch()) {
497 // load or store: just send to dcache
498 Fault fault
= curStaticInst
->initiateAcc(this, traceData
);
499 if (_status
!= Running
) {
500 // instruction will complete in dcache response callback
501 assert(_status
== DcacheWaitResponse
|| _status
== DcacheRetry
);
502 assert(fault
== NoFault
);
504 if (fault
== NoFault
) {
505 // early fail on store conditional: complete now
506 assert(dcache_pkt
!= NULL
);
507 fault
= curStaticInst
->completeAcc(dcache_pkt
, this,
509 delete dcache_pkt
->req
;
517 // non-memory instruction: execute completely now
518 Fault fault
= curStaticInst
->execute(this, traceData
);
525 TimingSimpleCPU::IcachePort::ITickEvent::process()
527 cpu
->completeIfetch(pkt
);
531 TimingSimpleCPU::IcachePort::recvTiming(PacketPtr pkt
)
533 if (pkt
->isResponse()) {
534 // delay processing of returned data until next CPU clock edge
535 Tick time
= pkt
->req
->getTime();
536 while (time
< curTick
)
540 cpu
->completeIfetch(pkt
);
542 tickEvent
.schedule(pkt
, time
);
547 //Snooping a Coherence Request, do nothing
553 TimingSimpleCPU::IcachePort::recvRetry()
555 // we shouldn't get a retry unless we have a packet that we're
556 // waiting to transmit
557 assert(cpu
->ifetch_pkt
!= NULL
);
558 assert(cpu
->_status
== IcacheRetry
);
559 PacketPtr tmp
= cpu
->ifetch_pkt
;
560 if (sendTiming(tmp
)) {
561 cpu
->_status
= IcacheWaitResponse
;
562 cpu
->ifetch_pkt
= NULL
;
567 TimingSimpleCPU::completeDataAccess(PacketPtr pkt
)
569 // received a response from the dcache: complete the load or store
571 assert(pkt
->result
== Packet::Success
);
572 assert(_status
== DcacheWaitResponse
);
575 numCycles
+= curTick
- previousTick
;
576 previousTick
= curTick
;
578 Fault fault
= curStaticInst
->completeAcc(pkt
, this, traceData
);
580 if (pkt
->isRead() && pkt
->req
->isLocked()) {
581 TheISA::handleLockedRead(thread
, pkt
->req
);
589 if (getState() == SimObject::Draining
) {
601 TimingSimpleCPU::completeDrain()
603 DPRINTF(Config
, "Done draining\n");
604 changeState(SimObject::Drained
);
605 drainEvent
->process();
609 TimingSimpleCPU::DcachePort::recvTiming(PacketPtr pkt
)
611 if (pkt
->isResponse()) {
612 // delay processing of returned data until next CPU clock edge
613 Tick time
= pkt
->req
->getTime();
614 while (time
< curTick
)
618 cpu
->completeDataAccess(pkt
);
620 tickEvent
.schedule(pkt
, time
);
625 //Snooping a coherence req, do nothing
631 TimingSimpleCPU::DcachePort::DTickEvent::process()
633 cpu
->completeDataAccess(pkt
);
637 TimingSimpleCPU::DcachePort::recvRetry()
639 // we shouldn't get a retry unless we have a packet that we're
640 // waiting to transmit
641 assert(cpu
->dcache_pkt
!= NULL
);
642 assert(cpu
->_status
== DcacheRetry
);
643 PacketPtr tmp
= cpu
->dcache_pkt
;
644 if (sendTiming(tmp
)) {
645 cpu
->_status
= DcacheWaitResponse
;
646 // memory system takes ownership of packet
647 cpu
->dcache_pkt
= NULL
;
652 ////////////////////////////////////////////////////////////////////////
654 // TimingSimpleCPU Simulation Object
656 BEGIN_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
658 Param
<Counter
> max_insts_any_thread
;
659 Param
<Counter
> max_insts_all_threads
;
660 Param
<Counter
> max_loads_any_thread
;
661 Param
<Counter
> max_loads_all_threads
;
662 Param
<Tick
> progress_interval
;
663 SimObjectParam
<MemObject
*> mem
;
664 SimObjectParam
<System
*> system
;
668 SimObjectParam
<AlphaITB
*> itb
;
669 SimObjectParam
<AlphaDTB
*> dtb
;
672 SimObjectParam
<Process
*> workload
;
673 #endif // FULL_SYSTEM
677 Param
<bool> defer_registration
;
679 Param
<bool> function_trace
;
680 Param
<Tick
> function_trace_start
;
681 Param
<bool> simulate_stalls
;
683 END_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
685 BEGIN_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
687 INIT_PARAM(max_insts_any_thread
,
688 "terminate when any thread reaches this inst count"),
689 INIT_PARAM(max_insts_all_threads
,
690 "terminate when all threads have reached this inst count"),
691 INIT_PARAM(max_loads_any_thread
,
692 "terminate when any thread reaches this load count"),
693 INIT_PARAM(max_loads_all_threads
,
694 "terminate when all threads have reached this load count"),
695 INIT_PARAM(progress_interval
, "Progress interval"),
696 INIT_PARAM(mem
, "memory"),
697 INIT_PARAM(system
, "system object"),
698 INIT_PARAM(cpu_id
, "processor ID"),
701 INIT_PARAM(itb
, "Instruction TLB"),
702 INIT_PARAM(dtb
, "Data TLB"),
703 INIT_PARAM(profile
, ""),
705 INIT_PARAM(workload
, "processes to run"),
706 #endif // FULL_SYSTEM
708 INIT_PARAM(clock
, "clock speed"),
709 INIT_PARAM(defer_registration
, "defer system registration (for sampling)"),
710 INIT_PARAM(width
, "cpu width"),
711 INIT_PARAM(function_trace
, "Enable function trace"),
712 INIT_PARAM(function_trace_start
, "Cycle to start function trace"),
713 INIT_PARAM(simulate_stalls
, "Simulate cache stall cycles")
715 END_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
718 CREATE_SIM_OBJECT(TimingSimpleCPU
)
720 TimingSimpleCPU::Params
*params
= new TimingSimpleCPU::Params();
721 params
->name
= getInstanceName();
722 params
->numberOfThreads
= 1;
723 params
->max_insts_any_thread
= max_insts_any_thread
;
724 params
->max_insts_all_threads
= max_insts_all_threads
;
725 params
->max_loads_any_thread
= max_loads_any_thread
;
726 params
->max_loads_all_threads
= max_loads_all_threads
;
727 params
->progress_interval
= progress_interval
;
728 params
->deferRegistration
= defer_registration
;
729 params
->clock
= clock
;
730 params
->functionTrace
= function_trace
;
731 params
->functionTraceStart
= function_trace_start
;
733 params
->system
= system
;
734 params
->cpu_id
= cpu_id
;
739 params
->profile
= profile
;
741 params
->process
= workload
;
744 TimingSimpleCPU
*cpu
= new TimingSimpleCPU(params
);
748 REGISTER_SIM_OBJECT("TimingSimpleCPU", TimingSimpleCPU
)