1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * Copyright (c) 2009 Advanced Micro Devices, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "cpu/testers/rubytest/Check.hh"
31
32#include "base/random.hh"
33#include "base/trace.hh"
34#include "debug/RubyTest.hh"
35#include "mem/ruby/common/SubBlock.hh"
36
37typedef RubyTester::SenderState SenderState;
38
39Check::Check(Addr address, Addr pc, int _num_writers, int _num_readers,
40             RubyTester* _tester)
41    : m_num_writers(_num_writers), m_num_readers(_num_readers),
42      m_tester_ptr(_tester)
43{
44    m_status = TesterStatus_Idle;
45
46    pickValue();
47    pickInitiatingNode();
48    changeAddress(address);
49    m_pc = pc;
50    m_access_mode = RubyAccessMode(random_mt.random(0,
51                                                    RubyAccessMode_NUM - 1));
52    m_store_count = 0;
53}
54
55void
56Check::initiate()
57{
58    DPRINTF(RubyTest, "initiating\n");
59    debugPrint();
60
61    // currently no protocols support prefetches
62    if (false && (random_mt.random(0, 0xf) == 0)) {
63        initiatePrefetch(); // Prefetch from random processor
64    }
65
66        if (m_tester_ptr->getCheckFlush() && (random_mt.random(0, 0xff) == 0)) {
67        initiateFlush(); // issue a Flush request from random processor
68    }
69
70    if (m_status == TesterStatus_Idle) {
71        initiateAction();
72    } else if (m_status == TesterStatus_Ready) {
73        initiateCheck();
74    } else {
75        // Pending - do nothing
76        DPRINTF(RubyTest,
77                "initiating action/check - failed: action/check is pending\n");
78    }
79}
80
81void
82Check::initiatePrefetch()
83{
84    DPRINTF(RubyTest, "initiating prefetch\n");
85
86    int index = random_mt.random(0, m_num_readers - 1);
87    MasterPort* port = m_tester_ptr->getReadableCpuPort(index);
88
89    Request::Flags flags;
90    flags.set(Request::PREFETCH);
91
92    Packet::Command cmd;
93
94    // 1 in 8 chance this will be an exclusive prefetch
95    if (random_mt.random(0, 0x7) != 0) {
96        cmd = MemCmd::ReadReq;
97
98        // if necessary, make the request an instruction fetch
99        if (m_tester_ptr->isInstOnlyCpuPort(index) ||
100            (m_tester_ptr->isInstDataCpuPort(index) &&
101             (random_mt.random(0, 0x1)))) {
102            flags.set(Request::INST_FETCH);
103        }
104    } else {
105        cmd = MemCmd::WriteReq;
106        flags.set(Request::PF_EXCLUSIVE);
107    }
108
109    // Prefetches are assumed to be 0 sized
110    RequestPtr req = std::make_shared<Request>(m_address, 0, flags,
111            m_tester_ptr->masterId(), curTick(), m_pc);
112    req->setContext(index);
113
114    PacketPtr pkt = new Packet(req, cmd);
115    // despite the oddity of the 0 size (questionable if this should
116    // even be allowed), a prefetch is still a read and as such needs
117    // a place to store the result
118    uint8_t *data = new uint8_t[1];
119    pkt->dataDynamic(data);
120
121    // push the subblock onto the sender state.  The sequencer will
122    // update the subblock on the return
123    pkt->senderState = new SenderState(m_address, req->getSize());
124
125    if (port->sendTimingReq(pkt)) {
126        DPRINTF(RubyTest, "successfully initiated prefetch.\n");
127    } else {
128        // If the packet did not issue, must delete
129        delete pkt->senderState;
130        delete pkt;
131
132        DPRINTF(RubyTest,
133                "prefetch initiation failed because Port was busy.\n");
134    }
135}
136
137void
138Check::initiateFlush()
139{
140
141    DPRINTF(RubyTest, "initiating Flush\n");
142
143    int index = random_mt.random(0, m_num_writers - 1);
144    MasterPort* port = m_tester_ptr->getWritableCpuPort(index);
145
146    Request::Flags flags;
147
148    RequestPtr req = std::make_shared<Request>(m_address, CHECK_SIZE, flags,
149            m_tester_ptr->masterId(), curTick(), m_pc);
150
151    Packet::Command cmd;
152
153    cmd = MemCmd::FlushReq;
154
155    PacketPtr pkt = new Packet(req, cmd);
156
157    // push the subblock onto the sender state.  The sequencer will
158    // update the subblock on the return
159    pkt->senderState = new SenderState(m_address, req->getSize());
160
161    if (port->sendTimingReq(pkt)) {
162        DPRINTF(RubyTest, "initiating Flush - successful\n");
163    }
164}
165
166void
167Check::initiateAction()
168{
169    DPRINTF(RubyTest, "initiating Action\n");
170    assert(m_status == TesterStatus_Idle);
171
172    int index = random_mt.random(0, m_num_writers - 1);
173    MasterPort* port = m_tester_ptr->getWritableCpuPort(index);
174
175    Request::Flags flags;
176
177    // Create the particular address for the next byte to be written
178    Addr writeAddr(m_address + m_store_count);
179
180    // Stores are assumed to be 1 byte-sized
181    RequestPtr req = std::make_shared<Request>(
182        writeAddr, 1, flags, m_tester_ptr->masterId(), curTick(), m_pc);
183
184    req->setContext(index);
185    Packet::Command cmd;
186
187    // 1 out of 8 chance, issue an atomic rather than a write
188    // if ((random() & 0x7) == 0) {
189    //     cmd = MemCmd::SwapReq;
190    // } else {
191    cmd = MemCmd::WriteReq;
192    // }
193
194    PacketPtr pkt = new Packet(req, cmd);
195    uint8_t *writeData = new uint8_t[1];
196    *writeData = m_value + m_store_count;
197    pkt->dataDynamic(writeData);
198
199    DPRINTF(RubyTest, "Seq write: index %d data 0x%x check 0x%x\n", index,
200            *(pkt->getConstPtr<uint8_t>()), *writeData);
201
202    // push the subblock onto the sender state.  The sequencer will
203    // update the subblock on the return
204    pkt->senderState = new SenderState(writeAddr, req->getSize());
205
206    if (port->sendTimingReq(pkt)) {
207        DPRINTF(RubyTest, "initiating action - successful\n");
208        DPRINTF(RubyTest, "status before action update: %s\n",
209                (TesterStatus_to_string(m_status)).c_str());
210        m_status = TesterStatus_Action_Pending;
211        DPRINTF(RubyTest, "Check %#x, State=Action_Pending\n", m_address);
212    } else {
213        // If the packet did not issue, must delete
214        // Note: No need to delete the data, the packet destructor
215        // will delete it
216        delete pkt->senderState;
217        delete pkt;
218
219        DPRINTF(RubyTest, "failed to initiate action - sequencer not ready\n");
220    }
221
222    DPRINTF(RubyTest, "status after action update: %s\n",
223            (TesterStatus_to_string(m_status)).c_str());
224}
225
226void
227Check::initiateCheck()
228{
229    DPRINTF(RubyTest, "Initiating Check\n");
230    assert(m_status == TesterStatus_Ready);
231
232    int index = random_mt.random(0, m_num_readers - 1);
233    MasterPort* port = m_tester_ptr->getReadableCpuPort(index);
234
235    Request::Flags flags;
236
237    // If necessary, make the request an instruction fetch
238    if (m_tester_ptr->isInstOnlyCpuPort(index) ||
239        (m_tester_ptr->isInstDataCpuPort(index) &&
240         (random_mt.random(0, 0x1)))) {
241        flags.set(Request::INST_FETCH);
242    }
243
244    // Checks are sized depending on the number of bytes written
245    RequestPtr req = std::make_shared<Request>(m_address, CHECK_SIZE, flags,
246                               m_tester_ptr->masterId(), curTick(), m_pc);
247
248    req->setContext(index);
249    PacketPtr pkt = new Packet(req, MemCmd::ReadReq);
250    uint8_t *dataArray = new uint8_t[CHECK_SIZE];
251    pkt->dataDynamic(dataArray);
252
253    DPRINTF(RubyTest, "Seq read: index %d\n", index);
254
255    // push the subblock onto the sender state.  The sequencer will
256    // update the subblock on the return
257    pkt->senderState = new SenderState(m_address, req->getSize());
258
259    if (port->sendTimingReq(pkt)) {
260        DPRINTF(RubyTest, "initiating check - successful\n");
261        DPRINTF(RubyTest, "status before check update: %s\n",
262                TesterStatus_to_string(m_status).c_str());
263        m_status = TesterStatus_Check_Pending;
264        DPRINTF(RubyTest, "Check %#x, State=Check_Pending\n", m_address);
265    } else {
266        // If the packet did not issue, must delete
267        // Note: No need to delete the data, the packet destructor
268        // will delete it
269        delete pkt->senderState;
270        delete pkt;
271
272        DPRINTF(RubyTest, "failed to initiate check - cpu port not ready\n");
273    }
274
275    DPRINTF(RubyTest, "status after check update: %s\n",
276            TesterStatus_to_string(m_status).c_str());
277}
278
279void
280Check::performCallback(NodeID proc, SubBlock* data, Cycles curTime)
281{
282    Addr address = data->getAddress();
283
284    // This isn't exactly right since we now have multi-byte checks
285    //  assert(getAddress() == address);
286
287    assert(makeLineAddress(m_address) == makeLineAddress(address));
288    assert(data != NULL);
289
290    DPRINTF(RubyTest, "RubyTester Callback\n");
291    debugPrint();
292
293    if (m_status == TesterStatus_Action_Pending) {
294        DPRINTF(RubyTest, "Action callback write value: %d, currently %d\n",
295                (m_value + m_store_count), data->getByte(0));
296        // Perform store one byte at a time
297        data->setByte(0, (m_value + m_store_count));
298        m_store_count++;
299        if (m_store_count == CHECK_SIZE) {
300            m_status = TesterStatus_Ready;
301            DPRINTF(RubyTest, "Check %#x, State=Ready\n", m_address);
302        } else {
303            m_status = TesterStatus_Idle;
304            DPRINTF(RubyTest, "Check %#x, State=Idle store_count: %d\n",
305                    m_address, m_store_count);
306        }
307        DPRINTF(RubyTest, "Action callback return data now %d\n",
308                data->getByte(0));
309    } else if (m_status == TesterStatus_Check_Pending) {
310        DPRINTF(RubyTest, "Check callback\n");
311        // Perform load/check
312        for (int byte_number=0; byte_number<CHECK_SIZE; byte_number++) {
313            if (uint8_t(m_value + byte_number) != data->getByte(byte_number)) {
314                panic("Action/check failure: proc: %d address: %#x data: %s "
315                      "byte_number: %d m_value+byte_number: %d byte: %d %s"
316                      "Time: %d\n",
317                      proc, address, data, byte_number,
318                      (int)m_value + byte_number,
319                      (int)data->getByte(byte_number), *this, curTime);
320            }
321        }
322        DPRINTF(RubyTest, "Action/check success\n");
323        debugPrint();
324
325        // successful check complete, increment complete
326        m_tester_ptr->incrementCheckCompletions();
327
328        m_status = TesterStatus_Idle;
329        DPRINTF(RubyTest, "Check %#x, State=Idle\n", m_address);
330        pickValue();
331
332    } else {
333        panic("Unexpected TesterStatus: %s proc: %d data: %s m_status: %s "
334              "time: %d\n", *this, proc, data, m_status, curTime);
335    }
336
337    DPRINTF(RubyTest, "proc: %d, Address: 0x%x\n", proc,
338            makeLineAddress(m_address));
339    DPRINTF(RubyTest, "Callback done\n");
340    debugPrint();
341}
342
343void
344Check::changeAddress(Addr address)
345{
346    assert(m_status == TesterStatus_Idle || m_status == TesterStatus_Ready);
347    m_status = TesterStatus_Idle;
348    m_address = address;
349    DPRINTF(RubyTest, "Check %#x, State=Idle\n", m_address);
350    m_store_count = 0;
351}
352
353void
354Check::pickValue()
355{
356    assert(m_status == TesterStatus_Idle);
357    m_value = random_mt.random(0, 0xff); // One byte
358    m_store_count = 0;
359}
360
361void
362Check::pickInitiatingNode()
363{
364    assert(m_status == TesterStatus_Idle || m_status == TesterStatus_Ready);
365    m_status = TesterStatus_Idle;
366    m_initiatingNode = (random_mt.random(0, m_num_writers - 1));
367    DPRINTF(RubyTest, "Check %#x, State=Idle, picked initiating node %d\n",
368            m_address, m_initiatingNode);
369    m_store_count = 0;
370}
371
372void
373Check::print(std::ostream& out) const
374{
375    out << "["
376        << m_address << ", value: "
377        << (int)m_value << ", status: "
378        << m_status << ", initiating node: "
379        << m_initiatingNode << ", store_count: "
380        << m_store_count
381        << "]" << std::flush;
382}
383
384void
385Check::debugPrint()
386{
387    DPRINTF(RubyTest,
388        "[%#x, value: %d, status: %s, initiating node: %d, store_count: %d]\n",
389        m_address, (int)m_value, TesterStatus_to_string(m_status).c_str(),
390        m_initiatingNode, m_store_count);
391}
392