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/atomic.hh"
34 #include "mem/packet_impl.hh"
35 #include "sim/builder.hh"
36 #include "sim/system.hh"
39 using namespace TheISA
;
41 AtomicSimpleCPU::TickEvent::TickEvent(AtomicSimpleCPU
*c
)
42 : Event(&mainEventQueue
, CPU_Tick_Pri
), cpu(c
)
48 AtomicSimpleCPU::TickEvent::process()
54 AtomicSimpleCPU::TickEvent::description()
56 return "AtomicSimpleCPU tick event";
60 AtomicSimpleCPU::getPort(const std::string
&if_name
, int idx
)
62 if (if_name
== "dcache_port")
64 else if (if_name
== "icache_port")
67 panic("No Such Port\n");
71 AtomicSimpleCPU::init()
73 //Create Memory Ports (conect them up)
74 // Port *mem_dport = mem->getPort("");
75 // dcachePort.setPeer(mem_dport);
76 // mem_dport->setPeer(&dcachePort);
78 // Port *mem_iport = mem->getPort("");
79 // icachePort.setPeer(mem_iport);
80 // mem_iport->setPeer(&icachePort);
84 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
85 ThreadContext
*tc
= threadContexts
[i
];
87 // initialize CPU, including PC
88 TheISA::initCPU(tc
, tc
->readCpuId());
94 AtomicSimpleCPU::CpuPort::recvTiming(Packet
*pkt
)
96 panic("AtomicSimpleCPU doesn't expect recvAtomic callback!");
101 AtomicSimpleCPU::CpuPort::recvAtomic(Packet
*pkt
)
103 panic("AtomicSimpleCPU doesn't expect recvAtomic callback!");
108 AtomicSimpleCPU::CpuPort::recvFunctional(Packet
*pkt
)
110 panic("AtomicSimpleCPU doesn't expect recvFunctional callback!");
114 AtomicSimpleCPU::CpuPort::recvStatusChange(Status status
)
116 if (status
== RangeChange
)
119 panic("AtomicSimpleCPU doesn't expect recvStatusChange callback!");
123 AtomicSimpleCPU::CpuPort::recvRetry()
125 panic("AtomicSimpleCPU doesn't expect recvRetry callback!");
129 AtomicSimpleCPU::AtomicSimpleCPU(Params
*p
)
130 : BaseSimpleCPU(p
), tickEvent(this),
131 width(p
->width
), simulate_stalls(p
->simulate_stalls
),
132 icachePort(name() + "-iport", this), dcachePort(name() + "-iport", this)
136 // @todo fix me and get the real cpu id & thread number!!!
137 ifetch_req
= new Request();
138 ifetch_req
->setThreadContext(0,0); //Need CPU/Thread IDS HERE
139 ifetch_pkt
= new Packet(ifetch_req
, Packet::ReadReq
, Packet::Broadcast
);
140 ifetch_pkt
->dataStatic(&inst
);
142 data_read_req
= new Request();
143 data_read_req
->setThreadContext(0,0); //Need CPU/Thread IDS HERE
144 data_read_pkt
= new Packet(data_read_req
, Packet::ReadReq
,
146 data_read_pkt
->dataStatic(&dataReg
);
148 data_write_req
= new Request();
149 data_write_req
->setThreadContext(0,0); //Need CPU/Thread IDS HERE
150 data_write_pkt
= new Packet(data_write_req
, Packet::WriteReq
,
155 AtomicSimpleCPU::~AtomicSimpleCPU()
160 AtomicSimpleCPU::serialize(ostream
&os
)
162 SimObject::State so_state
= SimObject::getState();
163 SERIALIZE_ENUM(so_state
);
164 BaseSimpleCPU::serialize(os
);
165 nameOut(os
, csprintf("%s.tickEvent", name()));
166 tickEvent
.serialize(os
);
170 AtomicSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
172 SimObject::State so_state
;
173 UNSERIALIZE_ENUM(so_state
);
174 BaseSimpleCPU::unserialize(cp
, section
);
175 tickEvent
.unserialize(cp
, csprintf("%s.tickEvent", section
));
179 AtomicSimpleCPU::resume()
181 assert(system
->getMemoryMode() == System::Atomic
);
182 changeState(SimObject::Running
);
183 if (thread
->status() == ThreadContext::Active
) {
184 if (!tickEvent
.scheduled())
185 tickEvent
.schedule(curTick
);
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(curTick
);
220 AtomicSimpleCPU::activateContext(int thread_num
, int delay
)
222 assert(thread_num
== 0);
225 assert(_status
== Idle
);
226 assert(!tickEvent
.scheduled());
229 tickEvent
.schedule(curTick
+ cycles(delay
));
235 AtomicSimpleCPU::suspendContext(int thread_num
)
237 assert(thread_num
== 0);
240 assert(_status
== Running
);
242 // tick event may not be scheduled if this gets called from inside
243 // an instruction's execution, e.g. "quiesce"
244 if (tickEvent
.scheduled())
245 tickEvent
.deschedule();
254 AtomicSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
256 data_read_req
->setVirt(0, addr
, sizeof(T
), flags
, thread
->readPC());
259 traceData
->setAddr(addr
);
262 // translate to physical address
263 Fault fault
= thread
->translateDataReadReq(data_read_req
);
265 // Now do the access.
266 if (fault
== NoFault
) {
267 data_read_pkt
->reinitFromRequest();
269 dcache_latency
= dcachePort
.sendAtomic(data_read_pkt
);
270 dcache_access
= true;
272 assert(data_read_pkt
->result
== Packet::Success
);
273 data
= data_read_pkt
->get
<T
>();
277 // This will need a new way to tell if it has a dcache attached.
278 if (data_read_req
->getFlags() & UNCACHEABLE
)
279 recordEvent("Uncached Read");
284 #ifndef DOXYGEN_SHOULD_SKIP_THIS
288 AtomicSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
292 AtomicSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
296 AtomicSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
300 AtomicSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
302 #endif //DOXYGEN_SHOULD_SKIP_THIS
306 AtomicSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
308 return read(addr
, *(uint64_t*)&data
, flags
);
313 AtomicSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
315 return read(addr
, *(uint32_t*)&data
, flags
);
321 AtomicSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
323 return read(addr
, (uint32_t&)data
, flags
);
329 AtomicSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
331 data_write_req
->setVirt(0, addr
, sizeof(T
), flags
, thread
->readPC());
334 traceData
->setAddr(addr
);
337 // translate to physical address
338 Fault fault
= thread
->translateDataWriteReq(data_write_req
);
340 // Now do the access.
341 if (fault
== NoFault
) {
343 data_write_pkt
->reinitFromRequest();
344 data_write_pkt
->dataStatic(&data
);
346 dcache_latency
= dcachePort
.sendAtomic(data_write_pkt
);
347 dcache_access
= true;
349 assert(data_write_pkt
->result
== Packet::Success
);
351 if (res
&& data_write_req
->getFlags() & LOCKED
) {
352 *res
= data_write_req
->getScResult();
356 // This will need a new way to tell if it's hooked up to a cache or not.
357 if (data_write_req
->getFlags() & UNCACHEABLE
)
358 recordEvent("Uncached Write");
360 // If the write needs to have a fault on the access, consider calling
361 // changeStatus() and changing it to "bad addr write" or something.
366 #ifndef DOXYGEN_SHOULD_SKIP_THIS
369 AtomicSimpleCPU::write(uint64_t data
, Addr addr
,
370 unsigned flags
, uint64_t *res
);
374 AtomicSimpleCPU::write(uint32_t data
, Addr addr
,
375 unsigned flags
, uint64_t *res
);
379 AtomicSimpleCPU::write(uint16_t data
, Addr addr
,
380 unsigned flags
, uint64_t *res
);
384 AtomicSimpleCPU::write(uint8_t data
, Addr addr
,
385 unsigned flags
, uint64_t *res
);
387 #endif //DOXYGEN_SHOULD_SKIP_THIS
391 AtomicSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
393 return write(*(uint64_t*)&data
, addr
, flags
, res
);
398 AtomicSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
400 return write(*(uint32_t*)&data
, addr
, flags
, res
);
406 AtomicSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
408 return write((uint32_t)data
, addr
, flags
, res
);
413 AtomicSimpleCPU::tick()
415 Tick latency
= cycles(1); // instruction takes one cycle by default
417 for (int i
= 0; i
< width
; ++i
) {
420 checkForInterrupts();
422 Fault fault
= setupFetchRequest(ifetch_req
);
424 if (fault
== NoFault
) {
425 ifetch_pkt
->reinitFromRequest();
427 Tick icache_latency
= icachePort
.sendAtomic(ifetch_pkt
);
428 // ifetch_req is initialized to read the instruction directly
429 // into the CPU object's inst field.
431 dcache_access
= false; // assume no dcache access
433 fault
= curStaticInst
->execute(this, traceData
);
436 if (simulate_stalls
) {
437 Tick icache_stall
= icache_latency
- cycles(1);
439 dcache_access
? dcache_latency
- cycles(1) : 0;
440 Tick stall_cycles
= (icache_stall
+ dcache_stall
) / cycles(1);
441 if (cycles(stall_cycles
) < (icache_stall
+ dcache_stall
))
442 latency
+= cycles(stall_cycles
+1);
444 latency
+= cycles(stall_cycles
);
453 tickEvent
.schedule(curTick
+ latency
);
457 ////////////////////////////////////////////////////////////////////////
459 // AtomicSimpleCPU Simulation Object
461 BEGIN_DECLARE_SIM_OBJECT_PARAMS(AtomicSimpleCPU
)
463 Param
<Counter
> max_insts_any_thread
;
464 Param
<Counter
> max_insts_all_threads
;
465 Param
<Counter
> max_loads_any_thread
;
466 Param
<Counter
> max_loads_all_threads
;
467 Param
<Tick
> progress_interval
;
468 SimObjectParam
<MemObject
*> mem
;
469 SimObjectParam
<System
*> system
;
472 SimObjectParam
<AlphaITB
*> itb
;
473 SimObjectParam
<AlphaDTB
*> dtb
;
477 SimObjectParam
<Process
*> workload
;
478 #endif // FULL_SYSTEM
482 Param
<bool> defer_registration
;
484 Param
<bool> function_trace
;
485 Param
<Tick
> function_trace_start
;
486 Param
<bool> simulate_stalls
;
488 END_DECLARE_SIM_OBJECT_PARAMS(AtomicSimpleCPU
)
490 BEGIN_INIT_SIM_OBJECT_PARAMS(AtomicSimpleCPU
)
492 INIT_PARAM(max_insts_any_thread
,
493 "terminate when any thread reaches this inst count"),
494 INIT_PARAM(max_insts_all_threads
,
495 "terminate when all threads have reached this inst count"),
496 INIT_PARAM(max_loads_any_thread
,
497 "terminate when any thread reaches this load count"),
498 INIT_PARAM(max_loads_all_threads
,
499 "terminate when all threads have reached this load count"),
500 INIT_PARAM(progress_interval
, "Progress interval"),
501 INIT_PARAM(mem
, "memory"),
502 INIT_PARAM(system
, "system object"),
505 INIT_PARAM(itb
, "Instruction TLB"),
506 INIT_PARAM(dtb
, "Data TLB"),
507 INIT_PARAM(cpu_id
, "processor ID"),
508 INIT_PARAM(profile
, ""),
510 INIT_PARAM(workload
, "processes to run"),
511 #endif // FULL_SYSTEM
513 INIT_PARAM(clock
, "clock speed"),
514 INIT_PARAM(defer_registration
, "defer system registration (for sampling)"),
515 INIT_PARAM(width
, "cpu width"),
516 INIT_PARAM(function_trace
, "Enable function trace"),
517 INIT_PARAM(function_trace_start
, "Cycle to start function trace"),
518 INIT_PARAM(simulate_stalls
, "Simulate cache stall cycles")
520 END_INIT_SIM_OBJECT_PARAMS(AtomicSimpleCPU
)
523 CREATE_SIM_OBJECT(AtomicSimpleCPU
)
525 AtomicSimpleCPU::Params
*params
= new AtomicSimpleCPU::Params();
526 params
->name
= getInstanceName();
527 params
->numberOfThreads
= 1;
528 params
->max_insts_any_thread
= max_insts_any_thread
;
529 params
->max_insts_all_threads
= max_insts_all_threads
;
530 params
->max_loads_any_thread
= max_loads_any_thread
;
531 params
->max_loads_all_threads
= max_loads_all_threads
;
532 params
->progress_interval
= progress_interval
;
533 params
->deferRegistration
= defer_registration
;
534 params
->clock
= clock
;
535 params
->functionTrace
= function_trace
;
536 params
->functionTraceStart
= function_trace_start
;
537 params
->width
= width
;
538 params
->simulate_stalls
= simulate_stalls
;
540 params
->system
= system
;
545 params
->cpu_id
= cpu_id
;
546 params
->profile
= profile
;
548 params
->process
= workload
;
551 AtomicSimpleCPU
*cpu
= new AtomicSimpleCPU(params
);
555 REGISTER_SIM_OBJECT("AtomicSimpleCPU", AtomicSimpleCPU
)