atomic.cc revision 9424
12623SN/A/*
28926Sandreas.hansson@arm.com * Copyright (c) 2012 ARM Limited
38926Sandreas.hansson@arm.com * All rights reserved.
48926Sandreas.hansson@arm.com *
58926Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
68926Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
78926Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
88926Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
98926Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
108926Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
118926Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
128926Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
138926Sandreas.hansson@arm.com *
142623SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
152623SN/A * All rights reserved.
162623SN/A *
172623SN/A * Redistribution and use in source and binary forms, with or without
182623SN/A * modification, are permitted provided that the following conditions are
192623SN/A * met: redistributions of source code must retain the above copyright
202623SN/A * notice, this list of conditions and the following disclaimer;
212623SN/A * redistributions in binary form must reproduce the above copyright
222623SN/A * notice, this list of conditions and the following disclaimer in the
232623SN/A * documentation and/or other materials provided with the distribution;
242623SN/A * neither the name of the copyright holders nor the names of its
252623SN/A * contributors may be used to endorse or promote products derived from
262623SN/A * this software without specific prior written permission.
272623SN/A *
282623SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292623SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302623SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312623SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322623SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332623SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342623SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352623SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362623SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372623SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382623SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665Ssaidi@eecs.umich.edu *
402665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
412623SN/A */
422623SN/A
433170Sstever@eecs.umich.edu#include "arch/locked_mem.hh"
448105Sgblack@eecs.umich.edu#include "arch/mmapped_ipr.hh"
452623SN/A#include "arch/utility.hh"
464040Ssaidi@eecs.umich.edu#include "base/bigint.hh"
476658Snate@binkert.org#include "config/the_isa.hh"
488229Snate@binkert.org#include "cpu/simple/atomic.hh"
492623SN/A#include "cpu/exetrace.hh"
508232Snate@binkert.org#include "debug/ExecFaulting.hh"
518232Snate@binkert.org#include "debug/SimpleCPU.hh"
523348Sbinkertn@umich.edu#include "mem/packet.hh"
533348Sbinkertn@umich.edu#include "mem/packet_access.hh"
548926Sandreas.hansson@arm.com#include "mem/physical.hh"
554762Snate@binkert.org#include "params/AtomicSimpleCPU.hh"
567678Sgblack@eecs.umich.edu#include "sim/faults.hh"
572901Ssaidi@eecs.umich.edu#include "sim/system.hh"
588779Sgblack@eecs.umich.edu#include "sim/full_system.hh"
592623SN/A
602623SN/Ausing namespace std;
612623SN/Ausing namespace TheISA;
622623SN/A
632623SN/AAtomicSimpleCPU::TickEvent::TickEvent(AtomicSimpleCPU *c)
645606Snate@binkert.org    : Event(CPU_Tick_Pri), cpu(c)
652623SN/A{
662623SN/A}
672623SN/A
682623SN/A
692623SN/Avoid
702623SN/AAtomicSimpleCPU::TickEvent::process()
712623SN/A{
722623SN/A    cpu->tick();
732623SN/A}
742623SN/A
752623SN/Aconst char *
765336Shines@cs.fsu.eduAtomicSimpleCPU::TickEvent::description() const
772623SN/A{
784873Sstever@eecs.umich.edu    return "AtomicSimpleCPU tick";
792623SN/A}
802623SN/A
812623SN/Avoid
822623SN/AAtomicSimpleCPU::init()
832623SN/A{
842623SN/A    BaseCPU::init();
858921Sandreas.hansson@arm.com
869424SAndreas.Sandberg@ARM.com    if (!params()->defer_registration &&
879424SAndreas.Sandberg@ARM.com        system->getMemoryMode() != Enums::atomic) {
889424SAndreas.Sandberg@ARM.com        fatal("The atomic CPU requires the memory system to be in "
899424SAndreas.Sandberg@ARM.com              "'atomic' mode.\n");
909424SAndreas.Sandberg@ARM.com    }
919424SAndreas.Sandberg@ARM.com
928921Sandreas.hansson@arm.com    // Initialise the ThreadContext's memory proxies
938921Sandreas.hansson@arm.com    tcBase()->initMemProxies(tcBase());
948921Sandreas.hansson@arm.com
959058Satgutier@umich.edu    if (FullSystem && !params()->defer_registration) {
968779Sgblack@eecs.umich.edu        ThreadID size = threadContexts.size();
978779Sgblack@eecs.umich.edu        for (ThreadID i = 0; i < size; ++i) {
988779Sgblack@eecs.umich.edu            ThreadContext *tc = threadContexts[i];
998779Sgblack@eecs.umich.edu            // initialize CPU, including PC
1008779Sgblack@eecs.umich.edu            TheISA::initCPU(tc, tc->contextId());
1018779Sgblack@eecs.umich.edu        }
1022623SN/A    }
1038706Sandreas.hansson@arm.com
1045714Shsul@eecs.umich.edu    // Atomic doesn't do MT right now, so contextId == threadId
1055712Shsul@eecs.umich.edu    ifetch_req.setThreadContext(_cpuId, 0); // Add thread ID if we add MT
1065712Shsul@eecs.umich.edu    data_read_req.setThreadContext(_cpuId, 0); // Add thread ID here too
1075712Shsul@eecs.umich.edu    data_write_req.setThreadContext(_cpuId, 0); // Add thread ID here too
1082623SN/A}
1092623SN/A
1105529Snate@binkert.orgAtomicSimpleCPU::AtomicSimpleCPU(AtomicSimpleCPUParams *p)
1116078Sgblack@eecs.umich.edu    : BaseSimpleCPU(p), tickEvent(this), width(p->width), locked(false),
1125487Snate@binkert.org      simulate_data_stalls(p->simulate_data_stalls),
1135487Snate@binkert.org      simulate_inst_stalls(p->simulate_inst_stalls),
1149095Sandreas.hansson@arm.com      icachePort(name() + ".icache_port", this),
1159095Sandreas.hansson@arm.com      dcachePort(name() + ".dcache_port", this),
1168926Sandreas.hansson@arm.com      fastmem(p->fastmem)
1172623SN/A{
1182623SN/A    _status = Idle;
1192623SN/A}
1202623SN/A
1212623SN/A
1222623SN/AAtomicSimpleCPU::~AtomicSimpleCPU()
1232623SN/A{
1246775SBrad.Beckmann@amd.com    if (tickEvent.scheduled()) {
1256775SBrad.Beckmann@amd.com        deschedule(tickEvent);
1266775SBrad.Beckmann@amd.com    }
1272623SN/A}
1282623SN/A
1292623SN/Avoid
1302623SN/AAtomicSimpleCPU::serialize(ostream &os)
1312623SN/A{
1329342SAndreas.Sandberg@arm.com    Drainable::State so_state(getDrainState());
1332915Sktlim@umich.edu    SERIALIZE_ENUM(so_state);
1346078Sgblack@eecs.umich.edu    SERIALIZE_SCALAR(locked);
1353145Shsul@eecs.umich.edu    BaseSimpleCPU::serialize(os);
1362623SN/A    nameOut(os, csprintf("%s.tickEvent", name()));
1372623SN/A    tickEvent.serialize(os);
1382623SN/A}
1392623SN/A
1402623SN/Avoid
1412623SN/AAtomicSimpleCPU::unserialize(Checkpoint *cp, const string &section)
1422623SN/A{
1439342SAndreas.Sandberg@arm.com    Drainable::State so_state;
1442915Sktlim@umich.edu    UNSERIALIZE_ENUM(so_state);
1456078Sgblack@eecs.umich.edu    UNSERIALIZE_SCALAR(locked);
1463145Shsul@eecs.umich.edu    BaseSimpleCPU::unserialize(cp, section);
1472915Sktlim@umich.edu    tickEvent.unserialize(cp, csprintf("%s.tickEvent", section));
1482915Sktlim@umich.edu}
1492915Sktlim@umich.edu
1509342SAndreas.Sandberg@arm.comunsigned int
1519342SAndreas.Sandberg@arm.comAtomicSimpleCPU::drain(DrainManager *drain_manager)
1529342SAndreas.Sandberg@arm.com{
1539342SAndreas.Sandberg@arm.com    setDrainState(Drainable::Drained);
1549342SAndreas.Sandberg@arm.com    return 0;
1559342SAndreas.Sandberg@arm.com}
1569342SAndreas.Sandberg@arm.com
1572915Sktlim@umich.eduvoid
1589342SAndreas.Sandberg@arm.comAtomicSimpleCPU::drainResume()
1592915Sktlim@umich.edu{
1605220Ssaidi@eecs.umich.edu    if (_status == Idle || _status == SwitchedOut)
1615220Ssaidi@eecs.umich.edu        return;
1625220Ssaidi@eecs.umich.edu
1634940Snate@binkert.org    DPRINTF(SimpleCPU, "Resume\n");
1649424SAndreas.Sandberg@ARM.com    if (system->getMemoryMode() != Enums::atomic) {
1659424SAndreas.Sandberg@ARM.com        fatal("The atomic CPU requires the memory system to be in "
1669424SAndreas.Sandberg@ARM.com              "'atomic' mode.\n");
1679424SAndreas.Sandberg@ARM.com    }
1683324Shsul@eecs.umich.edu
1699342SAndreas.Sandberg@arm.com    setDrainState(Drainable::Running);
1705220Ssaidi@eecs.umich.edu    if (thread->status() == ThreadContext::Active) {
1715606Snate@binkert.org        if (!tickEvent.scheduled())
1725606Snate@binkert.org            schedule(tickEvent, nextCycle());
1732915Sktlim@umich.edu    }
1747897Shestness@cs.utexas.edu    system->totalNumInsts = 0;
1752623SN/A}
1762623SN/A
1772623SN/Avoid
1782798Sktlim@umich.eduAtomicSimpleCPU::switchOut()
1792623SN/A{
1809342SAndreas.Sandberg@arm.com    assert(_status == BaseSimpleCPU::Running || _status == Idle);
1812798Sktlim@umich.edu    _status = SwitchedOut;
1822623SN/A
1832798Sktlim@umich.edu    tickEvent.squash();
1842623SN/A}
1852623SN/A
1862623SN/A
1872623SN/Avoid
1882623SN/AAtomicSimpleCPU::takeOverFrom(BaseCPU *oldCPU)
1892623SN/A{
1908737Skoansin.tan@gmail.com    BaseCPU::takeOverFrom(oldCPU);
1912623SN/A
1922623SN/A    assert(!tickEvent.scheduled());
1932623SN/A
1942680Sktlim@umich.edu    // if any of this CPU's ThreadContexts are active, mark the CPU as
1952623SN/A    // running and schedule its tick event.
1966221Snate@binkert.org    ThreadID size = threadContexts.size();
1976221Snate@binkert.org    for (ThreadID i = 0; i < size; ++i) {
1982680Sktlim@umich.edu        ThreadContext *tc = threadContexts[i];
1999342SAndreas.Sandberg@arm.com        if (tc->status() == ThreadContext::Active &&
2009342SAndreas.Sandberg@arm.com            _status != BaseSimpleCPU::Running) {
2019342SAndreas.Sandberg@arm.com            _status = BaseSimpleCPU::Running;
2025606Snate@binkert.org            schedule(tickEvent, nextCycle());
2032623SN/A            break;
2042623SN/A        }
2052623SN/A    }
2069342SAndreas.Sandberg@arm.com    if (_status != BaseSimpleCPU::Running) {
2073512Sktlim@umich.edu        _status = Idle;
2083512Sktlim@umich.edu    }
2095169Ssaidi@eecs.umich.edu    assert(threadContexts.size() == 1);
2105712Shsul@eecs.umich.edu    ifetch_req.setThreadContext(_cpuId, 0); // Add thread ID if we add MT
2115712Shsul@eecs.umich.edu    data_read_req.setThreadContext(_cpuId, 0); // Add thread ID here too
2125712Shsul@eecs.umich.edu    data_write_req.setThreadContext(_cpuId, 0); // Add thread ID here too
2132623SN/A}
2142623SN/A
2152623SN/A
2162623SN/Avoid
2179180Sandreas.hansson@arm.comAtomicSimpleCPU::activateContext(ThreadID thread_num, Cycles delay)
2182623SN/A{
2194940Snate@binkert.org    DPRINTF(SimpleCPU, "ActivateContext %d (%d cycles)\n", thread_num, delay);
2204940Snate@binkert.org
2212623SN/A    assert(thread_num == 0);
2222683Sktlim@umich.edu    assert(thread);
2232623SN/A
2242623SN/A    assert(_status == Idle);
2252623SN/A    assert(!tickEvent.scheduled());
2262623SN/A
2272623SN/A    notIdleFraction++;
2289180Sandreas.hansson@arm.com    numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend);
2293686Sktlim@umich.edu
2303430Sgblack@eecs.umich.edu    //Make sure ticks are still on multiples of cycles
2319179Sandreas.hansson@arm.com    schedule(tickEvent, clockEdge(delay));
2329342SAndreas.Sandberg@arm.com    _status = BaseSimpleCPU::Running;
2332623SN/A}
2342623SN/A
2352623SN/A
2362623SN/Avoid
2378737Skoansin.tan@gmail.comAtomicSimpleCPU::suspendContext(ThreadID thread_num)
2382623SN/A{
2394940Snate@binkert.org    DPRINTF(SimpleCPU, "SuspendContext %d\n", thread_num);
2404940Snate@binkert.org
2412623SN/A    assert(thread_num == 0);
2422683Sktlim@umich.edu    assert(thread);
2432623SN/A
2446043Sgblack@eecs.umich.edu    if (_status == Idle)
2456043Sgblack@eecs.umich.edu        return;
2466043Sgblack@eecs.umich.edu
2479342SAndreas.Sandberg@arm.com    assert(_status == BaseSimpleCPU::Running);
2482626SN/A
2492626SN/A    // tick event may not be scheduled if this gets called from inside
2502626SN/A    // an instruction's execution, e.g. "quiesce"
2512626SN/A    if (tickEvent.scheduled())
2525606Snate@binkert.org        deschedule(tickEvent);
2532623SN/A
2542623SN/A    notIdleFraction--;
2552623SN/A    _status = Idle;
2562623SN/A}
2572623SN/A
2582623SN/A
2592623SN/AFault
2608444Sgblack@eecs.umich.eduAtomicSimpleCPU::readMem(Addr addr, uint8_t * data,
2618444Sgblack@eecs.umich.edu                         unsigned size, unsigned flags)
2622623SN/A{
2633169Sstever@eecs.umich.edu    // use the CPU's statically allocated read request and packet objects
2644870Sstever@eecs.umich.edu    Request *req = &data_read_req;
2652623SN/A
2662623SN/A    if (traceData) {
2672623SN/A        traceData->setAddr(addr);
2682623SN/A    }
2692623SN/A
2704999Sgblack@eecs.umich.edu    //The block size of our peer.
2716227Snate@binkert.org    unsigned blockSize = dcachePort.peerBlockSize();
2724999Sgblack@eecs.umich.edu    //The size of the data we're trying to read.
2737520Sgblack@eecs.umich.edu    int fullSize = size;
2742623SN/A
2754999Sgblack@eecs.umich.edu    //The address of the second part of this access if it needs to be split
2764999Sgblack@eecs.umich.edu    //across a cache line boundary.
2777520Sgblack@eecs.umich.edu    Addr secondAddr = roundDown(addr + size - 1, blockSize);
2784999Sgblack@eecs.umich.edu
2797520Sgblack@eecs.umich.edu    if (secondAddr > addr)
2807520Sgblack@eecs.umich.edu        size = secondAddr - addr;
2814999Sgblack@eecs.umich.edu
2824999Sgblack@eecs.umich.edu    dcache_latency = 0;
2834999Sgblack@eecs.umich.edu
2847520Sgblack@eecs.umich.edu    while (1) {
2858832SAli.Saidi@ARM.com        req->setVirt(0, addr, size, flags, dataMasterId(), thread->pcState().instAddr());
2864999Sgblack@eecs.umich.edu
2874999Sgblack@eecs.umich.edu        // translate to physical address
2886023Snate@binkert.org        Fault fault = thread->dtb->translateAtomic(req, tc, BaseTLB::Read);
2894999Sgblack@eecs.umich.edu
2904999Sgblack@eecs.umich.edu        // Now do the access.
2916623Sgblack@eecs.umich.edu        if (fault == NoFault && !req->getFlags().isSet(Request::NO_ACCESS)) {
2924999Sgblack@eecs.umich.edu            Packet pkt = Packet(req,
2938949Sandreas.hansson@arm.com                                req->isLLSC() ? MemCmd::LoadLockedReq :
2948949Sandreas.hansson@arm.com                                MemCmd::ReadReq);
2957520Sgblack@eecs.umich.edu            pkt.dataStatic(data);
2964999Sgblack@eecs.umich.edu
2978105Sgblack@eecs.umich.edu            if (req->isMmappedIpr())
2984999Sgblack@eecs.umich.edu                dcache_latency += TheISA::handleIprRead(thread->getTC(), &pkt);
2994999Sgblack@eecs.umich.edu            else {
3008931Sandreas.hansson@arm.com                if (fastmem && system->isMemAddr(pkt.getAddr()))
3018931Sandreas.hansson@arm.com                    system->getPhysMem().access(&pkt);
3024999Sgblack@eecs.umich.edu                else
3034999Sgblack@eecs.umich.edu                    dcache_latency += dcachePort.sendAtomic(&pkt);
3044999Sgblack@eecs.umich.edu            }
3054999Sgblack@eecs.umich.edu            dcache_access = true;
3065012Sgblack@eecs.umich.edu
3074999Sgblack@eecs.umich.edu            assert(!pkt.isError());
3084999Sgblack@eecs.umich.edu
3096102Sgblack@eecs.umich.edu            if (req->isLLSC()) {
3104999Sgblack@eecs.umich.edu                TheISA::handleLockedRead(thread, req);
3114999Sgblack@eecs.umich.edu            }
3124968Sacolyte@umich.edu        }
3134986Ssaidi@eecs.umich.edu
3144999Sgblack@eecs.umich.edu        //If there's a fault, return it
3156739Sgblack@eecs.umich.edu        if (fault != NoFault) {
3166739Sgblack@eecs.umich.edu            if (req->isPrefetch()) {
3176739Sgblack@eecs.umich.edu                return NoFault;
3186739Sgblack@eecs.umich.edu            } else {
3196739Sgblack@eecs.umich.edu                return fault;
3206739Sgblack@eecs.umich.edu            }
3216739Sgblack@eecs.umich.edu        }
3226739Sgblack@eecs.umich.edu
3234999Sgblack@eecs.umich.edu        //If we don't need to access a second cache line, stop now.
3244999Sgblack@eecs.umich.edu        if (secondAddr <= addr)
3254999Sgblack@eecs.umich.edu        {
3266078Sgblack@eecs.umich.edu            if (req->isLocked() && fault == NoFault) {
3276078Sgblack@eecs.umich.edu                assert(!locked);
3286078Sgblack@eecs.umich.edu                locked = true;
3296078Sgblack@eecs.umich.edu            }
3304999Sgblack@eecs.umich.edu            return fault;
3314968Sacolyte@umich.edu        }
3323170Sstever@eecs.umich.edu
3334999Sgblack@eecs.umich.edu        /*
3344999Sgblack@eecs.umich.edu         * Set up for accessing the second cache line.
3354999Sgblack@eecs.umich.edu         */
3364999Sgblack@eecs.umich.edu
3374999Sgblack@eecs.umich.edu        //Move the pointer we're reading into to the correct location.
3387520Sgblack@eecs.umich.edu        data += size;
3394999Sgblack@eecs.umich.edu        //Adjust the size to get the remaining bytes.
3407520Sgblack@eecs.umich.edu        size = addr + fullSize - secondAddr;
3414999Sgblack@eecs.umich.edu        //And access the right address.
3424999Sgblack@eecs.umich.edu        addr = secondAddr;
3432623SN/A    }
3442623SN/A}
3452623SN/A
3467520Sgblack@eecs.umich.edu
3472623SN/AFault
3488444Sgblack@eecs.umich.eduAtomicSimpleCPU::writeMem(uint8_t *data, unsigned size,
3498444Sgblack@eecs.umich.edu                          Addr addr, unsigned flags, uint64_t *res)
3502623SN/A{
3513169Sstever@eecs.umich.edu    // use the CPU's statically allocated write request and packet objects
3524870Sstever@eecs.umich.edu    Request *req = &data_write_req;
3532623SN/A
3542623SN/A    if (traceData) {
3552623SN/A        traceData->setAddr(addr);
3562623SN/A    }
3572623SN/A
3584999Sgblack@eecs.umich.edu    //The block size of our peer.
3596227Snate@binkert.org    unsigned blockSize = dcachePort.peerBlockSize();
3604999Sgblack@eecs.umich.edu    //The size of the data we're trying to read.
3617520Sgblack@eecs.umich.edu    int fullSize = size;
3622623SN/A
3634999Sgblack@eecs.umich.edu    //The address of the second part of this access if it needs to be split
3644999Sgblack@eecs.umich.edu    //across a cache line boundary.
3657520Sgblack@eecs.umich.edu    Addr secondAddr = roundDown(addr + size - 1, blockSize);
3664999Sgblack@eecs.umich.edu
3674999Sgblack@eecs.umich.edu    if(secondAddr > addr)
3687520Sgblack@eecs.umich.edu        size = secondAddr - addr;
3694999Sgblack@eecs.umich.edu
3704999Sgblack@eecs.umich.edu    dcache_latency = 0;
3714999Sgblack@eecs.umich.edu
3724999Sgblack@eecs.umich.edu    while(1) {
3738832SAli.Saidi@ARM.com        req->setVirt(0, addr, size, flags, dataMasterId(), thread->pcState().instAddr());
3744999Sgblack@eecs.umich.edu
3754999Sgblack@eecs.umich.edu        // translate to physical address
3766023Snate@binkert.org        Fault fault = thread->dtb->translateAtomic(req, tc, BaseTLB::Write);
3774999Sgblack@eecs.umich.edu
3784999Sgblack@eecs.umich.edu        // Now do the access.
3794999Sgblack@eecs.umich.edu        if (fault == NoFault) {
3804999Sgblack@eecs.umich.edu            MemCmd cmd = MemCmd::WriteReq; // default
3814999Sgblack@eecs.umich.edu            bool do_access = true;  // flag to suppress cache access
3824999Sgblack@eecs.umich.edu
3836102Sgblack@eecs.umich.edu            if (req->isLLSC()) {
3844999Sgblack@eecs.umich.edu                cmd = MemCmd::StoreCondReq;
3854999Sgblack@eecs.umich.edu                do_access = TheISA::handleLockedWrite(thread, req);
3864999Sgblack@eecs.umich.edu            } else if (req->isSwap()) {
3874999Sgblack@eecs.umich.edu                cmd = MemCmd::SwapReq;
3884999Sgblack@eecs.umich.edu                if (req->isCondSwap()) {
3894999Sgblack@eecs.umich.edu                    assert(res);
3904999Sgblack@eecs.umich.edu                    req->setExtraData(*res);
3914999Sgblack@eecs.umich.edu                }
3924999Sgblack@eecs.umich.edu            }
3934999Sgblack@eecs.umich.edu
3946623Sgblack@eecs.umich.edu            if (do_access && !req->getFlags().isSet(Request::NO_ACCESS)) {
3958949Sandreas.hansson@arm.com                Packet pkt = Packet(req, cmd);
3967520Sgblack@eecs.umich.edu                pkt.dataStatic(data);
3974999Sgblack@eecs.umich.edu
3988105Sgblack@eecs.umich.edu                if (req->isMmappedIpr()) {
3994999Sgblack@eecs.umich.edu                    dcache_latency +=
4004999Sgblack@eecs.umich.edu                        TheISA::handleIprWrite(thread->getTC(), &pkt);
4014999Sgblack@eecs.umich.edu                } else {
4028931Sandreas.hansson@arm.com                    if (fastmem && system->isMemAddr(pkt.getAddr()))
4038931Sandreas.hansson@arm.com                        system->getPhysMem().access(&pkt);
4044999Sgblack@eecs.umich.edu                    else
4054999Sgblack@eecs.umich.edu                        dcache_latency += dcachePort.sendAtomic(&pkt);
4064999Sgblack@eecs.umich.edu                }
4074999Sgblack@eecs.umich.edu                dcache_access = true;
4084999Sgblack@eecs.umich.edu                assert(!pkt.isError());
4094999Sgblack@eecs.umich.edu
4104999Sgblack@eecs.umich.edu                if (req->isSwap()) {
4114999Sgblack@eecs.umich.edu                    assert(res);
4127520Sgblack@eecs.umich.edu                    memcpy(res, pkt.getPtr<uint8_t>(), fullSize);
4134999Sgblack@eecs.umich.edu                }
4144999Sgblack@eecs.umich.edu            }
4154999Sgblack@eecs.umich.edu
4164999Sgblack@eecs.umich.edu            if (res && !req->isSwap()) {
4174999Sgblack@eecs.umich.edu                *res = req->getExtraData();
4184878Sstever@eecs.umich.edu            }
4194040Ssaidi@eecs.umich.edu        }
4204040Ssaidi@eecs.umich.edu
4214999Sgblack@eecs.umich.edu        //If there's a fault or we don't need to access a second cache line,
4224999Sgblack@eecs.umich.edu        //stop now.
4234999Sgblack@eecs.umich.edu        if (fault != NoFault || secondAddr <= addr)
4244999Sgblack@eecs.umich.edu        {
4256078Sgblack@eecs.umich.edu            if (req->isLocked() && fault == NoFault) {
4266078Sgblack@eecs.umich.edu                assert(locked);
4276078Sgblack@eecs.umich.edu                locked = false;
4286078Sgblack@eecs.umich.edu            }
4296739Sgblack@eecs.umich.edu            if (fault != NoFault && req->isPrefetch()) {
4306739Sgblack@eecs.umich.edu                return NoFault;
4316739Sgblack@eecs.umich.edu            } else {
4326739Sgblack@eecs.umich.edu                return fault;
4336739Sgblack@eecs.umich.edu            }
4343170Sstever@eecs.umich.edu        }
4353170Sstever@eecs.umich.edu
4364999Sgblack@eecs.umich.edu        /*
4374999Sgblack@eecs.umich.edu         * Set up for accessing the second cache line.
4384999Sgblack@eecs.umich.edu         */
4394999Sgblack@eecs.umich.edu
4404999Sgblack@eecs.umich.edu        //Move the pointer we're reading into to the correct location.
4417520Sgblack@eecs.umich.edu        data += size;
4424999Sgblack@eecs.umich.edu        //Adjust the size to get the remaining bytes.
4437520Sgblack@eecs.umich.edu        size = addr + fullSize - secondAddr;
4444999Sgblack@eecs.umich.edu        //And access the right address.
4454999Sgblack@eecs.umich.edu        addr = secondAddr;
4462623SN/A    }
4472623SN/A}
4482623SN/A
4492623SN/A
4502623SN/Avoid
4512623SN/AAtomicSimpleCPU::tick()
4522623SN/A{
4534940Snate@binkert.org    DPRINTF(SimpleCPU, "Tick\n");
4544940Snate@binkert.org
4555487Snate@binkert.org    Tick latency = 0;
4562623SN/A
4576078Sgblack@eecs.umich.edu    for (int i = 0; i < width || locked; ++i) {
4582623SN/A        numCycles++;
4592623SN/A
4603387Sgblack@eecs.umich.edu        if (!curStaticInst || !curStaticInst->isDelayedCommit())
4613387Sgblack@eecs.umich.edu            checkForInterrupts();
4622626SN/A
4635348Ssaidi@eecs.umich.edu        checkPcEventQueue();
4648143SAli.Saidi@ARM.com        // We must have just got suspended by a PC event
4658143SAli.Saidi@ARM.com        if (_status == Idle)
4668143SAli.Saidi@ARM.com            return;
4675348Ssaidi@eecs.umich.edu
4685669Sgblack@eecs.umich.edu        Fault fault = NoFault;
4695669Sgblack@eecs.umich.edu
4707720Sgblack@eecs.umich.edu        TheISA::PCState pcState = thread->pcState();
4717720Sgblack@eecs.umich.edu
4727720Sgblack@eecs.umich.edu        bool needToFetch = !isRomMicroPC(pcState.microPC()) &&
4737720Sgblack@eecs.umich.edu                           !curMacroStaticInst;
4747720Sgblack@eecs.umich.edu        if (needToFetch) {
4755894Sgblack@eecs.umich.edu            setupFetchRequest(&ifetch_req);
4766023Snate@binkert.org            fault = thread->itb->translateAtomic(&ifetch_req, tc,
4776023Snate@binkert.org                                                 BaseTLB::Execute);
4785894Sgblack@eecs.umich.edu        }
4792623SN/A
4802623SN/A        if (fault == NoFault) {
4814182Sgblack@eecs.umich.edu            Tick icache_latency = 0;
4824182Sgblack@eecs.umich.edu            bool icache_access = false;
4834182Sgblack@eecs.umich.edu            dcache_access = false; // assume no dcache access
4842662Sstever@eecs.umich.edu
4857720Sgblack@eecs.umich.edu            if (needToFetch) {
4869023Sgblack@eecs.umich.edu                // This is commented out because the decoder would act like
4875694Sgblack@eecs.umich.edu                // a tiny cache otherwise. It wouldn't be flushed when needed
4885694Sgblack@eecs.umich.edu                // like the I cache. It should be flushed, and when that works
4895694Sgblack@eecs.umich.edu                // this code should be uncommented.
4905669Sgblack@eecs.umich.edu                //Fetch more instruction memory if necessary
4919023Sgblack@eecs.umich.edu                //if(decoder.needMoreBytes())
4925669Sgblack@eecs.umich.edu                //{
4935669Sgblack@eecs.umich.edu                    icache_access = true;
4948949Sandreas.hansson@arm.com                    Packet ifetch_pkt = Packet(&ifetch_req, MemCmd::ReadReq);
4955669Sgblack@eecs.umich.edu                    ifetch_pkt.dataStatic(&inst);
4962623SN/A
4978931Sandreas.hansson@arm.com                    if (fastmem && system->isMemAddr(ifetch_pkt.getAddr()))
4988931Sandreas.hansson@arm.com                        system->getPhysMem().access(&ifetch_pkt);
4995669Sgblack@eecs.umich.edu                    else
5005669Sgblack@eecs.umich.edu                        icache_latency = icachePort.sendAtomic(&ifetch_pkt);
5014968Sacolyte@umich.edu
5025669Sgblack@eecs.umich.edu                    assert(!ifetch_pkt.isError());
5034968Sacolyte@umich.edu
5045669Sgblack@eecs.umich.edu                    // ifetch_req is initialized to read the instruction directly
5055669Sgblack@eecs.umich.edu                    // into the CPU object's inst field.
5065669Sgblack@eecs.umich.edu                //}
5075669Sgblack@eecs.umich.edu            }
5084182Sgblack@eecs.umich.edu
5092623SN/A            preExecute();
5103814Ssaidi@eecs.umich.edu
5115001Sgblack@eecs.umich.edu            if (curStaticInst) {
5124182Sgblack@eecs.umich.edu                fault = curStaticInst->execute(this, traceData);
5134998Sgblack@eecs.umich.edu
5144998Sgblack@eecs.umich.edu                // keep an instruction count
5154998Sgblack@eecs.umich.edu                if (fault == NoFault)
5164998Sgblack@eecs.umich.edu                    countInst();
5177655Sali.saidi@arm.com                else if (traceData && !DTRACE(ExecFaulting)) {
5185001Sgblack@eecs.umich.edu                    delete traceData;
5195001Sgblack@eecs.umich.edu                    traceData = NULL;
5205001Sgblack@eecs.umich.edu                }
5214998Sgblack@eecs.umich.edu
5224182Sgblack@eecs.umich.edu                postExecute();
5234182Sgblack@eecs.umich.edu            }
5242623SN/A
5253814Ssaidi@eecs.umich.edu            // @todo remove me after debugging with legion done
5264539Sgblack@eecs.umich.edu            if (curStaticInst && (!curStaticInst->isMicroop() ||
5274539Sgblack@eecs.umich.edu                        curStaticInst->isFirstMicroop()))
5283814Ssaidi@eecs.umich.edu                instCnt++;
5293814Ssaidi@eecs.umich.edu
5305487Snate@binkert.org            Tick stall_ticks = 0;
5315487Snate@binkert.org            if (simulate_inst_stalls && icache_access)
5325487Snate@binkert.org                stall_ticks += icache_latency;
5335487Snate@binkert.org
5345487Snate@binkert.org            if (simulate_data_stalls && dcache_access)
5355487Snate@binkert.org                stall_ticks += dcache_latency;
5365487Snate@binkert.org
5375487Snate@binkert.org            if (stall_ticks) {
5389180Sandreas.hansson@arm.com                // the atomic cpu does its accounting in ticks, so
5399180Sandreas.hansson@arm.com                // keep counting in ticks but round to the clock
5409180Sandreas.hansson@arm.com                // period
5419180Sandreas.hansson@arm.com                latency += divCeil(stall_ticks, clockPeriod()) *
5429180Sandreas.hansson@arm.com                    clockPeriod();
5432623SN/A            }
5442623SN/A
5452623SN/A        }
5464377Sgblack@eecs.umich.edu        if(fault != NoFault || !stayAtPC)
5474182Sgblack@eecs.umich.edu            advancePC(fault);
5482623SN/A    }
5492623SN/A
5505487Snate@binkert.org    // instruction takes at least one cycle
5519179Sandreas.hansson@arm.com    if (latency < clockPeriod())
5529179Sandreas.hansson@arm.com        latency = clockPeriod();
5535487Snate@binkert.org
5542626SN/A    if (_status != Idle)
5557823Ssteve.reinhardt@amd.com        schedule(tickEvent, curTick() + latency);
5562623SN/A}
5572623SN/A
5582623SN/A
5595315Sstever@gmail.comvoid
5605315Sstever@gmail.comAtomicSimpleCPU::printAddr(Addr a)
5615315Sstever@gmail.com{
5625315Sstever@gmail.com    dcachePort.printAddr(a);
5635315Sstever@gmail.com}
5645315Sstever@gmail.com
5655315Sstever@gmail.com
5662623SN/A////////////////////////////////////////////////////////////////////////
5672623SN/A//
5682623SN/A//  AtomicSimpleCPU Simulation Object
5692623SN/A//
5704762Snate@binkert.orgAtomicSimpleCPU *
5714762Snate@binkert.orgAtomicSimpleCPUParams::create()
5722623SN/A{
5735529Snate@binkert.org    numThreads = 1;
5748779Sgblack@eecs.umich.edu    if (!FullSystem && workload.size() != 1)
5754762Snate@binkert.org        panic("only one workload allowed");
5765529Snate@binkert.org    return new AtomicSimpleCPU(this);
5772623SN/A}
578