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 "config/the_isa.hh"
36 #include "cpu/exetrace.hh"
37 #include "cpu/simple/atomic.hh"
38 #include "mem/packet.hh"
39 #include "mem/packet_access.hh"
40 #include "params/AtomicSimpleCPU.hh"
41 #include "sim/faults.hh"
42 #include "sim/system.hh"
45 using namespace TheISA
;
47 AtomicSimpleCPU::TickEvent::TickEvent(AtomicSimpleCPU
*c
)
48 : Event(CPU_Tick_Pri
), cpu(c
)
54 AtomicSimpleCPU::TickEvent::process()
60 AtomicSimpleCPU::TickEvent::description() const
62 return "AtomicSimpleCPU tick";
66 AtomicSimpleCPU::getPort(const string
&if_name
, int idx
)
68 if (if_name
== "dcache_port")
70 else if (if_name
== "icache_port")
72 else if (if_name
== "physmem_port") {
73 hasPhysMemPort
= true;
77 panic("No Such Port\n");
81 AtomicSimpleCPU::init()
85 ThreadID size
= threadContexts
.size();
86 for (ThreadID i
= 0; i
< size
; ++i
) {
87 ThreadContext
*tc
= threadContexts
[i
];
89 // initialize CPU, including PC
90 TheISA::initCPU(tc
, tc
->contextId());
95 AddrRangeList pmAddrList
;
96 physmemPort
.getPeerAddressRanges(pmAddrList
, snoop
);
97 physMemAddr
= *pmAddrList
.begin();
99 // Atomic doesn't do MT right now, so contextId == threadId
100 ifetch_req
.setThreadContext(_cpuId
, 0); // Add thread ID if we add MT
101 data_read_req
.setThreadContext(_cpuId
, 0); // Add thread ID here too
102 data_write_req
.setThreadContext(_cpuId
, 0); // Add thread ID here too
106 AtomicSimpleCPU::CpuPort::recvTiming(PacketPtr pkt
)
108 panic("AtomicSimpleCPU doesn't expect recvTiming callback!");
113 AtomicSimpleCPU::CpuPort::recvAtomic(PacketPtr pkt
)
115 //Snooping a coherence request, just return
120 AtomicSimpleCPU::CpuPort::recvFunctional(PacketPtr pkt
)
122 //No internal storage to update, just return
127 AtomicSimpleCPU::CpuPort::recvStatusChange(Status status
)
129 if (status
== RangeChange
) {
130 if (!snoopRangeSent
) {
131 snoopRangeSent
= true;
132 sendStatusChange(Port::RangeChange
);
137 panic("AtomicSimpleCPU doesn't expect recvStatusChange callback!");
141 AtomicSimpleCPU::CpuPort::recvRetry()
143 panic("AtomicSimpleCPU doesn't expect recvRetry callback!");
147 AtomicSimpleCPU::DcachePort::setPeer(Port
*port
)
152 // Update the ThreadContext's memory ports (Functional/Virtual
154 cpu
->tcBase()->connectMemPorts(cpu
->tcBase());
158 AtomicSimpleCPU::AtomicSimpleCPU(AtomicSimpleCPUParams
*p
)
159 : BaseSimpleCPU(p
), tickEvent(this), width(p
->width
), locked(false),
160 simulate_data_stalls(p
->simulate_data_stalls
),
161 simulate_inst_stalls(p
->simulate_inst_stalls
),
162 icachePort(name() + "-iport", this), dcachePort(name() + "-iport", this),
163 physmemPort(name() + "-iport", this), hasPhysMemPort(false)
167 icachePort
.snoopRangeSent
= false;
168 dcachePort
.snoopRangeSent
= false;
173 AtomicSimpleCPU::~AtomicSimpleCPU()
175 if (tickEvent
.scheduled()) {
176 deschedule(tickEvent
);
181 AtomicSimpleCPU::serialize(ostream
&os
)
183 SimObject::State so_state
= SimObject::getState();
184 SERIALIZE_ENUM(so_state
);
185 SERIALIZE_SCALAR(locked
);
186 BaseSimpleCPU::serialize(os
);
187 nameOut(os
, csprintf("%s.tickEvent", name()));
188 tickEvent
.serialize(os
);
192 AtomicSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
194 SimObject::State so_state
;
195 UNSERIALIZE_ENUM(so_state
);
196 UNSERIALIZE_SCALAR(locked
);
197 BaseSimpleCPU::unserialize(cp
, section
);
198 tickEvent
.unserialize(cp
, csprintf("%s.tickEvent", section
));
202 AtomicSimpleCPU::resume()
204 if (_status
== Idle
|| _status
== SwitchedOut
)
207 DPRINTF(SimpleCPU
, "Resume\n");
208 assert(system
->getMemoryMode() == Enums::atomic
);
210 changeState(SimObject::Running
);
211 if (thread
->status() == ThreadContext::Active
) {
212 if (!tickEvent
.scheduled())
213 schedule(tickEvent
, nextCycle());
215 system
->totalNumInsts
= 0;
219 AtomicSimpleCPU::switchOut()
221 assert(_status
== Running
|| _status
== Idle
);
222 _status
= SwitchedOut
;
229 AtomicSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
231 BaseCPU::takeOverFrom(oldCPU
, &icachePort
, &dcachePort
);
233 assert(!tickEvent
.scheduled());
235 // if any of this CPU's ThreadContexts are active, mark the CPU as
236 // running and schedule its tick event.
237 ThreadID size
= threadContexts
.size();
238 for (ThreadID i
= 0; i
< size
; ++i
) {
239 ThreadContext
*tc
= threadContexts
[i
];
240 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
242 schedule(tickEvent
, nextCycle());
246 if (_status
!= Running
) {
249 assert(threadContexts
.size() == 1);
250 ifetch_req
.setThreadContext(_cpuId
, 0); // Add thread ID if we add MT
251 data_read_req
.setThreadContext(_cpuId
, 0); // Add thread ID here too
252 data_write_req
.setThreadContext(_cpuId
, 0); // Add thread ID here too
257 AtomicSimpleCPU::activateContext(int thread_num
, int delay
)
259 DPRINTF(SimpleCPU
, "ActivateContext %d (%d cycles)\n", thread_num
, delay
);
261 assert(thread_num
== 0);
264 assert(_status
== Idle
);
265 assert(!tickEvent
.scheduled());
268 numCycles
+= tickToCycles(thread
->lastActivate
- thread
->lastSuspend
);
270 //Make sure ticks are still on multiples of cycles
271 schedule(tickEvent
, nextCycle(curTick() + ticks(delay
)));
277 AtomicSimpleCPU::suspendContext(int thread_num
)
279 DPRINTF(SimpleCPU
, "SuspendContext %d\n", thread_num
);
281 assert(thread_num
== 0);
287 assert(_status
== Running
);
289 // tick event may not be scheduled if this gets called from inside
290 // an instruction's execution, e.g. "quiesce"
291 if (tickEvent
.scheduled())
292 deschedule(tickEvent
);
300 AtomicSimpleCPU::readBytes(Addr addr
, uint8_t * data
,
301 unsigned size
, unsigned flags
)
303 // use the CPU's statically allocated read request and packet objects
304 Request
*req
= &data_read_req
;
307 traceData
->setAddr(addr
);
310 //The block size of our peer.
311 unsigned blockSize
= dcachePort
.peerBlockSize();
312 //The size of the data we're trying to read.
315 //The address of the second part of this access if it needs to be split
316 //across a cache line boundary.
317 Addr secondAddr
= roundDown(addr
+ size
- 1, blockSize
);
319 if (secondAddr
> addr
)
320 size
= secondAddr
- addr
;
325 req
->setVirt(0, addr
, size
, flags
, thread
->pcState().instAddr());
327 // translate to physical address
328 Fault fault
= thread
->dtb
->translateAtomic(req
, tc
, BaseTLB::Read
);
330 // Now do the access.
331 if (fault
== NoFault
&& !req
->getFlags().isSet(Request::NO_ACCESS
)) {
332 Packet pkt
= Packet(req
,
333 req
->isLLSC() ? MemCmd::LoadLockedReq
: MemCmd::ReadReq
,
335 pkt
.dataStatic(data
);
337 if (req
->isMmapedIpr())
338 dcache_latency
+= TheISA::handleIprRead(thread
->getTC(), &pkt
);
340 if (hasPhysMemPort
&& pkt
.getAddr() == physMemAddr
)
341 dcache_latency
+= physmemPort
.sendAtomic(&pkt
);
343 dcache_latency
+= dcachePort
.sendAtomic(&pkt
);
345 dcache_access
= true;
347 assert(!pkt
.isError());
350 TheISA::handleLockedRead(thread
, req
);
354 //If there's a fault, return it
355 if (fault
!= NoFault
) {
356 if (req
->isPrefetch()) {
363 //If we don't need to access a second cache line, stop now.
364 if (secondAddr
<= addr
)
366 if (req
->isLocked() && fault
== NoFault
) {
374 * Set up for accessing the second cache line.
377 //Move the pointer we're reading into to the correct location.
379 //Adjust the size to get the remaining bytes.
380 size
= addr
+ fullSize
- secondAddr
;
381 //And access the right address.
389 AtomicSimpleCPU::read(Addr addr
, T
&data
, unsigned flags
)
391 uint8_t *dataPtr
= (uint8_t *)&data
;
392 memset(dataPtr
, 0, sizeof(data
));
393 Fault fault
= readBytes(addr
, dataPtr
, sizeof(data
), flags
);
394 if (fault
== NoFault
) {
397 traceData
->setData(data
);
402 #ifndef DOXYGEN_SHOULD_SKIP_THIS
406 AtomicSimpleCPU::read(Addr addr
, Twin32_t
&data
, unsigned flags
);
410 AtomicSimpleCPU::read(Addr addr
, Twin64_t
&data
, unsigned flags
);
414 AtomicSimpleCPU::read(Addr addr
, uint64_t &data
, unsigned flags
);
418 AtomicSimpleCPU::read(Addr addr
, uint32_t &data
, unsigned flags
);
422 AtomicSimpleCPU::read(Addr addr
, uint16_t &data
, unsigned flags
);
426 AtomicSimpleCPU::read(Addr addr
, uint8_t &data
, unsigned flags
);
428 #endif //DOXYGEN_SHOULD_SKIP_THIS
432 AtomicSimpleCPU::read(Addr addr
, double &data
, unsigned flags
)
434 return read(addr
, *(uint64_t*)&data
, flags
);
439 AtomicSimpleCPU::read(Addr addr
, float &data
, unsigned flags
)
441 return read(addr
, *(uint32_t*)&data
, flags
);
447 AtomicSimpleCPU::read(Addr addr
, int32_t &data
, unsigned flags
)
449 return read(addr
, (uint32_t&)data
, flags
);
454 AtomicSimpleCPU::writeBytes(uint8_t *data
, unsigned size
,
455 Addr addr
, unsigned flags
, uint64_t *res
)
457 // use the CPU's statically allocated write request and packet objects
458 Request
*req
= &data_write_req
;
461 traceData
->setAddr(addr
);
464 //The block size of our peer.
465 unsigned blockSize
= dcachePort
.peerBlockSize();
466 //The size of the data we're trying to read.
469 //The address of the second part of this access if it needs to be split
470 //across a cache line boundary.
471 Addr secondAddr
= roundDown(addr
+ size
- 1, blockSize
);
473 if(secondAddr
> addr
)
474 size
= secondAddr
- addr
;
479 req
->setVirt(0, addr
, size
, flags
, thread
->pcState().instAddr());
481 // translate to physical address
482 Fault fault
= thread
->dtb
->translateAtomic(req
, tc
, BaseTLB::Write
);
484 // Now do the access.
485 if (fault
== NoFault
) {
486 MemCmd cmd
= MemCmd::WriteReq
; // default
487 bool do_access
= true; // flag to suppress cache access
490 cmd
= MemCmd::StoreCondReq
;
491 do_access
= TheISA::handleLockedWrite(thread
, req
);
492 } else if (req
->isSwap()) {
493 cmd
= MemCmd::SwapReq
;
494 if (req
->isCondSwap()) {
496 req
->setExtraData(*res
);
500 if (do_access
&& !req
->getFlags().isSet(Request::NO_ACCESS
)) {
501 Packet pkt
= Packet(req
, cmd
, Packet::Broadcast
);
502 pkt
.dataStatic(data
);
504 if (req
->isMmapedIpr()) {
506 TheISA::handleIprWrite(thread
->getTC(), &pkt
);
508 if (hasPhysMemPort
&& pkt
.getAddr() == physMemAddr
)
509 dcache_latency
+= physmemPort
.sendAtomic(&pkt
);
511 dcache_latency
+= dcachePort
.sendAtomic(&pkt
);
513 dcache_access
= true;
514 assert(!pkt
.isError());
518 memcpy(res
, pkt
.getPtr
<uint8_t>(), fullSize
);
522 if (res
&& !req
->isSwap()) {
523 *res
= req
->getExtraData();
527 //If there's a fault or we don't need to access a second cache line,
529 if (fault
!= NoFault
|| secondAddr
<= addr
)
531 if (req
->isLocked() && fault
== NoFault
) {
535 if (fault
!= NoFault
&& req
->isPrefetch()) {
543 * Set up for accessing the second cache line.
546 //Move the pointer we're reading into to the correct location.
548 //Adjust the size to get the remaining bytes.
549 size
= addr
+ fullSize
- secondAddr
;
550 //And access the right address.
558 AtomicSimpleCPU::write(T data
, Addr addr
, unsigned flags
, uint64_t *res
)
560 uint8_t *dataPtr
= (uint8_t *)&data
;
562 traceData
->setData(data
);
565 Fault fault
= writeBytes(dataPtr
, sizeof(data
), addr
, flags
, res
);
566 if (fault
== NoFault
&& data_write_req
.isSwap()) {
567 *res
= gtoh((T
)*res
);
573 #ifndef DOXYGEN_SHOULD_SKIP_THIS
577 AtomicSimpleCPU::write(Twin32_t data
, Addr addr
,
578 unsigned flags
, uint64_t *res
);
582 AtomicSimpleCPU::write(Twin64_t data
, Addr addr
,
583 unsigned flags
, uint64_t *res
);
587 AtomicSimpleCPU::write(uint64_t data
, Addr addr
,
588 unsigned flags
, uint64_t *res
);
592 AtomicSimpleCPU::write(uint32_t data
, Addr addr
,
593 unsigned flags
, uint64_t *res
);
597 AtomicSimpleCPU::write(uint16_t data
, Addr addr
,
598 unsigned flags
, uint64_t *res
);
602 AtomicSimpleCPU::write(uint8_t data
, Addr addr
,
603 unsigned flags
, uint64_t *res
);
605 #endif //DOXYGEN_SHOULD_SKIP_THIS
609 AtomicSimpleCPU::write(double data
, Addr addr
, unsigned flags
, uint64_t *res
)
611 return write(*(uint64_t*)&data
, addr
, flags
, res
);
616 AtomicSimpleCPU::write(float data
, Addr addr
, unsigned flags
, uint64_t *res
)
618 return write(*(uint32_t*)&data
, addr
, flags
, res
);
624 AtomicSimpleCPU::write(int32_t data
, Addr addr
, unsigned flags
, uint64_t *res
)
626 return write((uint32_t)data
, addr
, flags
, res
);
631 AtomicSimpleCPU::tick()
633 DPRINTF(SimpleCPU
, "Tick\n");
637 for (int i
= 0; i
< width
|| locked
; ++i
) {
640 if (!curStaticInst
|| !curStaticInst
->isDelayedCommit())
641 checkForInterrupts();
645 Fault fault
= NoFault
;
647 TheISA::PCState pcState
= thread
->pcState();
649 bool needToFetch
= !isRomMicroPC(pcState
.microPC()) &&
652 setupFetchRequest(&ifetch_req
);
653 fault
= thread
->itb
->translateAtomic(&ifetch_req
, tc
,
657 if (fault
== NoFault
) {
658 Tick icache_latency
= 0;
659 bool icache_access
= false;
660 dcache_access
= false; // assume no dcache access
663 // This is commented out because the predecoder would act like
664 // a tiny cache otherwise. It wouldn't be flushed when needed
665 // like the I cache. It should be flushed, and when that works
666 // this code should be uncommented.
667 //Fetch more instruction memory if necessary
668 //if(predecoder.needMoreBytes())
670 icache_access
= true;
671 Packet ifetch_pkt
= Packet(&ifetch_req
, MemCmd::ReadReq
,
673 ifetch_pkt
.dataStatic(&inst
);
675 if (hasPhysMemPort
&& ifetch_pkt
.getAddr() == physMemAddr
)
676 icache_latency
= physmemPort
.sendAtomic(&ifetch_pkt
);
678 icache_latency
= icachePort
.sendAtomic(&ifetch_pkt
);
680 assert(!ifetch_pkt
.isError());
682 // ifetch_req is initialized to read the instruction directly
683 // into the CPU object's inst field.
690 fault
= curStaticInst
->execute(this, traceData
);
692 // keep an instruction count
693 if (fault
== NoFault
)
695 else if (traceData
&& !DTRACE(ExecFaulting
)) {
703 // @todo remove me after debugging with legion done
704 if (curStaticInst
&& (!curStaticInst
->isMicroop() ||
705 curStaticInst
->isFirstMicroop()))
708 Tick stall_ticks
= 0;
709 if (simulate_inst_stalls
&& icache_access
)
710 stall_ticks
+= icache_latency
;
712 if (simulate_data_stalls
&& dcache_access
)
713 stall_ticks
+= dcache_latency
;
716 Tick stall_cycles
= stall_ticks
/ ticks(1);
717 Tick aligned_stall_ticks
= ticks(stall_cycles
);
719 if (aligned_stall_ticks
< stall_ticks
)
720 aligned_stall_ticks
+= 1;
722 latency
+= aligned_stall_ticks
;
726 if(fault
!= NoFault
|| !stayAtPC
)
730 // instruction takes at least one cycle
731 if (latency
< ticks(1))
735 schedule(tickEvent
, curTick() + latency
);
740 AtomicSimpleCPU::printAddr(Addr a
)
742 dcachePort
.printAddr(a
);
746 ////////////////////////////////////////////////////////////////////////
748 // AtomicSimpleCPU Simulation Object
751 AtomicSimpleCPUParams::create()
755 if (workload
.size() != 1)
756 panic("only one workload allowed");
758 return new AtomicSimpleCPU(this);