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/atomic.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
;
43 AtomicSimpleCPU::TickEvent::TickEvent(AtomicSimpleCPU
*c
)
44 : Event(&mainEventQueue
, CPU_Tick_Pri
), cpu(c
)
50 AtomicSimpleCPU::TickEvent::process()
56 AtomicSimpleCPU::TickEvent::description()
58 return "AtomicSimpleCPU tick event";
62 AtomicSimpleCPU::getPort(const std::string
&if_name
, int idx
)
64 if (if_name
== "dcache_port")
66 else if (if_name
== "icache_port")
69 panic("No Such Port\n");
73 AtomicSimpleCPU::init()
77 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
78 ThreadContext
*tc
= threadContexts
[i
];
80 // initialize CPU, including PC
81 TheISA::initCPU(tc
, tc
->readCpuId());
87 AtomicSimpleCPU::CpuPort::recvTiming(PacketPtr pkt
)
89 panic("AtomicSimpleCPU doesn't expect recvTiming callback!");
94 AtomicSimpleCPU::CpuPort::recvAtomic(PacketPtr pkt
)
96 //Snooping a coherence request, just return
101 AtomicSimpleCPU::CpuPort::recvFunctional(PacketPtr pkt
)
103 //No internal storage to update, just return
108 AtomicSimpleCPU::CpuPort::recvStatusChange(Status status
)
110 if (status
== RangeChange
)
113 panic("AtomicSimpleCPU doesn't expect recvStatusChange callback!");
117 AtomicSimpleCPU::CpuPort::recvRetry()
119 panic("AtomicSimpleCPU doesn't expect recvRetry callback!");
123 AtomicSimpleCPU::AtomicSimpleCPU(Params
*p
)
124 : BaseSimpleCPU(p
), tickEvent(this),
125 width(p
->width
), simulate_stalls(p
->simulate_stalls
),
126 icachePort(name() + "-iport", this), dcachePort(name() + "-iport", this)
130 ifetch_req
= new Request();
131 ifetch_req
->setThreadContext(p
->cpu_id
, 0); // Add thread ID if we add MT
132 ifetch_pkt
= new Packet(ifetch_req
, Packet::ReadReq
, Packet::Broadcast
);
133 ifetch_pkt
->dataStatic(&inst
);
135 data_read_req
= new Request();
136 data_read_req
->setThreadContext(p
->cpu_id
, 0); // Add thread ID here too
137 data_read_pkt
= new Packet(data_read_req
, Packet::ReadReq
,
139 data_read_pkt
->dataStatic(&dataReg
);
141 data_write_req
= new Request();
142 data_write_req
->setThreadContext(p
->cpu_id
, 0); // Add thread ID here too
143 data_write_pkt
= new Packet(data_write_req
, Packet::WriteReq
,
148 AtomicSimpleCPU::~AtomicSimpleCPU()
153 AtomicSimpleCPU::serialize(ostream
&os
)
155 SimObject::State so_state
= SimObject::getState();
156 SERIALIZE_ENUM(so_state
);
157 Status _status
= status();
158 SERIALIZE_ENUM(_status
);
159 BaseSimpleCPU::serialize(os
);
160 nameOut(os
, csprintf("%s.tickEvent", name()));
161 tickEvent
.serialize(os
);
165 AtomicSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
167 SimObject::State so_state
;
168 UNSERIALIZE_ENUM(so_state
);
169 UNSERIALIZE_ENUM(_status
);
170 BaseSimpleCPU::unserialize(cp
, section
);
171 tickEvent
.unserialize(cp
, csprintf("%s.tickEvent", section
));
175 AtomicSimpleCPU::resume()
177 if (_status
!= SwitchedOut
&& _status
!= Idle
) {
178 assert(system
->getMemoryMode() == System::Atomic
);
180 changeState(SimObject::Running
);
181 if (thread
->status() == ThreadContext::Active
) {
182 if (!tickEvent
.scheduled()) {
183 tickEvent
.schedule(nextCycle());
190 AtomicSimpleCPU::switchOut()
192 assert(status() == Running
|| status() == Idle
);
193 _status
= SwitchedOut
;
200 AtomicSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
202 BaseCPU::takeOverFrom(oldCPU
);
204 assert(!tickEvent
.scheduled());
206 // if any of this CPU's ThreadContexts are active, mark the CPU as
207 // running and schedule its tick event.
208 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
209 ThreadContext
*tc
= threadContexts
[i
];
210 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
212 tickEvent
.schedule(nextCycle());
216 if (_status
!= Running
) {
223 AtomicSimpleCPU::activateContext(int thread_num
, int delay
)
225 assert(thread_num
== 0);
228 assert(_status
== Idle
);
229 assert(!tickEvent
.scheduled());
232 //Make sure ticks are still on multiples of cycles
233 tickEvent
.schedule(nextCycle(curTick
+ cycles(delay
)));
239 AtomicSimpleCPU::suspendContext(int thread_num
)
241 assert(thread_num
== 0);
244 assert(_status
== Running
);
246 // tick event may not be scheduled if this gets called from inside
247 // an instruction's execution, e.g. "quiesce"
248 if (tickEvent
.scheduled())
249 tickEvent
.deschedule();
258 AtomicSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
260 // use the CPU's statically allocated read request and packet objects
261 Request
*req
= data_read_req
;
262 PacketPtr pkt
= data_read_pkt
;
264 req
->setVirt(0, addr
, sizeof(T
), flags
, thread
->readPC());
267 traceData
->setAddr(addr
);
270 // translate to physical address
271 Fault fault
= thread
->translateDataReadReq(req
);
273 // Now do the access.
274 if (fault
== NoFault
) {
275 pkt
->reinitFromRequest();
277 dcache_latency
= dcachePort
.sendAtomic(pkt
);
278 dcache_access
= true;
280 assert(pkt
->result
== Packet::Success
);
281 data
= pkt
->get
<T
>();
283 if (req
->isLocked()) {
284 TheISA::handleLockedRead(thread
, req
);
288 // This will need a new way to tell if it has a dcache attached.
289 if (req
->isUncacheable())
290 recordEvent("Uncached Read");
295 #ifndef DOXYGEN_SHOULD_SKIP_THIS
299 AtomicSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
303 AtomicSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
307 AtomicSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
311 AtomicSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
313 #endif //DOXYGEN_SHOULD_SKIP_THIS
317 AtomicSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
319 return read(addr
, *(uint64_t*)&data
, flags
);
324 AtomicSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
326 return read(addr
, *(uint32_t*)&data
, flags
);
332 AtomicSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
334 return read(addr
, (uint32_t&)data
, flags
);
340 AtomicSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
342 // use the CPU's statically allocated write request and packet objects
343 Request
*req
= data_write_req
;
344 PacketPtr pkt
= data_write_pkt
;
346 req
->setVirt(0, addr
, sizeof(T
), flags
, thread
->readPC());
349 traceData
->setAddr(addr
);
352 // translate to physical address
353 Fault fault
= thread
->translateDataWriteReq(req
);
355 // Now do the access.
356 if (fault
== NoFault
) {
357 bool do_access
= true; // flag to suppress cache access
359 if (req
->isLocked()) {
360 do_access
= TheISA::handleLockedWrite(thread
, req
);
365 pkt
->reinitFromRequest();
366 pkt
->dataStatic(&data
);
368 dcache_latency
= dcachePort
.sendAtomic(pkt
);
369 dcache_access
= true;
371 assert(pkt
->result
== Packet::Success
);
374 if (req
->isLocked()) {
375 uint64_t scResult
= req
->getScResult();
377 // clear failure counter
378 thread
->setStCondFailures(0);
381 *res
= req
->getScResult();
386 // This will need a new way to tell if it's hooked up to a cache or not.
387 if (req
->isUncacheable())
388 recordEvent("Uncached Write");
390 // If the write needs to have a fault on the access, consider calling
391 // changeStatus() and changing it to "bad addr write" or something.
396 #ifndef DOXYGEN_SHOULD_SKIP_THIS
399 AtomicSimpleCPU::write(uint64_t data
, Addr addr
,
400 unsigned flags
, uint64_t *res
);
404 AtomicSimpleCPU::write(uint32_t data
, Addr addr
,
405 unsigned flags
, uint64_t *res
);
409 AtomicSimpleCPU::write(uint16_t data
, Addr addr
,
410 unsigned flags
, uint64_t *res
);
414 AtomicSimpleCPU::write(uint8_t data
, Addr addr
,
415 unsigned flags
, uint64_t *res
);
417 #endif //DOXYGEN_SHOULD_SKIP_THIS
421 AtomicSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
423 return write(*(uint64_t*)&data
, addr
, flags
, res
);
428 AtomicSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
430 return write(*(uint32_t*)&data
, addr
, flags
, res
);
436 AtomicSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
438 return write((uint32_t)data
, addr
, flags
, res
);
443 AtomicSimpleCPU::tick()
445 Tick latency
= cycles(1); // instruction takes one cycle by default
447 for (int i
= 0; i
< width
; ++i
) {
450 if (!curStaticInst
|| !curStaticInst
->isDelayedCommit())
451 checkForInterrupts();
453 Fault fault
= setupFetchRequest(ifetch_req
);
455 if (fault
== NoFault
) {
456 ifetch_pkt
->reinitFromRequest();
458 Tick icache_latency
= icachePort
.sendAtomic(ifetch_pkt
);
459 // ifetch_req is initialized to read the instruction directly
460 // into the CPU object's inst field.
462 dcache_access
= false; // assume no dcache access
464 fault
= curStaticInst
->execute(this, traceData
);
467 if (simulate_stalls
) {
468 Tick icache_stall
= icache_latency
- cycles(1);
470 dcache_access
? dcache_latency
- cycles(1) : 0;
471 Tick stall_cycles
= (icache_stall
+ dcache_stall
) / cycles(1);
472 if (cycles(stall_cycles
) < (icache_stall
+ dcache_stall
))
473 latency
+= cycles(stall_cycles
+1);
475 latency
+= cycles(stall_cycles
);
484 tickEvent
.schedule(curTick
+ latency
);
488 ////////////////////////////////////////////////////////////////////////
490 // AtomicSimpleCPU Simulation Object
492 BEGIN_DECLARE_SIM_OBJECT_PARAMS(AtomicSimpleCPU
)
494 Param
<Counter
> max_insts_any_thread
;
495 Param
<Counter
> max_insts_all_threads
;
496 Param
<Counter
> max_loads_any_thread
;
497 Param
<Counter
> max_loads_all_threads
;
498 Param
<Tick
> progress_interval
;
499 SimObjectParam
<System
*> system
;
503 SimObjectParam
<TheISA::ITB
*> itb
;
504 SimObjectParam
<TheISA::DTB
*> dtb
;
507 Param
<bool> do_quiesce
;
508 Param
<bool> do_checkpoint_insts
;
509 Param
<bool> do_statistics_insts
;
511 SimObjectParam
<Process
*> workload
;
512 #endif // FULL_SYSTEM
516 Param
<bool> defer_registration
;
518 Param
<bool> function_trace
;
519 Param
<Tick
> function_trace_start
;
520 Param
<bool> simulate_stalls
;
522 END_DECLARE_SIM_OBJECT_PARAMS(AtomicSimpleCPU
)
524 BEGIN_INIT_SIM_OBJECT_PARAMS(AtomicSimpleCPU
)
526 INIT_PARAM(max_insts_any_thread
,
527 "terminate when any thread reaches this inst count"),
528 INIT_PARAM(max_insts_all_threads
,
529 "terminate when all threads have reached this inst count"),
530 INIT_PARAM(max_loads_any_thread
,
531 "terminate when any thread reaches this load count"),
532 INIT_PARAM(max_loads_all_threads
,
533 "terminate when all threads have reached this load count"),
534 INIT_PARAM(progress_interval
, "Progress interval"),
535 INIT_PARAM(system
, "system object"),
536 INIT_PARAM(cpu_id
, "processor ID"),
539 INIT_PARAM(itb
, "Instruction TLB"),
540 INIT_PARAM(dtb
, "Data TLB"),
541 INIT_PARAM(profile
, ""),
542 INIT_PARAM(do_quiesce
, ""),
543 INIT_PARAM(do_checkpoint_insts
, ""),
544 INIT_PARAM(do_statistics_insts
, ""),
546 INIT_PARAM(workload
, "processes to run"),
547 #endif // FULL_SYSTEM
549 INIT_PARAM(clock
, "clock speed"),
550 INIT_PARAM(defer_registration
, "defer system registration (for sampling)"),
551 INIT_PARAM(width
, "cpu width"),
552 INIT_PARAM(function_trace
, "Enable function trace"),
553 INIT_PARAM(function_trace_start
, "Cycle to start function trace"),
554 INIT_PARAM(simulate_stalls
, "Simulate cache stall cycles")
556 END_INIT_SIM_OBJECT_PARAMS(AtomicSimpleCPU
)
559 CREATE_SIM_OBJECT(AtomicSimpleCPU
)
561 AtomicSimpleCPU::Params
*params
= new AtomicSimpleCPU::Params();
562 params
->name
= getInstanceName();
563 params
->numberOfThreads
= 1;
564 params
->max_insts_any_thread
= max_insts_any_thread
;
565 params
->max_insts_all_threads
= max_insts_all_threads
;
566 params
->max_loads_any_thread
= max_loads_any_thread
;
567 params
->max_loads_all_threads
= max_loads_all_threads
;
568 params
->progress_interval
= progress_interval
;
569 params
->deferRegistration
= defer_registration
;
570 params
->clock
= clock
;
571 params
->functionTrace
= function_trace
;
572 params
->functionTraceStart
= function_trace_start
;
573 params
->width
= width
;
574 params
->simulate_stalls
= simulate_stalls
;
575 params
->system
= system
;
576 params
->cpu_id
= cpu_id
;
581 params
->profile
= profile
;
582 params
->do_quiesce
= do_quiesce
;
583 params
->do_checkpoint_insts
= do_checkpoint_insts
;
584 params
->do_statistics_insts
= do_statistics_insts
;
586 params
->process
= workload
;
589 AtomicSimpleCPU
*cpu
= new AtomicSimpleCPU(params
);
593 REGISTER_SIM_OBJECT("AtomicSimpleCPU", AtomicSimpleCPU
)