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