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/mmapped_ipr.hh"
33 #include "arch/utility.hh"
34 #include "base/bigint.hh"
35 #include "config/the_isa.hh"
36 #include "cpu/simple/atomic.hh"
37 #include "cpu/exetrace.hh"
38 #include "debug/ExecFaulting.hh"
39 #include "debug/SimpleCPU.hh"
40 #include "mem/packet.hh"
41 #include "mem/packet_access.hh"
42 #include "params/AtomicSimpleCPU.hh"
43 #include "sim/faults.hh"
44 #include "sim/system.hh"
45 #include "sim/full_system.hh"
48 using namespace TheISA
;
50 AtomicSimpleCPU::TickEvent::TickEvent(AtomicSimpleCPU
*c
)
51 : Event(CPU_Tick_Pri
), cpu(c
)
57 AtomicSimpleCPU::TickEvent::process()
63 AtomicSimpleCPU::TickEvent::description() const
65 return "AtomicSimpleCPU tick";
69 AtomicSimpleCPU::getPort(const string
&if_name
, int idx
)
71 if (if_name
== "dcache_port")
73 else if (if_name
== "icache_port")
75 else if (if_name
== "physmem_port") {
76 hasPhysMemPort
= true;
80 panic("No Such Port\n");
84 AtomicSimpleCPU::init()
88 ThreadID size
= threadContexts
.size();
89 for (ThreadID i
= 0; i
< size
; ++i
) {
90 ThreadContext
*tc
= threadContexts
[i
];
91 // initialize CPU, including PC
92 TheISA::initCPU(tc
, tc
->contextId());
97 AddrRangeList pmAddrList
;
98 physmemPort
.getPeerAddressRanges(pmAddrList
, snoop
);
99 physMemAddr
= *pmAddrList
.begin();
101 // Atomic doesn't do MT right now, so contextId == threadId
102 ifetch_req
.setThreadContext(_cpuId
, 0); // Add thread ID if we add MT
103 data_read_req
.setThreadContext(_cpuId
, 0); // Add thread ID here too
104 data_write_req
.setThreadContext(_cpuId
, 0); // Add thread ID here too
108 AtomicSimpleCPU::CpuPort::recvTiming(PacketPtr pkt
)
110 panic("AtomicSimpleCPU doesn't expect recvTiming callback!");
115 AtomicSimpleCPU::CpuPort::recvAtomic(PacketPtr pkt
)
117 //Snooping a coherence request, just return
122 AtomicSimpleCPU::CpuPort::recvFunctional(PacketPtr pkt
)
124 //No internal storage to update, just return
129 AtomicSimpleCPU::CpuPort::recvStatusChange(Status status
)
131 if (status
== RangeChange
) {
132 if (!snoopRangeSent
) {
133 snoopRangeSent
= true;
134 sendStatusChange(Port::RangeChange
);
139 panic("AtomicSimpleCPU doesn't expect recvStatusChange callback!");
143 AtomicSimpleCPU::CpuPort::recvRetry()
145 panic("AtomicSimpleCPU doesn't expect recvRetry callback!");
149 AtomicSimpleCPU::DcachePort::setPeer(Port
*port
)
154 // Update the ThreadContext's memory ports (Functional/Virtual
156 cpu
->tcBase()->connectMemPorts(cpu
->tcBase());
160 AtomicSimpleCPU::AtomicSimpleCPU(AtomicSimpleCPUParams
*p
)
161 : BaseSimpleCPU(p
), tickEvent(this), width(p
->width
), locked(false),
162 simulate_data_stalls(p
->simulate_data_stalls
),
163 simulate_inst_stalls(p
->simulate_inst_stalls
),
164 icachePort(name() + "-iport", this), dcachePort(name() + "-iport", this),
165 physmemPort(name() + "-iport", this), hasPhysMemPort(false)
169 icachePort
.snoopRangeSent
= false;
170 dcachePort
.snoopRangeSent
= false;
175 AtomicSimpleCPU::~AtomicSimpleCPU()
177 if (tickEvent
.scheduled()) {
178 deschedule(tickEvent
);
183 AtomicSimpleCPU::serialize(ostream
&os
)
185 SimObject::State so_state
= SimObject::getState();
186 SERIALIZE_ENUM(so_state
);
187 SERIALIZE_SCALAR(locked
);
188 BaseSimpleCPU::serialize(os
);
189 nameOut(os
, csprintf("%s.tickEvent", name()));
190 tickEvent
.serialize(os
);
194 AtomicSimpleCPU::unserialize(Checkpoint
*cp
, const string
§ion
)
196 SimObject::State so_state
;
197 UNSERIALIZE_ENUM(so_state
);
198 UNSERIALIZE_SCALAR(locked
);
199 BaseSimpleCPU::unserialize(cp
, section
);
200 tickEvent
.unserialize(cp
, csprintf("%s.tickEvent", section
));
204 AtomicSimpleCPU::resume()
206 if (_status
== Idle
|| _status
== SwitchedOut
)
209 DPRINTF(SimpleCPU
, "Resume\n");
210 assert(system
->getMemoryMode() == Enums::atomic
);
212 changeState(SimObject::Running
);
213 if (thread
->status() == ThreadContext::Active
) {
214 if (!tickEvent
.scheduled())
215 schedule(tickEvent
, nextCycle());
217 system
->totalNumInsts
= 0;
221 AtomicSimpleCPU::switchOut()
223 assert(_status
== Running
|| _status
== Idle
);
224 _status
= SwitchedOut
;
231 AtomicSimpleCPU::takeOverFrom(BaseCPU
*oldCPU
)
233 BaseCPU::takeOverFrom(oldCPU
, &icachePort
, &dcachePort
);
235 assert(!tickEvent
.scheduled());
237 // if any of this CPU's ThreadContexts are active, mark the CPU as
238 // running and schedule its tick event.
239 ThreadID size
= threadContexts
.size();
240 for (ThreadID i
= 0; i
< size
; ++i
) {
241 ThreadContext
*tc
= threadContexts
[i
];
242 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
244 schedule(tickEvent
, nextCycle());
248 if (_status
!= Running
) {
251 assert(threadContexts
.size() == 1);
252 ifetch_req
.setThreadContext(_cpuId
, 0); // Add thread ID if we add MT
253 data_read_req
.setThreadContext(_cpuId
, 0); // Add thread ID here too
254 data_write_req
.setThreadContext(_cpuId
, 0); // Add thread ID here too
259 AtomicSimpleCPU::activateContext(int thread_num
, int delay
)
261 DPRINTF(SimpleCPU
, "ActivateContext %d (%d cycles)\n", thread_num
, delay
);
263 assert(thread_num
== 0);
266 assert(_status
== Idle
);
267 assert(!tickEvent
.scheduled());
270 numCycles
+= tickToCycles(thread
->lastActivate
- thread
->lastSuspend
);
272 //Make sure ticks are still on multiples of cycles
273 schedule(tickEvent
, nextCycle(curTick() + ticks(delay
)));
279 AtomicSimpleCPU::suspendContext(int thread_num
)
281 DPRINTF(SimpleCPU
, "SuspendContext %d\n", thread_num
);
283 assert(thread_num
== 0);
289 assert(_status
== Running
);
291 // tick event may not be scheduled if this gets called from inside
292 // an instruction's execution, e.g. "quiesce"
293 if (tickEvent
.scheduled())
294 deschedule(tickEvent
);
302 AtomicSimpleCPU::readMem(Addr addr
, uint8_t * data
,
303 unsigned size
, unsigned flags
)
305 // use the CPU's statically allocated read request and packet objects
306 Request
*req
= &data_read_req
;
309 traceData
->setAddr(addr
);
312 //The block size of our peer.
313 unsigned blockSize
= dcachePort
.peerBlockSize();
314 //The size of the data we're trying to read.
317 //The address of the second part of this access if it needs to be split
318 //across a cache line boundary.
319 Addr secondAddr
= roundDown(addr
+ size
- 1, blockSize
);
321 if (secondAddr
> addr
)
322 size
= secondAddr
- addr
;
327 req
->setVirt(0, addr
, size
, flags
, thread
->pcState().instAddr());
329 // translate to physical address
330 Fault fault
= thread
->dtb
->translateAtomic(req
, tc
, BaseTLB::Read
);
332 // Now do the access.
333 if (fault
== NoFault
&& !req
->getFlags().isSet(Request::NO_ACCESS
)) {
334 Packet pkt
= Packet(req
,
335 req
->isLLSC() ? MemCmd::LoadLockedReq
: MemCmd::ReadReq
,
337 pkt
.dataStatic(data
);
339 if (req
->isMmappedIpr())
340 dcache_latency
+= TheISA::handleIprRead(thread
->getTC(), &pkt
);
342 if (hasPhysMemPort
&& pkt
.getAddr() == physMemAddr
)
343 dcache_latency
+= physmemPort
.sendAtomic(&pkt
);
345 dcache_latency
+= dcachePort
.sendAtomic(&pkt
);
347 dcache_access
= true;
349 assert(!pkt
.isError());
352 TheISA::handleLockedRead(thread
, req
);
356 //If there's a fault, return it
357 if (fault
!= NoFault
) {
358 if (req
->isPrefetch()) {
365 //If we don't need to access a second cache line, stop now.
366 if (secondAddr
<= addr
)
368 if (req
->isLocked() && fault
== NoFault
) {
376 * Set up for accessing the second cache line.
379 //Move the pointer we're reading into to the correct location.
381 //Adjust the size to get the remaining bytes.
382 size
= addr
+ fullSize
- secondAddr
;
383 //And access the right address.
390 AtomicSimpleCPU::writeMem(uint8_t *data
, unsigned size
,
391 Addr addr
, unsigned flags
, uint64_t *res
)
393 // use the CPU's statically allocated write request and packet objects
394 Request
*req
= &data_write_req
;
397 traceData
->setAddr(addr
);
400 //The block size of our peer.
401 unsigned blockSize
= dcachePort
.peerBlockSize();
402 //The size of the data we're trying to read.
405 //The address of the second part of this access if it needs to be split
406 //across a cache line boundary.
407 Addr secondAddr
= roundDown(addr
+ size
- 1, blockSize
);
409 if(secondAddr
> addr
)
410 size
= secondAddr
- addr
;
415 req
->setVirt(0, addr
, size
, flags
, thread
->pcState().instAddr());
417 // translate to physical address
418 Fault fault
= thread
->dtb
->translateAtomic(req
, tc
, BaseTLB::Write
);
420 // Now do the access.
421 if (fault
== NoFault
) {
422 MemCmd cmd
= MemCmd::WriteReq
; // default
423 bool do_access
= true; // flag to suppress cache access
426 cmd
= MemCmd::StoreCondReq
;
427 do_access
= TheISA::handleLockedWrite(thread
, req
);
428 } else if (req
->isSwap()) {
429 cmd
= MemCmd::SwapReq
;
430 if (req
->isCondSwap()) {
432 req
->setExtraData(*res
);
436 if (do_access
&& !req
->getFlags().isSet(Request::NO_ACCESS
)) {
437 Packet pkt
= Packet(req
, cmd
, Packet::Broadcast
);
438 pkt
.dataStatic(data
);
440 if (req
->isMmappedIpr()) {
442 TheISA::handleIprWrite(thread
->getTC(), &pkt
);
444 if (hasPhysMemPort
&& pkt
.getAddr() == physMemAddr
)
445 dcache_latency
+= physmemPort
.sendAtomic(&pkt
);
447 dcache_latency
+= dcachePort
.sendAtomic(&pkt
);
449 dcache_access
= true;
450 assert(!pkt
.isError());
454 memcpy(res
, pkt
.getPtr
<uint8_t>(), fullSize
);
458 if (res
&& !req
->isSwap()) {
459 *res
= req
->getExtraData();
463 //If there's a fault or we don't need to access a second cache line,
465 if (fault
!= NoFault
|| secondAddr
<= addr
)
467 if (req
->isLocked() && fault
== NoFault
) {
471 if (fault
!= NoFault
&& req
->isPrefetch()) {
479 * Set up for accessing the second cache line.
482 //Move the pointer we're reading into to the correct location.
484 //Adjust the size to get the remaining bytes.
485 size
= addr
+ fullSize
- secondAddr
;
486 //And access the right address.
493 AtomicSimpleCPU::tick()
495 DPRINTF(SimpleCPU
, "Tick\n");
499 for (int i
= 0; i
< width
|| locked
; ++i
) {
502 if (!curStaticInst
|| !curStaticInst
->isDelayedCommit())
503 checkForInterrupts();
506 // We must have just got suspended by a PC event
510 Fault fault
= NoFault
;
512 TheISA::PCState pcState
= thread
->pcState();
514 bool needToFetch
= !isRomMicroPC(pcState
.microPC()) &&
517 setupFetchRequest(&ifetch_req
);
518 fault
= thread
->itb
->translateAtomic(&ifetch_req
, tc
,
522 if (fault
== NoFault
) {
523 Tick icache_latency
= 0;
524 bool icache_access
= false;
525 dcache_access
= false; // assume no dcache access
528 // This is commented out because the predecoder would act like
529 // a tiny cache otherwise. It wouldn't be flushed when needed
530 // like the I cache. It should be flushed, and when that works
531 // this code should be uncommented.
532 //Fetch more instruction memory if necessary
533 //if(predecoder.needMoreBytes())
535 icache_access
= true;
536 Packet ifetch_pkt
= Packet(&ifetch_req
, MemCmd::ReadReq
,
538 ifetch_pkt
.dataStatic(&inst
);
540 if (hasPhysMemPort
&& ifetch_pkt
.getAddr() == physMemAddr
)
541 icache_latency
= physmemPort
.sendAtomic(&ifetch_pkt
);
543 icache_latency
= icachePort
.sendAtomic(&ifetch_pkt
);
545 assert(!ifetch_pkt
.isError());
547 // ifetch_req is initialized to read the instruction directly
548 // into the CPU object's inst field.
555 fault
= curStaticInst
->execute(this, traceData
);
557 // keep an instruction count
558 if (fault
== NoFault
)
560 else if (traceData
&& !DTRACE(ExecFaulting
)) {
568 // @todo remove me after debugging with legion done
569 if (curStaticInst
&& (!curStaticInst
->isMicroop() ||
570 curStaticInst
->isFirstMicroop()))
573 Tick stall_ticks
= 0;
574 if (simulate_inst_stalls
&& icache_access
)
575 stall_ticks
+= icache_latency
;
577 if (simulate_data_stalls
&& dcache_access
)
578 stall_ticks
+= dcache_latency
;
581 Tick stall_cycles
= stall_ticks
/ ticks(1);
582 Tick aligned_stall_ticks
= ticks(stall_cycles
);
584 if (aligned_stall_ticks
< stall_ticks
)
585 aligned_stall_ticks
+= 1;
587 latency
+= aligned_stall_ticks
;
591 if(fault
!= NoFault
|| !stayAtPC
)
595 // instruction takes at least one cycle
596 if (latency
< ticks(1))
600 schedule(tickEvent
, curTick() + latency
);
605 AtomicSimpleCPU::printAddr(Addr a
)
607 dcachePort
.printAddr(a
);
611 ////////////////////////////////////////////////////////////////////////
613 // AtomicSimpleCPU Simulation Object
616 AtomicSimpleCPUParams::create()
619 if (!FullSystem
&& workload
.size() != 1)
620 panic("only one workload allowed");
621 return new AtomicSimpleCPU(this);