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_impl.hh"
36 #include "sim/builder.hh"
37 #include "sim/system.hh"
40 using namespace TheISA
;
43 TimingSimpleCPU::getPort(const std::string
&if_name
, int idx
)
45 if (if_name
== "dcache_port")
47 else if (if_name
== "icache_port")
50 panic("No Such Port\n");
54 TimingSimpleCPU::init()
58 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
59 ThreadContext
*tc
= threadContexts
[i
];
61 // initialize CPU, including PC
62 TheISA::initCPU(tc
, tc
->readCpuId());
68 TimingSimpleCPU::CpuPort::recvAtomic(Packet
*pkt
)
70 panic("TimingSimpleCPU doesn't expect recvAtomic callback!");
75 TimingSimpleCPU::CpuPort::recvFunctional(Packet
*pkt
)
77 panic("TimingSimpleCPU doesn't expect recvFunctional callback!");
81 TimingSimpleCPU::CpuPort::recvStatusChange(Status status
)
83 if (status
== RangeChange
)
86 panic("TimingSimpleCPU doesn't expect recvStatusChange callback!");
91 TimingSimpleCPU::CpuPort::TickEvent::schedule(Packet
*_pkt
, Tick t
)
97 TimingSimpleCPU::TimingSimpleCPU(Params
*p
)
98 : BaseSimpleCPU(p
), icachePort(this, p
->clock
), dcachePort(this, p
->clock
),
102 ifetch_pkt
= dcache_pkt
= NULL
;
105 changeState(SimObject::Running
);
109 TimingSimpleCPU::~TimingSimpleCPU()
114 TimingSimpleCPU::serialize(ostream
&os
)
116 SimObject::State so_state
= SimObject::getState();
117 SERIALIZE_ENUM(so_state
);
118 BaseSimpleCPU::serialize(os
);
122 TimingSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
124 SimObject::State so_state
;
125 UNSERIALIZE_ENUM(so_state
);
126 BaseSimpleCPU::unserialize(cp
, section
);
130 TimingSimpleCPU::drain(Event
*drain_event
)
132 // TimingSimpleCPU is ready to drain if it's not waiting for
133 // an access to complete.
134 if (status() == Idle
|| status() == Running
|| status() == SwitchedOut
) {
135 changeState(SimObject::Drained
);
138 changeState(SimObject::Draining
);
139 drainEvent
= drain_event
;
145 TimingSimpleCPU::resume()
147 if (_status
!= SwitchedOut
&& _status
!= Idle
) {
148 // Delete the old event if it existed.
150 if (fetchEvent
->scheduled())
151 fetchEvent
->deschedule();
157 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
158 fetchEvent
->schedule(curTick
);
161 assert(system
->getMemoryMode() == System::Timing
);
162 changeState(SimObject::Running
);
166 TimingSimpleCPU::switchOut()
168 assert(status() == Running
|| status() == Idle
);
169 _status
= SwitchedOut
;
171 // If we've been scheduled to resume but are then told to switch out,
172 // we'll need to cancel it.
173 if (fetchEvent
&& fetchEvent
->scheduled())
174 fetchEvent
->deschedule();
179 TimingSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
181 BaseCPU::takeOverFrom(oldCPU
);
183 // if any of this CPU's ThreadContexts are active, mark the CPU as
184 // running and schedule its tick event.
185 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
186 ThreadContext
*tc
= threadContexts
[i
];
187 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
196 TimingSimpleCPU::activateContext(int thread_num
, int delay
)
198 assert(thread_num
== 0);
201 assert(_status
== Idle
);
205 // kick things off by initiating the fetch of the next instruction
207 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
208 fetchEvent
->schedule(curTick
+ cycles(delay
));
213 TimingSimpleCPU::suspendContext(int thread_num
)
215 assert(thread_num
== 0);
218 assert(_status
== Running
);
220 // just change status to Idle... if status != Running,
221 // completeInst() will not initiate fetch of next instruction.
230 TimingSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
233 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
234 cpu_id
, /* thread ID */ 0);
237 traceData
->setAddr(req
->getVaddr());
240 // translate to physical address
241 Fault fault
= thread
->translateDataReadReq(req
);
243 // Now do the access.
244 if (fault
== NoFault
) {
246 new Packet(req
, Packet::ReadReq
, Packet::Broadcast
);
247 pkt
->dataDynamic
<T
>(new T
);
249 if (!dcachePort
.sendTiming(pkt
)) {
250 _status
= DcacheRetry
;
253 _status
= DcacheWaitResponse
;
254 // memory system takes ownership of packet
259 // This will need a new way to tell if it has a dcache attached.
260 if (req
->isUncacheable())
261 recordEvent("Uncached Read");
266 #ifndef DOXYGEN_SHOULD_SKIP_THIS
270 TimingSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
274 TimingSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
278 TimingSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
282 TimingSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
284 #endif //DOXYGEN_SHOULD_SKIP_THIS
288 TimingSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
290 return read(addr
, *(uint64_t*)&data
, flags
);
295 TimingSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
297 return read(addr
, *(uint32_t*)&data
, flags
);
303 TimingSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
305 return read(addr
, (uint32_t&)data
, flags
);
311 TimingSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
314 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
315 cpu_id
, /* thread ID */ 0);
317 // translate to physical address
318 Fault fault
= thread
->translateDataWriteReq(req
);
320 // Now do the access.
321 if (fault
== NoFault
) {
322 assert(dcache_pkt
== NULL
);
323 dcache_pkt
= new Packet(req
, Packet::WriteReq
, Packet::Broadcast
);
324 dcache_pkt
->allocate();
325 dcache_pkt
->set(data
);
327 bool do_access
= true; // flag to suppress cache access
329 if (req
->isLocked()) {
330 do_access
= TheISA::handleLockedWrite(thread
, req
);
334 if (!dcachePort
.sendTiming(dcache_pkt
)) {
335 _status
= DcacheRetry
;
337 _status
= DcacheWaitResponse
;
338 // memory system takes ownership of packet
344 // This will need a new way to tell if it's hooked up to a cache or not.
345 if (req
->isUncacheable())
346 recordEvent("Uncached Write");
348 // If the write needs to have a fault on the access, consider calling
349 // changeStatus() and changing it to "bad addr write" or something.
354 #ifndef DOXYGEN_SHOULD_SKIP_THIS
357 TimingSimpleCPU::write(uint64_t data
, Addr addr
,
358 unsigned flags
, uint64_t *res
);
362 TimingSimpleCPU::write(uint32_t data
, Addr addr
,
363 unsigned flags
, uint64_t *res
);
367 TimingSimpleCPU::write(uint16_t data
, Addr addr
,
368 unsigned flags
, uint64_t *res
);
372 TimingSimpleCPU::write(uint8_t data
, Addr addr
,
373 unsigned flags
, uint64_t *res
);
375 #endif //DOXYGEN_SHOULD_SKIP_THIS
379 TimingSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
381 return write(*(uint64_t*)&data
, addr
, flags
, res
);
386 TimingSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
388 return write(*(uint32_t*)&data
, addr
, flags
, res
);
394 TimingSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
396 return write((uint32_t)data
, addr
, flags
, res
);
401 TimingSimpleCPU::fetch()
403 checkForInterrupts();
405 Request
*ifetch_req
= new Request();
406 ifetch_req
->setThreadContext(cpu_id
, /* thread ID */ 0);
407 Fault fault
= setupFetchRequest(ifetch_req
);
409 ifetch_pkt
= new Packet(ifetch_req
, Packet::ReadReq
, Packet::Broadcast
);
410 ifetch_pkt
->dataStatic(&inst
);
412 if (fault
== NoFault
) {
413 if (!icachePort
.sendTiming(ifetch_pkt
)) {
414 // Need to wait for retry
415 _status
= IcacheRetry
;
417 // Need to wait for cache to respond
418 _status
= IcacheWaitResponse
;
419 // ownership of packet transferred to memory system
423 // fetch fault: advance directly to next instruction (fault handler)
430 TimingSimpleCPU::advanceInst(Fault fault
)
434 if (_status
== Running
) {
435 // kick off fetch of next instruction... callback from icache
436 // response will cause that instruction to be executed,
437 // keeping the CPU running.
444 TimingSimpleCPU::completeIfetch(Packet
*pkt
)
446 // received a response from the icache: execute the received
448 assert(pkt
->result
== Packet::Success
);
449 assert(_status
== IcacheWaitResponse
);
456 if (getState() == SimObject::Draining
) {
462 if (curStaticInst
->isMemRef() && !curStaticInst
->isDataPrefetch()) {
463 // load or store: just send to dcache
464 Fault fault
= curStaticInst
->initiateAcc(this, traceData
);
465 if (_status
!= Running
) {
466 // instruction will complete in dcache response callback
467 assert(_status
== DcacheWaitResponse
|| _status
== DcacheRetry
);
468 assert(fault
== NoFault
);
470 if (fault
== NoFault
) {
471 // early fail on store conditional: complete now
472 assert(dcache_pkt
!= NULL
);
473 fault
= curStaticInst
->completeAcc(dcache_pkt
, this,
475 delete dcache_pkt
->req
;
483 // non-memory instruction: execute completely now
484 Fault fault
= curStaticInst
->execute(this, traceData
);
491 TimingSimpleCPU::IcachePort::ITickEvent::process()
493 cpu
->completeIfetch(pkt
);
497 TimingSimpleCPU::IcachePort::recvTiming(Packet
*pkt
)
499 // delay processing of returned data until next CPU clock edge
500 Tick time
= pkt
->req
->getTime();
501 while (time
< curTick
)
505 cpu
->completeIfetch(pkt
);
507 tickEvent
.schedule(pkt
, time
);
513 TimingSimpleCPU::IcachePort::recvRetry()
515 // we shouldn't get a retry unless we have a packet that we're
516 // waiting to transmit
517 assert(cpu
->ifetch_pkt
!= NULL
);
518 assert(cpu
->_status
== IcacheRetry
);
519 Packet
*tmp
= cpu
->ifetch_pkt
;
520 if (sendTiming(tmp
)) {
521 cpu
->_status
= IcacheWaitResponse
;
522 cpu
->ifetch_pkt
= NULL
;
527 TimingSimpleCPU::completeDataAccess(Packet
*pkt
)
529 // received a response from the dcache: complete the load or store
531 assert(pkt
->result
== Packet::Success
);
532 assert(_status
== DcacheWaitResponse
);
535 if (getState() == SimObject::Draining
) {
544 Fault fault
= curStaticInst
->completeAcc(pkt
, this, traceData
);
546 if (pkt
->isRead() && pkt
->req
->isLocked()) {
547 TheISA::handleLockedRead(thread
, pkt
->req
);
559 TimingSimpleCPU::completeDrain()
561 DPRINTF(Config
, "Done draining\n");
562 changeState(SimObject::Drained
);
563 drainEvent
->process();
567 TimingSimpleCPU::DcachePort::recvTiming(Packet
*pkt
)
569 // delay processing of returned data until next CPU clock edge
570 Tick time
= pkt
->req
->getTime();
571 while (time
< curTick
)
575 cpu
->completeDataAccess(pkt
);
577 tickEvent
.schedule(pkt
, time
);
583 TimingSimpleCPU::DcachePort::DTickEvent::process()
585 cpu
->completeDataAccess(pkt
);
589 TimingSimpleCPU::DcachePort::recvRetry()
591 // we shouldn't get a retry unless we have a packet that we're
592 // waiting to transmit
593 assert(cpu
->dcache_pkt
!= NULL
);
594 assert(cpu
->_status
== DcacheRetry
);
595 Packet
*tmp
= cpu
->dcache_pkt
;
596 if (sendTiming(tmp
)) {
597 cpu
->_status
= DcacheWaitResponse
;
598 // memory system takes ownership of packet
599 cpu
->dcache_pkt
= NULL
;
604 ////////////////////////////////////////////////////////////////////////
606 // TimingSimpleCPU Simulation Object
608 BEGIN_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
610 Param
<Counter
> max_insts_any_thread
;
611 Param
<Counter
> max_insts_all_threads
;
612 Param
<Counter
> max_loads_any_thread
;
613 Param
<Counter
> max_loads_all_threads
;
614 Param
<Tick
> progress_interval
;
615 SimObjectParam
<MemObject
*> mem
;
616 SimObjectParam
<System
*> system
;
620 SimObjectParam
<AlphaITB
*> itb
;
621 SimObjectParam
<AlphaDTB
*> dtb
;
624 SimObjectParam
<Process
*> workload
;
625 #endif // FULL_SYSTEM
629 Param
<bool> defer_registration
;
631 Param
<bool> function_trace
;
632 Param
<Tick
> function_trace_start
;
633 Param
<bool> simulate_stalls
;
635 END_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
637 BEGIN_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
639 INIT_PARAM(max_insts_any_thread
,
640 "terminate when any thread reaches this inst count"),
641 INIT_PARAM(max_insts_all_threads
,
642 "terminate when all threads have reached this inst count"),
643 INIT_PARAM(max_loads_any_thread
,
644 "terminate when any thread reaches this load count"),
645 INIT_PARAM(max_loads_all_threads
,
646 "terminate when all threads have reached this load count"),
647 INIT_PARAM(progress_interval
, "Progress interval"),
648 INIT_PARAM(mem
, "memory"),
649 INIT_PARAM(system
, "system object"),
650 INIT_PARAM(cpu_id
, "processor ID"),
653 INIT_PARAM(itb
, "Instruction TLB"),
654 INIT_PARAM(dtb
, "Data TLB"),
655 INIT_PARAM(profile
, ""),
657 INIT_PARAM(workload
, "processes to run"),
658 #endif // FULL_SYSTEM
660 INIT_PARAM(clock
, "clock speed"),
661 INIT_PARAM(defer_registration
, "defer system registration (for sampling)"),
662 INIT_PARAM(width
, "cpu width"),
663 INIT_PARAM(function_trace
, "Enable function trace"),
664 INIT_PARAM(function_trace_start
, "Cycle to start function trace"),
665 INIT_PARAM(simulate_stalls
, "Simulate cache stall cycles")
667 END_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
670 CREATE_SIM_OBJECT(TimingSimpleCPU
)
672 TimingSimpleCPU::Params
*params
= new TimingSimpleCPU::Params();
673 params
->name
= getInstanceName();
674 params
->numberOfThreads
= 1;
675 params
->max_insts_any_thread
= max_insts_any_thread
;
676 params
->max_insts_all_threads
= max_insts_all_threads
;
677 params
->max_loads_any_thread
= max_loads_any_thread
;
678 params
->max_loads_all_threads
= max_loads_all_threads
;
679 params
->progress_interval
= progress_interval
;
680 params
->deferRegistration
= defer_registration
;
681 params
->clock
= clock
;
682 params
->functionTrace
= function_trace
;
683 params
->functionTraceStart
= function_trace_start
;
685 params
->system
= system
;
686 params
->cpu_id
= cpu_id
;
691 params
->profile
= profile
;
693 params
->process
= workload
;
696 TimingSimpleCPU
*cpu
= new TimingSimpleCPU(params
);
700 REGISTER_SIM_OBJECT("TimingSimpleCPU", TimingSimpleCPU
)