RubyPort.cc revision 11596:329e49c419b1
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-2013 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                   p->no_retry_on_stall),
63      gotAddrRanges(p->port_master_connection_count),
64      m_isCPUSequencer(p->is_cpu_sequencer)
65{
66    assert(m_version != -1);
67
68    // create the slave ports based on the number of connected ports
69    for (size_t i = 0; i < p->port_slave_connection_count; ++i) {
70        slave_ports.push_back(new MemSlavePort(csprintf("%s.slave%d", name(),
71            i), this, p->ruby_system->getAccessBackingStore(),
72            i, p->no_retry_on_stall));
73    }
74
75    // create the master ports based on the number of connected ports
76    for (size_t i = 0; i < p->port_master_connection_count; ++i) {
77        master_ports.push_back(new PioMasterPort(csprintf("%s.master%d",
78            name(), i), this));
79    }
80}
81
82void
83RubyPort::init()
84{
85    assert(m_controller != NULL);
86    m_mandatory_q_ptr = m_controller->getMandatoryQueue();
87}
88
89BaseMasterPort &
90RubyPort::getMasterPort(const std::string &if_name, PortID idx)
91{
92    if (if_name == "mem_master_port") {
93        return memMasterPort;
94    }
95
96    if (if_name == "pio_master_port") {
97        return pioMasterPort;
98    }
99
100    // used by the x86 CPUs to connect the interrupt PIO and interrupt slave
101    // port
102    if (if_name != "master") {
103        // pass it along to our super class
104        return MemObject::getMasterPort(if_name, idx);
105    } else {
106        if (idx >= static_cast<PortID>(master_ports.size())) {
107            panic("RubyPort::getMasterPort: unknown index %d\n", idx);
108        }
109
110        return *master_ports[idx];
111    }
112}
113
114BaseSlavePort &
115RubyPort::getSlavePort(const std::string &if_name, PortID idx)
116{
117    if (if_name == "mem_slave_port") {
118        return memSlavePort;
119    }
120
121    if (if_name == "pio_slave_port")
122        return pioSlavePort;
123
124    // used by the CPUs to connect the caches to the interconnect, and
125    // for the x86 case also the interrupt master
126    if (if_name != "slave") {
127        // pass it along to our super class
128        return MemObject::getSlavePort(if_name, idx);
129    } else {
130        if (idx >= static_cast<PortID>(slave_ports.size())) {
131            panic("RubyPort::getSlavePort: unknown index %d\n", idx);
132        }
133
134        return *slave_ports[idx];
135    }
136}
137
138RubyPort::PioMasterPort::PioMasterPort(const std::string &_name,
139                           RubyPort *_port)
140    : QueuedMasterPort(_name, _port, reqQueue, snoopRespQueue),
141      reqQueue(*_port, *this), snoopRespQueue(*_port, *this)
142{
143    DPRINTF(RubyPort, "Created master pioport on sequencer %s\n", _name);
144}
145
146RubyPort::PioSlavePort::PioSlavePort(const std::string &_name,
147                           RubyPort *_port)
148    : QueuedSlavePort(_name, _port, queue), queue(*_port, *this)
149{
150    DPRINTF(RubyPort, "Created slave pioport on sequencer %s\n", _name);
151}
152
153RubyPort::MemMasterPort::MemMasterPort(const std::string &_name,
154                           RubyPort *_port)
155    : QueuedMasterPort(_name, _port, reqQueue, snoopRespQueue),
156      reqQueue(*_port, *this), snoopRespQueue(*_port, *this)
157{
158    DPRINTF(RubyPort, "Created master memport on ruby sequencer %s\n", _name);
159}
160
161RubyPort::MemSlavePort::MemSlavePort(const std::string &_name, RubyPort *_port,
162                                     bool _access_backing_store, PortID id,
163                                     bool _no_retry_on_stall)
164    : QueuedSlavePort(_name, _port, queue, id), queue(*_port, *this),
165      access_backing_store(_access_backing_store),
166      no_retry_on_stall(_no_retry_on_stall)
167{
168    DPRINTF(RubyPort, "Created slave memport on ruby sequencer %s\n", _name);
169}
170
171bool
172RubyPort::PioMasterPort::recvTimingResp(PacketPtr pkt)
173{
174    RubyPort *rp = static_cast<RubyPort *>(&owner);
175    DPRINTF(RubyPort, "Response for address: 0x%#x\n", pkt->getAddr());
176
177    // send next cycle
178    rp->pioSlavePort.schedTimingResp(
179            pkt, curTick() + rp->m_ruby_system->clockPeriod());
180    return true;
181}
182
183bool RubyPort::MemMasterPort::recvTimingResp(PacketPtr pkt)
184{
185    // got a response from a device
186    assert(pkt->isResponse());
187
188    // First we must retrieve the request port from the sender State
189    RubyPort::SenderState *senderState =
190        safe_cast<RubyPort::SenderState *>(pkt->popSenderState());
191    MemSlavePort *port = senderState->port;
192    assert(port != NULL);
193    delete senderState;
194
195    // In FS mode, ruby memory will receive pio responses from devices
196    // and it must forward these responses back to the particular CPU.
197    DPRINTF(RubyPort,  "Pio response for address %#x, going to %s\n",
198            pkt->getAddr(), port->name());
199
200    // attempt to send the response in the next cycle
201    RubyPort *rp = static_cast<RubyPort *>(&owner);
202    port->schedTimingResp(pkt, curTick() + rp->m_ruby_system->clockPeriod());
203
204    return true;
205}
206
207bool
208RubyPort::PioSlavePort::recvTimingReq(PacketPtr pkt)
209{
210    RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
211
212    for (size_t i = 0; i < ruby_port->master_ports.size(); ++i) {
213        AddrRangeList l = ruby_port->master_ports[i]->getAddrRanges();
214        for (auto it = l.begin(); it != l.end(); ++it) {
215            if (it->contains(pkt->getAddr())) {
216                // generally it is not safe to assume success here as
217                // the port could be blocked
218                bool M5_VAR_USED success =
219                    ruby_port->master_ports[i]->sendTimingReq(pkt);
220                assert(success);
221                return true;
222            }
223        }
224    }
225    panic("Should never reach here!\n");
226}
227
228bool
229RubyPort::MemSlavePort::recvTimingReq(PacketPtr pkt)
230{
231    DPRINTF(RubyPort, "Timing request for address %#x on port %d\n",
232            pkt->getAddr(), id);
233    RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
234
235    if (pkt->cacheResponding())
236        panic("RubyPort should never see request with the "
237              "cacheResponding flag set\n");
238
239    // Check for pio requests and directly send them to the dedicated
240    // pio port.
241    if (pkt->cmd != MemCmd::MemFenceReq) {
242        if (!isPhysMemAddress(pkt->getAddr())) {
243            assert(ruby_port->memMasterPort.isConnected());
244            DPRINTF(RubyPort, "Request address %#x assumed to be a "
245                    "pio address\n", pkt->getAddr());
246
247            // Save the port in the sender state object to be used later to
248            // route the response
249            pkt->pushSenderState(new SenderState(this));
250
251            // send next cycle
252            RubySystem *rs = ruby_port->m_ruby_system;
253            ruby_port->memMasterPort.schedTimingReq(pkt,
254                curTick() + rs->clockPeriod());
255            return true;
256        }
257
258        assert(getOffset(pkt->getAddr()) + pkt->getSize() <=
259               RubySystem::getBlockSizeBytes());
260    }
261
262    // Submit the ruby request
263    RequestStatus requestStatus = ruby_port->makeRequest(pkt);
264
265    // If the request successfully issued then we should return true.
266    // Otherwise, we need to tell the port to retry at a later point
267    // and return false.
268    if (requestStatus == RequestStatus_Issued) {
269        // Save the port in the sender state object to be used later to
270        // route the response
271        pkt->pushSenderState(new SenderState(this));
272
273        DPRINTF(RubyPort, "Request %s 0x%x issued\n", pkt->cmdString(),
274                pkt->getAddr());
275        return true;
276    }
277
278    if (pkt->cmd != MemCmd::MemFenceReq) {
279        DPRINTF(RubyPort,
280                "Request for address %#x did not issued because %s\n",
281                pkt->getAddr(), RequestStatus_to_string(requestStatus));
282    }
283
284    addToRetryList();
285
286    return false;
287}
288
289void
290RubyPort::MemSlavePort::addToRetryList()
291{
292    RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
293
294    //
295    // Unless the requestor do not want retries (e.g., the Ruby tester),
296    // record the stalled M5 port for later retry when the sequencer
297    // becomes free.
298    //
299    if (!no_retry_on_stall && !ruby_port->onRetryList(this)) {
300        ruby_port->addToRetryList(this);
301    }
302}
303
304void
305RubyPort::MemSlavePort::recvFunctional(PacketPtr pkt)
306{
307    DPRINTF(RubyPort, "Functional access for address: %#x\n", pkt->getAddr());
308
309    RubyPort *rp M5_VAR_USED = static_cast<RubyPort *>(&owner);
310    RubySystem *rs = rp->m_ruby_system;
311
312    // Check for pio requests and directly send them to the dedicated
313    // pio port.
314    if (!isPhysMemAddress(pkt->getAddr())) {
315        DPRINTF(RubyPort, "Pio Request for address: 0x%#x\n", pkt->getAddr());
316        assert(rp->pioMasterPort.isConnected());
317        rp->pioMasterPort.sendFunctional(pkt);
318        return;
319    }
320
321    assert(pkt->getAddr() + pkt->getSize() <=
322           makeLineAddress(pkt->getAddr()) + RubySystem::getBlockSizeBytes());
323
324    if (access_backing_store) {
325        // The attached physmem contains the official version of data.
326        // The following command performs the real functional access.
327        // This line should be removed once Ruby supplies the official version
328        // of data.
329        rs->getPhysMem()->functionalAccess(pkt);
330    } else {
331        bool accessSucceeded = false;
332        bool needsResponse = pkt->needsResponse();
333
334        // Do the functional access on ruby memory
335        if (pkt->isRead()) {
336            accessSucceeded = rs->functionalRead(pkt);
337        } else if (pkt->isWrite()) {
338            accessSucceeded = rs->functionalWrite(pkt);
339        } else {
340            panic("Unsupported functional command %s\n", pkt->cmdString());
341        }
342
343        // Unless the requester explicitly said otherwise, generate an error if
344        // the functional request failed
345        if (!accessSucceeded && !pkt->suppressFuncError()) {
346            fatal("Ruby functional %s failed for address %#x\n",
347                  pkt->isWrite() ? "write" : "read", pkt->getAddr());
348        }
349
350        // turn packet around to go back to requester if response expected
351        if (needsResponse) {
352            pkt->setFunctionalResponseStatus(accessSucceeded);
353        }
354
355        DPRINTF(RubyPort, "Functional access %s!\n",
356                accessSucceeded ? "successful":"failed");
357    }
358}
359
360void
361RubyPort::ruby_hit_callback(PacketPtr pkt)
362{
363    DPRINTF(RubyPort, "Hit callback for %s 0x%x\n", pkt->cmdString(),
364            pkt->getAddr());
365
366    // The packet was destined for memory and has not yet been turned
367    // into a response
368    assert(system->isMemAddr(pkt->getAddr()));
369    assert(pkt->isRequest());
370
371    // First we must retrieve the request port from the sender State
372    RubyPort::SenderState *senderState =
373        safe_cast<RubyPort::SenderState *>(pkt->popSenderState());
374    MemSlavePort *port = senderState->port;
375    assert(port != NULL);
376    delete senderState;
377
378    port->hitCallback(pkt);
379
380    trySendRetries();
381}
382
383void
384RubyPort::trySendRetries()
385{
386    //
387    // If we had to stall the MemSlavePorts, wake them up because the sequencer
388    // likely has free resources now.
389    //
390    if (!retryList.empty()) {
391        // Record the current list of ports to retry on a temporary list
392        // before calling sendRetryReq on those ports. sendRetryReq will cause
393        // an immediate retry, which may result in the ports being put back on
394        // the list. Therefore we want to clear the retryList before calling
395        // sendRetryReq.
396        std::vector<MemSlavePort *> curRetryList(retryList);
397
398        retryList.clear();
399
400        for (auto i = curRetryList.begin(); i != curRetryList.end(); ++i) {
401            DPRINTF(RubyPort,
402                    "Sequencer may now be free. SendRetry to port %s\n",
403                    (*i)->name());
404            (*i)->sendRetryReq();
405        }
406    }
407}
408
409void
410RubyPort::testDrainComplete()
411{
412    //If we weren't able to drain before, we might be able to now.
413    if (drainState() == DrainState::Draining) {
414        unsigned int drainCount = outstandingCount();
415        DPRINTF(Drain, "Drain count: %u\n", drainCount);
416        if (drainCount == 0) {
417            DPRINTF(Drain, "RubyPort done draining, signaling drain done\n");
418            signalDrainDone();
419        }
420    }
421}
422
423DrainState
424RubyPort::drain()
425{
426    if (isDeadlockEventScheduled()) {
427        descheduleDeadlockEvent();
428    }
429
430    //
431    // If the RubyPort is not empty, then it needs to clear all outstanding
432    // requests before it should call signalDrainDone()
433    //
434    DPRINTF(Config, "outstanding count %d\n", outstandingCount());
435    if (outstandingCount() > 0) {
436        DPRINTF(Drain, "RubyPort not drained\n");
437        return DrainState::Draining;
438    } else {
439        return DrainState::Drained;
440    }
441}
442
443void
444RubyPort::MemSlavePort::hitCallback(PacketPtr pkt)
445{
446    bool needsResponse = pkt->needsResponse();
447
448    // Unless specified at configuraiton, all responses except failed SC
449    // and Flush operations access M5 physical memory.
450    bool accessPhysMem = access_backing_store;
451
452    if (pkt->isLLSC()) {
453        if (pkt->isWrite()) {
454            if (pkt->req->getExtraData() != 0) {
455                //
456                // Successful SC packets convert to normal writes
457                //
458                pkt->convertScToWrite();
459            } else {
460                //
461                // Failed SC packets don't access physical memory and thus
462                // the RubyPort itself must convert it to a response.
463                //
464                accessPhysMem = false;
465            }
466        } else {
467            //
468            // All LL packets convert to normal loads so that M5 PhysMem does
469            // not lock the blocks.
470            //
471            pkt->convertLlToRead();
472        }
473    }
474
475    // Flush, acquire, release requests don't access physical memory
476    if (pkt->isFlush() || pkt->cmd == MemCmd::MemFenceReq) {
477        accessPhysMem = false;
478    }
479
480    if (pkt->req->isKernel()) {
481        accessPhysMem = false;
482        needsResponse = true;
483    }
484
485    DPRINTF(RubyPort, "Hit callback needs response %d\n", needsResponse);
486
487    RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
488    RubySystem *rs = ruby_port->m_ruby_system;
489    if (accessPhysMem) {
490        rs->getPhysMem()->access(pkt);
491    } else if (needsResponse) {
492        pkt->makeResponse();
493    }
494
495    // turn packet around to go back to requester if response expected
496    if (needsResponse) {
497        DPRINTF(RubyPort, "Sending packet back over port\n");
498        // Send a response in the same cycle. There is no need to delay the
499        // response because the response latency is already incurred in the
500        // Ruby protocol.
501        schedTimingResp(pkt, curTick());
502    } else {
503        delete pkt;
504    }
505
506    DPRINTF(RubyPort, "Hit callback done!\n");
507}
508
509AddrRangeList
510RubyPort::PioSlavePort::getAddrRanges() const
511{
512    // at the moment the assumption is that the master does not care
513    AddrRangeList ranges;
514    RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
515
516    for (size_t i = 0; i < ruby_port->master_ports.size(); ++i) {
517        ranges.splice(ranges.begin(),
518                ruby_port->master_ports[i]->getAddrRanges());
519    }
520    for (const auto M5_VAR_USED &r : ranges)
521        DPRINTF(RubyPort, "%s\n", r.to_string());
522    return ranges;
523}
524
525bool
526RubyPort::MemSlavePort::isPhysMemAddress(Addr addr) const
527{
528    RubyPort *ruby_port = static_cast<RubyPort *>(&owner);
529    return ruby_port->system->isMemAddr(addr);
530}
531
532void
533RubyPort::ruby_eviction_callback(Addr address)
534{
535    DPRINTF(RubyPort, "Sending invalidations.\n");
536    // Allocate the invalidate request and packet on the stack, as it is
537    // assumed they will not be modified or deleted by receivers.
538    // TODO: should this really be using funcMasterId?
539    Request request(address, RubySystem::getBlockSizeBytes(), 0,
540                    Request::funcMasterId);
541    // Use a single packet to signal all snooping ports of the invalidation.
542    // This assumes that snooping ports do NOT modify the packet/request
543    Packet pkt(&request, MemCmd::InvalidateReq);
544    for (CpuPortIter p = slave_ports.begin(); p != slave_ports.end(); ++p) {
545        // check if the connected master port is snooping
546        if ((*p)->isSnooping()) {
547            // send as a snoop request
548            (*p)->sendTimingSnoopReq(&pkt);
549        }
550    }
551}
552
553void
554RubyPort::PioMasterPort::recvRangeChange()
555{
556    RubyPort &r = static_cast<RubyPort &>(owner);
557    r.gotAddrRanges--;
558    if (r.gotAddrRanges == 0 && FullSystem) {
559        r.pioSlavePort.sendRangeChange();
560    }
561}
562