Sequencer.cc revision 7560
16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
36145Snate@binkert.org * All rights reserved.
46145Snate@binkert.org *
56145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66145Snate@binkert.org * modification, are permitted provided that the following conditions are
76145Snate@binkert.org * met: redistributions of source code must retain the above copyright
86145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116145Snate@binkert.org * documentation and/or other materials provided with the distribution;
126145Snate@binkert.org * neither the name of the copyright holders nor the names of its
136145Snate@binkert.org * contributors may be used to endorse or promote products derived from
146145Snate@binkert.org * this software without specific prior written permission.
156145Snate@binkert.org *
166145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276145Snate@binkert.org */
286145Snate@binkert.org
297056Snate@binkert.org#include "base/str.hh"
307039Snate@binkert.org#include "cpu/rubytest/RubyTester.hh"
317039Snate@binkert.org#include "mem/protocol/CacheMsg.hh"
327039Snate@binkert.org#include "mem/protocol/Protocol.hh"
337039Snate@binkert.org#include "mem/protocol/Protocol.hh"
347039Snate@binkert.org#include "mem/ruby/buffers/MessageBuffer.hh"
357039Snate@binkert.org#include "mem/ruby/common/Global.hh"
367039Snate@binkert.org#include "mem/ruby/common/SubBlock.hh"
376845Sdrh5@cs.wisc.edu#include "mem/ruby/libruby.hh"
387039Snate@binkert.org#include "mem/ruby/profiler/Profiler.hh"
397039Snate@binkert.org#include "mem/ruby/recorder/Tracer.hh"
407039Snate@binkert.org#include "mem/ruby/slicc_interface/AbstractController.hh"
417039Snate@binkert.org#include "mem/ruby/system/CacheMemory.hh"
426154Snate@binkert.org#include "mem/ruby/system/Sequencer.hh"
436154Snate@binkert.org#include "mem/ruby/system/System.hh"
447550SBrad.Beckmann@amd.com#include "mem/packet.hh"
456876Ssteve.reinhardt@amd.com#include "params/RubySequencer.hh"
466876Ssteve.reinhardt@amd.com
477055Snate@binkert.orgusing namespace std;
487055Snate@binkert.org
496876Ssteve.reinhardt@amd.comSequencer *
506876Ssteve.reinhardt@amd.comRubySequencerParams::create()
516285Snate@binkert.org{
526876Ssteve.reinhardt@amd.com    return new Sequencer(this);
536285Snate@binkert.org}
547039Snate@binkert.org
556876Ssteve.reinhardt@amd.comSequencer::Sequencer(const Params *p)
566886SBrad.Beckmann@amd.com    : RubyPort(p), deadlockCheckEvent(this)
576876Ssteve.reinhardt@amd.com{
586876Ssteve.reinhardt@amd.com    m_store_waiting_on_load_cycles = 0;
596876Ssteve.reinhardt@amd.com    m_store_waiting_on_store_cycles = 0;
606876Ssteve.reinhardt@amd.com    m_load_waiting_on_store_cycles = 0;
616876Ssteve.reinhardt@amd.com    m_load_waiting_on_load_cycles = 0;
627039Snate@binkert.org
636876Ssteve.reinhardt@amd.com    m_outstanding_count = 0;
646285Snate@binkert.org
656876Ssteve.reinhardt@amd.com    m_max_outstanding_requests = 0;
666876Ssteve.reinhardt@amd.com    m_deadlock_threshold = 0;
676876Ssteve.reinhardt@amd.com    m_instCache_ptr = NULL;
686876Ssteve.reinhardt@amd.com    m_dataCache_ptr = NULL;
696145Snate@binkert.org
706876Ssteve.reinhardt@amd.com    m_instCache_ptr = p->icache;
716876Ssteve.reinhardt@amd.com    m_dataCache_ptr = p->dcache;
726876Ssteve.reinhardt@amd.com    m_max_outstanding_requests = p->max_outstanding_requests;
736876Ssteve.reinhardt@amd.com    m_deadlock_threshold = p->deadlock_threshold;
746899SBrad.Beckmann@amd.com    m_usingRubyTester = p->using_ruby_tester;
756899SBrad.Beckmann@amd.com
766876Ssteve.reinhardt@amd.com    assert(m_max_outstanding_requests > 0);
776876Ssteve.reinhardt@amd.com    assert(m_deadlock_threshold > 0);
786876Ssteve.reinhardt@amd.com    assert(m_instCache_ptr != NULL);
796876Ssteve.reinhardt@amd.com    assert(m_dataCache_ptr != NULL);
806145Snate@binkert.org}
816145Snate@binkert.org
827039Snate@binkert.orgSequencer::~Sequencer()
837039Snate@binkert.org{
846145Snate@binkert.org}
856145Snate@binkert.org
867039Snate@binkert.orgvoid
877039Snate@binkert.orgSequencer::wakeup()
887039Snate@binkert.org{
897039Snate@binkert.org    // Check for deadlock of any of the requests
907039Snate@binkert.org    Time current_time = g_eventQueue_ptr->getTime();
916145Snate@binkert.org
927039Snate@binkert.org    // Check across all outstanding requests
937039Snate@binkert.org    int total_outstanding = 0;
946285Snate@binkert.org
957455Snate@binkert.org    RequestTable::iterator read = m_readRequestTable.begin();
967455Snate@binkert.org    RequestTable::iterator read_end = m_readRequestTable.end();
977455Snate@binkert.org    for (; read != read_end; ++read) {
987455Snate@binkert.org        SequencerRequest* request = read->second;
997455Snate@binkert.org        if (current_time - request->issue_time < m_deadlock_threshold)
1007455Snate@binkert.org            continue;
1017455Snate@binkert.org
1027455Snate@binkert.org        WARN_MSG("Possible Deadlock detected");
1037455Snate@binkert.org        WARN_EXPR(m_version);
1047455Snate@binkert.org        WARN_EXPR(request->ruby_request.paddr);
1057455Snate@binkert.org        WARN_EXPR(m_readRequestTable.size());
1067455Snate@binkert.org        WARN_EXPR(current_time);
1077455Snate@binkert.org        WARN_EXPR(request->issue_time);
1087455Snate@binkert.org        WARN_EXPR(current_time - request->issue_time);
1097455Snate@binkert.org        ERROR_MSG("Aborting");
1106145Snate@binkert.org    }
1116145Snate@binkert.org
1127455Snate@binkert.org    RequestTable::iterator write = m_writeRequestTable.begin();
1137455Snate@binkert.org    RequestTable::iterator write_end = m_writeRequestTable.end();
1147455Snate@binkert.org    for (; write != write_end; ++write) {
1157455Snate@binkert.org        SequencerRequest* request = write->second;
1167455Snate@binkert.org        if (current_time - request->issue_time < m_deadlock_threshold)
1177455Snate@binkert.org            continue;
1187455Snate@binkert.org
1197455Snate@binkert.org        WARN_MSG("Possible Deadlock detected");
1207455Snate@binkert.org        WARN_EXPR(m_version);
1217537SBrad.Beckmann@amd.com        WARN_EXPR(request->ruby_request.paddr);
1227455Snate@binkert.org        WARN_EXPR(current_time);
1237455Snate@binkert.org        WARN_EXPR(request->issue_time);
1247455Snate@binkert.org        WARN_EXPR(current_time - request->issue_time);
1257455Snate@binkert.org        WARN_EXPR(m_writeRequestTable.size());
1267455Snate@binkert.org        ERROR_MSG("Aborting");
1276145Snate@binkert.org    }
1286285Snate@binkert.org
1297039Snate@binkert.org    total_outstanding += m_writeRequestTable.size();
1307039Snate@binkert.org    total_outstanding += m_readRequestTable.size();
1316145Snate@binkert.org
1327039Snate@binkert.org    assert(m_outstanding_count == total_outstanding);
1337039Snate@binkert.org
1347039Snate@binkert.org    if (m_outstanding_count > 0) {
1357039Snate@binkert.org        // If there are still outstanding requests, keep checking
1367039Snate@binkert.org        schedule(deadlockCheckEvent,
1377039Snate@binkert.org                 m_deadlock_threshold * g_eventQueue_ptr->getClock() +
1387039Snate@binkert.org                 curTick);
1397039Snate@binkert.org    }
1406145Snate@binkert.org}
1416145Snate@binkert.org
1427039Snate@binkert.orgvoid
1437039Snate@binkert.orgSequencer::printStats(ostream & out) const
1447039Snate@binkert.org{
1457039Snate@binkert.org    out << "Sequencer: " << m_name << endl
1467039Snate@binkert.org        << "  store_waiting_on_load_cycles: "
1477039Snate@binkert.org        << m_store_waiting_on_load_cycles << endl
1487039Snate@binkert.org        << "  store_waiting_on_store_cycles: "
1497039Snate@binkert.org        << m_store_waiting_on_store_cycles << endl
1507039Snate@binkert.org        << "  load_waiting_on_load_cycles: "
1517039Snate@binkert.org        << m_load_waiting_on_load_cycles << endl
1527039Snate@binkert.org        << "  load_waiting_on_store_cycles: "
1537039Snate@binkert.org        << m_load_waiting_on_store_cycles << endl;
1546859Sdrh5@cs.wisc.edu}
1556859Sdrh5@cs.wisc.edu
1567039Snate@binkert.orgvoid
1577039Snate@binkert.orgSequencer::printProgress(ostream& out) const
1587039Snate@binkert.org{
1597039Snate@binkert.org#if 0
1607039Snate@binkert.org    int total_demand = 0;
1617039Snate@binkert.org    out << "Sequencer Stats Version " << m_version << endl;
1627039Snate@binkert.org    out << "Current time = " << g_eventQueue_ptr->getTime() << endl;
1637039Snate@binkert.org    out << "---------------" << endl;
1647039Snate@binkert.org    out << "outstanding requests" << endl;
1656145Snate@binkert.org
1667455Snate@binkert.org    out << "proc " << m_Read
1677455Snate@binkert.org        << " version Requests = " << m_readRequestTable.size() << endl;
1686145Snate@binkert.org
1697039Snate@binkert.org    // print the request table
1707455Snate@binkert.org    RequestTable::iterator read = m_readRequestTable.begin();
1717455Snate@binkert.org    RequestTable::iterator read_end = m_readRequestTable.end();
1727455Snate@binkert.org    for (; read != read_end; ++read) {
1737455Snate@binkert.org        SequencerRequest* request = read->second;
1747039Snate@binkert.org        out << "\tRequest[ " << i << " ] = " << request->type
1757039Snate@binkert.org            << " Address " << rkeys[i]
1767039Snate@binkert.org            << " Posted " << request->issue_time
1777039Snate@binkert.org            << " PF " << PrefetchBit_No << endl;
1786145Snate@binkert.org        total_demand++;
1797039Snate@binkert.org    }
1806145Snate@binkert.org
1817455Snate@binkert.org    out << "proc " << m_version
1827455Snate@binkert.org        << " Write Requests = " << m_writeRequestTable.size << endl;
1836285Snate@binkert.org
1847039Snate@binkert.org    // print the request table
1857455Snate@binkert.org    RequestTable::iterator write = m_writeRequestTable.begin();
1867455Snate@binkert.org    RequestTable::iterator write_end = m_writeRequestTable.end();
1877455Snate@binkert.org    for (; write != write_end; ++write) {
1887455Snate@binkert.org        SequencerRequest* request = write->second;
1897039Snate@binkert.org        out << "\tRequest[ " << i << " ] = " << request.getType()
1907039Snate@binkert.org            << " Address " << wkeys[i]
1917039Snate@binkert.org            << " Posted " << request.getTime()
1927039Snate@binkert.org            << " PF " << request.getPrefetch() << endl;
1937039Snate@binkert.org        if (request.getPrefetch() == PrefetchBit_No) {
1947039Snate@binkert.org            total_demand++;
1957039Snate@binkert.org        }
1967039Snate@binkert.org    }
1977039Snate@binkert.org
1987039Snate@binkert.org    out << endl;
1997039Snate@binkert.org
2007039Snate@binkert.org    out << "Total Number Outstanding: " << m_outstanding_count << endl
2017039Snate@binkert.org        << "Total Number Demand     : " << total_demand << endl
2027039Snate@binkert.org        << "Total Number Prefetches : " << m_outstanding_count - total_demand
2037039Snate@binkert.org        << endl << endl << endl;
2047039Snate@binkert.org#endif
2056145Snate@binkert.org}
2066145Snate@binkert.org
2077039Snate@binkert.orgvoid
2087039Snate@binkert.orgSequencer::printConfig(ostream& out) const
2097039Snate@binkert.org{
2107039Snate@binkert.org    out << "Seqeuncer config: " << m_name << endl
2117039Snate@binkert.org        << "  controller: " << m_controller->getName() << endl
2127039Snate@binkert.org        << "  version: " << m_version << endl
2137039Snate@binkert.org        << "  max_outstanding_requests: " << m_max_outstanding_requests << endl
2147039Snate@binkert.org        << "  deadlock_threshold: " << m_deadlock_threshold << endl;
2156145Snate@binkert.org}
2166145Snate@binkert.org
2176145Snate@binkert.org// Insert the request on the correct request table.  Return true if
2186145Snate@binkert.org// the entry was already present.
2197039Snate@binkert.orgbool
2207039Snate@binkert.orgSequencer::insertRequest(SequencerRequest* request)
2217039Snate@binkert.org{
2227039Snate@binkert.org    int total_outstanding =
2237039Snate@binkert.org        m_writeRequestTable.size() + m_readRequestTable.size();
2246285Snate@binkert.org
2257039Snate@binkert.org    assert(m_outstanding_count == total_outstanding);
2266145Snate@binkert.org
2277039Snate@binkert.org    // See if we should schedule a deadlock check
2287039Snate@binkert.org    if (deadlockCheckEvent.scheduled() == false) {
2297039Snate@binkert.org        schedule(deadlockCheckEvent, m_deadlock_threshold + curTick);
2307039Snate@binkert.org    }
2316145Snate@binkert.org
2327039Snate@binkert.org    Address line_addr(request->ruby_request.paddr);
2337039Snate@binkert.org    line_addr.makeLineAddress();
2347039Snate@binkert.org    if ((request->ruby_request.type == RubyRequestType_ST) ||
2357039Snate@binkert.org        (request->ruby_request.type == RubyRequestType_RMW_Read) ||
2367039Snate@binkert.org        (request->ruby_request.type == RubyRequestType_RMW_Write) ||
2377039Snate@binkert.org        (request->ruby_request.type == RubyRequestType_Locked_Read) ||
2387039Snate@binkert.org        (request->ruby_request.type == RubyRequestType_Locked_Write)) {
2397455Snate@binkert.org        pair<RequestTable::iterator, bool> r =
2407455Snate@binkert.org            m_writeRequestTable.insert(RequestTable::value_type(line_addr, 0));
2417455Snate@binkert.org        bool success = r.second;
2427455Snate@binkert.org        RequestTable::iterator i = r.first;
2437455Snate@binkert.org        if (!success) {
2447455Snate@binkert.org            i->second = request;
2457039Snate@binkert.org            // return true;
2467039Snate@binkert.org
2477039Snate@binkert.org            // drh5: isn't this an error?  do you lose the initial request?
2487039Snate@binkert.org            assert(0);
2497039Snate@binkert.org        }
2507455Snate@binkert.org        i->second = request;
2517039Snate@binkert.org        m_outstanding_count++;
2527039Snate@binkert.org    } else {
2537455Snate@binkert.org        pair<RequestTable::iterator, bool> r =
2547455Snate@binkert.org            m_readRequestTable.insert(RequestTable::value_type(line_addr, 0));
2557455Snate@binkert.org        bool success = r.second;
2567455Snate@binkert.org        RequestTable::iterator i = r.first;
2577455Snate@binkert.org        if (!success) {
2587455Snate@binkert.org            i->second = request;
2597039Snate@binkert.org            // return true;
2607039Snate@binkert.org
2617039Snate@binkert.org            // drh5: isn't this an error?  do you lose the initial request?
2627039Snate@binkert.org            assert(0);
2637039Snate@binkert.org        }
2647455Snate@binkert.org        i->second = request;
2657039Snate@binkert.org        m_outstanding_count++;
2666145Snate@binkert.org    }
2676145Snate@binkert.org
2687039Snate@binkert.org    g_system_ptr->getProfiler()->sequencerRequests(m_outstanding_count);
2696145Snate@binkert.org
2707039Snate@binkert.org    total_outstanding = m_writeRequestTable.size() + m_readRequestTable.size();
2717039Snate@binkert.org    assert(m_outstanding_count == total_outstanding);
2726145Snate@binkert.org
2737039Snate@binkert.org    return false;
2746145Snate@binkert.org}
2756145Snate@binkert.org
2767039Snate@binkert.orgvoid
2777455Snate@binkert.orgSequencer::markRemoved()
2787455Snate@binkert.org{
2797455Snate@binkert.org    m_outstanding_count--;
2807455Snate@binkert.org    assert(m_outstanding_count ==
2817455Snate@binkert.org           m_writeRequestTable.size() + m_readRequestTable.size());
2827455Snate@binkert.org}
2837455Snate@binkert.org
2847455Snate@binkert.orgvoid
2857039Snate@binkert.orgSequencer::removeRequest(SequencerRequest* srequest)
2867039Snate@binkert.org{
2877039Snate@binkert.org    assert(m_outstanding_count ==
2887039Snate@binkert.org           m_writeRequestTable.size() + m_readRequestTable.size());
2896145Snate@binkert.org
2907039Snate@binkert.org    const RubyRequest & ruby_request = srequest->ruby_request;
2917039Snate@binkert.org    Address line_addr(ruby_request.paddr);
2927039Snate@binkert.org    line_addr.makeLineAddress();
2937039Snate@binkert.org    if ((ruby_request.type == RubyRequestType_ST) ||
2947039Snate@binkert.org        (ruby_request.type == RubyRequestType_RMW_Read) ||
2957039Snate@binkert.org        (ruby_request.type == RubyRequestType_RMW_Write) ||
2967039Snate@binkert.org        (ruby_request.type == RubyRequestType_Locked_Read) ||
2977039Snate@binkert.org        (ruby_request.type == RubyRequestType_Locked_Write)) {
2987455Snate@binkert.org        m_writeRequestTable.erase(line_addr);
2997039Snate@binkert.org    } else {
3007455Snate@binkert.org        m_readRequestTable.erase(line_addr);
3017039Snate@binkert.org    }
3026285Snate@binkert.org
3037455Snate@binkert.org    markRemoved();
3046145Snate@binkert.org}
3056145Snate@binkert.org
3067560SBrad.Beckmann@amd.combool
3077560SBrad.Beckmann@amd.comSequencer::handleLlsc(const Address& address, SequencerRequest* request)
3087550SBrad.Beckmann@amd.com{
3097560SBrad.Beckmann@amd.com    //
3107560SBrad.Beckmann@amd.com    // The success flag indicates whether the LLSC operation was successful.
3117560SBrad.Beckmann@amd.com    // LL ops will always succeed, but SC may fail if the cache line is no
3127560SBrad.Beckmann@amd.com    // longer locked.
3137560SBrad.Beckmann@amd.com    //
3147560SBrad.Beckmann@amd.com    bool success = true;
3157550SBrad.Beckmann@amd.com    if (request->ruby_request.type == RubyRequestType_Locked_Write) {
3167550SBrad.Beckmann@amd.com        if (!m_dataCache_ptr->isLocked(address, m_version)) {
3177550SBrad.Beckmann@amd.com            //
3187550SBrad.Beckmann@amd.com            // For failed SC requests, indicate the failure to the cpu by
3197550SBrad.Beckmann@amd.com            // setting the extra data to zero.
3207550SBrad.Beckmann@amd.com            //
3217550SBrad.Beckmann@amd.com            request->ruby_request.pkt->req->setExtraData(0);
3227560SBrad.Beckmann@amd.com            success = false;
3237550SBrad.Beckmann@amd.com        } else {
3247550SBrad.Beckmann@amd.com            //
3257550SBrad.Beckmann@amd.com            // For successful SC requests, indicate the success to the cpu by
3267550SBrad.Beckmann@amd.com            // setting the extra data to one.
3277550SBrad.Beckmann@amd.com            //
3287550SBrad.Beckmann@amd.com            request->ruby_request.pkt->req->setExtraData(1);
3297550SBrad.Beckmann@amd.com        }
3307560SBrad.Beckmann@amd.com        //
3317560SBrad.Beckmann@amd.com        // Independent of success, all SC operations must clear the lock
3327560SBrad.Beckmann@amd.com        //
3337550SBrad.Beckmann@amd.com        m_dataCache_ptr->clearLocked(address);
3347550SBrad.Beckmann@amd.com    } else if (request->ruby_request.type == RubyRequestType_Locked_Read) {
3357550SBrad.Beckmann@amd.com        //
3367550SBrad.Beckmann@amd.com        // Note: To fully follow Alpha LLSC semantics, should the LL clear any
3377550SBrad.Beckmann@amd.com        // previously locked cache lines?
3387550SBrad.Beckmann@amd.com        //
3397550SBrad.Beckmann@amd.com        m_dataCache_ptr->setLocked(address, m_version);
3407550SBrad.Beckmann@amd.com    } else if (m_dataCache_ptr->isLocked(address, m_version)) {
3417550SBrad.Beckmann@amd.com        //
3427550SBrad.Beckmann@amd.com        // Normal writes should clear the locked address
3437550SBrad.Beckmann@amd.com        //
3447550SBrad.Beckmann@amd.com        m_dataCache_ptr->clearLocked(address);
3457550SBrad.Beckmann@amd.com    }
3467560SBrad.Beckmann@amd.com    return success;
3477550SBrad.Beckmann@amd.com}
3487550SBrad.Beckmann@amd.com
3497550SBrad.Beckmann@amd.comvoid
3507039Snate@binkert.orgSequencer::writeCallback(const Address& address, DataBlock& data)
3517039Snate@binkert.org{
3527546SBrad.Beckmann@amd.com    writeCallback(address, GenericMachineType_NULL, data);
3537546SBrad.Beckmann@amd.com}
3547546SBrad.Beckmann@amd.com
3557546SBrad.Beckmann@amd.comvoid
3567546SBrad.Beckmann@amd.comSequencer::writeCallback(const Address& address,
3577546SBrad.Beckmann@amd.com                         GenericMachineType mach,
3587546SBrad.Beckmann@amd.com                         DataBlock& data)
3597546SBrad.Beckmann@amd.com{
3607039Snate@binkert.org    assert(address == line_address(address));
3617455Snate@binkert.org    assert(m_writeRequestTable.count(line_address(address)));
3626145Snate@binkert.org
3637455Snate@binkert.org    RequestTable::iterator i = m_writeRequestTable.find(address);
3647455Snate@binkert.org    assert(i != m_writeRequestTable.end());
3657455Snate@binkert.org    SequencerRequest* request = i->second;
3666145Snate@binkert.org
3677455Snate@binkert.org    m_writeRequestTable.erase(i);
3687455Snate@binkert.org    markRemoved();
3696846Spdudnik@cs.wisc.edu
3707039Snate@binkert.org    assert((request->ruby_request.type == RubyRequestType_ST) ||
3717039Snate@binkert.org           (request->ruby_request.type == RubyRequestType_RMW_Read) ||
3727039Snate@binkert.org           (request->ruby_request.type == RubyRequestType_RMW_Write) ||
3737039Snate@binkert.org           (request->ruby_request.type == RubyRequestType_Locked_Read) ||
3747039Snate@binkert.org           (request->ruby_request.type == RubyRequestType_Locked_Write));
3756145Snate@binkert.org
3767550SBrad.Beckmann@amd.com    //
3777550SBrad.Beckmann@amd.com    // For Alpha, properly handle LL, SC, and write requests with respect to
3787550SBrad.Beckmann@amd.com    // locked cache blocks.
3797550SBrad.Beckmann@amd.com    //
3807560SBrad.Beckmann@amd.com    bool success = handleLlsc(address, request);
3817550SBrad.Beckmann@amd.com
3827550SBrad.Beckmann@amd.com    if (request->ruby_request.type == RubyRequestType_RMW_Read) {
3837039Snate@binkert.org        m_controller->blockOnQueue(address, m_mandatory_q_ptr);
3847039Snate@binkert.org    } else if (request->ruby_request.type == RubyRequestType_RMW_Write) {
3857039Snate@binkert.org        m_controller->unblock(address);
3867039Snate@binkert.org    }
3876863Sdrh5@cs.wisc.edu
3887560SBrad.Beckmann@amd.com    hitCallback(request, mach, data, success);
3896145Snate@binkert.org}
3906145Snate@binkert.org
3917039Snate@binkert.orgvoid
3927039Snate@binkert.orgSequencer::readCallback(const Address& address, DataBlock& data)
3937039Snate@binkert.org{
3947546SBrad.Beckmann@amd.com    readCallback(address, GenericMachineType_NULL, data);
3957546SBrad.Beckmann@amd.com}
3967546SBrad.Beckmann@amd.com
3977546SBrad.Beckmann@amd.comvoid
3987546SBrad.Beckmann@amd.comSequencer::readCallback(const Address& address,
3997546SBrad.Beckmann@amd.com                        GenericMachineType mach,
4007546SBrad.Beckmann@amd.com                        DataBlock& data)
4017546SBrad.Beckmann@amd.com{
4027039Snate@binkert.org    assert(address == line_address(address));
4037455Snate@binkert.org    assert(m_readRequestTable.count(line_address(address)));
4046145Snate@binkert.org
4057455Snate@binkert.org    RequestTable::iterator i = m_readRequestTable.find(address);
4067455Snate@binkert.org    assert(i != m_readRequestTable.end());
4077455Snate@binkert.org    SequencerRequest* request = i->second;
4087455Snate@binkert.org
4097455Snate@binkert.org    m_readRequestTable.erase(i);
4107455Snate@binkert.org    markRemoved();
4116145Snate@binkert.org
4127039Snate@binkert.org    assert((request->ruby_request.type == RubyRequestType_LD) ||
4137039Snate@binkert.org           (request->ruby_request.type == RubyRequestType_RMW_Read) ||
4147039Snate@binkert.org           (request->ruby_request.type == RubyRequestType_IFETCH));
4156285Snate@binkert.org
4167560SBrad.Beckmann@amd.com    hitCallback(request, mach, data, true);
4176145Snate@binkert.org}
4186145Snate@binkert.org
4197039Snate@binkert.orgvoid
4207546SBrad.Beckmann@amd.comSequencer::hitCallback(SequencerRequest* srequest,
4217546SBrad.Beckmann@amd.com                       GenericMachineType mach,
4227560SBrad.Beckmann@amd.com                       DataBlock& data,
4237560SBrad.Beckmann@amd.com                       bool success)
4247039Snate@binkert.org{
4257039Snate@binkert.org    const RubyRequest & ruby_request = srequest->ruby_request;
4267039Snate@binkert.org    Address request_address(ruby_request.paddr);
4277039Snate@binkert.org    Address request_line_address(ruby_request.paddr);
4287039Snate@binkert.org    request_line_address.makeLineAddress();
4297039Snate@binkert.org    RubyRequestType type = ruby_request.type;
4307039Snate@binkert.org    Time issued_time = srequest->issue_time;
4316145Snate@binkert.org
4327039Snate@binkert.org    // Set this cache entry to the most recently used
4337039Snate@binkert.org    if (type == RubyRequestType_IFETCH) {
4347039Snate@binkert.org        if (m_instCache_ptr->isTagPresent(request_line_address))
4357039Snate@binkert.org            m_instCache_ptr->setMRU(request_line_address);
4367039Snate@binkert.org    } else {
4377039Snate@binkert.org        if (m_dataCache_ptr->isTagPresent(request_line_address))
4387039Snate@binkert.org            m_dataCache_ptr->setMRU(request_line_address);
4397039Snate@binkert.org    }
4406145Snate@binkert.org
4417039Snate@binkert.org    assert(g_eventQueue_ptr->getTime() >= issued_time);
4427039Snate@binkert.org    Time miss_latency = g_eventQueue_ptr->getTime() - issued_time;
4436145Snate@binkert.org
4447039Snate@binkert.org    // Profile the miss latency for all non-zero demand misses
4457039Snate@binkert.org    if (miss_latency != 0) {
4467546SBrad.Beckmann@amd.com        g_system_ptr->getProfiler()->missLatency(miss_latency, type, mach);
4476285Snate@binkert.org
4487039Snate@binkert.org        if (Debug::getProtocolTrace()) {
4497560SBrad.Beckmann@amd.com            if (success) {
4507560SBrad.Beckmann@amd.com                g_system_ptr->getProfiler()->
4517560SBrad.Beckmann@amd.com                    profileTransition("Seq", m_version,
4527560SBrad.Beckmann@amd.com                                      Address(ruby_request.paddr), "", "Done", "",
4537560SBrad.Beckmann@amd.com                                      csprintf("%d cycles", miss_latency));
4547560SBrad.Beckmann@amd.com            } else {
4557560SBrad.Beckmann@amd.com                g_system_ptr->getProfiler()->
4567560SBrad.Beckmann@amd.com                    profileTransition("Seq", m_version,
4577560SBrad.Beckmann@amd.com                                      Address(ruby_request.paddr), "", "SC_Failed", "",
4587560SBrad.Beckmann@amd.com                                      csprintf("%d cycles", miss_latency));
4597560SBrad.Beckmann@amd.com            }
4607039Snate@binkert.org        }
4616285Snate@binkert.org    }
4627039Snate@binkert.org#if 0
4637039Snate@binkert.org    if (request.getPrefetch() == PrefetchBit_Yes) {
4647039Snate@binkert.org        return; // Ignore the prefetch
4657039Snate@binkert.org    }
4667039Snate@binkert.org#endif
4676285Snate@binkert.org
4687039Snate@binkert.org    // update the data
4697039Snate@binkert.org    if (ruby_request.data != NULL) {
4707039Snate@binkert.org        if ((type == RubyRequestType_LD) ||
4717039Snate@binkert.org            (type == RubyRequestType_IFETCH) ||
4727039Snate@binkert.org            (type == RubyRequestType_RMW_Read) ||
4737039Snate@binkert.org            (type == RubyRequestType_Locked_Read)) {
4747023SBrad.Beckmann@amd.com
4757039Snate@binkert.org            memcpy(ruby_request.data,
4767039Snate@binkert.org                   data.getData(request_address.getOffset(), ruby_request.len),
4777039Snate@binkert.org                   ruby_request.len);
4787039Snate@binkert.org        } else {
4797039Snate@binkert.org            data.setData(ruby_request.data, request_address.getOffset(),
4807039Snate@binkert.org                         ruby_request.len);
4817039Snate@binkert.org        }
4826285Snate@binkert.org    } else {
4837039Snate@binkert.org        DPRINTF(MemoryAccess,
4847039Snate@binkert.org                "WARNING.  Data not transfered from Ruby to M5 for type %s\n",
4857039Snate@binkert.org                RubyRequestType_to_string(type));
4867039Snate@binkert.org    }
4877023SBrad.Beckmann@amd.com
4887039Snate@binkert.org    // If using the RubyTester, update the RubyTester sender state's
4897039Snate@binkert.org    // subBlock with the recieved data.  The tester will later access
4907039Snate@binkert.org    // this state.
4917039Snate@binkert.org    // Note: RubyPort will access it's sender state before the
4927039Snate@binkert.org    // RubyTester.
4937039Snate@binkert.org    if (m_usingRubyTester) {
4947039Snate@binkert.org        RubyPort::SenderState *requestSenderState =
4957039Snate@binkert.org            safe_cast<RubyPort::SenderState*>(ruby_request.pkt->senderState);
4967039Snate@binkert.org        RubyTester::SenderState* testerSenderState =
4977039Snate@binkert.org            safe_cast<RubyTester::SenderState*>(requestSenderState->saved);
4987039Snate@binkert.org        testerSenderState->subBlock->mergeFrom(data);
4997039Snate@binkert.org    }
5007023SBrad.Beckmann@amd.com
5017039Snate@binkert.org    ruby_hit_callback(ruby_request.pkt);
5027039Snate@binkert.org    delete srequest;
5036285Snate@binkert.org}
5046285Snate@binkert.org
5056285Snate@binkert.org// Returns true if the sequencer already has a load or store outstanding
5067039Snate@binkert.orgRequestStatus
5077039Snate@binkert.orgSequencer::getRequestStatus(const RubyRequest& request)
5087039Snate@binkert.org{
5097039Snate@binkert.org    bool is_outstanding_store =
5107455Snate@binkert.org        !!m_writeRequestTable.count(line_address(Address(request.paddr)));
5117039Snate@binkert.org    bool is_outstanding_load =
5127455Snate@binkert.org        !!m_readRequestTable.count(line_address(Address(request.paddr)));
5137039Snate@binkert.org    if (is_outstanding_store) {
5147039Snate@binkert.org        if ((request.type == RubyRequestType_LD) ||
5157039Snate@binkert.org            (request.type == RubyRequestType_IFETCH) ||
5167039Snate@binkert.org            (request.type == RubyRequestType_RMW_Read)) {
5177039Snate@binkert.org            m_store_waiting_on_load_cycles++;
5187039Snate@binkert.org        } else {
5197039Snate@binkert.org            m_store_waiting_on_store_cycles++;
5207039Snate@binkert.org        }
5217039Snate@binkert.org        return RequestStatus_Aliased;
5227039Snate@binkert.org    } else if (is_outstanding_load) {
5237039Snate@binkert.org        if ((request.type == RubyRequestType_ST) ||
5247039Snate@binkert.org            (request.type == RubyRequestType_RMW_Write)) {
5257039Snate@binkert.org            m_load_waiting_on_store_cycles++;
5267039Snate@binkert.org        } else {
5277039Snate@binkert.org            m_load_waiting_on_load_cycles++;
5287039Snate@binkert.org        }
5297039Snate@binkert.org        return RequestStatus_Aliased;
5306859Sdrh5@cs.wisc.edu    }
5317039Snate@binkert.org
5327039Snate@binkert.org    if (m_outstanding_count >= m_max_outstanding_requests) {
5337039Snate@binkert.org        return RequestStatus_BufferFull;
5346859Sdrh5@cs.wisc.edu    }
5356145Snate@binkert.org
5367039Snate@binkert.org    return RequestStatus_Ready;
5376145Snate@binkert.org}
5386145Snate@binkert.org
5397039Snate@binkert.orgbool
5407039Snate@binkert.orgSequencer::empty() const
5417039Snate@binkert.org{
5427455Snate@binkert.org    return m_writeRequestTable.empty() && m_readRequestTable.empty();
5436145Snate@binkert.org}
5446145Snate@binkert.org
5457039Snate@binkert.orgRequestStatus
5467039Snate@binkert.orgSequencer::makeRequest(const RubyRequest &request)
5477039Snate@binkert.org{
5487039Snate@binkert.org    assert(Address(request.paddr).getOffset() + request.len <=
5497039Snate@binkert.org           RubySystem::getBlockSizeBytes());
5507039Snate@binkert.org    RequestStatus status = getRequestStatus(request);
5517039Snate@binkert.org    if (status != RequestStatus_Ready)
5527039Snate@binkert.org        return status;
5536349Spdudnik@gmail.com
5547039Snate@binkert.org    SequencerRequest *srequest =
5557039Snate@binkert.org        new SequencerRequest(request, g_eventQueue_ptr->getTime());
5566285Snate@binkert.org    bool found = insertRequest(srequest);
5577039Snate@binkert.org    if (found) {
5587039Snate@binkert.org        panic("Sequencer::makeRequest should never be called if the "
5597039Snate@binkert.org              "request is already outstanding\n");
5607039Snate@binkert.org        return RequestStatus_NULL;
5617039Snate@binkert.org    }
5627023SBrad.Beckmann@amd.com
5637039Snate@binkert.org    issueRequest(request);
5646145Snate@binkert.org
5657039Snate@binkert.org    // TODO: issue hardware prefetches here
5667039Snate@binkert.org    return RequestStatus_Issued;
5676145Snate@binkert.org}
5686145Snate@binkert.org
5697039Snate@binkert.orgvoid
5707039Snate@binkert.orgSequencer::issueRequest(const RubyRequest& request)
5717039Snate@binkert.org{
5727039Snate@binkert.org    // TODO: get rid of CacheMsg, CacheRequestType, and
5737039Snate@binkert.org    // AccessModeTYpe, & have SLICC use RubyRequest and subtypes
5747039Snate@binkert.org    // natively
5757039Snate@binkert.org    CacheRequestType ctype;
5767039Snate@binkert.org    switch(request.type) {
5777039Snate@binkert.org      case RubyRequestType_IFETCH:
5787039Snate@binkert.org        ctype = CacheRequestType_IFETCH;
5797039Snate@binkert.org        break;
5807039Snate@binkert.org      case RubyRequestType_LD:
5817039Snate@binkert.org        ctype = CacheRequestType_LD;
5827039Snate@binkert.org        break;
5837039Snate@binkert.org      case RubyRequestType_ST:
5847039Snate@binkert.org        ctype = CacheRequestType_ST;
5857039Snate@binkert.org        break;
5867039Snate@binkert.org      case RubyRequestType_Locked_Read:
5877039Snate@binkert.org      case RubyRequestType_Locked_Write:
5887039Snate@binkert.org        ctype = CacheRequestType_ATOMIC;
5897039Snate@binkert.org        break;
5907039Snate@binkert.org      case RubyRequestType_RMW_Read:
5917039Snate@binkert.org        ctype = CacheRequestType_ATOMIC;
5927039Snate@binkert.org        break;
5937039Snate@binkert.org      case RubyRequestType_RMW_Write:
5947039Snate@binkert.org        ctype = CacheRequestType_ATOMIC;
5957039Snate@binkert.org        break;
5967039Snate@binkert.org      default:
5977039Snate@binkert.org        assert(0);
5987039Snate@binkert.org    }
5996285Snate@binkert.org
6007039Snate@binkert.org    AccessModeType amtype;
6017039Snate@binkert.org    switch(request.access_mode){
6027039Snate@binkert.org      case RubyAccessMode_User:
6037039Snate@binkert.org        amtype = AccessModeType_UserMode;
6047039Snate@binkert.org        break;
6057039Snate@binkert.org      case RubyAccessMode_Supervisor:
6067039Snate@binkert.org        amtype = AccessModeType_SupervisorMode;
6077039Snate@binkert.org        break;
6087039Snate@binkert.org      case RubyAccessMode_Device:
6097039Snate@binkert.org        amtype = AccessModeType_UserMode;
6107039Snate@binkert.org        break;
6117039Snate@binkert.org      default:
6127039Snate@binkert.org        assert(0);
6137039Snate@binkert.org    }
6146285Snate@binkert.org
6157039Snate@binkert.org    Address line_addr(request.paddr);
6167039Snate@binkert.org    line_addr.makeLineAddress();
6177453Snate@binkert.org    CacheMsg *msg = new CacheMsg(line_addr, Address(request.paddr), ctype,
6187453Snate@binkert.org        Address(request.pc), amtype, request.len, PrefetchBit_No,
6197453Snate@binkert.org        request.proc_id);
6206285Snate@binkert.org
6217039Snate@binkert.org    if (Debug::getProtocolTrace()) {
6227039Snate@binkert.org        g_system_ptr->getProfiler()->
6237039Snate@binkert.org            profileTransition("Seq", m_version, Address(request.paddr),
6247039Snate@binkert.org                              "", "Begin", "",
6257039Snate@binkert.org                              RubyRequestType_to_string(request.type));
6267039Snate@binkert.org    }
6276285Snate@binkert.org
6287039Snate@binkert.org    if (g_system_ptr->getTracer()->traceEnabled()) {
6297039Snate@binkert.org        g_system_ptr->getTracer()->
6307039Snate@binkert.org            traceRequest(this, line_addr, Address(request.pc),
6317039Snate@binkert.org                         request.type, g_eventQueue_ptr->getTime());
6327039Snate@binkert.org    }
6336285Snate@binkert.org
6347039Snate@binkert.org    Time latency = 0;  // initialzed to an null value
6356285Snate@binkert.org
6367039Snate@binkert.org    if (request.type == RubyRequestType_IFETCH)
6377039Snate@binkert.org        latency = m_instCache_ptr->getLatency();
6387039Snate@binkert.org    else
6397039Snate@binkert.org        latency = m_dataCache_ptr->getLatency();
6406285Snate@binkert.org
6417039Snate@binkert.org    // Send the message to the cache controller
6427039Snate@binkert.org    assert(latency > 0);
6436145Snate@binkert.org
6447039Snate@binkert.org    assert(m_mandatory_q_ptr != NULL);
6457039Snate@binkert.org    m_mandatory_q_ptr->enqueue(msg, latency);
6466145Snate@binkert.org}
6476145Snate@binkert.org
6487039Snate@binkert.org#if 0
6497039Snate@binkert.orgbool
6507039Snate@binkert.orgSequencer::tryCacheAccess(const Address& addr, CacheRequestType type,
6517039Snate@binkert.org                          AccessModeType access_mode,
6527039Snate@binkert.org                          int size, DataBlock*& data_ptr)
6537039Snate@binkert.org{
6547039Snate@binkert.org    CacheMemory *cache =
6557039Snate@binkert.org        (type == CacheRequestType_IFETCH) ? m_instCache_ptr : m_dataCache_ptr;
6567039Snate@binkert.org
6577039Snate@binkert.org    return cache->tryCacheAccess(line_address(addr), type, data_ptr);
6587039Snate@binkert.org}
6597039Snate@binkert.org#endif
6607039Snate@binkert.org
6617455Snate@binkert.orgtemplate <class KEY, class VALUE>
6627455Snate@binkert.orgstd::ostream &
6637455Snate@binkert.orgoperator<<(ostream &out, const m5::hash_map<KEY, VALUE> &map)
6647455Snate@binkert.org{
6657455Snate@binkert.org    typename m5::hash_map<KEY, VALUE>::const_iterator i = map.begin();
6667455Snate@binkert.org    typename m5::hash_map<KEY, VALUE>::const_iterator end = map.end();
6677455Snate@binkert.org
6687455Snate@binkert.org    out << "[";
6697455Snate@binkert.org    for (; i != end; ++i)
6707455Snate@binkert.org        out << " " << i->first << "=" << i->second;
6717455Snate@binkert.org    out << " ]";
6727455Snate@binkert.org
6737455Snate@binkert.org    return out;
6747455Snate@binkert.org}
6757455Snate@binkert.org
6767039Snate@binkert.orgvoid
6777039Snate@binkert.orgSequencer::print(ostream& out) const
6787039Snate@binkert.org{
6797039Snate@binkert.org    out << "[Sequencer: " << m_version
6807039Snate@binkert.org        << ", outstanding requests: " << m_outstanding_count
6817039Snate@binkert.org        << ", read request table: " << m_readRequestTable
6827039Snate@binkert.org        << ", write request table: " << m_writeRequestTable
6837039Snate@binkert.org        << "]";
6847039Snate@binkert.org}
6857039Snate@binkert.org
6867039Snate@binkert.org// this can be called from setState whenever coherence permissions are
6877039Snate@binkert.org// upgraded when invoked, coherence violations will be checked for the
6887039Snate@binkert.org// given block
6897039Snate@binkert.orgvoid
6907039Snate@binkert.orgSequencer::checkCoherence(const Address& addr)
6917039Snate@binkert.org{
6926145Snate@binkert.org#ifdef CHECK_COHERENCE
6937039Snate@binkert.org    g_system_ptr->checkGlobalCoherenceInvariant(addr);
6946145Snate@binkert.org#endif
6956145Snate@binkert.org}
696