Sequencer.cc revision 6506
16145Snate@binkert.org
26145Snate@binkert.org/*
36145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
46145Snate@binkert.org * All rights reserved.
56145Snate@binkert.org *
66145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
76145Snate@binkert.org * modification, are permitted provided that the following conditions are
86145Snate@binkert.org * met: redistributions of source code must retain the above copyright
96145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
106145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
116145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
126145Snate@binkert.org * documentation and/or other materials provided with the distribution;
136145Snate@binkert.org * neither the name of the copyright holders nor the names of its
146145Snate@binkert.org * contributors may be used to endorse or promote products derived from
156145Snate@binkert.org * this software without specific prior written permission.
166145Snate@binkert.org *
176145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286145Snate@binkert.org */
296145Snate@binkert.org
306154Snate@binkert.org#include "mem/ruby/common/Global.hh"
316154Snate@binkert.org#include "mem/ruby/system/Sequencer.hh"
326154Snate@binkert.org#include "mem/ruby/system/System.hh"
336154Snate@binkert.org#include "mem/protocol/Protocol.hh"
346154Snate@binkert.org#include "mem/ruby/profiler/Profiler.hh"
356154Snate@binkert.org#include "mem/ruby/system/CacheMemory.hh"
366285Snate@binkert.org#include "mem/protocol/CacheMsg.hh"
376285Snate@binkert.org#include "mem/ruby/recorder/Tracer.hh"
386154Snate@binkert.org#include "mem/ruby/common/SubBlock.hh"
396154Snate@binkert.org#include "mem/protocol/Protocol.hh"
406154Snate@binkert.org#include "mem/gems_common/Map.hh"
416285Snate@binkert.org#include "mem/ruby/buffers/MessageBuffer.hh"
426285Snate@binkert.org#include "mem/ruby/slicc_interface/AbstractController.hh"
436145Snate@binkert.org
446285Snate@binkert.org//Sequencer::Sequencer(int core_id, MessageBuffer* mandatory_q)
456145Snate@binkert.org
466355Spdudnik@gmail.com#define LLSC_FAIL -2
476355Spdudnik@gmail.com
486285Snate@binkert.orgSequencer::Sequencer(const string & name)
496285Snate@binkert.org  :RubyPort(name)
506285Snate@binkert.org{
516285Snate@binkert.org}
526285Snate@binkert.org
536285Snate@binkert.orgvoid Sequencer::init(const vector<string> & argv)
546285Snate@binkert.org{
556145Snate@binkert.org  m_deadlock_check_scheduled = false;
566145Snate@binkert.org  m_outstanding_count = 0;
576145Snate@binkert.org
586285Snate@binkert.org  m_max_outstanding_requests = 0;
596285Snate@binkert.org  m_deadlock_threshold = 0;
606285Snate@binkert.org  m_version = -1;
616285Snate@binkert.org  m_instCache_ptr = NULL;
626285Snate@binkert.org  m_dataCache_ptr = NULL;
636285Snate@binkert.org  m_controller = NULL;
646505Spdudnik@gmail.com  m_servicing_atomic = -1;
656505Spdudnik@gmail.com  m_atomics_counter = 0;
666285Snate@binkert.org  for (size_t i=0; i<argv.size(); i+=2) {
676285Snate@binkert.org    if ( argv[i] == "controller") {
686285Snate@binkert.org      m_controller = RubySystem::getController(argv[i+1]); // args[i] = "L1Cache"
696285Snate@binkert.org      m_mandatory_q_ptr = m_controller->getMandatoryQueue();
706285Snate@binkert.org    } else if ( argv[i] == "icache")
716285Snate@binkert.org      m_instCache_ptr = RubySystem::getCache(argv[i+1]);
726285Snate@binkert.org    else if ( argv[i] == "dcache")
736285Snate@binkert.org      m_dataCache_ptr = RubySystem::getCache(argv[i+1]);
746285Snate@binkert.org    else if ( argv[i] == "version")
756285Snate@binkert.org      m_version = atoi(argv[i+1].c_str());
766285Snate@binkert.org    else if ( argv[i] == "max_outstanding_requests")
776285Snate@binkert.org      m_max_outstanding_requests = atoi(argv[i+1].c_str());
786285Snate@binkert.org    else if ( argv[i] == "deadlock_threshold")
796285Snate@binkert.org      m_deadlock_threshold = atoi(argv[i+1].c_str());
806285Snate@binkert.org    else {
816285Snate@binkert.org      cerr << "WARNING: Sequencer: Unkown configuration parameter: " << argv[i] << endl;
826285Snate@binkert.org      assert(false);
836285Snate@binkert.org    }
846145Snate@binkert.org  }
856285Snate@binkert.org  assert(m_max_outstanding_requests > 0);
866285Snate@binkert.org  assert(m_deadlock_threshold > 0);
876285Snate@binkert.org  assert(m_version > -1);
886285Snate@binkert.org  assert(m_instCache_ptr != NULL);
896285Snate@binkert.org  assert(m_dataCache_ptr != NULL);
906285Snate@binkert.org  assert(m_controller != NULL);
916145Snate@binkert.org}
926145Snate@binkert.org
936145Snate@binkert.orgSequencer::~Sequencer() {
946285Snate@binkert.org
956145Snate@binkert.org}
966145Snate@binkert.org
976145Snate@binkert.orgvoid Sequencer::wakeup() {
986145Snate@binkert.org  // Check for deadlock of any of the requests
996145Snate@binkert.org  Time current_time = g_eventQueue_ptr->getTime();
1006145Snate@binkert.org
1016145Snate@binkert.org  // Check across all outstanding requests
1026145Snate@binkert.org  int total_outstanding = 0;
1036285Snate@binkert.org
1046285Snate@binkert.org  Vector<Address> keys = m_readRequestTable.keys();
1056285Snate@binkert.org  for (int i=0; i<keys.size(); i++) {
1066285Snate@binkert.org    SequencerRequest* request = m_readRequestTable.lookup(keys[i]);
1076285Snate@binkert.org    if (current_time - request->issue_time >= m_deadlock_threshold) {
1086285Snate@binkert.org      WARN_MSG("Possible Deadlock detected");
1096285Snate@binkert.org      WARN_EXPR(request);
1106285Snate@binkert.org      WARN_EXPR(m_version);
1116285Snate@binkert.org      WARN_EXPR(keys.size());
1126285Snate@binkert.org      WARN_EXPR(current_time);
1136285Snate@binkert.org      WARN_EXPR(request->issue_time);
1146285Snate@binkert.org      WARN_EXPR(current_time - request->issue_time);
1156285Snate@binkert.org      ERROR_MSG("Aborting");
1166145Snate@binkert.org    }
1176285Snate@binkert.org  }
1186145Snate@binkert.org
1196285Snate@binkert.org  keys = m_writeRequestTable.keys();
1206285Snate@binkert.org  for (int i=0; i<keys.size(); i++) {
1216285Snate@binkert.org    SequencerRequest* request = m_writeRequestTable.lookup(keys[i]);
1226285Snate@binkert.org    if (current_time - request->issue_time >= m_deadlock_threshold) {
1236285Snate@binkert.org      WARN_MSG("Possible Deadlock detected");
1246285Snate@binkert.org      WARN_EXPR(request);
1256285Snate@binkert.org      WARN_EXPR(m_version);
1266285Snate@binkert.org      WARN_EXPR(current_time);
1276285Snate@binkert.org      WARN_EXPR(request->issue_time);
1286285Snate@binkert.org      WARN_EXPR(current_time - request->issue_time);
1296285Snate@binkert.org      WARN_EXPR(keys.size());
1306285Snate@binkert.org      ERROR_MSG("Aborting");
1316145Snate@binkert.org    }
1326285Snate@binkert.org  }
1336285Snate@binkert.org  total_outstanding += m_writeRequestTable.size() + m_readRequestTable.size();
1346285Snate@binkert.org
1356145Snate@binkert.org  assert(m_outstanding_count == total_outstanding);
1366145Snate@binkert.org
1376145Snate@binkert.org  if (m_outstanding_count > 0) { // If there are still outstanding requests, keep checking
1386285Snate@binkert.org    g_eventQueue_ptr->scheduleEvent(this, m_deadlock_threshold);
1396145Snate@binkert.org  } else {
1406145Snate@binkert.org    m_deadlock_check_scheduled = false;
1416145Snate@binkert.org  }
1426145Snate@binkert.org}
1436145Snate@binkert.org
1446145Snate@binkert.orgvoid Sequencer::printProgress(ostream& out) const{
1456285Snate@binkert.org  /*
1466145Snate@binkert.org  int total_demand = 0;
1476145Snate@binkert.org  out << "Sequencer Stats Version " << m_version << endl;
1486145Snate@binkert.org  out << "Current time = " << g_eventQueue_ptr->getTime() << endl;
1496145Snate@binkert.org  out << "---------------" << endl;
1506145Snate@binkert.org  out << "outstanding requests" << endl;
1516145Snate@binkert.org
1526285Snate@binkert.org  Vector<Address> rkeys = m_readRequestTable.keys();
1536285Snate@binkert.org  int read_size = rkeys.size();
1546285Snate@binkert.org  out << "proc " << m_version << " Read Requests = " << read_size << endl;
1556285Snate@binkert.org  // print the request table
1566285Snate@binkert.org  for(int i=0; i < read_size; ++i){
1576285Snate@binkert.org    SequencerRequest * request = m_readRequestTable.lookup(rkeys[i]);
1586285Snate@binkert.org    out << "\tRequest[ " << i << " ] = " << request->type << " Address " << rkeys[i]  << " Posted " << request->issue_time << " PF " << PrefetchBit_No << endl;
1596285Snate@binkert.org    total_demand++;
1606285Snate@binkert.org  }
1616145Snate@binkert.org
1626285Snate@binkert.org  Vector<Address> wkeys = m_writeRequestTable.keys();
1636285Snate@binkert.org  int write_size = wkeys.size();
1646285Snate@binkert.org  out << "proc " << m_version << " Write Requests = " << write_size << endl;
1656285Snate@binkert.org  // print the request table
1666285Snate@binkert.org  for(int i=0; i < write_size; ++i){
1676285Snate@binkert.org      CacheMsg & request = m_writeRequestTable.lookup(wkeys[i]);
1686145Snate@binkert.org      out << "\tRequest[ " << i << " ] = " << request.getType() << " Address " << wkeys[i]  << " Posted " << request.getTime() << " PF " << request.getPrefetch() << endl;
1696145Snate@binkert.org      if( request.getPrefetch() == PrefetchBit_No ){
1706145Snate@binkert.org        total_demand++;
1716145Snate@binkert.org      }
1726285Snate@binkert.org  }
1736145Snate@binkert.org
1746285Snate@binkert.org  out << endl;
1756285Snate@binkert.org
1766145Snate@binkert.org  out << "Total Number Outstanding: " << m_outstanding_count << endl;
1776145Snate@binkert.org  out << "Total Number Demand     : " << total_demand << endl;
1786145Snate@binkert.org  out << "Total Number Prefetches : " << m_outstanding_count - total_demand << endl;
1796145Snate@binkert.org  out << endl;
1806145Snate@binkert.org  out << endl;
1816285Snate@binkert.org  */
1826145Snate@binkert.org}
1836145Snate@binkert.org
1846285Snate@binkert.orgvoid Sequencer::printConfig(ostream& out) const {
1856285Snate@binkert.org  out << "Seqeuncer config: " << m_name << endl;
1866285Snate@binkert.org  out << "  controller: " << m_controller->getName() << endl;
1876285Snate@binkert.org  out << "  version: " << m_version << endl;
1886285Snate@binkert.org  out << "  max_outstanding_requests: " << m_max_outstanding_requests << endl;
1896285Snate@binkert.org  out << "  deadlock_threshold: " << m_deadlock_threshold << endl;
1906145Snate@binkert.org}
1916145Snate@binkert.org
1926145Snate@binkert.org// Insert the request on the correct request table.  Return true if
1936145Snate@binkert.org// the entry was already present.
1946285Snate@binkert.orgbool Sequencer::insertRequest(SequencerRequest* request) {
1956285Snate@binkert.org  int total_outstanding = m_writeRequestTable.size() + m_readRequestTable.size();
1966285Snate@binkert.org
1976145Snate@binkert.org  assert(m_outstanding_count == total_outstanding);
1986145Snate@binkert.org
1996145Snate@binkert.org  // See if we should schedule a deadlock check
2006145Snate@binkert.org  if (m_deadlock_check_scheduled == false) {
2016285Snate@binkert.org    g_eventQueue_ptr->scheduleEvent(this, m_deadlock_threshold);
2026145Snate@binkert.org    m_deadlock_check_scheduled = true;
2036145Snate@binkert.org  }
2046145Snate@binkert.org
2056285Snate@binkert.org  Address line_addr(request->ruby_request.paddr);
2066285Snate@binkert.org  line_addr.makeLineAddress();
2076285Snate@binkert.org  if ((request->ruby_request.type == RubyRequestType_ST) ||
2086355Spdudnik@gmail.com      (request->ruby_request.type == RubyRequestType_RMW_Read) ||
2096355Spdudnik@gmail.com      (request->ruby_request.type == RubyRequestType_RMW_Write) ||
2106350Spdudnik@gmail.com      (request->ruby_request.type == RubyRequestType_Locked_Read) ||
2116350Spdudnik@gmail.com      (request->ruby_request.type == RubyRequestType_Locked_Write)) {
2126285Snate@binkert.org    if (m_writeRequestTable.exist(line_addr)) {
2136285Snate@binkert.org      m_writeRequestTable.lookup(line_addr) = request;
2146285Snate@binkert.org      //      return true;
2156285Snate@binkert.org      assert(0); // drh5: isn't this an error?  do you lose the initial request?
2166145Snate@binkert.org    }
2176285Snate@binkert.org    m_writeRequestTable.allocate(line_addr);
2186285Snate@binkert.org    m_writeRequestTable.lookup(line_addr) = request;
2196145Snate@binkert.org    m_outstanding_count++;
2206145Snate@binkert.org  } else {
2216285Snate@binkert.org    if (m_readRequestTable.exist(line_addr)) {
2226285Snate@binkert.org      m_readRequestTable.lookup(line_addr) = request;
2236285Snate@binkert.org      //      return true;
2246285Snate@binkert.org      assert(0); // drh5: isn't this an error?  do you lose the initial request?
2256145Snate@binkert.org    }
2266285Snate@binkert.org    m_readRequestTable.allocate(line_addr);
2276285Snate@binkert.org    m_readRequestTable.lookup(line_addr) = request;
2286145Snate@binkert.org    m_outstanding_count++;
2296145Snate@binkert.org  }
2306145Snate@binkert.org
2316145Snate@binkert.org  g_system_ptr->getProfiler()->sequencerRequests(m_outstanding_count);
2326145Snate@binkert.org
2336285Snate@binkert.org  total_outstanding = m_writeRequestTable.size() + m_readRequestTable.size();
2346285Snate@binkert.org  assert(m_outstanding_count == total_outstanding);
2356145Snate@binkert.org
2366145Snate@binkert.org  return false;
2376145Snate@binkert.org}
2386145Snate@binkert.org
2396285Snate@binkert.orgvoid Sequencer::removeRequest(SequencerRequest* srequest) {
2406145Snate@binkert.org
2416285Snate@binkert.org  assert(m_outstanding_count == m_writeRequestTable.size() + m_readRequestTable.size());
2426285Snate@binkert.org
2436285Snate@binkert.org  const RubyRequest & ruby_request = srequest->ruby_request;
2446285Snate@binkert.org  Address line_addr(ruby_request.paddr);
2456285Snate@binkert.org  line_addr.makeLineAddress();
2466285Snate@binkert.org  if ((ruby_request.type == RubyRequestType_ST) ||
2476355Spdudnik@gmail.com      (ruby_request.type == RubyRequestType_RMW_Read) ||
2486355Spdudnik@gmail.com      (ruby_request.type == RubyRequestType_RMW_Write) ||
2496350Spdudnik@gmail.com      (ruby_request.type == RubyRequestType_Locked_Read) ||
2506350Spdudnik@gmail.com      (ruby_request.type == RubyRequestType_Locked_Write)) {
2516285Snate@binkert.org    m_writeRequestTable.deallocate(line_addr);
2526145Snate@binkert.org  } else {
2536285Snate@binkert.org    m_readRequestTable.deallocate(line_addr);
2546145Snate@binkert.org  }
2556145Snate@binkert.org  m_outstanding_count--;
2566145Snate@binkert.org
2576285Snate@binkert.org  assert(m_outstanding_count == m_writeRequestTable.size() + m_readRequestTable.size());
2586145Snate@binkert.org}
2596145Snate@binkert.org
2606145Snate@binkert.orgvoid Sequencer::writeCallback(const Address& address, DataBlock& data) {
2616145Snate@binkert.org
2626145Snate@binkert.org  assert(address == line_address(address));
2636285Snate@binkert.org  assert(m_writeRequestTable.exist(line_address(address)));
2646145Snate@binkert.org
2656285Snate@binkert.org  SequencerRequest* request = m_writeRequestTable.lookup(address);
2666145Snate@binkert.org  removeRequest(request);
2676145Snate@binkert.org
2686285Snate@binkert.org  assert((request->ruby_request.type == RubyRequestType_ST) ||
2696355Spdudnik@gmail.com         (request->ruby_request.type == RubyRequestType_RMW_Read) ||
2706355Spdudnik@gmail.com         (request->ruby_request.type == RubyRequestType_RMW_Write) ||
2716350Spdudnik@gmail.com         (request->ruby_request.type == RubyRequestType_Locked_Read) ||
2726350Spdudnik@gmail.com         (request->ruby_request.type == RubyRequestType_Locked_Write));
2736347Spdudnik@gmail.com  // POLINA: the assumption is that atomics are only on data cache and not instruction cache
2746350Spdudnik@gmail.com  if (request->ruby_request.type == RubyRequestType_Locked_Read) {
2756347Spdudnik@gmail.com    m_dataCache_ptr->setLocked(address, m_version);
2766347Spdudnik@gmail.com  }
2776506Spdudnik@gmail.com  else if (request->ruby_request.type == RubyRequestType_RMW_Read) {
2786506Spdudnik@gmail.com    m_controller->set_atomic(address);
2796506Spdudnik@gmail.com  }
2806506Spdudnik@gmail.com  else if (request->ruby_request.type == RubyRequestType_RMW_Write) {
2816506Spdudnik@gmail.com    m_controller->clear_atomic();
2826506Spdudnik@gmail.com  }
2836145Snate@binkert.org
2846285Snate@binkert.org  hitCallback(request, data);
2856145Snate@binkert.org}
2866145Snate@binkert.org
2876145Snate@binkert.orgvoid Sequencer::readCallback(const Address& address, DataBlock& data) {
2886145Snate@binkert.org
2896285Snate@binkert.org  assert(address == line_address(address));
2906285Snate@binkert.org  assert(m_readRequestTable.exist(line_address(address)));
2916145Snate@binkert.org
2926285Snate@binkert.org  SequencerRequest* request = m_readRequestTable.lookup(address);
2936285Snate@binkert.org  removeRequest(request);
2946285Snate@binkert.org
2956285Snate@binkert.org  assert((request->ruby_request.type == RubyRequestType_LD) ||
2966381Sdrh5@cs.wisc.edu	 (request->ruby_request.type == RubyRequestType_RMW_Read) ||
2976285Snate@binkert.org         (request->ruby_request.type == RubyRequestType_IFETCH));
2986285Snate@binkert.org
2996285Snate@binkert.org  hitCallback(request, data);
3006145Snate@binkert.org}
3016145Snate@binkert.org
3026285Snate@binkert.orgvoid Sequencer::hitCallback(SequencerRequest* srequest, DataBlock& data) {
3036285Snate@binkert.org  const RubyRequest & ruby_request = srequest->ruby_request;
3046285Snate@binkert.org  Address request_address(ruby_request.paddr);
3056285Snate@binkert.org  Address request_line_address(ruby_request.paddr);
3066285Snate@binkert.org  request_line_address.makeLineAddress();
3076285Snate@binkert.org  RubyRequestType type = ruby_request.type;
3086285Snate@binkert.org  Time issued_time = srequest->issue_time;
3096145Snate@binkert.org
3106145Snate@binkert.org  // Set this cache entry to the most recently used
3116285Snate@binkert.org  if (type == RubyRequestType_IFETCH) {
3126285Snate@binkert.org    if (m_instCache_ptr->isTagPresent(request_line_address) )
3136285Snate@binkert.org      m_instCache_ptr->setMRU(request_line_address);
3146145Snate@binkert.org  } else {
3156285Snate@binkert.org    if (m_dataCache_ptr->isTagPresent(request_line_address) )
3166285Snate@binkert.org      m_dataCache_ptr->setMRU(request_line_address);
3176145Snate@binkert.org  }
3186145Snate@binkert.org
3196145Snate@binkert.org  assert(g_eventQueue_ptr->getTime() >= issued_time);
3206145Snate@binkert.org  Time miss_latency = g_eventQueue_ptr->getTime() - issued_time;
3216145Snate@binkert.org
3226285Snate@binkert.org  // Profile the miss latency for all non-zero demand misses
3236285Snate@binkert.org  if (miss_latency != 0) {
3246285Snate@binkert.org    g_system_ptr->getProfiler()->missLatency(miss_latency, type);
3256285Snate@binkert.org
3266285Snate@binkert.org    if (Debug::getProtocolTrace()) {
3276285Snate@binkert.org      g_system_ptr->getProfiler()->profileTransition("Seq", m_version, Address(ruby_request.paddr),
3286285Snate@binkert.org                                                     "", "Done", "", int_to_string(miss_latency)+" cycles");
3296285Snate@binkert.org    }
3306285Snate@binkert.org  }
3316285Snate@binkert.org  /*
3326285Snate@binkert.org  if (request.getPrefetch() == PrefetchBit_Yes) {
3336285Snate@binkert.org    return; // Ignore the prefetch
3346285Snate@binkert.org  }
3356285Snate@binkert.org  */
3366285Snate@binkert.org
3376285Snate@binkert.org  // update the data
3386285Snate@binkert.org  if (ruby_request.data != NULL) {
3396285Snate@binkert.org    if ((type == RubyRequestType_LD) ||
3406381Sdrh5@cs.wisc.edu        (type == RubyRequestType_IFETCH) ||
3416381Sdrh5@cs.wisc.edu        (type == RubyRequestType_RMW_Read)) {
3426285Snate@binkert.org      memcpy(ruby_request.data, data.getData(request_address.getOffset(), ruby_request.len), ruby_request.len);
3436285Snate@binkert.org    } else {
3446285Snate@binkert.org      data.setData(ruby_request.data, request_address.getOffset(), ruby_request.len);
3456285Snate@binkert.org    }
3466145Snate@binkert.org  }
3476145Snate@binkert.org
3486285Snate@binkert.org  m_hit_callback(srequest->id);
3496285Snate@binkert.org  delete srequest;
3506285Snate@binkert.org}
3516285Snate@binkert.org
3526285Snate@binkert.org// Returns true if the sequencer already has a load or store outstanding
3536505Spdudnik@gmail.combool Sequencer::isReady(const RubyRequest& request) {
3546285Snate@binkert.org  // POLINA: check if we are currently flushing the write buffer, if so Ruby is returned as not ready
3556285Snate@binkert.org  // to simulate stalling of the front-end
3566285Snate@binkert.org  // Do we stall all the sequencers? If it is atomic instruction - yes!
3576285Snate@binkert.org  if (m_outstanding_count >= m_max_outstanding_requests) {
3586285Snate@binkert.org    return false;
3596145Snate@binkert.org  }
3606145Snate@binkert.org
3616285Snate@binkert.org  if( m_writeRequestTable.exist(line_address(Address(request.paddr))) ||
3626285Snate@binkert.org      m_readRequestTable.exist(line_address(Address(request.paddr))) ){
3636285Snate@binkert.org    //cout << "OUTSTANDING REQUEST EXISTS " << p << " VER " << m_version << endl;
3646145Snate@binkert.org    //printProgress(cout);
3656145Snate@binkert.org    return false;
3666145Snate@binkert.org  }
3676145Snate@binkert.org
3686505Spdudnik@gmail.com  if (m_servicing_atomic != -1 && m_servicing_atomic != (int)request.proc_id) {
3696505Spdudnik@gmail.com    assert(m_atomics_counter > 0);
3706505Spdudnik@gmail.com    return false;
3716505Spdudnik@gmail.com  }
3726505Spdudnik@gmail.com  else {
3736505Spdudnik@gmail.com    if (request.type == RubyRequestType_RMW_Read) {
3746505Spdudnik@gmail.com      if (m_servicing_atomic == -1) {
3756505Spdudnik@gmail.com        assert(m_atomics_counter == 0);
3766505Spdudnik@gmail.com        m_servicing_atomic = (int)request.proc_id;
3776505Spdudnik@gmail.com      }
3786505Spdudnik@gmail.com      else {
3796505Spdudnik@gmail.com        assert(m_servicing_atomic == (int)request.proc_id);
3806505Spdudnik@gmail.com      }
3816505Spdudnik@gmail.com      m_atomics_counter++;
3826505Spdudnik@gmail.com    }
3836505Spdudnik@gmail.com    else if (request.type == RubyRequestType_RMW_Write) {
3846505Spdudnik@gmail.com      assert(m_servicing_atomic == (int)request.proc_id);
3856505Spdudnik@gmail.com      assert(m_atomics_counter > 0);
3866505Spdudnik@gmail.com      m_atomics_counter--;
3876505Spdudnik@gmail.com      if (m_atomics_counter == 0) {
3886505Spdudnik@gmail.com        m_servicing_atomic = -1;
3896505Spdudnik@gmail.com      }
3906505Spdudnik@gmail.com    }
3916505Spdudnik@gmail.com  }
3926505Spdudnik@gmail.com
3936145Snate@binkert.org  return true;
3946145Snate@binkert.org}
3956145Snate@binkert.org
3966285Snate@binkert.orgbool Sequencer::empty() const {
3976285Snate@binkert.org  return (m_writeRequestTable.size() == 0) && (m_readRequestTable.size() == 0);
3986145Snate@binkert.org}
3996145Snate@binkert.org
4006349Spdudnik@gmail.com
4016285Snate@binkert.orgint64_t Sequencer::makeRequest(const RubyRequest & request)
4026285Snate@binkert.org{
4036285Snate@binkert.org  assert(Address(request.paddr).getOffset() + request.len <= RubySystem::getBlockSizeBytes());
4046285Snate@binkert.org  if (isReady(request)) {
4056285Snate@binkert.org    int64_t id = makeUniqueRequestID();
4066285Snate@binkert.org    SequencerRequest *srequest = new SequencerRequest(request, id, g_eventQueue_ptr->getTime());
4076285Snate@binkert.org    bool found = insertRequest(srequest);
4086285Snate@binkert.org    if (!found)
4096350Spdudnik@gmail.com      if (request.type == RubyRequestType_Locked_Write) {
4106355Spdudnik@gmail.com        // NOTE: it is OK to check the locked flag here as the mandatory queue will be checked first
4116355Spdudnik@gmail.com        // ensuring that nothing comes between checking the flag and servicing the store
4126349Spdudnik@gmail.com        if (!m_dataCache_ptr->isLocked(line_address(Address(request.paddr)), m_version)) {
4136355Spdudnik@gmail.com          return LLSC_FAIL;
4146349Spdudnik@gmail.com        }
4156349Spdudnik@gmail.com        else {
4166349Spdudnik@gmail.com          m_dataCache_ptr->clearLocked(line_address(Address(request.paddr)));
4176349Spdudnik@gmail.com        }
4186349Spdudnik@gmail.com      }
4196285Snate@binkert.org      issueRequest(request);
4206145Snate@binkert.org
4216285Snate@binkert.org    // TODO: issue hardware prefetches here
4226285Snate@binkert.org    return id;
4236145Snate@binkert.org  }
4246285Snate@binkert.org  else {
4256285Snate@binkert.org    return -1;
4266145Snate@binkert.org  }
4276145Snate@binkert.org}
4286145Snate@binkert.org
4296285Snate@binkert.orgvoid Sequencer::issueRequest(const RubyRequest& request) {
4306285Snate@binkert.org
4316285Snate@binkert.org  // TODO: get rid of CacheMsg, CacheRequestType, and AccessModeTYpe, & have SLICC use RubyRequest and subtypes natively
4326285Snate@binkert.org  CacheRequestType ctype;
4336285Snate@binkert.org  switch(request.type) {
4346285Snate@binkert.org  case RubyRequestType_IFETCH:
4356285Snate@binkert.org    ctype = CacheRequestType_IFETCH;
4366285Snate@binkert.org    break;
4376285Snate@binkert.org  case RubyRequestType_LD:
4386285Snate@binkert.org    ctype = CacheRequestType_LD;
4396285Snate@binkert.org    break;
4406285Snate@binkert.org  case RubyRequestType_ST:
4416285Snate@binkert.org    ctype = CacheRequestType_ST;
4426285Snate@binkert.org    break;
4436350Spdudnik@gmail.com  case RubyRequestType_Locked_Read:
4446353Spdudnik@gmail.com    ctype = CacheRequestType_ST;
4456347Spdudnik@gmail.com    break;
4466350Spdudnik@gmail.com  case RubyRequestType_Locked_Write:
4476353Spdudnik@gmail.com    ctype = CacheRequestType_ST;
4486285Snate@binkert.org    break;
4496355Spdudnik@gmail.com  case RubyRequestType_RMW_Read:
4506355Spdudnik@gmail.com    ctype = CacheRequestType_ATOMIC;
4516355Spdudnik@gmail.com    break;
4526355Spdudnik@gmail.com  case RubyRequestType_RMW_Write:
4536355Spdudnik@gmail.com    ctype = CacheRequestType_ATOMIC;
4546355Spdudnik@gmail.com    break;
4556285Snate@binkert.org  default:
4566285Snate@binkert.org    assert(0);
4576145Snate@binkert.org  }
4586285Snate@binkert.org  AccessModeType amtype;
4596285Snate@binkert.org  switch(request.access_mode){
4606285Snate@binkert.org  case RubyAccessMode_User:
4616285Snate@binkert.org    amtype = AccessModeType_UserMode;
4626285Snate@binkert.org    break;
4636285Snate@binkert.org  case RubyAccessMode_Supervisor:
4646285Snate@binkert.org    amtype = AccessModeType_SupervisorMode;
4656285Snate@binkert.org    break;
4666285Snate@binkert.org  case RubyAccessMode_Device:
4676285Snate@binkert.org    amtype = AccessModeType_UserMode;
4686285Snate@binkert.org    break;
4696285Snate@binkert.org  default:
4706285Snate@binkert.org    assert(0);
4716285Snate@binkert.org  }
4726285Snate@binkert.org  Address line_addr(request.paddr);
4736285Snate@binkert.org  line_addr.makeLineAddress();
4746505Spdudnik@gmail.com  CacheMsg msg(line_addr, Address(request.paddr), ctype, Address(request.pc), amtype, request.len, PrefetchBit_No, request.proc_id);
4756285Snate@binkert.org
4766285Snate@binkert.org  if (Debug::getProtocolTrace()) {
4776285Snate@binkert.org    g_system_ptr->getProfiler()->profileTransition("Seq", m_version, Address(request.paddr),
4786285Snate@binkert.org                                                   "", "Begin", "", RubyRequestType_to_string(request.type));
4796285Snate@binkert.org  }
4806285Snate@binkert.org
4816285Snate@binkert.org  if (g_system_ptr->getTracer()->traceEnabled()) {
4826285Snate@binkert.org    g_system_ptr->getTracer()->traceRequest(m_name, line_addr, Address(request.pc),
4836285Snate@binkert.org                                            request.type, g_eventQueue_ptr->getTime());
4846285Snate@binkert.org  }
4856285Snate@binkert.org
4866285Snate@binkert.org  Time latency = 0;  // initialzed to an null value
4876285Snate@binkert.org
4886285Snate@binkert.org  if (request.type == RubyRequestType_IFETCH)
4896285Snate@binkert.org    latency = m_instCache_ptr->getLatency();
4906285Snate@binkert.org  else
4916285Snate@binkert.org    latency = m_dataCache_ptr->getLatency();
4926285Snate@binkert.org
4936285Snate@binkert.org  // Send the message to the cache controller
4946285Snate@binkert.org  assert(latency > 0);
4956285Snate@binkert.org
4966285Snate@binkert.org
4976285Snate@binkert.org  m_mandatory_q_ptr->enqueue(msg, latency);
4986285Snate@binkert.org}
4996285Snate@binkert.org/*
5006285Snate@binkert.orgbool Sequencer::tryCacheAccess(const Address& addr, CacheRequestType type,
5016285Snate@binkert.org                               AccessModeType access_mode,
5026285Snate@binkert.org                               int size, DataBlock*& data_ptr) {
5036285Snate@binkert.org  if (type == CacheRequestType_IFETCH) {
5046285Snate@binkert.org    return m_instCache_ptr->tryCacheAccess(line_address(addr), type, data_ptr);
5056285Snate@binkert.org  } else {
5066285Snate@binkert.org    return m_dataCache_ptr->tryCacheAccess(line_address(addr), type, data_ptr);
5076145Snate@binkert.org  }
5086145Snate@binkert.org}
5096285Snate@binkert.org*/
5106145Snate@binkert.org
5116145Snate@binkert.orgvoid Sequencer::print(ostream& out) const {
5126285Snate@binkert.org  out << "[Sequencer: " << m_version
5136145Snate@binkert.org      << ", outstanding requests: " << m_outstanding_count;
5146145Snate@binkert.org
5156285Snate@binkert.org  out << ", read request table: " << m_readRequestTable
5166285Snate@binkert.org      << ", write request table: " << m_writeRequestTable;
5176145Snate@binkert.org  out << "]";
5186145Snate@binkert.org}
5196145Snate@binkert.org
5206145Snate@binkert.org// this can be called from setState whenever coherence permissions are upgraded
5216145Snate@binkert.org// when invoked, coherence violations will be checked for the given block
5226145Snate@binkert.orgvoid Sequencer::checkCoherence(const Address& addr) {
5236145Snate@binkert.org#ifdef CHECK_COHERENCE
5246145Snate@binkert.org  g_system_ptr->checkGlobalCoherenceInvariant(addr);
5256145Snate@binkert.org#endif
5266145Snate@binkert.org}
5276145Snate@binkert.org
528