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 checkForInterrupts();
432 Request
*ifetch_req
= new Request();
433 ifetch_req
->setThreadContext(cpu_id
, /* thread ID */ 0);
434 Fault fault
= setupFetchRequest(ifetch_req
);
436 ifetch_pkt
= new Packet(ifetch_req
, Packet::ReadReq
, Packet::Broadcast
);
437 ifetch_pkt
->dataStatic(&inst
);
439 if (fault
== NoFault
) {
440 if (!icachePort
.sendTiming(ifetch_pkt
)) {
441 // Need to wait for retry
442 _status
= IcacheRetry
;
444 // Need to wait for cache to respond
445 _status
= IcacheWaitResponse
;
446 // ownership of packet transferred to memory system
450 // fetch fault: advance directly to next instruction (fault handler)
454 numCycles
+= curTick
- previousTick
;
455 previousTick
= curTick
;
460 TimingSimpleCPU::advanceInst(Fault fault
)
464 if (_status
== Running
) {
465 // kick off fetch of next instruction... callback from icache
466 // response will cause that instruction to be executed,
467 // keeping the CPU running.
474 TimingSimpleCPU::completeIfetch(PacketPtr pkt
)
476 // received a response from the icache: execute the received
478 assert(pkt
->result
== Packet::Success
);
479 assert(_status
== IcacheWaitResponse
);
486 numCycles
+= curTick
- previousTick
;
487 previousTick
= curTick
;
489 if (getState() == SimObject::Draining
) {
495 if (curStaticInst
->isMemRef() && !curStaticInst
->isDataPrefetch()) {
496 // load or store: just send to dcache
497 Fault fault
= curStaticInst
->initiateAcc(this, traceData
);
498 if (_status
!= Running
) {
499 // instruction will complete in dcache response callback
500 assert(_status
== DcacheWaitResponse
|| _status
== DcacheRetry
);
501 assert(fault
== NoFault
);
503 if (fault
== NoFault
) {
504 // early fail on store conditional: complete now
505 assert(dcache_pkt
!= NULL
);
506 fault
= curStaticInst
->completeAcc(dcache_pkt
, this,
508 delete dcache_pkt
->req
;
516 // non-memory instruction: execute completely now
517 Fault fault
= curStaticInst
->execute(this, traceData
);
524 TimingSimpleCPU::IcachePort::ITickEvent::process()
526 cpu
->completeIfetch(pkt
);
530 TimingSimpleCPU::IcachePort::recvTiming(PacketPtr pkt
)
532 if (pkt
->isResponse()) {
533 // delay processing of returned data until next CPU clock edge
534 Tick time
= pkt
->req
->getTime();
535 while (time
< curTick
)
539 cpu
->completeIfetch(pkt
);
541 tickEvent
.schedule(pkt
, time
);
546 //Snooping a Coherence Request, do nothing
552 TimingSimpleCPU::IcachePort::recvRetry()
554 // we shouldn't get a retry unless we have a packet that we're
555 // waiting to transmit
556 assert(cpu
->ifetch_pkt
!= NULL
);
557 assert(cpu
->_status
== IcacheRetry
);
558 PacketPtr tmp
= cpu
->ifetch_pkt
;
559 if (sendTiming(tmp
)) {
560 cpu
->_status
= IcacheWaitResponse
;
561 cpu
->ifetch_pkt
= NULL
;
566 TimingSimpleCPU::completeDataAccess(PacketPtr pkt
)
568 // received a response from the dcache: complete the load or store
570 assert(pkt
->result
== Packet::Success
);
571 assert(_status
== DcacheWaitResponse
);
574 numCycles
+= curTick
- previousTick
;
575 previousTick
= curTick
;
577 Fault fault
= curStaticInst
->completeAcc(pkt
, this, traceData
);
579 if (pkt
->isRead() && pkt
->req
->isLocked()) {
580 TheISA::handleLockedRead(thread
, pkt
->req
);
588 if (getState() == SimObject::Draining
) {
600 TimingSimpleCPU::completeDrain()
602 DPRINTF(Config
, "Done draining\n");
603 changeState(SimObject::Drained
);
604 drainEvent
->process();
608 TimingSimpleCPU::DcachePort::recvTiming(PacketPtr pkt
)
610 if (pkt
->isResponse()) {
611 // delay processing of returned data until next CPU clock edge
612 Tick time
= pkt
->req
->getTime();
613 while (time
< curTick
)
617 cpu
->completeDataAccess(pkt
);
619 tickEvent
.schedule(pkt
, time
);
624 //Snooping a coherence req, do nothing
630 TimingSimpleCPU::DcachePort::DTickEvent::process()
632 cpu
->completeDataAccess(pkt
);
636 TimingSimpleCPU::DcachePort::recvRetry()
638 // we shouldn't get a retry unless we have a packet that we're
639 // waiting to transmit
640 assert(cpu
->dcache_pkt
!= NULL
);
641 assert(cpu
->_status
== DcacheRetry
);
642 PacketPtr tmp
= cpu
->dcache_pkt
;
643 if (sendTiming(tmp
)) {
644 cpu
->_status
= DcacheWaitResponse
;
645 // memory system takes ownership of packet
646 cpu
->dcache_pkt
= NULL
;
651 ////////////////////////////////////////////////////////////////////////
653 // TimingSimpleCPU Simulation Object
655 BEGIN_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
657 Param
<Counter
> max_insts_any_thread
;
658 Param
<Counter
> max_insts_all_threads
;
659 Param
<Counter
> max_loads_any_thread
;
660 Param
<Counter
> max_loads_all_threads
;
661 Param
<Tick
> progress_interval
;
662 SimObjectParam
<MemObject
*> mem
;
663 SimObjectParam
<System
*> system
;
667 SimObjectParam
<AlphaITB
*> itb
;
668 SimObjectParam
<AlphaDTB
*> dtb
;
671 SimObjectParam
<Process
*> workload
;
672 #endif // FULL_SYSTEM
676 Param
<bool> defer_registration
;
678 Param
<bool> function_trace
;
679 Param
<Tick
> function_trace_start
;
680 Param
<bool> simulate_stalls
;
682 END_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
684 BEGIN_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
686 INIT_PARAM(max_insts_any_thread
,
687 "terminate when any thread reaches this inst count"),
688 INIT_PARAM(max_insts_all_threads
,
689 "terminate when all threads have reached this inst count"),
690 INIT_PARAM(max_loads_any_thread
,
691 "terminate when any thread reaches this load count"),
692 INIT_PARAM(max_loads_all_threads
,
693 "terminate when all threads have reached this load count"),
694 INIT_PARAM(progress_interval
, "Progress interval"),
695 INIT_PARAM(mem
, "memory"),
696 INIT_PARAM(system
, "system object"),
697 INIT_PARAM(cpu_id
, "processor ID"),
700 INIT_PARAM(itb
, "Instruction TLB"),
701 INIT_PARAM(dtb
, "Data TLB"),
702 INIT_PARAM(profile
, ""),
704 INIT_PARAM(workload
, "processes to run"),
705 #endif // FULL_SYSTEM
707 INIT_PARAM(clock
, "clock speed"),
708 INIT_PARAM(defer_registration
, "defer system registration (for sampling)"),
709 INIT_PARAM(width
, "cpu width"),
710 INIT_PARAM(function_trace
, "Enable function trace"),
711 INIT_PARAM(function_trace_start
, "Cycle to start function trace"),
712 INIT_PARAM(simulate_stalls
, "Simulate cache stall cycles")
714 END_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
717 CREATE_SIM_OBJECT(TimingSimpleCPU
)
719 TimingSimpleCPU::Params
*params
= new TimingSimpleCPU::Params();
720 params
->name
= getInstanceName();
721 params
->numberOfThreads
= 1;
722 params
->max_insts_any_thread
= max_insts_any_thread
;
723 params
->max_insts_all_threads
= max_insts_all_threads
;
724 params
->max_loads_any_thread
= max_loads_any_thread
;
725 params
->max_loads_all_threads
= max_loads_all_threads
;
726 params
->progress_interval
= progress_interval
;
727 params
->deferRegistration
= defer_registration
;
728 params
->clock
= clock
;
729 params
->functionTrace
= function_trace
;
730 params
->functionTraceStart
= function_trace_start
;
732 params
->system
= system
;
733 params
->cpu_id
= cpu_id
;
738 params
->profile
= profile
;
740 params
->process
= workload
;
743 TimingSimpleCPU
*cpu
= new TimingSimpleCPU(params
);
747 REGISTER_SIM_OBJECT("TimingSimpleCPU", TimingSimpleCPU
)