atomic.cc revision 7655
12623SN/A/*
22623SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32623SN/A * All rights reserved.
42623SN/A *
52623SN/A * Redistribution and use in source and binary forms, with or without
62623SN/A * modification, are permitted provided that the following conditions are
72623SN/A * met: redistributions of source code must retain the above copyright
82623SN/A * notice, this list of conditions and the following disclaimer;
92623SN/A * redistributions in binary form must reproduce the above copyright
102623SN/A * notice, this list of conditions and the following disclaimer in the
112623SN/A * documentation and/or other materials provided with the distribution;
122623SN/A * neither the name of the copyright holders nor the names of its
132623SN/A * contributors may be used to endorse or promote products derived from
142623SN/A * this software without specific prior written permission.
152623SN/A *
162623SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172623SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182623SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192623SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202623SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212623SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222623SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232623SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242623SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252623SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262623SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292623SN/A */
302623SN/A
313170Sstever@eecs.umich.edu#include "arch/locked_mem.hh"
323806Ssaidi@eecs.umich.edu#include "arch/mmaped_ipr.hh"
332623SN/A#include "arch/utility.hh"
344040Ssaidi@eecs.umich.edu#include "base/bigint.hh"
356658Snate@binkert.org#include "config/the_isa.hh"
362623SN/A#include "cpu/exetrace.hh"
372623SN/A#include "cpu/simple/atomic.hh"
383348Sbinkertn@umich.edu#include "mem/packet.hh"
393348Sbinkertn@umich.edu#include "mem/packet_access.hh"
404762Snate@binkert.org#include "params/AtomicSimpleCPU.hh"
412901Ssaidi@eecs.umich.edu#include "sim/system.hh"
422623SN/A
432623SN/Ausing namespace std;
442623SN/Ausing namespace TheISA;
452623SN/A
462623SN/AAtomicSimpleCPU::TickEvent::TickEvent(AtomicSimpleCPU *c)
475606Snate@binkert.org    : Event(CPU_Tick_Pri), cpu(c)
482623SN/A{
492623SN/A}
502623SN/A
512623SN/A
522623SN/Avoid
532623SN/AAtomicSimpleCPU::TickEvent::process()
542623SN/A{
552623SN/A    cpu->tick();
562623SN/A}
572623SN/A
582623SN/Aconst char *
595336Shines@cs.fsu.eduAtomicSimpleCPU::TickEvent::description() const
602623SN/A{
614873Sstever@eecs.umich.edu    return "AtomicSimpleCPU tick";
622623SN/A}
632623SN/A
642856Srdreslin@umich.eduPort *
656227Snate@binkert.orgAtomicSimpleCPU::getPort(const string &if_name, int idx)
662856Srdreslin@umich.edu{
672856Srdreslin@umich.edu    if (if_name == "dcache_port")
682856Srdreslin@umich.edu        return &dcachePort;
692856Srdreslin@umich.edu    else if (if_name == "icache_port")
702856Srdreslin@umich.edu        return &icachePort;
714968Sacolyte@umich.edu    else if (if_name == "physmem_port") {
724968Sacolyte@umich.edu        hasPhysMemPort = true;
734968Sacolyte@umich.edu        return &physmemPort;
744968Sacolyte@umich.edu    }
752856Srdreslin@umich.edu    else
762856Srdreslin@umich.edu        panic("No Such Port\n");
772856Srdreslin@umich.edu}
782623SN/A
792623SN/Avoid
802623SN/AAtomicSimpleCPU::init()
812623SN/A{
822623SN/A    BaseCPU::init();
832623SN/A#if FULL_SYSTEM
846221Snate@binkert.org    ThreadID size = threadContexts.size();
856221Snate@binkert.org    for (ThreadID i = 0; i < size; ++i) {
862680Sktlim@umich.edu        ThreadContext *tc = threadContexts[i];
872623SN/A
882623SN/A        // initialize CPU, including PC
895714Shsul@eecs.umich.edu        TheISA::initCPU(tc, tc->contextId());
902623SN/A    }
912623SN/A#endif
924968Sacolyte@umich.edu    if (hasPhysMemPort) {
934968Sacolyte@umich.edu        bool snoop = false;
944968Sacolyte@umich.edu        AddrRangeList pmAddrList;
954968Sacolyte@umich.edu        physmemPort.getPeerAddressRanges(pmAddrList, snoop);
964968Sacolyte@umich.edu        physMemAddr = *pmAddrList.begin();
974968Sacolyte@umich.edu    }
985714Shsul@eecs.umich.edu    // Atomic doesn't do MT right now, so contextId == threadId
995712Shsul@eecs.umich.edu    ifetch_req.setThreadContext(_cpuId, 0); // Add thread ID if we add MT
1005712Shsul@eecs.umich.edu    data_read_req.setThreadContext(_cpuId, 0); // Add thread ID here too
1015712Shsul@eecs.umich.edu    data_write_req.setThreadContext(_cpuId, 0); // Add thread ID here too
1022623SN/A}
1032623SN/A
1042623SN/Abool
1053349Sbinkertn@umich.eduAtomicSimpleCPU::CpuPort::recvTiming(PacketPtr pkt)
1062623SN/A{
1073184Srdreslin@umich.edu    panic("AtomicSimpleCPU doesn't expect recvTiming callback!");
1082623SN/A    return true;
1092623SN/A}
1102623SN/A
1112623SN/ATick
1123349Sbinkertn@umich.eduAtomicSimpleCPU::CpuPort::recvAtomic(PacketPtr pkt)
1132623SN/A{
1143310Srdreslin@umich.edu    //Snooping a coherence request, just return
1153649Srdreslin@umich.edu    return 0;
1162623SN/A}
1172623SN/A
1182623SN/Avoid
1193349Sbinkertn@umich.eduAtomicSimpleCPU::CpuPort::recvFunctional(PacketPtr pkt)
1202623SN/A{
1213184Srdreslin@umich.edu    //No internal storage to update, just return
1223184Srdreslin@umich.edu    return;
1232623SN/A}
1242623SN/A
1252623SN/Avoid
1262623SN/AAtomicSimpleCPU::CpuPort::recvStatusChange(Status status)
1272623SN/A{
1283647Srdreslin@umich.edu    if (status == RangeChange) {
1293647Srdreslin@umich.edu        if (!snoopRangeSent) {
1303647Srdreslin@umich.edu            snoopRangeSent = true;
1313647Srdreslin@umich.edu            sendStatusChange(Port::RangeChange);
1323647Srdreslin@umich.edu        }
1332626SN/A        return;
1343647Srdreslin@umich.edu    }
1352626SN/A
1362623SN/A    panic("AtomicSimpleCPU doesn't expect recvStatusChange callback!");
1372623SN/A}
1382623SN/A
1392657Ssaidi@eecs.umich.eduvoid
1402623SN/AAtomicSimpleCPU::CpuPort::recvRetry()
1412623SN/A{
1422623SN/A    panic("AtomicSimpleCPU doesn't expect recvRetry callback!");
1432623SN/A}
1442623SN/A
1454192Sktlim@umich.eduvoid
1464192Sktlim@umich.eduAtomicSimpleCPU::DcachePort::setPeer(Port *port)
1474192Sktlim@umich.edu{
1484192Sktlim@umich.edu    Port::setPeer(port);
1494192Sktlim@umich.edu
1504192Sktlim@umich.edu#if FULL_SYSTEM
1514192Sktlim@umich.edu    // Update the ThreadContext's memory ports (Functional/Virtual
1524192Sktlim@umich.edu    // Ports)
1535497Ssaidi@eecs.umich.edu    cpu->tcBase()->connectMemPorts(cpu->tcBase());
1544192Sktlim@umich.edu#endif
1554192Sktlim@umich.edu}
1562623SN/A
1575529Snate@binkert.orgAtomicSimpleCPU::AtomicSimpleCPU(AtomicSimpleCPUParams *p)
1586078Sgblack@eecs.umich.edu    : BaseSimpleCPU(p), tickEvent(this), width(p->width), locked(false),
1595487Snate@binkert.org      simulate_data_stalls(p->simulate_data_stalls),
1605487Snate@binkert.org      simulate_inst_stalls(p->simulate_inst_stalls),
1614968Sacolyte@umich.edu      icachePort(name() + "-iport", this), dcachePort(name() + "-iport", this),
1624968Sacolyte@umich.edu      physmemPort(name() + "-iport", this), hasPhysMemPort(false)
1632623SN/A{
1642623SN/A    _status = Idle;
1652623SN/A
1663647Srdreslin@umich.edu    icachePort.snoopRangeSent = false;
1673647Srdreslin@umich.edu    dcachePort.snoopRangeSent = false;
1683647Srdreslin@umich.edu
1692623SN/A}
1702623SN/A
1712623SN/A
1722623SN/AAtomicSimpleCPU::~AtomicSimpleCPU()
1732623SN/A{
1746775SBrad.Beckmann@amd.com    if (tickEvent.scheduled()) {
1756775SBrad.Beckmann@amd.com        deschedule(tickEvent);
1766775SBrad.Beckmann@amd.com    }
1772623SN/A}
1782623SN/A
1792623SN/Avoid
1802623SN/AAtomicSimpleCPU::serialize(ostream &os)
1812623SN/A{
1822915Sktlim@umich.edu    SimObject::State so_state = SimObject::getState();
1832915Sktlim@umich.edu    SERIALIZE_ENUM(so_state);
1846078Sgblack@eecs.umich.edu    SERIALIZE_SCALAR(locked);
1853145Shsul@eecs.umich.edu    BaseSimpleCPU::serialize(os);
1862623SN/A    nameOut(os, csprintf("%s.tickEvent", name()));
1872623SN/A    tickEvent.serialize(os);
1882623SN/A}
1892623SN/A
1902623SN/Avoid
1912623SN/AAtomicSimpleCPU::unserialize(Checkpoint *cp, const string &section)
1922623SN/A{
1932915Sktlim@umich.edu    SimObject::State so_state;
1942915Sktlim@umich.edu    UNSERIALIZE_ENUM(so_state);
1956078Sgblack@eecs.umich.edu    UNSERIALIZE_SCALAR(locked);
1963145Shsul@eecs.umich.edu    BaseSimpleCPU::unserialize(cp, section);
1972915Sktlim@umich.edu    tickEvent.unserialize(cp, csprintf("%s.tickEvent", section));
1982915Sktlim@umich.edu}
1992915Sktlim@umich.edu
2002915Sktlim@umich.eduvoid
2012915Sktlim@umich.eduAtomicSimpleCPU::resume()
2022915Sktlim@umich.edu{
2035220Ssaidi@eecs.umich.edu    if (_status == Idle || _status == SwitchedOut)
2045220Ssaidi@eecs.umich.edu        return;
2055220Ssaidi@eecs.umich.edu
2064940Snate@binkert.org    DPRINTF(SimpleCPU, "Resume\n");
2075220Ssaidi@eecs.umich.edu    assert(system->getMemoryMode() == Enums::atomic);
2083324Shsul@eecs.umich.edu
2095220Ssaidi@eecs.umich.edu    changeState(SimObject::Running);
2105220Ssaidi@eecs.umich.edu    if (thread->status() == ThreadContext::Active) {
2115606Snate@binkert.org        if (!tickEvent.scheduled())
2125606Snate@binkert.org            schedule(tickEvent, nextCycle());
2132915Sktlim@umich.edu    }
2142623SN/A}
2152623SN/A
2162623SN/Avoid
2172798Sktlim@umich.eduAtomicSimpleCPU::switchOut()
2182623SN/A{
2195496Ssaidi@eecs.umich.edu    assert(_status == Running || _status == Idle);
2202798Sktlim@umich.edu    _status = SwitchedOut;
2212623SN/A
2222798Sktlim@umich.edu    tickEvent.squash();
2232623SN/A}
2242623SN/A
2252623SN/A
2262623SN/Avoid
2272623SN/AAtomicSimpleCPU::takeOverFrom(BaseCPU *oldCPU)
2282623SN/A{
2294192Sktlim@umich.edu    BaseCPU::takeOverFrom(oldCPU, &icachePort, &dcachePort);
2302623SN/A
2312623SN/A    assert(!tickEvent.scheduled());
2322623SN/A
2332680Sktlim@umich.edu    // if any of this CPU's ThreadContexts are active, mark the CPU as
2342623SN/A    // running and schedule its tick event.
2356221Snate@binkert.org    ThreadID size = threadContexts.size();
2366221Snate@binkert.org    for (ThreadID i = 0; i < size; ++i) {
2372680Sktlim@umich.edu        ThreadContext *tc = threadContexts[i];
2382680Sktlim@umich.edu        if (tc->status() == ThreadContext::Active && _status != Running) {
2392623SN/A            _status = Running;
2405606Snate@binkert.org            schedule(tickEvent, nextCycle());
2412623SN/A            break;
2422623SN/A        }
2432623SN/A    }
2443512Sktlim@umich.edu    if (_status != Running) {
2453512Sktlim@umich.edu        _status = Idle;
2463512Sktlim@umich.edu    }
2475169Ssaidi@eecs.umich.edu    assert(threadContexts.size() == 1);
2485712Shsul@eecs.umich.edu    ifetch_req.setThreadContext(_cpuId, 0); // Add thread ID if we add MT
2495712Shsul@eecs.umich.edu    data_read_req.setThreadContext(_cpuId, 0); // Add thread ID here too
2505712Shsul@eecs.umich.edu    data_write_req.setThreadContext(_cpuId, 0); // Add thread ID here too
2512623SN/A}
2522623SN/A
2532623SN/A
2542623SN/Avoid
2552623SN/AAtomicSimpleCPU::activateContext(int thread_num, int delay)
2562623SN/A{
2574940Snate@binkert.org    DPRINTF(SimpleCPU, "ActivateContext %d (%d cycles)\n", thread_num, delay);
2584940Snate@binkert.org
2592623SN/A    assert(thread_num == 0);
2602683Sktlim@umich.edu    assert(thread);
2612623SN/A
2622623SN/A    assert(_status == Idle);
2632623SN/A    assert(!tickEvent.scheduled());
2642623SN/A
2652623SN/A    notIdleFraction++;
2665101Ssaidi@eecs.umich.edu    numCycles += tickToCycles(thread->lastActivate - thread->lastSuspend);
2673686Sktlim@umich.edu
2683430Sgblack@eecs.umich.edu    //Make sure ticks are still on multiples of cycles
2695606Snate@binkert.org    schedule(tickEvent, nextCycle(curTick + ticks(delay)));
2702623SN/A    _status = Running;
2712623SN/A}
2722623SN/A
2732623SN/A
2742623SN/Avoid
2752623SN/AAtomicSimpleCPU::suspendContext(int thread_num)
2762623SN/A{
2774940Snate@binkert.org    DPRINTF(SimpleCPU, "SuspendContext %d\n", thread_num);
2784940Snate@binkert.org
2792623SN/A    assert(thread_num == 0);
2802683Sktlim@umich.edu    assert(thread);
2812623SN/A
2826043Sgblack@eecs.umich.edu    if (_status == Idle)
2836043Sgblack@eecs.umich.edu        return;
2846043Sgblack@eecs.umich.edu
2852623SN/A    assert(_status == Running);
2862626SN/A
2872626SN/A    // tick event may not be scheduled if this gets called from inside
2882626SN/A    // an instruction's execution, e.g. "quiesce"
2892626SN/A    if (tickEvent.scheduled())
2905606Snate@binkert.org        deschedule(tickEvent);
2912623SN/A
2922623SN/A    notIdleFraction--;
2932623SN/A    _status = Idle;
2942623SN/A}
2952623SN/A
2962623SN/A
2972623SN/AFault
2987520Sgblack@eecs.umich.eduAtomicSimpleCPU::readBytes(Addr addr, uint8_t * data,
2997520Sgblack@eecs.umich.edu                           unsigned size, unsigned flags)
3002623SN/A{
3013169Sstever@eecs.umich.edu    // use the CPU's statically allocated read request and packet objects
3024870Sstever@eecs.umich.edu    Request *req = &data_read_req;
3032623SN/A
3042623SN/A    if (traceData) {
3052623SN/A        traceData->setAddr(addr);
3062623SN/A    }
3072623SN/A
3084999Sgblack@eecs.umich.edu    //The block size of our peer.
3096227Snate@binkert.org    unsigned blockSize = dcachePort.peerBlockSize();
3104999Sgblack@eecs.umich.edu    //The size of the data we're trying to read.
3117520Sgblack@eecs.umich.edu    int fullSize = size;
3122623SN/A
3134999Sgblack@eecs.umich.edu    //The address of the second part of this access if it needs to be split
3144999Sgblack@eecs.umich.edu    //across a cache line boundary.
3157520Sgblack@eecs.umich.edu    Addr secondAddr = roundDown(addr + size - 1, blockSize);
3164999Sgblack@eecs.umich.edu
3177520Sgblack@eecs.umich.edu    if (secondAddr > addr)
3187520Sgblack@eecs.umich.edu        size = secondAddr - addr;
3194999Sgblack@eecs.umich.edu
3204999Sgblack@eecs.umich.edu    dcache_latency = 0;
3214999Sgblack@eecs.umich.edu
3227520Sgblack@eecs.umich.edu    while (1) {
3237520Sgblack@eecs.umich.edu        req->setVirt(0, addr, size, flags, thread->readPC());
3244999Sgblack@eecs.umich.edu
3254999Sgblack@eecs.umich.edu        // translate to physical address
3266023Snate@binkert.org        Fault fault = thread->dtb->translateAtomic(req, tc, BaseTLB::Read);
3274999Sgblack@eecs.umich.edu
3284999Sgblack@eecs.umich.edu        // Now do the access.
3296623Sgblack@eecs.umich.edu        if (fault == NoFault && !req->getFlags().isSet(Request::NO_ACCESS)) {
3304999Sgblack@eecs.umich.edu            Packet pkt = Packet(req,
3316102Sgblack@eecs.umich.edu                    req->isLLSC() ? MemCmd::LoadLockedReq : MemCmd::ReadReq,
3324999Sgblack@eecs.umich.edu                    Packet::Broadcast);
3337520Sgblack@eecs.umich.edu            pkt.dataStatic(data);
3344999Sgblack@eecs.umich.edu
3354999Sgblack@eecs.umich.edu            if (req->isMmapedIpr())
3364999Sgblack@eecs.umich.edu                dcache_latency += TheISA::handleIprRead(thread->getTC(), &pkt);
3374999Sgblack@eecs.umich.edu            else {
3384999Sgblack@eecs.umich.edu                if (hasPhysMemPort && pkt.getAddr() == physMemAddr)
3394999Sgblack@eecs.umich.edu                    dcache_latency += physmemPort.sendAtomic(&pkt);
3404999Sgblack@eecs.umich.edu                else
3414999Sgblack@eecs.umich.edu                    dcache_latency += dcachePort.sendAtomic(&pkt);
3424999Sgblack@eecs.umich.edu            }
3434999Sgblack@eecs.umich.edu            dcache_access = true;
3445012Sgblack@eecs.umich.edu
3454999Sgblack@eecs.umich.edu            assert(!pkt.isError());
3464999Sgblack@eecs.umich.edu
3476102Sgblack@eecs.umich.edu            if (req->isLLSC()) {
3484999Sgblack@eecs.umich.edu                TheISA::handleLockedRead(thread, req);
3494999Sgblack@eecs.umich.edu            }
3504968Sacolyte@umich.edu        }
3514986Ssaidi@eecs.umich.edu
3524999Sgblack@eecs.umich.edu        //If there's a fault, return it
3536739Sgblack@eecs.umich.edu        if (fault != NoFault) {
3546739Sgblack@eecs.umich.edu            if (req->isPrefetch()) {
3556739Sgblack@eecs.umich.edu                return NoFault;
3566739Sgblack@eecs.umich.edu            } else {
3576739Sgblack@eecs.umich.edu                return fault;
3586739Sgblack@eecs.umich.edu            }
3596739Sgblack@eecs.umich.edu        }
3606739Sgblack@eecs.umich.edu
3614999Sgblack@eecs.umich.edu        //If we don't need to access a second cache line, stop now.
3624999Sgblack@eecs.umich.edu        if (secondAddr <= addr)
3634999Sgblack@eecs.umich.edu        {
3646078Sgblack@eecs.umich.edu            if (req->isLocked() && fault == NoFault) {
3656078Sgblack@eecs.umich.edu                assert(!locked);
3666078Sgblack@eecs.umich.edu                locked = true;
3676078Sgblack@eecs.umich.edu            }
3684999Sgblack@eecs.umich.edu            return fault;
3694968Sacolyte@umich.edu        }
3703170Sstever@eecs.umich.edu
3714999Sgblack@eecs.umich.edu        /*
3724999Sgblack@eecs.umich.edu         * Set up for accessing the second cache line.
3734999Sgblack@eecs.umich.edu         */
3744999Sgblack@eecs.umich.edu
3754999Sgblack@eecs.umich.edu        //Move the pointer we're reading into to the correct location.
3767520Sgblack@eecs.umich.edu        data += size;
3774999Sgblack@eecs.umich.edu        //Adjust the size to get the remaining bytes.
3787520Sgblack@eecs.umich.edu        size = addr + fullSize - secondAddr;
3794999Sgblack@eecs.umich.edu        //And access the right address.
3804999Sgblack@eecs.umich.edu        addr = secondAddr;
3812623SN/A    }
3822623SN/A}
3832623SN/A
3847520Sgblack@eecs.umich.edu
3857520Sgblack@eecs.umich.edutemplate <class T>
3867520Sgblack@eecs.umich.eduFault
3877520Sgblack@eecs.umich.eduAtomicSimpleCPU::read(Addr addr, T &data, unsigned flags)
3887520Sgblack@eecs.umich.edu{
3897520Sgblack@eecs.umich.edu    uint8_t *dataPtr = (uint8_t *)&data;
3907520Sgblack@eecs.umich.edu    memset(dataPtr, 0, sizeof(data));
3917520Sgblack@eecs.umich.edu    Fault fault = readBytes(addr, dataPtr, sizeof(data), flags);
3927520Sgblack@eecs.umich.edu    if (fault == NoFault) {
3937520Sgblack@eecs.umich.edu        data = gtoh(data);
3947520Sgblack@eecs.umich.edu        if (traceData)
3957520Sgblack@eecs.umich.edu            traceData->setData(data);
3967520Sgblack@eecs.umich.edu    }
3977520Sgblack@eecs.umich.edu    return fault;
3987520Sgblack@eecs.umich.edu}
3997520Sgblack@eecs.umich.edu
4002623SN/A#ifndef DOXYGEN_SHOULD_SKIP_THIS
4012623SN/A
4022623SN/Atemplate
4032623SN/AFault
4044115Ssaidi@eecs.umich.eduAtomicSimpleCPU::read(Addr addr, Twin32_t &data, unsigned flags);
4054115Ssaidi@eecs.umich.edu
4064115Ssaidi@eecs.umich.edutemplate
4074115Ssaidi@eecs.umich.eduFault
4084040Ssaidi@eecs.umich.eduAtomicSimpleCPU::read(Addr addr, Twin64_t &data, unsigned flags);
4094040Ssaidi@eecs.umich.edu
4104040Ssaidi@eecs.umich.edutemplate
4114040Ssaidi@eecs.umich.eduFault
4122623SN/AAtomicSimpleCPU::read(Addr addr, uint64_t &data, unsigned flags);
4132623SN/A
4142623SN/Atemplate
4152623SN/AFault
4162623SN/AAtomicSimpleCPU::read(Addr addr, uint32_t &data, unsigned flags);
4172623SN/A
4182623SN/Atemplate
4192623SN/AFault
4202623SN/AAtomicSimpleCPU::read(Addr addr, uint16_t &data, unsigned flags);
4212623SN/A
4222623SN/Atemplate
4232623SN/AFault
4242623SN/AAtomicSimpleCPU::read(Addr addr, uint8_t &data, unsigned flags);
4252623SN/A
4262623SN/A#endif //DOXYGEN_SHOULD_SKIP_THIS
4272623SN/A
4282623SN/Atemplate<>
4292623SN/AFault
4302623SN/AAtomicSimpleCPU::read(Addr addr, double &data, unsigned flags)
4312623SN/A{
4322623SN/A    return read(addr, *(uint64_t*)&data, flags);
4332623SN/A}
4342623SN/A
4352623SN/Atemplate<>
4362623SN/AFault
4372623SN/AAtomicSimpleCPU::read(Addr addr, float &data, unsigned flags)
4382623SN/A{
4392623SN/A    return read(addr, *(uint32_t*)&data, flags);
4402623SN/A}
4412623SN/A
4422623SN/A
4432623SN/Atemplate<>
4442623SN/AFault
4452623SN/AAtomicSimpleCPU::read(Addr addr, int32_t &data, unsigned flags)
4462623SN/A{
4472623SN/A    return read(addr, (uint32_t&)data, flags);
4482623SN/A}
4492623SN/A
4502623SN/A
4512623SN/AFault
4527520Sgblack@eecs.umich.eduAtomicSimpleCPU::writeBytes(uint8_t *data, unsigned size,
4537520Sgblack@eecs.umich.edu                            Addr addr, unsigned flags, uint64_t *res)
4542623SN/A{
4553169Sstever@eecs.umich.edu    // use the CPU's statically allocated write request and packet objects
4564870Sstever@eecs.umich.edu    Request *req = &data_write_req;
4572623SN/A
4582623SN/A    if (traceData) {
4592623SN/A        traceData->setAddr(addr);
4602623SN/A    }
4612623SN/A
4624999Sgblack@eecs.umich.edu    //The block size of our peer.
4636227Snate@binkert.org    unsigned blockSize = dcachePort.peerBlockSize();
4644999Sgblack@eecs.umich.edu    //The size of the data we're trying to read.
4657520Sgblack@eecs.umich.edu    int fullSize = size;
4662623SN/A
4674999Sgblack@eecs.umich.edu    //The address of the second part of this access if it needs to be split
4684999Sgblack@eecs.umich.edu    //across a cache line boundary.
4697520Sgblack@eecs.umich.edu    Addr secondAddr = roundDown(addr + size - 1, blockSize);
4704999Sgblack@eecs.umich.edu
4714999Sgblack@eecs.umich.edu    if(secondAddr > addr)
4727520Sgblack@eecs.umich.edu        size = secondAddr - addr;
4734999Sgblack@eecs.umich.edu
4744999Sgblack@eecs.umich.edu    dcache_latency = 0;
4754999Sgblack@eecs.umich.edu
4764999Sgblack@eecs.umich.edu    while(1) {
4777520Sgblack@eecs.umich.edu        req->setVirt(0, addr, size, flags, thread->readPC());
4784999Sgblack@eecs.umich.edu
4794999Sgblack@eecs.umich.edu        // translate to physical address
4806023Snate@binkert.org        Fault fault = thread->dtb->translateAtomic(req, tc, BaseTLB::Write);
4814999Sgblack@eecs.umich.edu
4824999Sgblack@eecs.umich.edu        // Now do the access.
4834999Sgblack@eecs.umich.edu        if (fault == NoFault) {
4844999Sgblack@eecs.umich.edu            MemCmd cmd = MemCmd::WriteReq; // default
4854999Sgblack@eecs.umich.edu            bool do_access = true;  // flag to suppress cache access
4864999Sgblack@eecs.umich.edu
4876102Sgblack@eecs.umich.edu            if (req->isLLSC()) {
4884999Sgblack@eecs.umich.edu                cmd = MemCmd::StoreCondReq;
4894999Sgblack@eecs.umich.edu                do_access = TheISA::handleLockedWrite(thread, req);
4904999Sgblack@eecs.umich.edu            } else if (req->isSwap()) {
4914999Sgblack@eecs.umich.edu                cmd = MemCmd::SwapReq;
4924999Sgblack@eecs.umich.edu                if (req->isCondSwap()) {
4934999Sgblack@eecs.umich.edu                    assert(res);
4944999Sgblack@eecs.umich.edu                    req->setExtraData(*res);
4954999Sgblack@eecs.umich.edu                }
4964999Sgblack@eecs.umich.edu            }
4974999Sgblack@eecs.umich.edu
4986623Sgblack@eecs.umich.edu            if (do_access && !req->getFlags().isSet(Request::NO_ACCESS)) {
4994999Sgblack@eecs.umich.edu                Packet pkt = Packet(req, cmd, Packet::Broadcast);
5007520Sgblack@eecs.umich.edu                pkt.dataStatic(data);
5014999Sgblack@eecs.umich.edu
5024999Sgblack@eecs.umich.edu                if (req->isMmapedIpr()) {
5034999Sgblack@eecs.umich.edu                    dcache_latency +=
5044999Sgblack@eecs.umich.edu                        TheISA::handleIprWrite(thread->getTC(), &pkt);
5054999Sgblack@eecs.umich.edu                } else {
5064999Sgblack@eecs.umich.edu                    if (hasPhysMemPort && pkt.getAddr() == physMemAddr)
5074999Sgblack@eecs.umich.edu                        dcache_latency += physmemPort.sendAtomic(&pkt);
5084999Sgblack@eecs.umich.edu                    else
5094999Sgblack@eecs.umich.edu                        dcache_latency += dcachePort.sendAtomic(&pkt);
5104999Sgblack@eecs.umich.edu                }
5114999Sgblack@eecs.umich.edu                dcache_access = true;
5124999Sgblack@eecs.umich.edu                assert(!pkt.isError());
5134999Sgblack@eecs.umich.edu
5144999Sgblack@eecs.umich.edu                if (req->isSwap()) {
5154999Sgblack@eecs.umich.edu                    assert(res);
5167520Sgblack@eecs.umich.edu                    memcpy(res, pkt.getPtr<uint8_t>(), fullSize);
5174999Sgblack@eecs.umich.edu                }
5184999Sgblack@eecs.umich.edu            }
5194999Sgblack@eecs.umich.edu
5204999Sgblack@eecs.umich.edu            if (res && !req->isSwap()) {
5214999Sgblack@eecs.umich.edu                *res = req->getExtraData();
5224878Sstever@eecs.umich.edu            }
5234040Ssaidi@eecs.umich.edu        }
5244040Ssaidi@eecs.umich.edu
5254999Sgblack@eecs.umich.edu        //If there's a fault or we don't need to access a second cache line,
5264999Sgblack@eecs.umich.edu        //stop now.
5274999Sgblack@eecs.umich.edu        if (fault != NoFault || secondAddr <= addr)
5284999Sgblack@eecs.umich.edu        {
5296078Sgblack@eecs.umich.edu            if (req->isLocked() && fault == NoFault) {
5306078Sgblack@eecs.umich.edu                assert(locked);
5316078Sgblack@eecs.umich.edu                locked = false;
5326078Sgblack@eecs.umich.edu            }
5336739Sgblack@eecs.umich.edu            if (fault != NoFault && req->isPrefetch()) {
5346739Sgblack@eecs.umich.edu                return NoFault;
5356739Sgblack@eecs.umich.edu            } else {
5366739Sgblack@eecs.umich.edu                return fault;
5376739Sgblack@eecs.umich.edu            }
5383170Sstever@eecs.umich.edu        }
5393170Sstever@eecs.umich.edu
5404999Sgblack@eecs.umich.edu        /*
5414999Sgblack@eecs.umich.edu         * Set up for accessing the second cache line.
5424999Sgblack@eecs.umich.edu         */
5434999Sgblack@eecs.umich.edu
5444999Sgblack@eecs.umich.edu        //Move the pointer we're reading into to the correct location.
5457520Sgblack@eecs.umich.edu        data += size;
5464999Sgblack@eecs.umich.edu        //Adjust the size to get the remaining bytes.
5477520Sgblack@eecs.umich.edu        size = addr + fullSize - secondAddr;
5484999Sgblack@eecs.umich.edu        //And access the right address.
5494999Sgblack@eecs.umich.edu        addr = secondAddr;
5502623SN/A    }
5512623SN/A}
5522623SN/A
5532623SN/A
5547520Sgblack@eecs.umich.edutemplate <class T>
5557520Sgblack@eecs.umich.eduFault
5567520Sgblack@eecs.umich.eduAtomicSimpleCPU::write(T data, Addr addr, unsigned flags, uint64_t *res)
5577520Sgblack@eecs.umich.edu{
5587520Sgblack@eecs.umich.edu    uint8_t *dataPtr = (uint8_t *)&data;
5597520Sgblack@eecs.umich.edu    if (traceData)
5607520Sgblack@eecs.umich.edu        traceData->setData(data);
5617520Sgblack@eecs.umich.edu    data = htog(data);
5627520Sgblack@eecs.umich.edu
5637520Sgblack@eecs.umich.edu    Fault fault = writeBytes(dataPtr, sizeof(data), addr, flags, res);
5647520Sgblack@eecs.umich.edu    if (fault == NoFault && data_write_req.isSwap()) {
5657520Sgblack@eecs.umich.edu        *res = gtoh((T)*res);
5667520Sgblack@eecs.umich.edu    }
5677520Sgblack@eecs.umich.edu    return fault;
5687520Sgblack@eecs.umich.edu}
5697520Sgblack@eecs.umich.edu
5707520Sgblack@eecs.umich.edu
5712623SN/A#ifndef DOXYGEN_SHOULD_SKIP_THIS
5724224Sgblack@eecs.umich.edu
5734224Sgblack@eecs.umich.edutemplate
5744224Sgblack@eecs.umich.eduFault
5754224Sgblack@eecs.umich.eduAtomicSimpleCPU::write(Twin32_t data, Addr addr,
5764224Sgblack@eecs.umich.edu                       unsigned flags, uint64_t *res);
5774224Sgblack@eecs.umich.edu
5784224Sgblack@eecs.umich.edutemplate
5794224Sgblack@eecs.umich.eduFault
5804224Sgblack@eecs.umich.eduAtomicSimpleCPU::write(Twin64_t data, Addr addr,
5814224Sgblack@eecs.umich.edu                       unsigned flags, uint64_t *res);
5824224Sgblack@eecs.umich.edu
5832623SN/Atemplate
5842623SN/AFault
5852623SN/AAtomicSimpleCPU::write(uint64_t data, Addr addr,
5862623SN/A                       unsigned flags, uint64_t *res);
5872623SN/A
5882623SN/Atemplate
5892623SN/AFault
5902623SN/AAtomicSimpleCPU::write(uint32_t data, Addr addr,
5912623SN/A                       unsigned flags, uint64_t *res);
5922623SN/A
5932623SN/Atemplate
5942623SN/AFault
5952623SN/AAtomicSimpleCPU::write(uint16_t data, Addr addr,
5962623SN/A                       unsigned flags, uint64_t *res);
5972623SN/A
5982623SN/Atemplate
5992623SN/AFault
6002623SN/AAtomicSimpleCPU::write(uint8_t data, Addr addr,
6012623SN/A                       unsigned flags, uint64_t *res);
6022623SN/A
6032623SN/A#endif //DOXYGEN_SHOULD_SKIP_THIS
6042623SN/A
6052623SN/Atemplate<>
6062623SN/AFault
6072623SN/AAtomicSimpleCPU::write(double data, Addr addr, unsigned flags, uint64_t *res)
6082623SN/A{
6092623SN/A    return write(*(uint64_t*)&data, addr, flags, res);
6102623SN/A}
6112623SN/A
6122623SN/Atemplate<>
6132623SN/AFault
6142623SN/AAtomicSimpleCPU::write(float data, Addr addr, unsigned flags, uint64_t *res)
6152623SN/A{
6162623SN/A    return write(*(uint32_t*)&data, addr, flags, res);
6172623SN/A}
6182623SN/A
6192623SN/A
6202623SN/Atemplate<>
6212623SN/AFault
6222623SN/AAtomicSimpleCPU::write(int32_t data, Addr addr, unsigned flags, uint64_t *res)
6232623SN/A{
6242623SN/A    return write((uint32_t)data, addr, flags, res);
6252623SN/A}
6262623SN/A
6272623SN/A
6282623SN/Avoid
6292623SN/AAtomicSimpleCPU::tick()
6302623SN/A{
6314940Snate@binkert.org    DPRINTF(SimpleCPU, "Tick\n");
6324940Snate@binkert.org
6335487Snate@binkert.org    Tick latency = 0;
6342623SN/A
6356078Sgblack@eecs.umich.edu    for (int i = 0; i < width || locked; ++i) {
6362623SN/A        numCycles++;
6372623SN/A
6383387Sgblack@eecs.umich.edu        if (!curStaticInst || !curStaticInst->isDelayedCommit())
6393387Sgblack@eecs.umich.edu            checkForInterrupts();
6402626SN/A
6415348Ssaidi@eecs.umich.edu        checkPcEventQueue();
6425348Ssaidi@eecs.umich.edu
6435669Sgblack@eecs.umich.edu        Fault fault = NoFault;
6445669Sgblack@eecs.umich.edu
6455669Sgblack@eecs.umich.edu        bool fromRom = isRomMicroPC(thread->readMicroPC());
6465914Sgblack@eecs.umich.edu        if (!fromRom && !curMacroStaticInst) {
6475894Sgblack@eecs.umich.edu            setupFetchRequest(&ifetch_req);
6486023Snate@binkert.org            fault = thread->itb->translateAtomic(&ifetch_req, tc,
6496023Snate@binkert.org                                                 BaseTLB::Execute);
6505894Sgblack@eecs.umich.edu        }
6512623SN/A
6522623SN/A        if (fault == NoFault) {
6534182Sgblack@eecs.umich.edu            Tick icache_latency = 0;
6544182Sgblack@eecs.umich.edu            bool icache_access = false;
6554182Sgblack@eecs.umich.edu            dcache_access = false; // assume no dcache access
6562662Sstever@eecs.umich.edu
6575914Sgblack@eecs.umich.edu            if (!fromRom && !curMacroStaticInst) {
6585694Sgblack@eecs.umich.edu                // This is commented out because the predecoder would act like
6595694Sgblack@eecs.umich.edu                // a tiny cache otherwise. It wouldn't be flushed when needed
6605694Sgblack@eecs.umich.edu                // like the I cache. It should be flushed, and when that works
6615694Sgblack@eecs.umich.edu                // this code should be uncommented.
6625669Sgblack@eecs.umich.edu                //Fetch more instruction memory if necessary
6635669Sgblack@eecs.umich.edu                //if(predecoder.needMoreBytes())
6645669Sgblack@eecs.umich.edu                //{
6655669Sgblack@eecs.umich.edu                    icache_access = true;
6665669Sgblack@eecs.umich.edu                    Packet ifetch_pkt = Packet(&ifetch_req, MemCmd::ReadReq,
6675669Sgblack@eecs.umich.edu                                               Packet::Broadcast);
6685669Sgblack@eecs.umich.edu                    ifetch_pkt.dataStatic(&inst);
6692623SN/A
6705669Sgblack@eecs.umich.edu                    if (hasPhysMemPort && ifetch_pkt.getAddr() == physMemAddr)
6715669Sgblack@eecs.umich.edu                        icache_latency = physmemPort.sendAtomic(&ifetch_pkt);
6725669Sgblack@eecs.umich.edu                    else
6735669Sgblack@eecs.umich.edu                        icache_latency = icachePort.sendAtomic(&ifetch_pkt);
6744968Sacolyte@umich.edu
6755669Sgblack@eecs.umich.edu                    assert(!ifetch_pkt.isError());
6764968Sacolyte@umich.edu
6775669Sgblack@eecs.umich.edu                    // ifetch_req is initialized to read the instruction directly
6785669Sgblack@eecs.umich.edu                    // into the CPU object's inst field.
6795669Sgblack@eecs.umich.edu                //}
6805669Sgblack@eecs.umich.edu            }
6814182Sgblack@eecs.umich.edu
6822623SN/A            preExecute();
6833814Ssaidi@eecs.umich.edu
6845001Sgblack@eecs.umich.edu            if (curStaticInst) {
6854182Sgblack@eecs.umich.edu                fault = curStaticInst->execute(this, traceData);
6864998Sgblack@eecs.umich.edu
6874998Sgblack@eecs.umich.edu                // keep an instruction count
6884998Sgblack@eecs.umich.edu                if (fault == NoFault)
6894998Sgblack@eecs.umich.edu                    countInst();
6907655Sali.saidi@arm.com                else if (traceData && !DTRACE(ExecFaulting)) {
6915001Sgblack@eecs.umich.edu                    delete traceData;
6925001Sgblack@eecs.umich.edu                    traceData = NULL;
6935001Sgblack@eecs.umich.edu                }
6944998Sgblack@eecs.umich.edu
6954182Sgblack@eecs.umich.edu                postExecute();
6964182Sgblack@eecs.umich.edu            }
6972623SN/A
6983814Ssaidi@eecs.umich.edu            // @todo remove me after debugging with legion done
6994539Sgblack@eecs.umich.edu            if (curStaticInst && (!curStaticInst->isMicroop() ||
7004539Sgblack@eecs.umich.edu                        curStaticInst->isFirstMicroop()))
7013814Ssaidi@eecs.umich.edu                instCnt++;
7023814Ssaidi@eecs.umich.edu
7035487Snate@binkert.org            Tick stall_ticks = 0;
7045487Snate@binkert.org            if (simulate_inst_stalls && icache_access)
7055487Snate@binkert.org                stall_ticks += icache_latency;
7065487Snate@binkert.org
7075487Snate@binkert.org            if (simulate_data_stalls && dcache_access)
7085487Snate@binkert.org                stall_ticks += dcache_latency;
7095487Snate@binkert.org
7105487Snate@binkert.org            if (stall_ticks) {
7115487Snate@binkert.org                Tick stall_cycles = stall_ticks / ticks(1);
7125487Snate@binkert.org                Tick aligned_stall_ticks = ticks(stall_cycles);
7135487Snate@binkert.org
7145487Snate@binkert.org                if (aligned_stall_ticks < stall_ticks)
7155487Snate@binkert.org                    aligned_stall_ticks += 1;
7165487Snate@binkert.org
7175487Snate@binkert.org                latency += aligned_stall_ticks;
7182623SN/A            }
7192623SN/A
7202623SN/A        }
7214377Sgblack@eecs.umich.edu        if(fault != NoFault || !stayAtPC)
7224182Sgblack@eecs.umich.edu            advancePC(fault);
7232623SN/A    }
7242623SN/A
7255487Snate@binkert.org    // instruction takes at least one cycle
7265487Snate@binkert.org    if (latency < ticks(1))
7275487Snate@binkert.org        latency = ticks(1);
7285487Snate@binkert.org
7292626SN/A    if (_status != Idle)
7305606Snate@binkert.org        schedule(tickEvent, curTick + latency);
7312623SN/A}
7322623SN/A
7332623SN/A
7345315Sstever@gmail.comvoid
7355315Sstever@gmail.comAtomicSimpleCPU::printAddr(Addr a)
7365315Sstever@gmail.com{
7375315Sstever@gmail.com    dcachePort.printAddr(a);
7385315Sstever@gmail.com}
7395315Sstever@gmail.com
7405315Sstever@gmail.com
7412623SN/A////////////////////////////////////////////////////////////////////////
7422623SN/A//
7432623SN/A//  AtomicSimpleCPU Simulation Object
7442623SN/A//
7454762Snate@binkert.orgAtomicSimpleCPU *
7464762Snate@binkert.orgAtomicSimpleCPUParams::create()
7472623SN/A{
7485529Snate@binkert.org    numThreads = 1;
7495529Snate@binkert.org#if !FULL_SYSTEM
7504762Snate@binkert.org    if (workload.size() != 1)
7514762Snate@binkert.org        panic("only one workload allowed");
7522623SN/A#endif
7535529Snate@binkert.org    return new AtomicSimpleCPU(this);
7542623SN/A}
755