Check.cc revision 8949
16899SN/A/*
26899SN/A * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
36899SN/A * Copyright (c) 2009 Advanced Micro Devices, Inc.
46899SN/A * All rights reserved.
56899SN/A *
66899SN/A * Redistribution and use in source and binary forms, with or without
76899SN/A * modification, are permitted provided that the following conditions are
86899SN/A * met: redistributions of source code must retain the above copyright
96899SN/A * notice, this list of conditions and the following disclaimer;
106899SN/A * redistributions in binary form must reproduce the above copyright
116899SN/A * notice, this list of conditions and the following disclaimer in the
126899SN/A * documentation and/or other materials provided with the distribution;
136899SN/A * neither the name of the copyright holders nor the names of its
146899SN/A * contributors may be used to endorse or promote products derived from
156899SN/A * this software without specific prior written permission.
166899SN/A *
176899SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186899SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196899SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206899SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216899SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226899SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236899SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246899SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256899SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266899SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276899SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286899SN/A */
296899SN/A
307632SBrad.Beckmann@amd.com#include "cpu/testers/rubytest/Check.hh"
318232Snate@binkert.org#include "debug/RubyTest.hh"
327053SN/A#include "mem/ruby/common/SubBlock.hh"
336899SN/A#include "mem/ruby/system/Sequencer.hh"
346899SN/A#include "mem/ruby/system/System.hh"
356899SN/A
367053SN/Atypedef RubyTester::SenderState SenderState;
377053SN/A
387053SN/ACheck::Check(const Address& address, const Address& pc,
398932SBrad.Beckmann@amd.com             int _num_writers, int _num_readers, RubyTester* _tester)
408932SBrad.Beckmann@amd.com    : m_num_writers(_num_writers), m_num_readers(_num_readers),
418932SBrad.Beckmann@amd.com      m_tester_ptr(_tester)
426899SN/A{
437053SN/A    m_status = TesterStatus_Idle;
446899SN/A
457053SN/A    pickValue();
467053SN/A    pickInitiatingNode();
477053SN/A    changeAddress(address);
487053SN/A    m_pc = pc;
498164Snilay@cs.wisc.edu    m_access_mode = RubyAccessMode(random() % RubyAccessMode_NUM);
507053SN/A    m_store_count = 0;
516899SN/A}
526899SN/A
537053SN/Avoid
547053SN/ACheck::initiate()
556899SN/A{
567053SN/A    DPRINTF(RubyTest, "initiating\n");
577053SN/A    debugPrint();
586899SN/A
597053SN/A    // currently no protocols support prefetches
607053SN/A    if (false && (random() & 0xf) == 0) {
617053SN/A        initiatePrefetch(); // Prefetch from random processor
627053SN/A    }
636899SN/A
648184Ssomayeh@cs.wisc.edu    if (m_tester_ptr->getCheckFlush() && (random() & 0xff) == 0) {
658184Ssomayeh@cs.wisc.edu        initiateFlush(); // issue a Flush request from random processor
668184Ssomayeh@cs.wisc.edu    }
678184Ssomayeh@cs.wisc.edu
687053SN/A    if (m_status == TesterStatus_Idle) {
697053SN/A        initiateAction();
707053SN/A    } else if (m_status == TesterStatus_Ready) {
717053SN/A        initiateCheck();
727053SN/A    } else {
737053SN/A        // Pending - do nothing
747053SN/A        DPRINTF(RubyTest,
757053SN/A                "initiating action/check - failed: action/check is pending\n");
767053SN/A    }
776899SN/A}
786899SN/A
797053SN/Avoid
807053SN/ACheck::initiatePrefetch()
816899SN/A{
827053SN/A    DPRINTF(RubyTest, "initiating prefetch\n");
836899SN/A
848932SBrad.Beckmann@amd.com    int index = random() % m_num_readers;
857053SN/A    RubyTester::CpuPort* port =
868932SBrad.Beckmann@amd.com      safe_cast<RubyTester::CpuPort*>(m_tester_ptr->getReadableCpuPort(index));
876899SN/A
887053SN/A    Request::Flags flags;
897053SN/A    flags.set(Request::PREFETCH);
906899SN/A
917053SN/A    Packet::Command cmd;
926899SN/A
937053SN/A    // 1 in 8 chance this will be an exclusive prefetch
947053SN/A    if ((random() & 0x7) != 0) {
957053SN/A        cmd = MemCmd::ReadReq;
967053SN/A
978932SBrad.Beckmann@amd.com        // if necessary, make the request an instruction fetch
988932SBrad.Beckmann@amd.com        if (port->type == RubyTester::CpuPort::InstOnly) {
997053SN/A            flags.set(Request::INST_FETCH);
1007053SN/A        }
1017053SN/A    } else {
1027053SN/A        cmd = MemCmd::WriteReq;
1037053SN/A        flags.set(Request::PF_EXCLUSIVE);
1046899SN/A    }
1056899SN/A
1067568SN/A    // Prefetches are assumed to be 0 sized
1078832SAli.Saidi@ARM.com    Request *req = new Request(m_address.getAddress(), 0, flags,
1088832SAli.Saidi@ARM.com            m_tester_ptr->masterId(), curTick(), m_pc.getAddress());
1098190SLisa.Hsu@amd.com    req->setThreadContext(index, 0);
1107568SN/A
1118949Sandreas.hansson@arm.com    PacketPtr pkt = new Packet(req, cmd);
1126899SN/A
1137053SN/A    // push the subblock onto the sender state.  The sequencer will
1147053SN/A    // update the subblock on the return
1157053SN/A    pkt->senderState =
1167053SN/A        new SenderState(m_address, req->getSize(), pkt->senderState);
1176899SN/A
1187053SN/A    if (port->sendTiming(pkt)) {
1197053SN/A        DPRINTF(RubyTest, "successfully initiated prefetch.\n");
1207053SN/A    } else {
1217053SN/A        // If the packet did not issue, must delete
1227053SN/A        SenderState* senderState =  safe_cast<SenderState*>(pkt->senderState);
1237053SN/A        pkt->senderState = senderState->saved;
1247053SN/A        delete senderState;
1257053SN/A        delete pkt->req;
1267053SN/A        delete pkt;
1276899SN/A
1287053SN/A        DPRINTF(RubyTest,
1297053SN/A                "prefetch initiation failed because Port was busy.\n");
1307053SN/A    }
1316899SN/A}
1326899SN/A
1337053SN/Avoid
1348184Ssomayeh@cs.wisc.eduCheck::initiateFlush()
1358184Ssomayeh@cs.wisc.edu{
1368184Ssomayeh@cs.wisc.edu
1378184Ssomayeh@cs.wisc.edu    DPRINTF(RubyTest, "initiating Flush\n");
1388184Ssomayeh@cs.wisc.edu
1398932SBrad.Beckmann@amd.com    int index = random() % m_num_writers;
1408184Ssomayeh@cs.wisc.edu    RubyTester::CpuPort* port =
1418932SBrad.Beckmann@amd.com      safe_cast<RubyTester::CpuPort*>(m_tester_ptr->getWritableCpuPort(index));
1428184Ssomayeh@cs.wisc.edu
1438184Ssomayeh@cs.wisc.edu    Request::Flags flags;
1448184Ssomayeh@cs.wisc.edu
1458832SAli.Saidi@ARM.com    Request *req = new Request(m_address.getAddress(), CHECK_SIZE, flags,
1468832SAli.Saidi@ARM.com            m_tester_ptr->masterId(), curTick(), m_pc.getAddress());
1478184Ssomayeh@cs.wisc.edu
1488184Ssomayeh@cs.wisc.edu    Packet::Command cmd;
1498184Ssomayeh@cs.wisc.edu
1508184Ssomayeh@cs.wisc.edu    cmd = MemCmd::FlushReq;
1518184Ssomayeh@cs.wisc.edu
1528949Sandreas.hansson@arm.com    PacketPtr pkt = new Packet(req, cmd);
1538184Ssomayeh@cs.wisc.edu
1548184Ssomayeh@cs.wisc.edu    // push the subblock onto the sender state.  The sequencer will
1558184Ssomayeh@cs.wisc.edu    // update the subblock on the return
1568184Ssomayeh@cs.wisc.edu    pkt->senderState =
1578184Ssomayeh@cs.wisc.edu        new SenderState(m_address, req->getSize(), pkt->senderState);
1588184Ssomayeh@cs.wisc.edu
1598184Ssomayeh@cs.wisc.edu    if (port->sendTiming(pkt)) {
1608184Ssomayeh@cs.wisc.edu        DPRINTF(RubyTest, "initiating Flush - successful\n");
1618184Ssomayeh@cs.wisc.edu    }
1628184Ssomayeh@cs.wisc.edu}
1638184Ssomayeh@cs.wisc.edu
1648184Ssomayeh@cs.wisc.eduvoid
1657053SN/ACheck::initiateAction()
1666899SN/A{
1677053SN/A    DPRINTF(RubyTest, "initiating Action\n");
1687053SN/A    assert(m_status == TesterStatus_Idle);
1696899SN/A
1708932SBrad.Beckmann@amd.com    int index = random() % m_num_writers;
1717053SN/A    RubyTester::CpuPort* port =
1728932SBrad.Beckmann@amd.com      safe_cast<RubyTester::CpuPort*>(m_tester_ptr->getWritableCpuPort(index));
1736899SN/A
1747053SN/A    Request::Flags flags;
1756899SN/A
1767053SN/A    // Create the particular address for the next byte to be written
1777053SN/A    Address writeAddr(m_address.getAddress() + m_store_count);
1786899SN/A
1797053SN/A    // Stores are assumed to be 1 byte-sized
1808832SAli.Saidi@ARM.com    Request *req = new Request(writeAddr.getAddress(), 1, flags,
1818832SAli.Saidi@ARM.com            m_tester_ptr->masterId(), curTick(),
1827053SN/A                               m_pc.getAddress());
1836899SN/A
1848190SLisa.Hsu@amd.com    req->setThreadContext(index, 0);
1857053SN/A    Packet::Command cmd;
1867053SN/A
1877053SN/A    // 1 out of 8 chance, issue an atomic rather than a write
1887053SN/A    // if ((random() & 0x7) == 0) {
1897053SN/A    //     cmd = MemCmd::SwapReq;
1907053SN/A    // } else {
1916899SN/A    cmd = MemCmd::WriteReq;
1927053SN/A    // }
1936899SN/A
1948949Sandreas.hansson@arm.com    PacketPtr pkt = new Packet(req, cmd);
1957053SN/A    uint8_t* writeData = new uint8_t;
1967053SN/A    *writeData = m_value + m_store_count;
1977053SN/A    pkt->dataDynamic(writeData);
1986899SN/A
1997053SN/A    DPRINTF(RubyTest, "data 0x%x check 0x%x\n",
2007053SN/A            *(pkt->getPtr<uint8_t>()), *writeData);
2016899SN/A
2027053SN/A    // push the subblock onto the sender state.  The sequencer will
2037053SN/A    // update the subblock on the return
2047053SN/A    pkt->senderState =
2057053SN/A        new SenderState(writeAddr, req->getSize(), pkt->senderState);
2066899SN/A
2077053SN/A    if (port->sendTiming(pkt)) {
2087053SN/A        DPRINTF(RubyTest, "initiating action - successful\n");
2097053SN/A        DPRINTF(RubyTest, "status before action update: %s\n",
2107053SN/A                (TesterStatus_to_string(m_status)).c_str());
2117053SN/A        m_status = TesterStatus_Action_Pending;
2127053SN/A    } else {
2137053SN/A        // If the packet did not issue, must delete
2147053SN/A        // Note: No need to delete the data, the packet destructor
2157053SN/A        // will delete it
2167053SN/A        SenderState* senderState = safe_cast<SenderState*>(pkt->senderState);
2177053SN/A        pkt->senderState = senderState->saved;
2187053SN/A        delete senderState;
2197053SN/A        delete pkt->req;
2207053SN/A        delete pkt;
2217053SN/A
2227053SN/A        DPRINTF(RubyTest, "failed to initiate action - sequencer not ready\n");
2237053SN/A    }
2247053SN/A
2257053SN/A    DPRINTF(RubyTest, "status after action update: %s\n",
2266899SN/A            (TesterStatus_to_string(m_status)).c_str());
2276899SN/A}
2286899SN/A
2297053SN/Avoid
2307053SN/ACheck::initiateCheck()
2316899SN/A{
2327053SN/A    DPRINTF(RubyTest, "Initiating Check\n");
2337053SN/A    assert(m_status == TesterStatus_Ready);
2346899SN/A
2358932SBrad.Beckmann@amd.com    int index = random() % m_num_readers;
2367053SN/A    RubyTester::CpuPort* port =
2378932SBrad.Beckmann@amd.com      safe_cast<RubyTester::CpuPort*>(m_tester_ptr->getReadableCpuPort(index));
2386899SN/A
2397053SN/A    Request::Flags flags;
2406899SN/A
2418932SBrad.Beckmann@amd.com    // If necessary, make the request an instruction fetch
2428932SBrad.Beckmann@amd.com    if (port->type == RubyTester::CpuPort::InstOnly) {
2437053SN/A        flags.set(Request::INST_FETCH);
2447053SN/A    }
2456899SN/A
2467568SN/A    // Checks are sized depending on the number of bytes written
2477568SN/A    Request *req = new Request(m_address.getAddress(), CHECK_SIZE, flags,
2488832SAli.Saidi@ARM.com                               m_tester_ptr->masterId(), curTick(), m_pc.getAddress());
2497568SN/A
2508190SLisa.Hsu@amd.com    req->setThreadContext(index, 0);
2518949Sandreas.hansson@arm.com    PacketPtr pkt = new Packet(req, MemCmd::ReadReq);
2527053SN/A    uint8_t* dataArray = new uint8_t[CHECK_SIZE];
2537053SN/A    pkt->dataDynamicArray(dataArray);
2546899SN/A
2557053SN/A    // push the subblock onto the sender state.  The sequencer will
2567053SN/A    // update the subblock on the return
2577053SN/A    pkt->senderState =
2587053SN/A        new SenderState(m_address, req->getSize(), pkt->senderState);
2596899SN/A
2607053SN/A    if (port->sendTiming(pkt)) {
2617053SN/A        DPRINTF(RubyTest, "initiating check - successful\n");
2627053SN/A        DPRINTF(RubyTest, "status before check update: %s\n",
2637053SN/A                TesterStatus_to_string(m_status).c_str());
2647053SN/A        m_status = TesterStatus_Check_Pending;
2657053SN/A    } else {
2667053SN/A        // If the packet did not issue, must delete
2677053SN/A        // Note: No need to delete the data, the packet destructor
2687053SN/A        // will delete it
2697053SN/A        SenderState* senderState = safe_cast<SenderState*>(pkt->senderState);
2707053SN/A        pkt->senderState = senderState->saved;
2717053SN/A        delete senderState;
2727053SN/A        delete pkt->req;
2737053SN/A        delete pkt;
2746899SN/A
2757053SN/A        DPRINTF(RubyTest, "failed to initiate check - cpu port not ready\n");
2767053SN/A    }
2777053SN/A
2787053SN/A    DPRINTF(RubyTest, "status after check update: %s\n",
2797053SN/A            TesterStatus_to_string(m_status).c_str());
2806899SN/A}
2816899SN/A
2827053SN/Avoid
2837053SN/ACheck::performCallback(NodeID proc, SubBlock* data)
2846899SN/A{
2857053SN/A    Address address = data->getAddress();
2866899SN/A
2877053SN/A    // This isn't exactly right since we now have multi-byte checks
2887053SN/A    //  assert(getAddress() == address);
2896899SN/A
2907053SN/A    assert(getAddress().getLineAddress() == address.getLineAddress());
2917053SN/A    assert(data != NULL);
2927053SN/A
2937053SN/A    DPRINTF(RubyTest, "RubyTester Callback\n");
2946899SN/A    debugPrint();
2956899SN/A
2967053SN/A    if (m_status == TesterStatus_Action_Pending) {
2977053SN/A        DPRINTF(RubyTest, "Action callback write value: %d, currently %d\n",
2987053SN/A                (m_value + m_store_count), data->getByte(0));
2997053SN/A        // Perform store one byte at a time
3007053SN/A        data->setByte(0, (m_value + m_store_count));
3017053SN/A        m_store_count++;
3027053SN/A        if (m_store_count == CHECK_SIZE) {
3037053SN/A            m_status = TesterStatus_Ready;
3047053SN/A        } else {
3057053SN/A            m_status = TesterStatus_Idle;
3067053SN/A        }
3077053SN/A        DPRINTF(RubyTest, "Action callback return data now %d\n",
3087053SN/A                data->getByte(0));
3097053SN/A    } else if (m_status == TesterStatus_Check_Pending) {
3107053SN/A        DPRINTF(RubyTest, "Check callback\n");
3117053SN/A        // Perform load/check
3127053SN/A        for (int byte_number=0; byte_number<CHECK_SIZE; byte_number++) {
3137053SN/A            if (uint8(m_value + byte_number) != data->getByte(byte_number)) {
3147805Snilay@cs.wisc.edu                panic("Action/check failure: proc: %d address: %s data: %s "
3157805Snilay@cs.wisc.edu                      "byte_number: %d m_value+byte_number: %d byte: %d %s"
3167805Snilay@cs.wisc.edu                      "Time: %d\n",
3177805Snilay@cs.wisc.edu                      proc, address, data, byte_number,
3187805Snilay@cs.wisc.edu                      (int)m_value + byte_number,
3197805Snilay@cs.wisc.edu                      (int)data->getByte(byte_number), *this,
3207805Snilay@cs.wisc.edu                      g_eventQueue_ptr->getTime());
3217053SN/A            }
3227053SN/A        }
3237053SN/A        DPRINTF(RubyTest, "Action/check success\n");
3247053SN/A        debugPrint();
3256899SN/A
3267053SN/A        // successful check complete, increment complete
3277053SN/A        m_tester_ptr->incrementCheckCompletions();
3286899SN/A
3297053SN/A        m_status = TesterStatus_Idle;
3307053SN/A        pickValue();
3317053SN/A
3327053SN/A    } else {
3337805Snilay@cs.wisc.edu        panic("Unexpected TesterStatus: %s proc: %d data: %s m_status: %s "
3347805Snilay@cs.wisc.edu              "time: %d\n",
3357805Snilay@cs.wisc.edu              *this, proc, data, m_status, g_eventQueue_ptr->getTime());
3367053SN/A    }
3377053SN/A
3387053SN/A    DPRINTF(RubyTest, "proc: %d, Address: 0x%x\n", proc,
3397053SN/A            getAddress().getLineAddress());
3407053SN/A    DPRINTF(RubyTest, "Callback done\n");
3417053SN/A    debugPrint();
3426899SN/A}
3436899SN/A
3447053SN/Avoid
3457053SN/ACheck::changeAddress(const Address& address)
3466899SN/A{
3477053SN/A    assert(m_status == TesterStatus_Idle || m_status == TesterStatus_Ready);
3487053SN/A    m_status = TesterStatus_Idle;
3497053SN/A    m_address = address;
3507053SN/A    m_store_count = 0;
3516899SN/A}
3526899SN/A
3537053SN/Avoid
3547053SN/ACheck::pickValue()
3556899SN/A{
3567053SN/A    assert(m_status == TesterStatus_Idle);
3577053SN/A    m_status = TesterStatus_Idle;
3587053SN/A    m_value = random() & 0xff; // One byte
3597053SN/A    m_store_count = 0;
3606899SN/A}
3616899SN/A
3627053SN/Avoid
3637053SN/ACheck::pickInitiatingNode()
3646899SN/A{
3657053SN/A    assert(m_status == TesterStatus_Idle || m_status == TesterStatus_Ready);
3667053SN/A    m_status = TesterStatus_Idle;
3678932SBrad.Beckmann@amd.com    m_initiatingNode = (random() % m_num_writers);
3687053SN/A    DPRINTF(RubyTest, "picked initiating node %d\n", m_initiatingNode);
3697053SN/A    m_store_count = 0;
3706899SN/A}
3716899SN/A
3727053SN/Avoid
3737055SN/ACheck::print(std::ostream& out) const
3746899SN/A{
3757053SN/A    out << "["
3767053SN/A        << m_address << ", value: "
3777053SN/A        << (int)m_value << ", status: "
3787053SN/A        << m_status << ", initiating node: "
3797053SN/A        << m_initiatingNode << ", store_count: "
3807053SN/A        << m_store_count
3817055SN/A        << "]" << std::flush;
3826899SN/A}
3836899SN/A
3847053SN/Avoid
3857053SN/ACheck::debugPrint()
3866899SN/A{
3877053SN/A    DPRINTF(RubyTest,
3887053SN/A        "[%#x, value: %d, status: %s, initiating node: %d, store_count: %d]\n",
3897053SN/A        m_address.getAddress(), (int)m_value,
3907053SN/A        TesterStatus_to_string(m_status).c_str(),
3917053SN/A        m_initiatingNode, m_store_count);
3926899SN/A}
393