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 //No internal storage to update, jusst return
82 TimingSimpleCPU::CpuPort::recvStatusChange(Status status
)
84 if (status
== RangeChange
)
87 panic("TimingSimpleCPU doesn't expect recvStatusChange callback!");
92 TimingSimpleCPU::CpuPort::TickEvent::schedule(Packet
*_pkt
, Tick t
)
98 TimingSimpleCPU::TimingSimpleCPU(Params
*p
)
99 : BaseSimpleCPU(p
), icachePort(this, p
->clock
), dcachePort(this, p
->clock
),
103 ifetch_pkt
= dcache_pkt
= NULL
;
107 changeState(SimObject::Running
);
111 TimingSimpleCPU::~TimingSimpleCPU()
116 TimingSimpleCPU::serialize(ostream
&os
)
118 SimObject::State so_state
= SimObject::getState();
119 SERIALIZE_ENUM(so_state
);
120 BaseSimpleCPU::serialize(os
);
124 TimingSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
126 SimObject::State so_state
;
127 UNSERIALIZE_ENUM(so_state
);
128 BaseSimpleCPU::unserialize(cp
, section
);
132 TimingSimpleCPU::drain(Event
*drain_event
)
134 // TimingSimpleCPU is ready to drain if it's not waiting for
135 // an access to complete.
136 if (status() == Idle
|| status() == Running
|| status() == SwitchedOut
) {
137 changeState(SimObject::Drained
);
140 changeState(SimObject::Draining
);
141 drainEvent
= drain_event
;
147 TimingSimpleCPU::resume()
149 if (_status
!= SwitchedOut
&& _status
!= Idle
) {
150 // Delete the old event if it existed.
152 if (fetchEvent
->scheduled())
153 fetchEvent
->deschedule();
159 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
160 fetchEvent
->schedule(curTick
);
163 assert(system
->getMemoryMode() == System::Timing
);
164 changeState(SimObject::Running
);
165 previousTick
= curTick
;
169 TimingSimpleCPU::switchOut()
171 assert(status() == Running
|| status() == Idle
);
172 _status
= SwitchedOut
;
173 numCycles
+= curTick
- previousTick
;
175 // If we've been scheduled to resume but are then told to switch out,
176 // we'll need to cancel it.
177 if (fetchEvent
&& fetchEvent
->scheduled())
178 fetchEvent
->deschedule();
183 TimingSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
185 BaseCPU::takeOverFrom(oldCPU
);
187 // if any of this CPU's ThreadContexts are active, mark the CPU as
188 // running and schedule its tick event.
189 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
190 ThreadContext
*tc
= threadContexts
[i
];
191 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
197 if (_status
!= Running
) {
202 if (icachePort
.getPeer() == NULL
) {
203 peer
= oldCPU
->getPort("icache_port")->getPeer();
204 icachePort
.setPeer(peer
);
206 peer
= icachePort
.getPeer();
208 peer
->setPeer(&icachePort
);
210 if (dcachePort
.getPeer() == NULL
) {
211 peer
= oldCPU
->getPort("dcache_port")->getPeer();
212 dcachePort
.setPeer(peer
);
214 peer
= dcachePort
.getPeer();
216 peer
->setPeer(&dcachePort
);
221 TimingSimpleCPU::activateContext(int thread_num
, int delay
)
223 assert(thread_num
== 0);
226 assert(_status
== Idle
);
230 // kick things off by initiating the fetch of the next instruction
232 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
233 fetchEvent
->schedule(curTick
+ cycles(delay
));
238 TimingSimpleCPU::suspendContext(int thread_num
)
240 assert(thread_num
== 0);
243 assert(_status
== Running
);
245 // just change status to Idle... if status != Running,
246 // completeInst() will not initiate fetch of next instruction.
255 TimingSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
258 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
259 cpu_id
, /* thread ID */ 0);
262 traceData
->setAddr(req
->getVaddr());
265 // translate to physical address
266 Fault fault
= thread
->translateDataReadReq(req
);
268 // Now do the access.
269 if (fault
== NoFault
) {
271 new Packet(req
, Packet::ReadReq
, Packet::Broadcast
);
272 pkt
->dataDynamic
<T
>(new T
);
274 if (!dcachePort
.sendTiming(pkt
)) {
275 _status
= DcacheRetry
;
278 _status
= DcacheWaitResponse
;
279 // memory system takes ownership of packet
284 // This will need a new way to tell if it has a dcache attached.
285 if (req
->isUncacheable())
286 recordEvent("Uncached Read");
291 #ifndef DOXYGEN_SHOULD_SKIP_THIS
295 TimingSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
299 TimingSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
303 TimingSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
307 TimingSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
309 #endif //DOXYGEN_SHOULD_SKIP_THIS
313 TimingSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
315 return read(addr
, *(uint64_t*)&data
, flags
);
320 TimingSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
322 return read(addr
, *(uint32_t*)&data
, flags
);
328 TimingSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
330 return read(addr
, (uint32_t&)data
, flags
);
336 TimingSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
339 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
340 cpu_id
, /* thread ID */ 0);
342 // translate to physical address
343 Fault fault
= thread
->translateDataWriteReq(req
);
345 // Now do the access.
346 if (fault
== NoFault
) {
347 assert(dcache_pkt
== NULL
);
348 dcache_pkt
= new Packet(req
, Packet::WriteReq
, Packet::Broadcast
);
349 dcache_pkt
->allocate();
350 dcache_pkt
->set(data
);
352 bool do_access
= true; // flag to suppress cache access
354 if (req
->isLocked()) {
355 do_access
= TheISA::handleLockedWrite(thread
, req
);
359 if (!dcachePort
.sendTiming(dcache_pkt
)) {
360 _status
= DcacheRetry
;
362 _status
= DcacheWaitResponse
;
363 // memory system takes ownership of packet
369 // This will need a new way to tell if it's hooked up to a cache or not.
370 if (req
->isUncacheable())
371 recordEvent("Uncached Write");
373 // If the write needs to have a fault on the access, consider calling
374 // changeStatus() and changing it to "bad addr write" or something.
379 #ifndef DOXYGEN_SHOULD_SKIP_THIS
382 TimingSimpleCPU::write(uint64_t data
, Addr addr
,
383 unsigned flags
, uint64_t *res
);
387 TimingSimpleCPU::write(uint32_t data
, Addr addr
,
388 unsigned flags
, uint64_t *res
);
392 TimingSimpleCPU::write(uint16_t data
, Addr addr
,
393 unsigned flags
, uint64_t *res
);
397 TimingSimpleCPU::write(uint8_t data
, Addr addr
,
398 unsigned flags
, uint64_t *res
);
400 #endif //DOXYGEN_SHOULD_SKIP_THIS
404 TimingSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
406 return write(*(uint64_t*)&data
, addr
, flags
, res
);
411 TimingSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
413 return write(*(uint32_t*)&data
, addr
, flags
, res
);
419 TimingSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
421 return write((uint32_t)data
, addr
, flags
, res
);
426 TimingSimpleCPU::fetch()
428 checkForInterrupts();
430 Request
*ifetch_req
= new Request();
431 ifetch_req
->setThreadContext(cpu_id
, /* thread ID */ 0);
432 Fault fault
= setupFetchRequest(ifetch_req
);
434 ifetch_pkt
= new Packet(ifetch_req
, Packet::ReadReq
, Packet::Broadcast
);
435 ifetch_pkt
->dataStatic(&inst
);
437 if (fault
== NoFault
) {
438 if (!icachePort
.sendTiming(ifetch_pkt
)) {
439 // Need to wait for retry
440 _status
= IcacheRetry
;
442 // Need to wait for cache to respond
443 _status
= IcacheWaitResponse
;
444 // ownership of packet transferred to memory system
448 // fetch fault: advance directly to next instruction (fault handler)
452 numCycles
+= curTick
- previousTick
;
453 previousTick
= curTick
;
458 TimingSimpleCPU::advanceInst(Fault fault
)
462 if (_status
== Running
) {
463 // kick off fetch of next instruction... callback from icache
464 // response will cause that instruction to be executed,
465 // keeping the CPU running.
472 TimingSimpleCPU::completeIfetch(Packet
*pkt
)
474 // received a response from the icache: execute the received
476 assert(pkt
->result
== Packet::Success
);
477 assert(_status
== IcacheWaitResponse
);
484 numCycles
+= curTick
- previousTick
;
485 previousTick
= curTick
;
487 if (getState() == SimObject::Draining
) {
493 if (curStaticInst
->isMemRef() && !curStaticInst
->isDataPrefetch()) {
494 // load or store: just send to dcache
495 Fault fault
= curStaticInst
->initiateAcc(this, traceData
);
496 if (_status
!= Running
) {
497 // instruction will complete in dcache response callback
498 assert(_status
== DcacheWaitResponse
|| _status
== DcacheRetry
);
499 assert(fault
== NoFault
);
501 if (fault
== NoFault
) {
502 // early fail on store conditional: complete now
503 assert(dcache_pkt
!= NULL
);
504 fault
= curStaticInst
->completeAcc(dcache_pkt
, this,
506 delete dcache_pkt
->req
;
514 // non-memory instruction: execute completely now
515 Fault fault
= curStaticInst
->execute(this, traceData
);
522 TimingSimpleCPU::IcachePort::ITickEvent::process()
524 cpu
->completeIfetch(pkt
);
528 TimingSimpleCPU::IcachePort::recvTiming(Packet
*pkt
)
530 // delay processing of returned data until next CPU clock edge
531 Tick time
= pkt
->req
->getTime();
532 while (time
< curTick
)
536 cpu
->completeIfetch(pkt
);
538 tickEvent
.schedule(pkt
, time
);
544 TimingSimpleCPU::IcachePort::recvRetry()
546 // we shouldn't get a retry unless we have a packet that we're
547 // waiting to transmit
548 assert(cpu
->ifetch_pkt
!= NULL
);
549 assert(cpu
->_status
== IcacheRetry
);
550 Packet
*tmp
= cpu
->ifetch_pkt
;
551 if (sendTiming(tmp
)) {
552 cpu
->_status
= IcacheWaitResponse
;
553 cpu
->ifetch_pkt
= NULL
;
558 TimingSimpleCPU::completeDataAccess(Packet
*pkt
)
560 // received a response from the dcache: complete the load or store
562 assert(pkt
->result
== Packet::Success
);
563 assert(_status
== DcacheWaitResponse
);
566 numCycles
+= curTick
- previousTick
;
567 previousTick
= curTick
;
569 Fault fault
= curStaticInst
->completeAcc(pkt
, this, traceData
);
571 if (pkt
->isRead() && pkt
->req
->isLocked()) {
572 TheISA::handleLockedRead(thread
, pkt
->req
);
580 if (getState() == SimObject::Draining
) {
592 TimingSimpleCPU::completeDrain()
594 DPRINTF(Config
, "Done draining\n");
595 changeState(SimObject::Drained
);
596 drainEvent
->process();
600 TimingSimpleCPU::DcachePort::recvTiming(Packet
*pkt
)
602 // delay processing of returned data until next CPU clock edge
603 Tick time
= pkt
->req
->getTime();
604 while (time
< curTick
)
608 cpu
->completeDataAccess(pkt
);
610 tickEvent
.schedule(pkt
, time
);
616 TimingSimpleCPU::DcachePort::DTickEvent::process()
618 cpu
->completeDataAccess(pkt
);
622 TimingSimpleCPU::DcachePort::recvRetry()
624 // we shouldn't get a retry unless we have a packet that we're
625 // waiting to transmit
626 assert(cpu
->dcache_pkt
!= NULL
);
627 assert(cpu
->_status
== DcacheRetry
);
628 Packet
*tmp
= cpu
->dcache_pkt
;
629 if (sendTiming(tmp
)) {
630 cpu
->_status
= DcacheWaitResponse
;
631 // memory system takes ownership of packet
632 cpu
->dcache_pkt
= NULL
;
637 ////////////////////////////////////////////////////////////////////////
639 // TimingSimpleCPU Simulation Object
641 BEGIN_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
643 Param
<Counter
> max_insts_any_thread
;
644 Param
<Counter
> max_insts_all_threads
;
645 Param
<Counter
> max_loads_any_thread
;
646 Param
<Counter
> max_loads_all_threads
;
647 Param
<Tick
> progress_interval
;
648 SimObjectParam
<MemObject
*> mem
;
649 SimObjectParam
<System
*> system
;
653 SimObjectParam
<AlphaITB
*> itb
;
654 SimObjectParam
<AlphaDTB
*> dtb
;
657 SimObjectParam
<Process
*> workload
;
658 #endif // FULL_SYSTEM
662 Param
<bool> defer_registration
;
664 Param
<bool> function_trace
;
665 Param
<Tick
> function_trace_start
;
666 Param
<bool> simulate_stalls
;
668 END_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
670 BEGIN_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
672 INIT_PARAM(max_insts_any_thread
,
673 "terminate when any thread reaches this inst count"),
674 INIT_PARAM(max_insts_all_threads
,
675 "terminate when all threads have reached this inst count"),
676 INIT_PARAM(max_loads_any_thread
,
677 "terminate when any thread reaches this load count"),
678 INIT_PARAM(max_loads_all_threads
,
679 "terminate when all threads have reached this load count"),
680 INIT_PARAM(progress_interval
, "Progress interval"),
681 INIT_PARAM(mem
, "memory"),
682 INIT_PARAM(system
, "system object"),
683 INIT_PARAM(cpu_id
, "processor ID"),
686 INIT_PARAM(itb
, "Instruction TLB"),
687 INIT_PARAM(dtb
, "Data TLB"),
688 INIT_PARAM(profile
, ""),
690 INIT_PARAM(workload
, "processes to run"),
691 #endif // FULL_SYSTEM
693 INIT_PARAM(clock
, "clock speed"),
694 INIT_PARAM(defer_registration
, "defer system registration (for sampling)"),
695 INIT_PARAM(width
, "cpu width"),
696 INIT_PARAM(function_trace
, "Enable function trace"),
697 INIT_PARAM(function_trace_start
, "Cycle to start function trace"),
698 INIT_PARAM(simulate_stalls
, "Simulate cache stall cycles")
700 END_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
703 CREATE_SIM_OBJECT(TimingSimpleCPU
)
705 TimingSimpleCPU::Params
*params
= new TimingSimpleCPU::Params();
706 params
->name
= getInstanceName();
707 params
->numberOfThreads
= 1;
708 params
->max_insts_any_thread
= max_insts_any_thread
;
709 params
->max_insts_all_threads
= max_insts_all_threads
;
710 params
->max_loads_any_thread
= max_loads_any_thread
;
711 params
->max_loads_all_threads
= max_loads_all_threads
;
712 params
->progress_interval
= progress_interval
;
713 params
->deferRegistration
= defer_registration
;
714 params
->clock
= clock
;
715 params
->functionTrace
= function_trace
;
716 params
->functionTraceStart
= function_trace_start
;
718 params
->system
= system
;
719 params
->cpu_id
= cpu_id
;
724 params
->profile
= profile
;
726 params
->process
= workload
;
729 TimingSimpleCPU
*cpu
= new TimingSimpleCPU(params
);
733 REGISTER_SIM_OBJECT("TimingSimpleCPU", TimingSimpleCPU
)