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 assert(system
->getMemoryMode() == System::Timing
);
152 // Delete the old event if it existed.
154 if (fetchEvent
->scheduled())
155 fetchEvent
->deschedule();
161 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
162 fetchEvent
->schedule(curTick
);
165 changeState(SimObject::Running
);
166 previousTick
= curTick
;
170 TimingSimpleCPU::switchOut()
172 assert(status() == Running
|| status() == Idle
);
173 _status
= SwitchedOut
;
174 numCycles
+= curTick
- previousTick
;
176 // If we've been scheduled to resume but are then told to switch out,
177 // we'll need to cancel it.
178 if (fetchEvent
&& fetchEvent
->scheduled())
179 fetchEvent
->deschedule();
184 TimingSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
186 BaseCPU::takeOverFrom(oldCPU
);
188 // if any of this CPU's ThreadContexts are active, mark the CPU as
189 // running and schedule its tick event.
190 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
191 ThreadContext
*tc
= threadContexts
[i
];
192 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
198 if (_status
!= Running
) {
203 if (icachePort
.getPeer() == NULL
) {
204 peer
= oldCPU
->getPort("icache_port")->getPeer();
205 icachePort
.setPeer(peer
);
207 peer
= icachePort
.getPeer();
209 peer
->setPeer(&icachePort
);
211 if (dcachePort
.getPeer() == NULL
) {
212 peer
= oldCPU
->getPort("dcache_port")->getPeer();
213 dcachePort
.setPeer(peer
);
215 peer
= dcachePort
.getPeer();
217 peer
->setPeer(&dcachePort
);
222 TimingSimpleCPU::activateContext(int thread_num
, int delay
)
224 assert(thread_num
== 0);
227 assert(_status
== Idle
);
231 // kick things off by initiating the fetch of the next instruction
233 new EventWrapper
<TimingSimpleCPU
, &TimingSimpleCPU::fetch
>(this, false);
234 fetchEvent
->schedule(curTick
+ cycles(delay
));
239 TimingSimpleCPU::suspendContext(int thread_num
)
241 assert(thread_num
== 0);
244 assert(_status
== Running
);
246 // just change status to Idle... if status != Running,
247 // completeInst() will not initiate fetch of next instruction.
256 TimingSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
259 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
260 cpu_id
, /* thread ID */ 0);
263 traceData
->setAddr(req
->getVaddr());
266 // translate to physical address
267 Fault fault
= thread
->translateDataReadReq(req
);
269 // Now do the access.
270 if (fault
== NoFault
) {
272 new Packet(req
, Packet::ReadReq
, Packet::Broadcast
);
273 pkt
->dataDynamic
<T
>(new T
);
275 if (!dcachePort
.sendTiming(pkt
)) {
276 _status
= DcacheRetry
;
279 _status
= DcacheWaitResponse
;
280 // memory system takes ownership of packet
285 // This will need a new way to tell if it has a dcache attached.
286 if (req
->isUncacheable())
287 recordEvent("Uncached Read");
292 #ifndef DOXYGEN_SHOULD_SKIP_THIS
296 TimingSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
300 TimingSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
304 TimingSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
308 TimingSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
310 #endif //DOXYGEN_SHOULD_SKIP_THIS
314 TimingSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
316 return read(addr
, *(uint64_t*)&data
, flags
);
321 TimingSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
323 return read(addr
, *(uint32_t*)&data
, flags
);
329 TimingSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
331 return read(addr
, (uint32_t&)data
, flags
);
337 TimingSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
340 new Request(/* asid */ 0, addr
, sizeof(T
), flags
, thread
->readPC(),
341 cpu_id
, /* thread ID */ 0);
343 // translate to physical address
344 Fault fault
= thread
->translateDataWriteReq(req
);
346 // Now do the access.
347 if (fault
== NoFault
) {
348 assert(dcache_pkt
== NULL
);
349 dcache_pkt
= new Packet(req
, Packet::WriteReq
, Packet::Broadcast
);
350 dcache_pkt
->allocate();
351 dcache_pkt
->set(data
);
353 bool do_access
= true; // flag to suppress cache access
355 if (req
->isLocked()) {
356 do_access
= TheISA::handleLockedWrite(thread
, req
);
360 if (!dcachePort
.sendTiming(dcache_pkt
)) {
361 _status
= DcacheRetry
;
363 _status
= DcacheWaitResponse
;
364 // memory system takes ownership of packet
370 // This will need a new way to tell if it's hooked up to a cache or not.
371 if (req
->isUncacheable())
372 recordEvent("Uncached Write");
374 // If the write needs to have a fault on the access, consider calling
375 // changeStatus() and changing it to "bad addr write" or something.
380 #ifndef DOXYGEN_SHOULD_SKIP_THIS
383 TimingSimpleCPU::write(uint64_t data
, Addr addr
,
384 unsigned flags
, uint64_t *res
);
388 TimingSimpleCPU::write(uint32_t data
, Addr addr
,
389 unsigned flags
, uint64_t *res
);
393 TimingSimpleCPU::write(uint16_t data
, Addr addr
,
394 unsigned flags
, uint64_t *res
);
398 TimingSimpleCPU::write(uint8_t data
, Addr addr
,
399 unsigned flags
, uint64_t *res
);
401 #endif //DOXYGEN_SHOULD_SKIP_THIS
405 TimingSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
407 return write(*(uint64_t*)&data
, addr
, flags
, res
);
412 TimingSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
414 return write(*(uint32_t*)&data
, addr
, flags
, res
);
420 TimingSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
422 return write((uint32_t)data
, addr
, flags
, res
);
427 TimingSimpleCPU::fetch()
429 checkForInterrupts();
431 Request
*ifetch_req
= new Request();
432 ifetch_req
->setThreadContext(cpu_id
, /* thread ID */ 0);
433 Fault fault
= setupFetchRequest(ifetch_req
);
435 ifetch_pkt
= new Packet(ifetch_req
, Packet::ReadReq
, Packet::Broadcast
);
436 ifetch_pkt
->dataStatic(&inst
);
438 if (fault
== NoFault
) {
439 if (!icachePort
.sendTiming(ifetch_pkt
)) {
440 // Need to wait for retry
441 _status
= IcacheRetry
;
443 // Need to wait for cache to respond
444 _status
= IcacheWaitResponse
;
445 // ownership of packet transferred to memory system
449 // fetch fault: advance directly to next instruction (fault handler)
453 numCycles
+= curTick
- previousTick
;
454 previousTick
= curTick
;
459 TimingSimpleCPU::advanceInst(Fault fault
)
463 if (_status
== Running
) {
464 // kick off fetch of next instruction... callback from icache
465 // response will cause that instruction to be executed,
466 // keeping the CPU running.
473 TimingSimpleCPU::completeIfetch(Packet
*pkt
)
475 // received a response from the icache: execute the received
477 assert(pkt
->result
== Packet::Success
);
478 assert(_status
== IcacheWaitResponse
);
485 numCycles
+= curTick
- previousTick
;
486 previousTick
= curTick
;
488 if (getState() == SimObject::Draining
) {
494 if (curStaticInst
->isMemRef() && !curStaticInst
->isDataPrefetch()) {
495 // load or store: just send to dcache
496 Fault fault
= curStaticInst
->initiateAcc(this, traceData
);
497 if (_status
!= Running
) {
498 // instruction will complete in dcache response callback
499 assert(_status
== DcacheWaitResponse
|| _status
== DcacheRetry
);
500 assert(fault
== NoFault
);
502 if (fault
== NoFault
) {
503 // early fail on store conditional: complete now
504 assert(dcache_pkt
!= NULL
);
505 fault
= curStaticInst
->completeAcc(dcache_pkt
, this,
507 delete dcache_pkt
->req
;
515 // non-memory instruction: execute completely now
516 Fault fault
= curStaticInst
->execute(this, traceData
);
523 TimingSimpleCPU::IcachePort::ITickEvent::process()
525 cpu
->completeIfetch(pkt
);
529 TimingSimpleCPU::IcachePort::recvTiming(Packet
*pkt
)
531 // delay processing of returned data until next CPU clock edge
532 Tick time
= pkt
->req
->getTime();
533 while (time
< curTick
)
537 cpu
->completeIfetch(pkt
);
539 tickEvent
.schedule(pkt
, time
);
545 TimingSimpleCPU::IcachePort::recvRetry()
547 // we shouldn't get a retry unless we have a packet that we're
548 // waiting to transmit
549 assert(cpu
->ifetch_pkt
!= NULL
);
550 assert(cpu
->_status
== IcacheRetry
);
551 Packet
*tmp
= cpu
->ifetch_pkt
;
552 if (sendTiming(tmp
)) {
553 cpu
->_status
= IcacheWaitResponse
;
554 cpu
->ifetch_pkt
= NULL
;
559 TimingSimpleCPU::completeDataAccess(Packet
*pkt
)
561 // received a response from the dcache: complete the load or store
563 assert(pkt
->result
== Packet::Success
);
564 assert(_status
== DcacheWaitResponse
);
567 numCycles
+= curTick
- previousTick
;
568 previousTick
= curTick
;
570 Fault fault
= curStaticInst
->completeAcc(pkt
, this, traceData
);
572 if (pkt
->isRead() && pkt
->req
->isLocked()) {
573 TheISA::handleLockedRead(thread
, pkt
->req
);
581 if (getState() == SimObject::Draining
) {
593 TimingSimpleCPU::completeDrain()
595 DPRINTF(Config
, "Done draining\n");
596 changeState(SimObject::Drained
);
597 drainEvent
->process();
601 TimingSimpleCPU::DcachePort::recvTiming(Packet
*pkt
)
603 // delay processing of returned data until next CPU clock edge
604 Tick time
= pkt
->req
->getTime();
605 while (time
< curTick
)
609 cpu
->completeDataAccess(pkt
);
611 tickEvent
.schedule(pkt
, time
);
617 TimingSimpleCPU::DcachePort::DTickEvent::process()
619 cpu
->completeDataAccess(pkt
);
623 TimingSimpleCPU::DcachePort::recvRetry()
625 // we shouldn't get a retry unless we have a packet that we're
626 // waiting to transmit
627 assert(cpu
->dcache_pkt
!= NULL
);
628 assert(cpu
->_status
== DcacheRetry
);
629 Packet
*tmp
= cpu
->dcache_pkt
;
630 if (sendTiming(tmp
)) {
631 cpu
->_status
= DcacheWaitResponse
;
632 // memory system takes ownership of packet
633 cpu
->dcache_pkt
= NULL
;
638 ////////////////////////////////////////////////////////////////////////
640 // TimingSimpleCPU Simulation Object
642 BEGIN_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
644 Param
<Counter
> max_insts_any_thread
;
645 Param
<Counter
> max_insts_all_threads
;
646 Param
<Counter
> max_loads_any_thread
;
647 Param
<Counter
> max_loads_all_threads
;
648 Param
<Tick
> progress_interval
;
649 SimObjectParam
<MemObject
*> mem
;
650 SimObjectParam
<System
*> system
;
654 SimObjectParam
<AlphaITB
*> itb
;
655 SimObjectParam
<AlphaDTB
*> dtb
;
658 SimObjectParam
<Process
*> workload
;
659 #endif // FULL_SYSTEM
663 Param
<bool> defer_registration
;
665 Param
<bool> function_trace
;
666 Param
<Tick
> function_trace_start
;
667 Param
<bool> simulate_stalls
;
669 END_DECLARE_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
671 BEGIN_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
673 INIT_PARAM(max_insts_any_thread
,
674 "terminate when any thread reaches this inst count"),
675 INIT_PARAM(max_insts_all_threads
,
676 "terminate when all threads have reached this inst count"),
677 INIT_PARAM(max_loads_any_thread
,
678 "terminate when any thread reaches this load count"),
679 INIT_PARAM(max_loads_all_threads
,
680 "terminate when all threads have reached this load count"),
681 INIT_PARAM(progress_interval
, "Progress interval"),
682 INIT_PARAM(mem
, "memory"),
683 INIT_PARAM(system
, "system object"),
684 INIT_PARAM(cpu_id
, "processor ID"),
687 INIT_PARAM(itb
, "Instruction TLB"),
688 INIT_PARAM(dtb
, "Data TLB"),
689 INIT_PARAM(profile
, ""),
691 INIT_PARAM(workload
, "processes to run"),
692 #endif // FULL_SYSTEM
694 INIT_PARAM(clock
, "clock speed"),
695 INIT_PARAM(defer_registration
, "defer system registration (for sampling)"),
696 INIT_PARAM(width
, "cpu width"),
697 INIT_PARAM(function_trace
, "Enable function trace"),
698 INIT_PARAM(function_trace_start
, "Cycle to start function trace"),
699 INIT_PARAM(simulate_stalls
, "Simulate cache stall cycles")
701 END_INIT_SIM_OBJECT_PARAMS(TimingSimpleCPU
)
704 CREATE_SIM_OBJECT(TimingSimpleCPU
)
706 TimingSimpleCPU::Params
*params
= new TimingSimpleCPU::Params();
707 params
->name
= getInstanceName();
708 params
->numberOfThreads
= 1;
709 params
->max_insts_any_thread
= max_insts_any_thread
;
710 params
->max_insts_all_threads
= max_insts_all_threads
;
711 params
->max_loads_any_thread
= max_loads_any_thread
;
712 params
->max_loads_all_threads
= max_loads_all_threads
;
713 params
->progress_interval
= progress_interval
;
714 params
->deferRegistration
= defer_registration
;
715 params
->clock
= clock
;
716 params
->functionTrace
= function_trace
;
717 params
->functionTraceStart
= function_trace_start
;
719 params
->system
= system
;
720 params
->cpu_id
= cpu_id
;
725 params
->profile
= profile
;
727 params
->process
= workload
;
730 TimingSimpleCPU
*cpu
= new TimingSimpleCPU(params
);
734 REGISTER_SIM_OBJECT("TimingSimpleCPU", TimingSimpleCPU
)