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/utility.hh"
32 #include "cpu/exetrace.hh"
33 #include "cpu/simple/timing.hh"
34 #include "mem/packet_impl.hh"
35 #include "sim/builder.hh"
38 using namespace TheISA
;
42 TimingSimpleCPU::init()
44 //Create Memory Ports (conect them up)
45 Port
*mem_dport
= mem
->getPort("");
46 dcachePort
.setPeer(mem_dport
);
47 mem_dport
->setPeer(&dcachePort
);
49 Port
*mem_iport
= mem
->getPort("");
50 icachePort
.setPeer(mem_iport
);
51 mem_iport
->setPeer(&icachePort
);
55 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
56 ThreadContext
*tc
= threadContexts
[i
];
58 // initialize CPU, including PC
59 TheISA::initCPU(tc
, tc
->readCpuId());
65 TimingSimpleCPU::CpuPort::recvAtomic(Packet
*pkt
)
67 panic("TimingSimpleCPU doesn't expect recvAtomic callback!");
72 TimingSimpleCPU::CpuPort::recvFunctional(Packet
*pkt
)
74 panic("TimingSimpleCPU doesn't expect recvFunctional callback!");
78 TimingSimpleCPU::CpuPort::recvStatusChange(Status status
)
80 if (status
== RangeChange
)
83 panic("TimingSimpleCPU doesn't expect recvStatusChange callback!");
86 TimingSimpleCPU::TimingSimpleCPU(Params
*p
)
87 : BaseSimpleCPU(p
), icachePort(this), dcachePort(this)
90 ifetch_pkt
= dcache_pkt
= NULL
;
93 state
= SimObject::Timing
;
97 TimingSimpleCPU::~TimingSimpleCPU()
102 TimingSimpleCPU::serialize(ostream
&os
)
104 SERIALIZE_ENUM(_status
);
105 BaseSimpleCPU::serialize(os
);
109 TimingSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
111 UNSERIALIZE_ENUM(_status
);
112 BaseSimpleCPU::unserialize(cp
, section
);
116 TimingSimpleCPU::drain(Event
*drain_event
)
118 // TimingSimpleCPU is ready to drain if it's not waiting for
119 // an access to complete.
120 if (status() == Idle
|| status() == Running
|| status() == SwitchedOut
) {
121 changeState(SimObject::DrainedTiming
);
124 changeState(SimObject::Draining
);
125 drainEvent
= drain_event
;
131 TimingSimpleCPU::resume()
133 if (_status
!= SwitchedOut
&& _status
!= Idle
) {
134 // Delete the old event if it existed.
136 assert(!fetchEvent
->scheduled());
141 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
142 fetchEvent
->schedule(curTick
);
147 TimingSimpleCPU::setMemoryMode(State new_mode
)
149 assert(new_mode
== SimObject::Timing
);
153 TimingSimpleCPU::switchOut()
155 assert(status() == Running
|| status() == Idle
);
156 _status
= SwitchedOut
;
158 // If we've been scheduled to resume but are then told to switch out,
159 // we'll need to cancel it.
160 if (fetchEvent
&& fetchEvent
->scheduled())
161 fetchEvent
->deschedule();
166 TimingSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
168 BaseCPU::takeOverFrom(oldCPU
);
170 // if any of this CPU's ThreadContexts are active, mark the CPU as
171 // running and schedule its tick event.
172 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
173 ThreadContext
*tc
= threadContexts
[i
];
174 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
183 TimingSimpleCPU::activateContext(int thread_num
, int delay
)
185 assert(thread_num
== 0);
188 assert(_status
== Idle
);
192 // kick things off by initiating the fetch of the next instruction
194 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
195 fetchEvent
->schedule(curTick
+ cycles(delay
));
200 TimingSimpleCPU::suspendContext(int thread_num
)
202 assert(thread_num
== 0);
205 assert(_status
== Running
);
207 // just change status to Idle... if status != Running,
208 // completeInst() will not initiate fetch of next instruction.
217 TimingSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
219 // need to fill in CPU & thread IDs here
220 Request
*data_read_req
= new Request();
221 data_read_req
->setThreadContext(0,0); //Need CPU/Thread IDS HERE
222 data_read_req
->setVirt(0, addr
, sizeof(T
), flags
, thread
->readPC());
225 traceData
->setAddr(data_read_req
->getVaddr());
228 // translate to physical address
229 Fault fault
= thread
->translateDataReadReq(data_read_req
);
231 // Now do the access.
232 if (fault
== NoFault
) {
233 Packet
*data_read_pkt
=
234 new Packet(data_read_req
, Packet::ReadReq
, Packet::Broadcast
);
235 data_read_pkt
->dataDynamic
<T
>(new T
);
237 if (!dcachePort
.sendTiming(data_read_pkt
)) {
238 _status
= DcacheRetry
;
239 dcache_pkt
= data_read_pkt
;
241 _status
= DcacheWaitResponse
;
246 // This will need a new way to tell if it has a dcache attached.
247 if (data_read_req
->getFlags() & UNCACHEABLE
)
248 recordEvent("Uncached Read");
253 #ifndef DOXYGEN_SHOULD_SKIP_THIS
257 TimingSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
261 TimingSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
265 TimingSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
269 TimingSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
271 #endif //DOXYGEN_SHOULD_SKIP_THIS
275 TimingSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
277 return read(addr
, *(uint64_t*)&data
, flags
);
282 TimingSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
284 return read(addr
, *(uint32_t*)&data
, flags
);
290 TimingSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
292 return read(addr
, (uint32_t&)data
, flags
);
298 TimingSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
300 // need to fill in CPU & thread IDs here
301 Request
*data_write_req
= new Request();
302 data_write_req
->setThreadContext(0,0); //Need CPU/Thread IDS HERE
303 data_write_req
->setVirt(0, addr
, sizeof(T
), flags
, thread
->readPC());
305 // translate to physical address
306 Fault fault
= thread
->translateDataWriteReq(data_write_req
);
307 // Now do the access.
308 if (fault
== NoFault
) {
309 Packet
*data_write_pkt
=
310 new Packet(data_write_req
, Packet::WriteReq
, Packet::Broadcast
);
311 data_write_pkt
->allocate();
312 data_write_pkt
->set(data
);
314 if (!dcachePort
.sendTiming(data_write_pkt
)) {
315 _status
= DcacheRetry
;
316 dcache_pkt
= data_write_pkt
;
318 _status
= DcacheWaitResponse
;
323 // This will need a new way to tell if it's hooked up to a cache or not.
324 if (data_write_req
->getFlags() & UNCACHEABLE
)
325 recordEvent("Uncached Write");
327 // If the write needs to have a fault on the access, consider calling
328 // changeStatus() and changing it to "bad addr write" or something.
333 #ifndef DOXYGEN_SHOULD_SKIP_THIS
336 TimingSimpleCPU::write(uint64_t data
, Addr addr
,
337 unsigned flags
, uint64_t *res
);
341 TimingSimpleCPU::write(uint32_t data
, Addr addr
,
342 unsigned flags
, uint64_t *res
);
346 TimingSimpleCPU::write(uint16_t data
, Addr addr
,
347 unsigned flags
, uint64_t *res
);
351 TimingSimpleCPU::write(uint8_t data
, Addr addr
,
352 unsigned flags
, uint64_t *res
);
354 #endif //DOXYGEN_SHOULD_SKIP_THIS
358 TimingSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
360 return write(*(uint64_t*)&data
, addr
, flags
, res
);
365 TimingSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
367 return write(*(uint32_t*)&data
, addr
, flags
, res
);
373 TimingSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
375 return write((uint32_t)data
, addr
, flags
, res
);
380 TimingSimpleCPU::fetch()
382 checkForInterrupts();
384 // need to fill in CPU & thread IDs here
385 Request
*ifetch_req
= new Request();
386 ifetch_req
->setThreadContext(0,0); //Need CPU/Thread IDS HERE
387 Fault fault
= setupFetchRequest(ifetch_req
);
389 ifetch_pkt
= new Packet(ifetch_req
, Packet::ReadReq
, Packet::Broadcast
);
390 ifetch_pkt
->dataStatic(&inst
);
392 if (fault
== NoFault
) {
393 if (!icachePort
.sendTiming(ifetch_pkt
)) {
394 // Need to wait for retry
395 _status
= IcacheRetry
;
397 // Need to wait for cache to respond
398 _status
= IcacheWaitResponse
;
399 // ownership of packet transferred to memory system
403 // fetch fault: advance directly to next instruction (fault handler)
410 TimingSimpleCPU::advanceInst(Fault fault
)
414 if (_status
== Running
) {
415 // kick off fetch of next instruction... callback from icache
416 // response will cause that instruction to be executed,
417 // keeping the CPU running.
424 TimingSimpleCPU::completeIfetch(Packet
*pkt
)
426 // received a response from the icache: execute the received
428 assert(pkt
->result
== Packet::Success
);
429 assert(_status
== IcacheWaitResponse
);
436 if (getState() == SimObject::Draining
) {
442 if (curStaticInst
->isMemRef() && !curStaticInst
->isDataPrefetch()) {
443 // load or store: just send to dcache
444 Fault fault
= curStaticInst
->initiateAcc(this, traceData
);
445 if (fault
== NoFault
) {
446 // successfully initiated access: instruction will
447 // complete in dcache response callback
448 assert(_status
== DcacheWaitResponse
);
450 // fault: complete now to invoke fault handler
455 // non-memory instruction: execute completely now
456 Fault fault
= curStaticInst
->execute(this, traceData
);
464 TimingSimpleCPU::IcachePort::recvTiming(Packet
*pkt
)
466 if (cpu
->_status
== DcacheWaitResponse
)
467 cpu
->completeDataAccess(pkt
);
468 else if (cpu
->_status
== IcacheWaitResponse
)
469 cpu
->completeIfetch(pkt
);
476 TimingSimpleCPU::IcachePort::recvRetry()
478 // we shouldn't get a retry unless we have a packet that we're
479 // waiting to transmit
480 assert(cpu
->ifetch_pkt
!= NULL
);
481 assert(cpu
->_status
== IcacheRetry
);
482 Packet
*tmp
= cpu
->ifetch_pkt
;
483 if (sendTiming(tmp
)) {
484 cpu
->_status
= IcacheWaitResponse
;
485 cpu
->ifetch_pkt
= NULL
;
490 TimingSimpleCPU::completeDataAccess(Packet
*pkt
)
492 // received a response from the dcache: complete the load or store
494 assert(pkt
->result
== Packet::Success
);
495 assert(_status
== DcacheWaitResponse
);
498 if (getState() == SimObject::Draining
) {
507 Fault fault
= curStaticInst
->completeAcc(pkt
, this, traceData
);
518 TimingSimpleCPU::completeDrain()
520 DPRINTF(Config
, "Done draining\n");
521 changeState(SimObject::DrainedTiming
);
522 drainEvent
->process();
526 TimingSimpleCPU::DcachePort::recvTiming(Packet
*pkt
)
528 cpu
->completeDataAccess(pkt
);
533 TimingSimpleCPU::DcachePort::recvRetry()
535 // we shouldn't get a retry unless we have a packet that we're
536 // waiting to transmit
537 assert(cpu
->dcache_pkt
!= NULL
);
538 assert(cpu
->_status
== DcacheRetry
);
539 Packet
*tmp
= cpu
->dcache_pkt
;
540 if (sendTiming(tmp
)) {
541 cpu
->_status
= DcacheWaitResponse
;
542 cpu
->dcache_pkt
= NULL
;
547 ////////////////////////////////////////////////////////////////////////
549 // TimingSimpleCPU Simulation Object
551 BEGIN_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
553 Param
<Counter
> max_insts_any_thread
;
554 Param
<Counter
> max_insts_all_threads
;
555 Param
<Counter
> max_loads_any_thread
;
556 Param
<Counter
> max_loads_all_threads
;
557 SimObjectParam
<MemObject
*> mem
;
560 SimObjectParam
<AlphaITB
*> itb
;
561 SimObjectParam
<AlphaDTB
*> dtb
;
562 SimObjectParam
<System
*> system
;
566 SimObjectParam
<Process
*> workload
;
567 #endif // FULL_SYSTEM
571 Param
<bool> defer_registration
;
573 Param
<bool> function_trace
;
574 Param
<Tick
> function_trace_start
;
575 Param
<bool> simulate_stalls
;
577 END_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
579 BEGIN_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
581 INIT_PARAM(max_insts_any_thread
,
582 "terminate when any thread reaches this inst count"),
583 INIT_PARAM(max_insts_all_threads
,
584 "terminate when all threads have reached this inst count"),
585 INIT_PARAM(max_loads_any_thread
,
586 "terminate when any thread reaches this load count"),
587 INIT_PARAM(max_loads_all_threads
,
588 "terminate when all threads have reached this load count"),
589 INIT_PARAM(mem
, "memory"),
592 INIT_PARAM(itb
, "Instruction TLB"),
593 INIT_PARAM(dtb
, "Data TLB"),
594 INIT_PARAM(system
, "system object"),
595 INIT_PARAM(cpu_id
, "processor ID"),
596 INIT_PARAM(profile
, ""),
598 INIT_PARAM(workload
, "processes to run"),
599 #endif // FULL_SYSTEM
601 INIT_PARAM(clock
, "clock speed"),
602 INIT_PARAM(defer_registration
, "defer system registration (for sampling)"),
603 INIT_PARAM(width
, "cpu width"),
604 INIT_PARAM(function_trace
, "Enable function trace"),
605 INIT_PARAM(function_trace_start
, "Cycle to start function trace"),
606 INIT_PARAM(simulate_stalls
, "Simulate cache stall cycles")
608 END_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
611 CREATE_SIM_OBJECT(TimingSimpleCPU
)
613 TimingSimpleCPU::Params
*params
= new TimingSimpleCPU::Params();
614 params
->name
= getInstanceName();
615 params
->numberOfThreads
= 1;
616 params
->max_insts_any_thread
= max_insts_any_thread
;
617 params
->max_insts_all_threads
= max_insts_all_threads
;
618 params
->max_loads_any_thread
= max_loads_any_thread
;
619 params
->max_loads_all_threads
= max_loads_all_threads
;
620 params
->deferRegistration
= defer_registration
;
621 params
->clock
= clock
;
622 params
->functionTrace
= function_trace
;
623 params
->functionTraceStart
= function_trace_start
;
629 params
->system
= system
;
630 params
->cpu_id
= cpu_id
;
631 params
->profile
= profile
;
633 params
->process
= workload
;
636 TimingSimpleCPU
*cpu
= new TimingSimpleCPU(params
);
640 REGISTER_SIM_OBJECT("TimingSimpleCPU", TimingSimpleCPU
)