Check.cc revision 8232
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,
397053SN/A             int _num_cpu_sequencers, RubyTester* _tester)
407053SN/A    : m_num_cpu_sequencers(_num_cpu_sequencers), m_tester_ptr(_tester)
416899SN/A{
427053SN/A    m_status = TesterStatus_Idle;
436899SN/A
447053SN/A    pickValue();
457053SN/A    pickInitiatingNode();
467053SN/A    changeAddress(address);
477053SN/A    m_pc = pc;
488164Snilay@cs.wisc.edu    m_access_mode = RubyAccessMode(random() % RubyAccessMode_NUM);
497053SN/A    m_store_count = 0;
506899SN/A}
516899SN/A
527053SN/Avoid
537053SN/ACheck::initiate()
546899SN/A{
557053SN/A    DPRINTF(RubyTest, "initiating\n");
567053SN/A    debugPrint();
576899SN/A
587053SN/A    // currently no protocols support prefetches
597053SN/A    if (false && (random() & 0xf) == 0) {
607053SN/A        initiatePrefetch(); // Prefetch from random processor
617053SN/A    }
626899SN/A
638184Ssomayeh@cs.wisc.edu    if (m_tester_ptr->getCheckFlush() && (random() & 0xff) == 0) {
648184Ssomayeh@cs.wisc.edu        initiateFlush(); // issue a Flush request from random processor
658184Ssomayeh@cs.wisc.edu    }
668184Ssomayeh@cs.wisc.edu
677053SN/A    if (m_status == TesterStatus_Idle) {
687053SN/A        initiateAction();
697053SN/A    } else if (m_status == TesterStatus_Ready) {
707053SN/A        initiateCheck();
717053SN/A    } else {
727053SN/A        // Pending - do nothing
737053SN/A        DPRINTF(RubyTest,
747053SN/A                "initiating action/check - failed: action/check is pending\n");
757053SN/A    }
766899SN/A}
776899SN/A
787053SN/Avoid
797053SN/ACheck::initiatePrefetch()
806899SN/A{
817053SN/A    DPRINTF(RubyTest, "initiating prefetch\n");
826899SN/A
837053SN/A    int index = random() % m_num_cpu_sequencers;
847053SN/A    RubyTester::CpuPort* port =
857053SN/A        safe_cast<RubyTester::CpuPort*>(m_tester_ptr->getCpuPort(index));
866899SN/A
877053SN/A    Request::Flags flags;
887053SN/A    flags.set(Request::PREFETCH);
896899SN/A
907053SN/A    Packet::Command cmd;
916899SN/A
927053SN/A    // 1 in 8 chance this will be an exclusive prefetch
937053SN/A    if ((random() & 0x7) != 0) {
947053SN/A        cmd = MemCmd::ReadReq;
957053SN/A
967053SN/A        // 50% chance that the request will be an instruction fetch
977053SN/A        if ((random() & 0x1) == 0) {
987053SN/A            flags.set(Request::INST_FETCH);
997053SN/A        }
1007053SN/A    } else {
1017053SN/A        cmd = MemCmd::WriteReq;
1027053SN/A        flags.set(Request::PF_EXCLUSIVE);
1036899SN/A    }
1046899SN/A
1057568SN/A    // Prefetches are assumed to be 0 sized
1067823Ssteve.reinhardt@amd.com    Request *req = new Request(m_address.getAddress(), 0, flags, curTick(),
1077568SN/A                               m_pc.getAddress());
1088190SLisa.Hsu@amd.com    req->setThreadContext(index, 0);
1097568SN/A
1107053SN/A    PacketPtr pkt = new Packet(req, cmd, port->idx);
1116899SN/A
1127053SN/A    // push the subblock onto the sender state.  The sequencer will
1137053SN/A    // update the subblock on the return
1147053SN/A    pkt->senderState =
1157053SN/A        new SenderState(m_address, req->getSize(), pkt->senderState);
1166899SN/A
1177053SN/A    if (port->sendTiming(pkt)) {
1187053SN/A        DPRINTF(RubyTest, "successfully initiated prefetch.\n");
1197053SN/A    } else {
1207053SN/A        // If the packet did not issue, must delete
1217053SN/A        SenderState* senderState =  safe_cast<SenderState*>(pkt->senderState);
1227053SN/A        pkt->senderState = senderState->saved;
1237053SN/A        delete senderState;
1247053SN/A        delete pkt->req;
1257053SN/A        delete pkt;
1266899SN/A
1277053SN/A        DPRINTF(RubyTest,
1287053SN/A                "prefetch initiation failed because Port was busy.\n");
1297053SN/A    }
1306899SN/A}
1316899SN/A
1327053SN/Avoid
1338184Ssomayeh@cs.wisc.eduCheck::initiateFlush()
1348184Ssomayeh@cs.wisc.edu{
1358184Ssomayeh@cs.wisc.edu
1368184Ssomayeh@cs.wisc.edu    DPRINTF(RubyTest, "initiating Flush\n");
1378184Ssomayeh@cs.wisc.edu
1388184Ssomayeh@cs.wisc.edu    int index = random() % m_num_cpu_sequencers;
1398184Ssomayeh@cs.wisc.edu    RubyTester::CpuPort* port =
1408184Ssomayeh@cs.wisc.edu        safe_cast<RubyTester::CpuPort*>(m_tester_ptr->getCpuPort(index));
1418184Ssomayeh@cs.wisc.edu
1428184Ssomayeh@cs.wisc.edu    Request::Flags flags;
1438184Ssomayeh@cs.wisc.edu
1448184Ssomayeh@cs.wisc.edu    Request *req = new Request(m_address.getAddress(), CHECK_SIZE, flags, curTick(),
1458184Ssomayeh@cs.wisc.edu                               m_pc.getAddress());
1468184Ssomayeh@cs.wisc.edu
1478184Ssomayeh@cs.wisc.edu    Packet::Command cmd;
1488184Ssomayeh@cs.wisc.edu
1498184Ssomayeh@cs.wisc.edu    cmd = MemCmd::FlushReq;
1508184Ssomayeh@cs.wisc.edu
1518184Ssomayeh@cs.wisc.edu    PacketPtr pkt = new Packet(req, cmd, port->idx);
1528184Ssomayeh@cs.wisc.edu
1538184Ssomayeh@cs.wisc.edu    // push the subblock onto the sender state.  The sequencer will
1548184Ssomayeh@cs.wisc.edu    // update the subblock on the return
1558184Ssomayeh@cs.wisc.edu    pkt->senderState =
1568184Ssomayeh@cs.wisc.edu        new SenderState(m_address, req->getSize(), pkt->senderState);
1578184Ssomayeh@cs.wisc.edu
1588184Ssomayeh@cs.wisc.edu    if (port->sendTiming(pkt)) {
1598184Ssomayeh@cs.wisc.edu        DPRINTF(RubyTest, "initiating Flush - successful\n");
1608184Ssomayeh@cs.wisc.edu    }
1618184Ssomayeh@cs.wisc.edu}
1628184Ssomayeh@cs.wisc.edu
1638184Ssomayeh@cs.wisc.eduvoid
1647053SN/ACheck::initiateAction()
1656899SN/A{
1667053SN/A    DPRINTF(RubyTest, "initiating Action\n");
1677053SN/A    assert(m_status == TesterStatus_Idle);
1686899SN/A
1697053SN/A    int index = random() % m_num_cpu_sequencers;
1707053SN/A    RubyTester::CpuPort* port =
1717053SN/A        safe_cast<RubyTester::CpuPort*>(m_tester_ptr->getCpuPort(index));
1726899SN/A
1737053SN/A    Request::Flags flags;
1746899SN/A
1757053SN/A    // Create the particular address for the next byte to be written
1767053SN/A    Address writeAddr(m_address.getAddress() + m_store_count);
1776899SN/A
1787053SN/A    // Stores are assumed to be 1 byte-sized
1797823Ssteve.reinhardt@amd.com    Request *req = new Request(writeAddr.getAddress(), 1, flags, curTick(),
1807053SN/A                               m_pc.getAddress());
1816899SN/A
1828190SLisa.Hsu@amd.com    req->setThreadContext(index, 0);
1837053SN/A    Packet::Command cmd;
1847053SN/A
1857053SN/A    // 1 out of 8 chance, issue an atomic rather than a write
1867053SN/A    // if ((random() & 0x7) == 0) {
1877053SN/A    //     cmd = MemCmd::SwapReq;
1887053SN/A    // } else {
1896899SN/A    cmd = MemCmd::WriteReq;
1907053SN/A    // }
1916899SN/A
1927053SN/A    PacketPtr pkt = new Packet(req, cmd, port->idx);
1937053SN/A    uint8_t* writeData = new uint8_t;
1947053SN/A    *writeData = m_value + m_store_count;
1957053SN/A    pkt->dataDynamic(writeData);
1966899SN/A
1977053SN/A    DPRINTF(RubyTest, "data 0x%x check 0x%x\n",
1987053SN/A            *(pkt->getPtr<uint8_t>()), *writeData);
1996899SN/A
2007053SN/A    // push the subblock onto the sender state.  The sequencer will
2017053SN/A    // update the subblock on the return
2027053SN/A    pkt->senderState =
2037053SN/A        new SenderState(writeAddr, req->getSize(), pkt->senderState);
2046899SN/A
2057053SN/A    if (port->sendTiming(pkt)) {
2067053SN/A        DPRINTF(RubyTest, "initiating action - successful\n");
2077053SN/A        DPRINTF(RubyTest, "status before action update: %s\n",
2087053SN/A                (TesterStatus_to_string(m_status)).c_str());
2097053SN/A        m_status = TesterStatus_Action_Pending;
2107053SN/A    } else {
2117053SN/A        // If the packet did not issue, must delete
2127053SN/A        // Note: No need to delete the data, the packet destructor
2137053SN/A        // will delete it
2147053SN/A        SenderState* senderState = safe_cast<SenderState*>(pkt->senderState);
2157053SN/A        pkt->senderState = senderState->saved;
2167053SN/A        delete senderState;
2177053SN/A        delete pkt->req;
2187053SN/A        delete pkt;
2197053SN/A
2207053SN/A        DPRINTF(RubyTest, "failed to initiate action - sequencer not ready\n");
2217053SN/A    }
2227053SN/A
2237053SN/A    DPRINTF(RubyTest, "status after action update: %s\n",
2246899SN/A            (TesterStatus_to_string(m_status)).c_str());
2256899SN/A}
2266899SN/A
2277053SN/Avoid
2287053SN/ACheck::initiateCheck()
2296899SN/A{
2307053SN/A    DPRINTF(RubyTest, "Initiating Check\n");
2317053SN/A    assert(m_status == TesterStatus_Ready);
2326899SN/A
2337053SN/A    int index = random() % m_num_cpu_sequencers;
2347053SN/A    RubyTester::CpuPort* port =
2357053SN/A        safe_cast<RubyTester::CpuPort*>(m_tester_ptr->getCpuPort(index));
2366899SN/A
2377053SN/A    Request::Flags flags;
2386899SN/A
2397053SN/A    // 50% chance that the request will be an instruction fetch
2407053SN/A    if ((random() & 0x1) == 0) {
2417053SN/A        flags.set(Request::INST_FETCH);
2427053SN/A    }
2436899SN/A
2447568SN/A    // Checks are sized depending on the number of bytes written
2457568SN/A    Request *req = new Request(m_address.getAddress(), CHECK_SIZE, flags,
2467823Ssteve.reinhardt@amd.com                               curTick(), m_pc.getAddress());
2477568SN/A
2488190SLisa.Hsu@amd.com    req->setThreadContext(index, 0);
2497053SN/A    PacketPtr pkt = new Packet(req, MemCmd::ReadReq, port->idx);
2507053SN/A    uint8_t* dataArray = new uint8_t[CHECK_SIZE];
2517053SN/A    pkt->dataDynamicArray(dataArray);
2526899SN/A
2537053SN/A    // push the subblock onto the sender state.  The sequencer will
2547053SN/A    // update the subblock on the return
2557053SN/A    pkt->senderState =
2567053SN/A        new SenderState(m_address, req->getSize(), pkt->senderState);
2576899SN/A
2587053SN/A    if (port->sendTiming(pkt)) {
2597053SN/A        DPRINTF(RubyTest, "initiating check - successful\n");
2607053SN/A        DPRINTF(RubyTest, "status before check update: %s\n",
2617053SN/A                TesterStatus_to_string(m_status).c_str());
2627053SN/A        m_status = TesterStatus_Check_Pending;
2637053SN/A    } else {
2647053SN/A        // If the packet did not issue, must delete
2657053SN/A        // Note: No need to delete the data, the packet destructor
2667053SN/A        // will delete it
2677053SN/A        SenderState* senderState = safe_cast<SenderState*>(pkt->senderState);
2687053SN/A        pkt->senderState = senderState->saved;
2697053SN/A        delete senderState;
2707053SN/A        delete pkt->req;
2717053SN/A        delete pkt;
2726899SN/A
2737053SN/A        DPRINTF(RubyTest, "failed to initiate check - cpu port not ready\n");
2747053SN/A    }
2757053SN/A
2767053SN/A    DPRINTF(RubyTest, "status after check update: %s\n",
2777053SN/A            TesterStatus_to_string(m_status).c_str());
2786899SN/A}
2796899SN/A
2807053SN/Avoid
2817053SN/ACheck::performCallback(NodeID proc, SubBlock* data)
2826899SN/A{
2837053SN/A    Address address = data->getAddress();
2846899SN/A
2857053SN/A    // This isn't exactly right since we now have multi-byte checks
2867053SN/A    //  assert(getAddress() == address);
2876899SN/A
2887053SN/A    assert(getAddress().getLineAddress() == address.getLineAddress());
2897053SN/A    assert(data != NULL);
2907053SN/A
2917053SN/A    DPRINTF(RubyTest, "RubyTester Callback\n");
2926899SN/A    debugPrint();
2936899SN/A
2947053SN/A    if (m_status == TesterStatus_Action_Pending) {
2957053SN/A        DPRINTF(RubyTest, "Action callback write value: %d, currently %d\n",
2967053SN/A                (m_value + m_store_count), data->getByte(0));
2977053SN/A        // Perform store one byte at a time
2987053SN/A        data->setByte(0, (m_value + m_store_count));
2997053SN/A        m_store_count++;
3007053SN/A        if (m_store_count == CHECK_SIZE) {
3017053SN/A            m_status = TesterStatus_Ready;
3027053SN/A        } else {
3037053SN/A            m_status = TesterStatus_Idle;
3047053SN/A        }
3057053SN/A        DPRINTF(RubyTest, "Action callback return data now %d\n",
3067053SN/A                data->getByte(0));
3077053SN/A    } else if (m_status == TesterStatus_Check_Pending) {
3087053SN/A        DPRINTF(RubyTest, "Check callback\n");
3097053SN/A        // Perform load/check
3107053SN/A        for (int byte_number=0; byte_number<CHECK_SIZE; byte_number++) {
3117053SN/A            if (uint8(m_value + byte_number) != data->getByte(byte_number)) {
3127805Snilay@cs.wisc.edu                panic("Action/check failure: proc: %d address: %s data: %s "
3137805Snilay@cs.wisc.edu                      "byte_number: %d m_value+byte_number: %d byte: %d %s"
3147805Snilay@cs.wisc.edu                      "Time: %d\n",
3157805Snilay@cs.wisc.edu                      proc, address, data, byte_number,
3167805Snilay@cs.wisc.edu                      (int)m_value + byte_number,
3177805Snilay@cs.wisc.edu                      (int)data->getByte(byte_number), *this,
3187805Snilay@cs.wisc.edu                      g_eventQueue_ptr->getTime());
3197053SN/A            }
3207053SN/A        }
3217053SN/A        DPRINTF(RubyTest, "Action/check success\n");
3227053SN/A        debugPrint();
3236899SN/A
3247053SN/A        // successful check complete, increment complete
3257053SN/A        m_tester_ptr->incrementCheckCompletions();
3266899SN/A
3277053SN/A        m_status = TesterStatus_Idle;
3287053SN/A        pickValue();
3297053SN/A
3307053SN/A    } else {
3317805Snilay@cs.wisc.edu        panic("Unexpected TesterStatus: %s proc: %d data: %s m_status: %s "
3327805Snilay@cs.wisc.edu              "time: %d\n",
3337805Snilay@cs.wisc.edu              *this, proc, data, m_status, g_eventQueue_ptr->getTime());
3347053SN/A    }
3357053SN/A
3367053SN/A    DPRINTF(RubyTest, "proc: %d, Address: 0x%x\n", proc,
3377053SN/A            getAddress().getLineAddress());
3387053SN/A    DPRINTF(RubyTest, "Callback done\n");
3397053SN/A    debugPrint();
3406899SN/A}
3416899SN/A
3427053SN/Avoid
3437053SN/ACheck::changeAddress(const Address& address)
3446899SN/A{
3457053SN/A    assert(m_status == TesterStatus_Idle || m_status == TesterStatus_Ready);
3467053SN/A    m_status = TesterStatus_Idle;
3477053SN/A    m_address = address;
3487053SN/A    m_store_count = 0;
3496899SN/A}
3506899SN/A
3517053SN/Avoid
3527053SN/ACheck::pickValue()
3536899SN/A{
3547053SN/A    assert(m_status == TesterStatus_Idle);
3557053SN/A    m_status = TesterStatus_Idle;
3567053SN/A    m_value = random() & 0xff; // One byte
3577053SN/A    m_store_count = 0;
3586899SN/A}
3596899SN/A
3607053SN/Avoid
3617053SN/ACheck::pickInitiatingNode()
3626899SN/A{
3637053SN/A    assert(m_status == TesterStatus_Idle || m_status == TesterStatus_Ready);
3647053SN/A    m_status = TesterStatus_Idle;
3657053SN/A    m_initiatingNode = (random() % m_num_cpu_sequencers);
3667053SN/A    DPRINTF(RubyTest, "picked initiating node %d\n", m_initiatingNode);
3677053SN/A    m_store_count = 0;
3686899SN/A}
3696899SN/A
3707053SN/Avoid
3717055SN/ACheck::print(std::ostream& out) const
3726899SN/A{
3737053SN/A    out << "["
3747053SN/A        << m_address << ", value: "
3757053SN/A        << (int)m_value << ", status: "
3767053SN/A        << m_status << ", initiating node: "
3777053SN/A        << m_initiatingNode << ", store_count: "
3787053SN/A        << m_store_count
3797055SN/A        << "]" << std::flush;
3806899SN/A}
3816899SN/A
3827053SN/Avoid
3837053SN/ACheck::debugPrint()
3846899SN/A{
3857053SN/A    DPRINTF(RubyTest,
3867053SN/A        "[%#x, value: %d, status: %s, initiating node: %d, store_count: %d]\n",
3877053SN/A        m_address.getAddress(), (int)m_value,
3887053SN/A        TesterStatus_to_string(m_status).c_str(),
3897053SN/A        m_initiatingNode, m_store_count);
3906899SN/A}
391