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/mmaped_ipr.hh"
33 #include "arch/utility.hh"
34 #include "base/bigint.hh"
35 #include "cpu/exetrace.hh"
36 #include "cpu/simple/atomic.hh"
37 #include "mem/packet.hh"
38 #include "mem/packet_access.hh"
39 #include "params/AtomicSimpleCPU.hh"
40 #include "sim/system.hh"
43 using namespace TheISA
;
45 AtomicSimpleCPU::TickEvent::TickEvent(AtomicSimpleCPU
*c
)
46 : Event(&mainEventQueue
, CPU_Tick_Pri
), cpu(c
)
52 AtomicSimpleCPU::TickEvent::process()
58 AtomicSimpleCPU::TickEvent::description()
60 return "AtomicSimpleCPU tick";
64 AtomicSimpleCPU::getPort(const std::string
&if_name
, int idx
)
66 if (if_name
== "dcache_port")
68 else if (if_name
== "icache_port")
70 else if (if_name
== "physmem_port") {
71 hasPhysMemPort
= true;
75 panic("No Such Port\n");
79 AtomicSimpleCPU::init()
83 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
84 ThreadContext
*tc
= threadContexts
[i
];
86 // initialize CPU, including PC
87 TheISA::initCPU(tc
, tc
->readCpuId());
92 AddrRangeList pmAddrList
;
93 physmemPort
.getPeerAddressRanges(pmAddrList
, snoop
);
94 physMemAddr
= *pmAddrList
.begin();
99 AtomicSimpleCPU::CpuPort::recvTiming(PacketPtr pkt
)
101 panic("AtomicSimpleCPU doesn't expect recvTiming callback!");
106 AtomicSimpleCPU::CpuPort::recvAtomic(PacketPtr pkt
)
108 //Snooping a coherence request, just return
113 AtomicSimpleCPU::CpuPort::recvFunctional(PacketPtr pkt
)
115 //No internal storage to update, just return
120 AtomicSimpleCPU::CpuPort::recvStatusChange(Status status
)
122 if (status
== RangeChange
) {
123 if (!snoopRangeSent
) {
124 snoopRangeSent
= true;
125 sendStatusChange(Port::RangeChange
);
130 panic("AtomicSimpleCPU doesn't expect recvStatusChange callback!");
134 AtomicSimpleCPU::CpuPort::recvRetry()
136 panic("AtomicSimpleCPU doesn't expect recvRetry callback!");
140 AtomicSimpleCPU::DcachePort::setPeer(Port
*port
)
145 // Update the ThreadContext's memory ports (Functional/Virtual
147 cpu
->tcBase()->connectMemPorts();
151 AtomicSimpleCPU::AtomicSimpleCPU(Params
*p
)
152 : BaseSimpleCPU(p
), tickEvent(this),
153 width(p
->width
), simulate_stalls(p
->simulate_stalls
),
154 icachePort(name() + "-iport", this), dcachePort(name() + "-iport", this),
155 physmemPort(name() + "-iport", this), hasPhysMemPort(false)
159 icachePort
.snoopRangeSent
= false;
160 dcachePort
.snoopRangeSent
= false;
162 ifetch_req
.setThreadContext(p
->cpu_id
, 0); // Add thread ID if we add MT
163 data_read_req
.setThreadContext(p
->cpu_id
, 0); // Add thread ID here too
164 data_write_req
.setThreadContext(p
->cpu_id
, 0); // Add thread ID here too
168 AtomicSimpleCPU::~AtomicSimpleCPU()
173 AtomicSimpleCPU::serialize(ostream
&os
)
175 SimObject::State so_state
= SimObject::getState();
176 SERIALIZE_ENUM(so_state
);
177 Status _status
= status();
178 SERIALIZE_ENUM(_status
);
179 BaseSimpleCPU::serialize(os
);
180 nameOut(os
, csprintf("%s.tickEvent", name()));
181 tickEvent
.serialize(os
);
185 AtomicSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
187 SimObject::State so_state
;
188 UNSERIALIZE_ENUM(so_state
);
189 UNSERIALIZE_ENUM(_status
);
190 BaseSimpleCPU::unserialize(cp
, section
);
191 tickEvent
.unserialize(cp
, csprintf("%s.tickEvent", section
));
195 AtomicSimpleCPU::resume()
197 DPRINTF(SimpleCPU
, "Resume\n");
198 if (_status
!= SwitchedOut
&& _status
!= Idle
) {
199 assert(system
->getMemoryMode() == Enums::atomic
);
201 changeState(SimObject::Running
);
202 if (thread
->status() == ThreadContext::Active
) {
203 if (!tickEvent
.scheduled()) {
204 tickEvent
.schedule(nextCycle());
211 AtomicSimpleCPU::switchOut()
213 assert(status() == Running
|| status() == Idle
);
214 _status
= SwitchedOut
;
221 AtomicSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
223 BaseCPU::takeOverFrom(oldCPU
, &icachePort
, &dcachePort
);
225 assert(!tickEvent
.scheduled());
227 // if any of this CPU's ThreadContexts are active, mark the CPU as
228 // running and schedule its tick event.
229 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
230 ThreadContext
*tc
= threadContexts
[i
];
231 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
233 tickEvent
.schedule(nextCycle());
237 if (_status
!= Running
) {
244 AtomicSimpleCPU::activateContext(int thread_num
, int delay
)
246 DPRINTF(SimpleCPU
, "ActivateContext %d (%d cycles)\n", thread_num
, delay
);
248 assert(thread_num
== 0);
251 assert(_status
== Idle
);
252 assert(!tickEvent
.scheduled());
255 numCycles
+= tickToCycles(thread
->lastActivate
- thread
->lastSuspend
);
257 //Make sure ticks are still on multiples of cycles
258 tickEvent
.schedule(nextCycle(curTick
+ ticks(delay
)));
264 AtomicSimpleCPU::suspendContext(int thread_num
)
266 DPRINTF(SimpleCPU
, "SuspendContext %d\n", thread_num
);
268 assert(thread_num
== 0);
271 assert(_status
== Running
);
273 // tick event may not be scheduled if this gets called from inside
274 // an instruction's execution, e.g. "quiesce"
275 if (tickEvent
.scheduled())
276 tickEvent
.deschedule();
285 AtomicSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
287 // use the CPU's statically allocated read request and packet objects
288 Request
*req
= &data_read_req
;
291 traceData
->setAddr(addr
);
294 //The block size of our peer.
295 int blockSize
= dcachePort
.peerBlockSize();
296 //The size of the data we're trying to read.
297 int dataSize
= sizeof(T
);
299 uint8_t * dataPtr
= (uint8_t *)&data
;
301 //The address of the second part of this access if it needs to be split
302 //across a cache line boundary.
303 Addr secondAddr
= roundDown(addr
+ dataSize
- 1, blockSize
);
305 if(secondAddr
> addr
)
306 dataSize
= secondAddr
- addr
;
311 req
->setVirt(0, addr
, dataSize
, flags
, thread
->readPC());
313 // translate to physical address
314 Fault fault
= thread
->translateDataReadReq(req
);
316 // Now do the access.
317 if (fault
== NoFault
) {
318 Packet pkt
= Packet(req
,
319 req
->isLocked() ? MemCmd::LoadLockedReq
: MemCmd::ReadReq
,
321 pkt
.dataStatic(dataPtr
);
323 if (req
->isMmapedIpr())
324 dcache_latency
+= TheISA::handleIprRead(thread
->getTC(), &pkt
);
326 if (hasPhysMemPort
&& pkt
.getAddr() == physMemAddr
)
327 dcache_latency
+= physmemPort
.sendAtomic(&pkt
);
329 dcache_latency
+= dcachePort
.sendAtomic(&pkt
);
331 dcache_access
= true;
333 assert(!pkt
.isError());
335 if (req
->isLocked()) {
336 TheISA::handleLockedRead(thread
, req
);
340 // This will need a new way to tell if it has a dcache attached.
341 if (req
->isUncacheable())
342 recordEvent("Uncached Read");
344 //If there's a fault, return it
345 if (fault
!= NoFault
)
347 //If we don't need to access a second cache line, stop now.
348 if (secondAddr
<= addr
)
355 * Set up for accessing the second cache line.
358 //Move the pointer we're reading into to the correct location.
360 //Adjust the size to get the remaining bytes.
361 dataSize
= addr
+ sizeof(T
) - secondAddr
;
362 //And access the right address.
367 #ifndef DOXYGEN_SHOULD_SKIP_THIS
371 AtomicSimpleCPU::read(Addr addr
, Twin32_t
&data
, unsigned flags
);
375 AtomicSimpleCPU::read(Addr addr
, Twin64_t
&data
, unsigned flags
);
379 AtomicSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
383 AtomicSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
387 AtomicSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
391 AtomicSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
393 #endif //DOXYGEN_SHOULD_SKIP_THIS
397 AtomicSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
399 return read(addr
, *(uint64_t*)&data
, flags
);
404 AtomicSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
406 return read(addr
, *(uint32_t*)&data
, flags
);
412 AtomicSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
414 return read(addr
, (uint32_t&)data
, flags
);
420 AtomicSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
422 // use the CPU's statically allocated write request and packet objects
423 Request
*req
= &data_write_req
;
426 traceData
->setAddr(addr
);
429 //The block size of our peer.
430 int blockSize
= dcachePort
.peerBlockSize();
431 //The size of the data we're trying to read.
432 int dataSize
= sizeof(T
);
434 uint8_t * dataPtr
= (uint8_t *)&data
;
436 //The address of the second part of this access if it needs to be split
437 //across a cache line boundary.
438 Addr secondAddr
= roundDown(addr
+ dataSize
- 1, blockSize
);
440 if(secondAddr
> addr
)
441 dataSize
= secondAddr
- addr
;
446 req
->setVirt(0, addr
, dataSize
, flags
, thread
->readPC());
448 // translate to physical address
449 Fault fault
= thread
->translateDataWriteReq(req
);
451 // Now do the access.
452 if (fault
== NoFault
) {
453 MemCmd cmd
= MemCmd::WriteReq
; // default
454 bool do_access
= true; // flag to suppress cache access
456 if (req
->isLocked()) {
457 cmd
= MemCmd::StoreCondReq
;
458 do_access
= TheISA::handleLockedWrite(thread
, req
);
459 } else if (req
->isSwap()) {
460 cmd
= MemCmd::SwapReq
;
461 if (req
->isCondSwap()) {
463 req
->setExtraData(*res
);
468 Packet pkt
= Packet(req
, cmd
, Packet::Broadcast
);
469 pkt
.dataStatic(dataPtr
);
471 if (req
->isMmapedIpr()) {
473 TheISA::handleIprWrite(thread
->getTC(), &pkt
);
475 //XXX This needs to be outside of the loop in order to
476 //work properly for cache line boundary crossing
477 //accesses in transendian simulations.
479 if (hasPhysMemPort
&& pkt
.getAddr() == physMemAddr
)
480 dcache_latency
+= physmemPort
.sendAtomic(&pkt
);
482 dcache_latency
+= dcachePort
.sendAtomic(&pkt
);
484 dcache_access
= true;
485 assert(!pkt
.isError());
493 if (res
&& !req
->isSwap()) {
494 *res
= req
->getExtraData();
498 // This will need a new way to tell if it's hooked up to a cache or not.
499 if (req
->isUncacheable())
500 recordEvent("Uncached Write");
502 //If there's a fault or we don't need to access a second cache line,
504 if (fault
!= NoFault
|| secondAddr
<= addr
)
506 // If the write needs to have a fault on the access, consider
507 // calling changeStatus() and changing it to "bad addr write"
513 * Set up for accessing the second cache line.
516 //Move the pointer we're reading into to the correct location.
518 //Adjust the size to get the remaining bytes.
519 dataSize
= addr
+ sizeof(T
) - secondAddr
;
520 //And access the right address.
526 #ifndef DOXYGEN_SHOULD_SKIP_THIS
530 AtomicSimpleCPU::write(Twin32_t data
, Addr addr
,
531 unsigned flags
, uint64_t *res
);
535 AtomicSimpleCPU::write(Twin64_t data
, Addr addr
,
536 unsigned flags
, uint64_t *res
);
540 AtomicSimpleCPU::write(uint64_t data
, Addr addr
,
541 unsigned flags
, uint64_t *res
);
545 AtomicSimpleCPU::write(uint32_t data
, Addr addr
,
546 unsigned flags
, uint64_t *res
);
550 AtomicSimpleCPU::write(uint16_t data
, Addr addr
,
551 unsigned flags
, uint64_t *res
);
555 AtomicSimpleCPU::write(uint8_t data
, Addr addr
,
556 unsigned flags
, uint64_t *res
);
558 #endif //DOXYGEN_SHOULD_SKIP_THIS
562 AtomicSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
564 return write(*(uint64_t*)&data
, addr
, flags
, res
);
569 AtomicSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
571 return write(*(uint32_t*)&data
, addr
, flags
, res
);
577 AtomicSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
579 return write((uint32_t)data
, addr
, flags
, res
);
584 AtomicSimpleCPU::tick()
586 DPRINTF(SimpleCPU
, "Tick\n");
588 Tick latency
= ticks(1); // instruction takes one cycle by default
590 for (int i
= 0; i
< width
; ++i
) {
593 if (!curStaticInst
|| !curStaticInst
->isDelayedCommit())
594 checkForInterrupts();
596 Fault fault
= setupFetchRequest(&ifetch_req
);
598 if (fault
== NoFault
) {
599 Tick icache_latency
= 0;
600 bool icache_access
= false;
601 dcache_access
= false; // assume no dcache access
603 //Fetch more instruction memory if necessary
604 //if(predecoder.needMoreBytes())
606 icache_access
= true;
607 Packet ifetch_pkt
= Packet(&ifetch_req
, MemCmd::ReadReq
,
609 ifetch_pkt
.dataStatic(&inst
);
611 if (hasPhysMemPort
&& ifetch_pkt
.getAddr() == physMemAddr
)
612 icache_latency
= physmemPort
.sendAtomic(&ifetch_pkt
);
614 icache_latency
= icachePort
.sendAtomic(&ifetch_pkt
);
616 assert(!ifetch_pkt
.isError());
618 // ifetch_req is initialized to read the instruction directly
619 // into the CPU object's inst field.
625 fault
= curStaticInst
->execute(this, traceData
);
627 // keep an instruction count
628 if (fault
== NoFault
)
630 else if (traceData
) {
631 // If there was a fault, we should trace this instruction.
639 // @todo remove me after debugging with legion done
640 if (curStaticInst
&& (!curStaticInst
->isMicroop() ||
641 curStaticInst
->isFirstMicroop()))
644 if (simulate_stalls
) {
646 icache_access
? icache_latency
- ticks(1) : 0;
648 dcache_access
? dcache_latency
- ticks(1) : 0;
649 Tick stall_cycles
= (icache_stall
+ dcache_stall
) / ticks(1);
650 if (ticks(stall_cycles
) < (icache_stall
+ dcache_stall
))
651 latency
+= ticks(stall_cycles
+1);
653 latency
+= ticks(stall_cycles
);
657 if(fault
!= NoFault
|| !stayAtPC
)
662 tickEvent
.schedule(curTick
+ latency
);
666 ////////////////////////////////////////////////////////////////////////
668 // AtomicSimpleCPU Simulation Object
671 AtomicSimpleCPUParams::create()
673 AtomicSimpleCPU::Params
*params
= new AtomicSimpleCPU::Params();
675 params
->numberOfThreads
= 1;
676 params
->max_insts_any_thread
= max_insts_any_thread
;
677 params
->max_insts_all_threads
= max_insts_all_threads
;
678 params
->max_loads_any_thread
= max_loads_any_thread
;
679 params
->max_loads_all_threads
= max_loads_all_threads
;
680 params
->progress_interval
= progress_interval
;
681 params
->deferRegistration
= defer_registration
;
682 params
->phase
= phase
;
683 params
->clock
= clock
;
684 params
->functionTrace
= function_trace
;
685 params
->functionTraceStart
= function_trace_start
;
686 params
->width
= width
;
687 params
->simulate_stalls
= simulate_stalls
;
688 params
->system
= system
;
689 params
->cpu_id
= cpu_id
;
690 params
->tracer
= tracer
;
695 params
->profile
= profile
;
696 params
->do_quiesce
= do_quiesce
;
697 params
->do_checkpoint_insts
= do_checkpoint_insts
;
698 params
->do_statistics_insts
= do_statistics_insts
;
700 if (workload
.size() != 1)
701 panic("only one workload allowed");
702 params
->process
= workload
[0];
705 AtomicSimpleCPU
*cpu
= new AtomicSimpleCPU(params
);