Check.cc revision 11435
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 "base/random.hh"
31#include "cpu/testers/rubytest/Check.hh"
32#include "debug/RubyTest.hh"
33#include "mem/ruby/common/SubBlock.hh"
34
35typedef RubyTester::SenderState SenderState;
36
37Check::Check(Addr address, Addr pc, int _num_writers, int _num_readers,
38             RubyTester* _tester)
39    : m_num_writers(_num_writers), m_num_readers(_num_readers),
40      m_tester_ptr(_tester)
41{
42    m_status = TesterStatus_Idle;
43
44    pickValue();
45    pickInitiatingNode();
46    changeAddress(address);
47    m_pc = pc;
48    m_access_mode = RubyAccessMode(random_mt.random(0,
49                                                    RubyAccessMode_NUM - 1));
50    m_store_count = 0;
51}
52
53void
54Check::initiate()
55{
56    DPRINTF(RubyTest, "initiating\n");
57    debugPrint();
58
59    // currently no protocols support prefetches
60    if (false && (random_mt.random(0, 0xf) == 0)) {
61        initiatePrefetch(); // Prefetch from random processor
62    }
63
64        if (m_tester_ptr->getCheckFlush() && (random_mt.random(0, 0xff) == 0)) {
65        initiateFlush(); // issue a Flush request from random processor
66    }
67
68    if (m_status == TesterStatus_Idle) {
69        initiateAction();
70    } else if (m_status == TesterStatus_Ready) {
71        initiateCheck();
72    } else {
73        // Pending - do nothing
74        DPRINTF(RubyTest,
75                "initiating action/check - failed: action/check is pending\n");
76    }
77}
78
79void
80Check::initiatePrefetch()
81{
82    DPRINTF(RubyTest, "initiating prefetch\n");
83
84    int index = random_mt.random(0, m_num_readers - 1);
85    MasterPort* port = m_tester_ptr->getReadableCpuPort(index);
86
87    Request::Flags flags;
88    flags.set(Request::PREFETCH);
89
90    Packet::Command cmd;
91
92    // 1 in 8 chance this will be an exclusive prefetch
93    if (random_mt.random(0, 0x7) != 0) {
94        cmd = MemCmd::ReadReq;
95
96        // if necessary, make the request an instruction fetch
97        if (m_tester_ptr->isInstOnlyCpuPort(index) ||
98            (m_tester_ptr->isInstDataCpuPort(index) &&
99             (random_mt.random(0, 0x1)))) {
100            flags.set(Request::INST_FETCH);
101        }
102    } else {
103        cmd = MemCmd::WriteReq;
104        flags.set(Request::PF_EXCLUSIVE);
105    }
106
107    // Prefetches are assumed to be 0 sized
108    Request *req = new Request(m_address, 0, flags,
109            m_tester_ptr->masterId(), curTick(), m_pc);
110    req->setContext(index);
111
112    PacketPtr pkt = new Packet(req, cmd);
113    // despite the oddity of the 0 size (questionable if this should
114    // even be allowed), a prefetch is still a read and as such needs
115    // a place to store the result
116    uint8_t *data = new uint8_t[1];
117    pkt->dataDynamic(data);
118
119    // push the subblock onto the sender state.  The sequencer will
120    // update the subblock on the return
121    pkt->senderState = new SenderState(m_address, req->getSize());
122
123    if (port->sendTimingReq(pkt)) {
124        DPRINTF(RubyTest, "successfully initiated prefetch.\n");
125    } else {
126        // If the packet did not issue, must delete
127        delete pkt->senderState;
128        delete pkt->req;
129        delete pkt;
130
131        DPRINTF(RubyTest,
132                "prefetch initiation failed because Port was busy.\n");
133    }
134}
135
136void
137Check::initiateFlush()
138{
139
140    DPRINTF(RubyTest, "initiating Flush\n");
141
142    int index = random_mt.random(0, m_num_writers - 1);
143    MasterPort* port = m_tester_ptr->getWritableCpuPort(index);
144
145    Request::Flags flags;
146
147    Request *req = new Request(m_address, CHECK_SIZE, flags,
148            m_tester_ptr->masterId(), curTick(), m_pc);
149
150    Packet::Command cmd;
151
152    cmd = MemCmd::FlushReq;
153
154    PacketPtr pkt = new Packet(req, cmd);
155
156    // push the subblock onto the sender state.  The sequencer will
157    // update the subblock on the return
158    pkt->senderState = new SenderState(m_address, req->getSize());
159
160    if (port->sendTimingReq(pkt)) {
161        DPRINTF(RubyTest, "initiating Flush - successful\n");
162    }
163}
164
165void
166Check::initiateAction()
167{
168    DPRINTF(RubyTest, "initiating Action\n");
169    assert(m_status == TesterStatus_Idle);
170
171    int index = random_mt.random(0, m_num_writers - 1);
172    MasterPort* port = m_tester_ptr->getWritableCpuPort(index);
173
174    Request::Flags flags;
175
176    // Create the particular address for the next byte to be written
177    Addr writeAddr(m_address + m_store_count);
178
179    // Stores are assumed to be 1 byte-sized
180    Request *req = new Request(writeAddr, 1, flags, m_tester_ptr->masterId(),
181                               curTick(), m_pc);
182
183    req->setContext(index);
184    Packet::Command cmd;
185
186    // 1 out of 8 chance, issue an atomic rather than a write
187    // if ((random() & 0x7) == 0) {
188    //     cmd = MemCmd::SwapReq;
189    // } else {
190    cmd = MemCmd::WriteReq;
191    // }
192
193    PacketPtr pkt = new Packet(req, cmd);
194    uint8_t *writeData = new uint8_t[1];
195    *writeData = m_value + m_store_count;
196    pkt->dataDynamic(writeData);
197
198    DPRINTF(RubyTest, "Seq write: index %d data 0x%x check 0x%x\n", index,
199            *(pkt->getConstPtr<uint8_t>()), *writeData);
200
201    // push the subblock onto the sender state.  The sequencer will
202    // update the subblock on the return
203    pkt->senderState = new SenderState(writeAddr, req->getSize());
204
205    if (port->sendTimingReq(pkt)) {
206        DPRINTF(RubyTest, "initiating action - successful\n");
207        DPRINTF(RubyTest, "status before action update: %s\n",
208                (TesterStatus_to_string(m_status)).c_str());
209        m_status = TesterStatus_Action_Pending;
210        DPRINTF(RubyTest, "Check %s, State=Action_Pending\n", m_address);
211    } else {
212        // If the packet did not issue, must delete
213        // Note: No need to delete the data, the packet destructor
214        // will delete it
215        delete pkt->senderState;
216        delete pkt->req;
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    Request *req = new 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 %s, 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->req;
271        delete pkt;
272
273        DPRINTF(RubyTest, "failed to initiate check - cpu port not ready\n");
274    }
275
276    DPRINTF(RubyTest, "status after check update: %s\n",
277            TesterStatus_to_string(m_status).c_str());
278}
279
280void
281Check::performCallback(NodeID proc, SubBlock* data, Cycles curTime)
282{
283    Addr address = data->getAddress();
284
285    // This isn't exactly right since we now have multi-byte checks
286    //  assert(getAddress() == address);
287
288    assert(makeLineAddress(m_address) == makeLineAddress(address));
289    assert(data != NULL);
290
291    DPRINTF(RubyTest, "RubyTester Callback\n");
292    debugPrint();
293
294    if (m_status == TesterStatus_Action_Pending) {
295        DPRINTF(RubyTest, "Action callback write value: %d, currently %d\n",
296                (m_value + m_store_count), data->getByte(0));
297        // Perform store one byte at a time
298        data->setByte(0, (m_value + m_store_count));
299        m_store_count++;
300        if (m_store_count == CHECK_SIZE) {
301            m_status = TesterStatus_Ready;
302            DPRINTF(RubyTest, "Check %s, State=Ready\n", m_address);
303        } else {
304            m_status = TesterStatus_Idle;
305            DPRINTF(RubyTest, "Check %s, State=Idle store_count: %d\n",
306                    m_address, m_store_count);
307        }
308        DPRINTF(RubyTest, "Action callback return data now %d\n",
309                data->getByte(0));
310    } else if (m_status == TesterStatus_Check_Pending) {
311        DPRINTF(RubyTest, "Check callback\n");
312        // Perform load/check
313        for (int byte_number=0; byte_number<CHECK_SIZE; byte_number++) {
314            if (uint8_t(m_value + byte_number) != data->getByte(byte_number)) {
315                panic("Action/check failure: proc: %d address: %s data: %s "
316                      "byte_number: %d m_value+byte_number: %d byte: %d %s"
317                      "Time: %d\n",
318                      proc, address, data, byte_number,
319                      (int)m_value + byte_number,
320                      (int)data->getByte(byte_number), *this, curTime);
321            }
322        }
323        DPRINTF(RubyTest, "Action/check success\n");
324        debugPrint();
325
326        // successful check complete, increment complete
327        m_tester_ptr->incrementCheckCompletions();
328
329        m_status = TesterStatus_Idle;
330        DPRINTF(RubyTest, "Check %s, State=Idle\n", m_address);
331        pickValue();
332
333    } else {
334        panic("Unexpected TesterStatus: %s proc: %d data: %s m_status: %s "
335              "time: %d\n", *this, proc, data, m_status, curTime);
336    }
337
338    DPRINTF(RubyTest, "proc: %d, Address: 0x%x\n", proc,
339            makeLineAddress(m_address));
340    DPRINTF(RubyTest, "Callback done\n");
341    debugPrint();
342}
343
344void
345Check::changeAddress(Addr address)
346{
347    assert(m_status == TesterStatus_Idle || m_status == TesterStatus_Ready);
348    m_status = TesterStatus_Idle;
349    m_address = address;
350    DPRINTF(RubyTest, "Check %s, State=Idle\n", m_address);
351    m_store_count = 0;
352}
353
354void
355Check::pickValue()
356{
357    assert(m_status == TesterStatus_Idle);
358    m_value = random_mt.random(0, 0xff); // One byte
359    m_store_count = 0;
360}
361
362void
363Check::pickInitiatingNode()
364{
365    assert(m_status == TesterStatus_Idle || m_status == TesterStatus_Ready);
366    m_status = TesterStatus_Idle;
367    m_initiatingNode = (random_mt.random(0, m_num_writers - 1));
368    DPRINTF(RubyTest, "Check %s, State=Idle, picked initiating node %d\n",
369            m_address, m_initiatingNode);
370    m_store_count = 0;
371}
372
373void
374Check::print(std::ostream& out) const
375{
376    out << "["
377        << m_address << ", value: "
378        << (int)m_value << ", status: "
379        << m_status << ", initiating node: "
380        << m_initiatingNode << ", store_count: "
381        << m_store_count
382        << "]" << std::flush;
383}
384
385void
386Check::debugPrint()
387{
388    DPRINTF(RubyTest,
389        "[%#x, value: %d, status: %s, initiating node: %d, store_count: %d]\n",
390        m_address, (int)m_value, TesterStatus_to_string(m_status).c_str(),
391        m_initiatingNode, m_store_count);
392}
393