RubyPort.cc revision 11143
1/*
2 * Copyright (c) 2012-2013 ARM Limited
3 * All rights reserved.
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2009 Advanced Micro Devices, Inc.
15 * Copyright (c) 2011 Mark D. Hill and David A. Wood
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#include "cpu/testers/rubytest/RubyTester.hh"
43#include "debug/Config.hh"
44#include "debug/Drain.hh"
45#include "debug/Ruby.hh"
46#include "mem/protocol/AccessPermission.hh"
47#include "mem/ruby/slicc_interface/AbstractController.hh"
48#include "mem/ruby/system/RubyPort.hh"
49#include "mem/simple_mem.hh"
50#include "sim/full_system.hh"
51#include "sim/system.hh"
52
53RubyPort::RubyPort(const Params *p)
54    : MemObject(p), m_ruby_system(p->ruby_system), m_version(p->version),
55      m_controller(NULL), m_mandatory_q_ptr(NULL),
56      m_usingRubyTester(p->using_ruby_tester), system(p->system),
57      pioMasterPort(csprintf("%s.pio-master-port", name()), this),
58      pioSlavePort(csprintf("%s.pio-slave-port", name()), this),
59      memMasterPort(csprintf("%s.mem-master-port", name()), this),
60      memSlavePort(csprintf("%s-mem-slave-port", name()), this,
61                   p->ruby_system->getAccessBackingStore(), -1),
62      gotAddrRanges(p->port_master_connection_count)
63{
64    assert(m_version != -1);
65
66    // create the slave ports based on the number of connected ports
67    for (size_t i = 0; i < p->port_slave_connection_count; ++i) {
68        slave_ports.push_back(new MemSlavePort(csprintf("%s.slave%d", name(),
69            i), this, p->ruby_system->getAccessBackingStore(), i));
70    }
71
72    // create the master ports based on the number of connected ports
73    for (size_t i = 0; i < p->port_master_connection_count; ++i) {
74        master_ports.push_back(new PioMasterPort(csprintf("%s.master%d",
75            name(), i), this));
76    }
77}
78
79void
80RubyPort::init()
81{
82    assert(m_controller != NULL);
83    m_mandatory_q_ptr = m_controller->getMandatoryQueue();
84}
85
86BaseMasterPort &
87RubyPort::getMasterPort(const std::string &if_name, PortID idx)
88{
89    if (if_name == "mem_master_port") {
90        return memMasterPort;
91    }
92
93    if (if_name == "pio_master_port") {
94        return pioMasterPort;
95    }
96
97    // used by the x86 CPUs to connect the interrupt PIO and interrupt slave
98    // port
99    if (if_name != "master") {
100        // pass it along to our super class
101        return MemObject::getMasterPort(if_name, idx);
102    } else {
103        if (idx >= static_cast<PortID>(master_ports.size())) {
104            panic("RubyPort::getMasterPort: unknown index %d\n", idx);
105        }
106
107        return *master_ports[idx];
108    }
109}
110
111BaseSlavePort &
112RubyPort::getSlavePort(const std::string &if_name, PortID idx)
113{
114    if (if_name == "mem_slave_port") {
115        return memSlavePort;
116    }
117
118    if (if_name == "pio_slave_port")
119        return pioSlavePort;
120
121    // used by the CPUs to connect the caches to the interconnect, and
122    // for the x86 case also the interrupt master
123    if (if_name != "slave") {
124        // pass it along to our super class
125        return MemObject::getSlavePort(if_name, idx);
126    } else {
127        if (idx >= static_cast<PortID>(slave_ports.size())) {
128            panic("RubyPort::getSlavePort: unknown index %d\n", idx);
129        }
130
131        return *slave_ports[idx];
132    }
133}
134
135RubyPort::PioMasterPort::PioMasterPort(const std::string &_name,
136                           RubyPort *_port)
137    : QueuedMasterPort(_name, _port, reqQueue, snoopRespQueue),
138      reqQueue(*_port, *this), snoopRespQueue(*_port, *this)
139{
140    DPRINTF(RubyPort, "Created master pioport on sequencer %s\n", _name);
141}
142
143RubyPort::PioSlavePort::PioSlavePort(const std::string &_name,
144                           RubyPort *_port)
145    : QueuedSlavePort(_name, _port, queue), queue(*_port, *this)
146{
147    DPRINTF(RubyPort, "Created slave pioport on sequencer %s\n", _name);
148}
149
150RubyPort::MemMasterPort::MemMasterPort(const std::string &_name,
151                           RubyPort *_port)
152    : QueuedMasterPort(_name, _port, reqQueue, snoopRespQueue),
153      reqQueue(*_port, *this), snoopRespQueue(*_port, *this)
154{
155    DPRINTF(RubyPort, "Created master memport on ruby sequencer %s\n", _name);
156}
157
158RubyPort::MemSlavePort::MemSlavePort(const std::string &_name, RubyPort *_port,
159                                     bool _access_backing_store, PortID id)
160    : QueuedSlavePort(_name, _port, queue, id), queue(*_port, *this),
161      access_backing_store(_access_backing_store)
162{
163    DPRINTF(RubyPort, "Created slave memport on ruby sequencer %s\n", _name);
164}
165
166bool
167RubyPort::PioMasterPort::recvTimingResp(PacketPtr pkt)
168{
169    RubyPort *rp = static_cast<RubyPort *>(&owner);
170    DPRINTF(RubyPort, "Response for address: 0x%#x\n", pkt->getAddr());
171
172    // send next cycle
173    rp->pioSlavePort.schedTimingResp(
174            pkt, curTick() + rp->m_ruby_system->clockPeriod());
175    return true;
176}
177
178bool RubyPort::MemMasterPort::recvTimingResp(PacketPtr pkt)
179{
180    // got a response from a device
181    assert(pkt->isResponse());
182
183    // First we must retrieve the request port from the sender State
184    RubyPort::SenderState *senderState =
185        safe_cast<RubyPort::SenderState *>(pkt->popSenderState());
186    MemSlavePort *port = senderState->port;
187    assert(port != NULL);
188    delete senderState;
189
190    // In FS mode, ruby memory will receive pio responses from devices
191    // and it must forward these responses back to the particular CPU.
192    DPRINTF(RubyPort,  "Pio response for address %#x, going to %s\n",
193            pkt->getAddr(), port->name());
194
195    // attempt to send the response in the next cycle
196    RubyPort *rp = static_cast<RubyPort *>(&owner);
197    port->schedTimingResp(pkt, curTick() + rp->m_ruby_system->clockPeriod());
198
199    return true;
200}
201
202bool
203RubyPort::PioSlavePort::recvTimingReq(PacketPtr pkt)
204{
205    RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
206
207    for (size_t i = 0; i < ruby_port->master_ports.size(); ++i) {
208        AddrRangeList l = ruby_port->master_ports[i]->getAddrRanges();
209        for (auto it = l.begin(); it != l.end(); ++it) {
210            if (it->contains(pkt->getAddr())) {
211                // generally it is not safe to assume success here as
212                // the port could be blocked
213                bool M5_VAR_USED success =
214                    ruby_port->master_ports[i]->sendTimingReq(pkt);
215                assert(success);
216                return true;
217            }
218        }
219    }
220    panic("Should never reach here!\n");
221}
222
223bool
224RubyPort::MemSlavePort::recvTimingReq(PacketPtr pkt)
225{
226    DPRINTF(RubyPort, "Timing request for address %#x on port %d\n",
227            pkt->getAddr(), id);
228    RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
229
230    if (pkt->memInhibitAsserted())
231        panic("RubyPort should never see an inhibited request\n");
232
233    // Check for pio requests and directly send them to the dedicated
234    // pio port.
235    if (!isPhysMemAddress(pkt->getAddr())) {
236        assert(ruby_port->memMasterPort.isConnected());
237        DPRINTF(RubyPort, "Request address %#x assumed to be a pio address\n",
238                pkt->getAddr());
239
240        // Save the port in the sender state object to be used later to
241        // route the response
242        pkt->pushSenderState(new SenderState(this));
243
244        // send next cycle
245        RubySystem *rs = ruby_port->m_ruby_system;
246        ruby_port->memMasterPort.schedTimingReq(pkt,
247            curTick() + rs->clockPeriod());
248        return true;
249    }
250
251    assert(getOffset(pkt->getAddr()) + pkt->getSize() <=
252           RubySystem::getBlockSizeBytes());
253
254    // Submit the ruby request
255    RequestStatus requestStatus = ruby_port->makeRequest(pkt);
256
257    // If the request successfully issued then we should return true.
258    // Otherwise, we need to tell the port to retry at a later point
259    // and return false.
260    if (requestStatus == RequestStatus_Issued) {
261        // Save the port in the sender state object to be used later to
262        // route the response
263        pkt->pushSenderState(new SenderState(this));
264
265        DPRINTF(RubyPort, "Request %s 0x%x issued\n", pkt->cmdString(),
266                pkt->getAddr());
267        return true;
268    }
269
270    //
271    // Unless one is using the ruby tester, record the stalled M5 port for
272    // later retry when the sequencer becomes free.
273    //
274    if (!ruby_port->m_usingRubyTester) {
275        ruby_port->addToRetryList(this);
276    }
277
278    DPRINTF(RubyPort, "Request for address %#x did not issued because %s\n",
279            pkt->getAddr(), RequestStatus_to_string(requestStatus));
280
281    return false;
282}
283
284void
285RubyPort::MemSlavePort::recvFunctional(PacketPtr pkt)
286{
287    DPRINTF(RubyPort, "Functional access for address: %#x\n", pkt->getAddr());
288
289    RubyPort *rp M5_VAR_USED = static_cast<RubyPort *>(&owner);
290    RubySystem *rs = rp->m_ruby_system;
291
292    // Check for pio requests and directly send them to the dedicated
293    // pio port.
294    if (!isPhysMemAddress(pkt->getAddr())) {
295        assert(rp->memMasterPort.isConnected());
296        DPRINTF(RubyPort, "Pio Request for address: 0x%#x\n", pkt->getAddr());
297        panic("RubyPort::PioMasterPort::recvFunctional() not implemented!\n");
298    }
299
300    assert(pkt->getAddr() + pkt->getSize() <=
301           makeLineAddress(pkt->getAddr()) + RubySystem::getBlockSizeBytes());
302
303    if (access_backing_store) {
304        // The attached physmem contains the official version of data.
305        // The following command performs the real functional access.
306        // This line should be removed once Ruby supplies the official version
307        // of data.
308        rs->getPhysMem()->functionalAccess(pkt);
309    } else {
310        bool accessSucceeded = false;
311        bool needsResponse = pkt->needsResponse();
312
313        // Do the functional access on ruby memory
314        if (pkt->isRead()) {
315            accessSucceeded = rs->functionalRead(pkt);
316        } else if (pkt->isWrite()) {
317            accessSucceeded = rs->functionalWrite(pkt);
318        } else {
319            panic("Unsupported functional command %s\n", pkt->cmdString());
320        }
321
322        // Unless the requester explicitly said otherwise, generate an error if
323        // the functional request failed
324        if (!accessSucceeded && !pkt->suppressFuncError()) {
325            fatal("Ruby functional %s failed for address %#x\n",
326                  pkt->isWrite() ? "write" : "read", pkt->getAddr());
327        }
328
329        // turn packet around to go back to requester if response expected
330        if (needsResponse) {
331            pkt->setFunctionalResponseStatus(accessSucceeded);
332        }
333
334        DPRINTF(RubyPort, "Functional access %s!\n",
335                accessSucceeded ? "successful":"failed");
336    }
337}
338
339void
340RubyPort::ruby_hit_callback(PacketPtr pkt)
341{
342    DPRINTF(RubyPort, "Hit callback for %s 0x%x\n", pkt->cmdString(),
343            pkt->getAddr());
344
345    // The packet was destined for memory and has not yet been turned
346    // into a response
347    assert(system->isMemAddr(pkt->getAddr()));
348    assert(pkt->isRequest());
349
350    // First we must retrieve the request port from the sender State
351    RubyPort::SenderState *senderState =
352        safe_cast<RubyPort::SenderState *>(pkt->popSenderState());
353    MemSlavePort *port = senderState->port;
354    assert(port != NULL);
355    delete senderState;
356
357    port->hitCallback(pkt);
358
359    //
360    // If we had to stall the MemSlavePorts, wake them up because the sequencer
361    // likely has free resources now.
362    //
363    if (!retryList.empty()) {
364        //
365        // Record the current list of ports to retry on a temporary list before
366        // calling sendRetry on those ports.  sendRetry will cause an
367        // immediate retry, which may result in the ports being put back on the
368        // list. Therefore we want to clear the retryList before calling
369        // sendRetry.
370        //
371        std::vector<MemSlavePort *> curRetryList(retryList);
372
373        retryList.clear();
374
375        for (auto i = curRetryList.begin(); i != curRetryList.end(); ++i) {
376            DPRINTF(RubyPort,
377                    "Sequencer may now be free.  SendRetry to port %s\n",
378                    (*i)->name());
379            (*i)->sendRetryReq();
380        }
381    }
382
383    testDrainComplete();
384}
385
386void
387RubyPort::testDrainComplete()
388{
389    //If we weren't able to drain before, we might be able to now.
390    if (drainState() == DrainState::Draining) {
391        unsigned int drainCount = outstandingCount();
392        DPRINTF(Drain, "Drain count: %u\n", drainCount);
393        if (drainCount == 0) {
394            DPRINTF(Drain, "RubyPort done draining, signaling drain done\n");
395            signalDrainDone();
396        }
397    }
398}
399
400DrainState
401RubyPort::drain()
402{
403    if (isDeadlockEventScheduled()) {
404        descheduleDeadlockEvent();
405    }
406
407    //
408    // If the RubyPort is not empty, then it needs to clear all outstanding
409    // requests before it should call signalDrainDone()
410    //
411    DPRINTF(Config, "outstanding count %d\n", outstandingCount());
412    if (outstandingCount() > 0) {
413        DPRINTF(Drain, "RubyPort not drained\n");
414        return DrainState::Draining;
415    } else {
416        return DrainState::Drained;
417    }
418}
419
420void
421RubyPort::MemSlavePort::hitCallback(PacketPtr pkt)
422{
423    bool needsResponse = pkt->needsResponse();
424
425    // Unless specified at configuraiton, all responses except failed SC
426    // and Flush operations access M5 physical memory.
427    bool accessPhysMem = access_backing_store;
428
429    if (pkt->isLLSC()) {
430        if (pkt->isWrite()) {
431            if (pkt->req->getExtraData() != 0) {
432                //
433                // Successful SC packets convert to normal writes
434                //
435                pkt->convertScToWrite();
436            } else {
437                //
438                // Failed SC packets don't access physical memory and thus
439                // the RubyPort itself must convert it to a response.
440                //
441                accessPhysMem = false;
442            }
443        } else {
444            //
445            // All LL packets convert to normal loads so that M5 PhysMem does
446            // not lock the blocks.
447            //
448            pkt->convertLlToRead();
449        }
450    }
451
452    // Flush requests don't access physical memory
453    if (pkt->isFlush()) {
454        accessPhysMem = false;
455    }
456
457    DPRINTF(RubyPort, "Hit callback needs response %d\n", needsResponse);
458
459    RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
460    RubySystem *rs = ruby_port->m_ruby_system;
461    if (accessPhysMem) {
462        rs->getPhysMem()->access(pkt);
463    } else if (needsResponse) {
464        pkt->makeResponse();
465    }
466
467    // turn packet around to go back to requester if response expected
468    if (needsResponse) {
469        DPRINTF(RubyPort, "Sending packet back over port\n");
470        // Send a response in the same cycle. There is no need to delay the
471        // response because the response latency is already incurred in the
472        // Ruby protocol.
473        schedTimingResp(pkt, curTick());
474    } else {
475        delete pkt;
476    }
477
478    DPRINTF(RubyPort, "Hit callback done!\n");
479}
480
481AddrRangeList
482RubyPort::PioSlavePort::getAddrRanges() const
483{
484    // at the moment the assumption is that the master does not care
485    AddrRangeList ranges;
486    RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
487
488    for (size_t i = 0; i < ruby_port->master_ports.size(); ++i) {
489        ranges.splice(ranges.begin(),
490                ruby_port->master_ports[i]->getAddrRanges());
491    }
492    for (const auto M5_VAR_USED &r : ranges)
493        DPRINTF(RubyPort, "%s\n", r.to_string());
494    return ranges;
495}
496
497bool
498RubyPort::MemSlavePort::isPhysMemAddress(Addr addr) const
499{
500    RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
501    return ruby_port->system->isMemAddr(addr);
502}
503
504void
505RubyPort::ruby_eviction_callback(Addr address)
506{
507    DPRINTF(RubyPort, "Sending invalidations.\n");
508    // Allocate the invalidate request and packet on the stack, as it is
509    // assumed they will not be modified or deleted by receivers.
510    // TODO: should this really be using funcMasterId?
511    Request request(address, RubySystem::getBlockSizeBytes(), 0,
512                    Request::funcMasterId);
513    // Use a single packet to signal all snooping ports of the invalidation.
514    // This assumes that snooping ports do NOT modify the packet/request
515    Packet pkt(&request, MemCmd::InvalidateReq);
516    for (CpuPortIter p = slave_ports.begin(); p != slave_ports.end(); ++p) {
517        // check if the connected master port is snooping
518        if ((*p)->isSnooping()) {
519            // send as a snoop request
520            (*p)->sendTimingSnoopReq(&pkt);
521        }
522    }
523}
524
525void
526RubyPort::PioMasterPort::recvRangeChange()
527{
528    RubyPort &r = static_cast<RubyPort &>(owner);
529    r.gotAddrRanges--;
530    if (r.gotAddrRanges == 0 && FullSystem) {
531        r.pioSlavePort.sendRangeChange();
532    }
533}
534