RubyPort.cc revision 9633:3bf3100e9fa1
1/*
2 * Copyright (c) 2012 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 "sim/system.hh"
50
51RubyPort::RubyPort(const Params *p)
52    : MemObject(p), m_version(p->version), m_controller(NULL),
53      m_mandatory_q_ptr(NULL),
54      pio_port(csprintf("%s-pio-port", name()), this),
55      m_usingRubyTester(p->using_ruby_tester), m_request_cnt(0),
56      drainManager(NULL), ruby_system(p->ruby_system), system(p->system),
57      waitingOnSequencer(false), access_phys_mem(p->access_phys_mem)
58{
59    assert(m_version != -1);
60
61    // create the slave ports based on the number of connected ports
62    for (size_t i = 0; i < p->port_slave_connection_count; ++i) {
63        slave_ports.push_back(new M5Port(csprintf("%s-slave%d", name(), i),
64                                         this, ruby_system, access_phys_mem));
65    }
66
67    // create the master ports based on the number of connected ports
68    for (size_t i = 0; i < p->port_master_connection_count; ++i) {
69        master_ports.push_back(new PioPort(csprintf("%s-master%d", name(), i),
70                                           this));
71    }
72}
73
74void
75RubyPort::init()
76{
77    assert(m_controller != NULL);
78    m_mandatory_q_ptr = m_controller->getMandatoryQueue();
79    m_mandatory_q_ptr->setSender(this);
80}
81
82BaseMasterPort &
83RubyPort::getMasterPort(const std::string &if_name, PortID idx)
84{
85    if (if_name == "pio_port") {
86        return pio_port;
87    }
88
89    // used by the x86 CPUs to connect the interrupt PIO and interrupt slave
90    // port
91    if (if_name != "master") {
92        // pass it along to our super class
93        return MemObject::getMasterPort(if_name, idx);
94    } else {
95        if (idx >= static_cast<PortID>(master_ports.size())) {
96            panic("RubyPort::getMasterPort: unknown index %d\n", idx);
97        }
98
99        return *master_ports[idx];
100    }
101}
102
103BaseSlavePort &
104RubyPort::getSlavePort(const std::string &if_name, PortID idx)
105{
106    // used by the CPUs to connect the caches to the interconnect, and
107    // for the x86 case also the interrupt master
108    if (if_name != "slave") {
109        // pass it along to our super class
110        return MemObject::getSlavePort(if_name, idx);
111    } else {
112        if (idx >= static_cast<PortID>(slave_ports.size())) {
113            panic("RubyPort::getSlavePort: unknown index %d\n", idx);
114        }
115
116        return *slave_ports[idx];
117    }
118}
119
120RubyPort::PioPort::PioPort(const std::string &_name,
121                           RubyPort *_port)
122    : QueuedMasterPort(_name, _port, queue), queue(*_port, *this)
123{
124    DPRINTF(RubyPort, "creating master port on ruby sequencer %s\n", _name);
125}
126
127RubyPort::M5Port::M5Port(const std::string &_name, RubyPort *_port,
128                         RubySystem *_system, bool _access_phys_mem)
129    : QueuedSlavePort(_name, _port, queue), queue(*_port, *this),
130      ruby_port(_port), ruby_system(_system),
131      _onRetryList(false), access_phys_mem(_access_phys_mem)
132{
133    DPRINTF(RubyPort, "creating slave port on ruby sequencer %s\n", _name);
134}
135
136Tick
137RubyPort::M5Port::recvAtomic(PacketPtr pkt)
138{
139    panic("RubyPort::M5Port::recvAtomic() not implemented!\n");
140    return 0;
141}
142
143
144bool
145RubyPort::PioPort::recvTimingResp(PacketPtr pkt)
146{
147    // In FS mode, ruby memory will receive pio responses from devices
148    // and it must forward these responses back to the particular CPU.
149    DPRINTF(RubyPort,  "Pio response for address %#x\n", pkt->getAddr());
150
151    // First we must retrieve the request port from the sender State
152    RubyPort::SenderState *senderState =
153        safe_cast<RubyPort::SenderState *>(pkt->popSenderState());
154    M5Port *port = senderState->port;
155    assert(port != NULL);
156    delete senderState;
157
158    port->sendTimingResp(pkt);
159
160    return true;
161}
162
163bool
164RubyPort::M5Port::recvTimingReq(PacketPtr pkt)
165{
166    DPRINTF(RubyPort,
167            "Timing access caught for address %#x\n", pkt->getAddr());
168
169    //dsm: based on SimpleTimingPort::recvTimingReq(pkt);
170
171    // The received packets should only be M5 requests, which should never
172    // get nacked.  There used to be code to hanldle nacks here, but
173    // I'm pretty sure it didn't work correctly with the drain code,
174    // so that would need to be fixed if we ever added it back.
175
176    if (pkt->memInhibitAsserted()) {
177        warn("memInhibitAsserted???");
178        // snooper will supply based on copy of packet
179        // still target's responsibility to delete packet
180        delete pkt;
181        return true;
182    }
183
184    // Save the port in the sender state object to be used later to
185    // route the response
186    pkt->pushSenderState(new SenderState(this));
187
188    // Check for pio requests and directly send them to the dedicated
189    // pio port.
190    if (!isPhysMemAddress(pkt->getAddr())) {
191        assert(ruby_port->pio_port.isConnected());
192        DPRINTF(RubyPort,
193                "Request for address 0x%#x is assumed to be a pio request\n",
194                pkt->getAddr());
195
196        // send next cycle
197        ruby_port->pio_port.schedTimingReq(pkt,
198            curTick() + g_system_ptr->clockPeriod());
199        return true;
200    }
201
202    assert(Address(pkt->getAddr()).getOffset() + pkt->getSize() <=
203           RubySystem::getBlockSizeBytes());
204
205    // Submit the ruby request
206    RequestStatus requestStatus = ruby_port->makeRequest(pkt);
207
208    // If the request successfully issued then we should return true.
209    // Otherwise, we need to delete the senderStatus we just created and return
210    // false.
211    if (requestStatus == RequestStatus_Issued) {
212        DPRINTF(RubyPort, "Request %#x issued\n", pkt->getAddr());
213        return true;
214    }
215
216    //
217    // Unless one is using the ruby tester, record the stalled M5 port for
218    // later retry when the sequencer becomes free.
219    //
220    if (!ruby_port->m_usingRubyTester) {
221        ruby_port->addToRetryList(this);
222    }
223
224    DPRINTF(RubyPort,
225            "Request for address %#x did not issue because %s\n",
226            pkt->getAddr(), RequestStatus_to_string(requestStatus));
227
228    SenderState* senderState = safe_cast<SenderState*>(pkt->senderState);
229    pkt->senderState = senderState->predecessor;
230    delete senderState;
231    return false;
232}
233
234void
235RubyPort::M5Port::recvFunctional(PacketPtr pkt)
236{
237    DPRINTF(RubyPort, "Functional access caught for address %#x\n",
238                                                           pkt->getAddr());
239
240    // Check for pio requests and directly send them to the dedicated
241    // pio port.
242    if (!isPhysMemAddress(pkt->getAddr())) {
243        assert(ruby_port->pio_port.isConnected());
244        DPRINTF(RubyPort, "Request for address 0x%#x is a pio request\n",
245                                                           pkt->getAddr());
246        panic("RubyPort::PioPort::recvFunctional() not implemented!\n");
247    }
248
249    assert(pkt->getAddr() + pkt->getSize() <=
250                line_address(Address(pkt->getAddr())).getAddress() +
251                RubySystem::getBlockSizeBytes());
252
253    bool accessSucceeded = false;
254    bool needsResponse = pkt->needsResponse();
255
256    // Do the functional access on ruby memory
257    if (pkt->isRead()) {
258        accessSucceeded = ruby_system->functionalRead(pkt);
259    } else if (pkt->isWrite()) {
260        accessSucceeded = ruby_system->functionalWrite(pkt);
261    } else {
262        panic("RubyPort: unsupported functional command %s\n",
263              pkt->cmdString());
264    }
265
266    // Unless the requester explicitly said otherwise, generate an error if
267    // the functional request failed
268    if (!accessSucceeded && !pkt->suppressFuncError()) {
269        fatal("Ruby functional %s failed for address %#x\n",
270              pkt->isWrite() ? "write" : "read", pkt->getAddr());
271    }
272
273    if (access_phys_mem) {
274        // The attached physmem contains the official version of data.
275        // The following command performs the real functional access.
276        // This line should be removed once Ruby supplies the official version
277        // of data.
278        ruby_port->system->getPhysMem().functionalAccess(pkt);
279    }
280
281    // turn packet around to go back to requester if response expected
282    if (needsResponse) {
283        pkt->setFunctionalResponseStatus(accessSucceeded);
284
285        // @todo There should not be a reverse call since the response is
286        // communicated through the packet pointer
287        // DPRINTF(RubyPort, "Sending packet back over port\n");
288        // sendFunctional(pkt);
289    }
290    DPRINTF(RubyPort, "Functional access %s!\n",
291            accessSucceeded ? "successful":"failed");
292}
293
294void
295RubyPort::ruby_hit_callback(PacketPtr pkt)
296{
297    // Retrieve the request port from the sender State
298    RubyPort::SenderState *senderState =
299        safe_cast<RubyPort::SenderState *>(pkt->senderState);
300    M5Port *port = senderState->port;
301    assert(port != NULL);
302
303    // pop the sender state from the packet
304    pkt->senderState = senderState->predecessor;
305    delete senderState;
306
307    port->hitCallback(pkt);
308
309    //
310    // If we had to stall the M5Ports, wake them up because the sequencer
311    // likely has free resources now.
312    //
313    if (waitingOnSequencer) {
314        //
315        // Record the current list of ports to retry on a temporary list before
316        // calling sendRetry on those ports.  sendRetry will cause an
317        // immediate retry, which may result in the ports being put back on the
318        // list. Therefore we want to clear the retryList before calling
319        // sendRetry.
320        //
321        std::list<M5Port*> curRetryList(retryList);
322
323        retryList.clear();
324        waitingOnSequencer = false;
325
326        for (std::list<M5Port*>::iterator i = curRetryList.begin();
327             i != curRetryList.end(); ++i) {
328            DPRINTF(RubyPort,
329                    "Sequencer may now be free.  SendRetry to port %s\n",
330                    (*i)->name());
331            (*i)->onRetryList(false);
332            (*i)->sendRetry();
333        }
334    }
335
336    testDrainComplete();
337}
338
339void
340RubyPort::testDrainComplete()
341{
342    //If we weren't able to drain before, we might be able to now.
343    if (drainManager != NULL) {
344        unsigned int drainCount = outstandingCount();
345        DPRINTF(Drain, "Drain count: %u\n", drainCount);
346        if (drainCount == 0) {
347            DPRINTF(Drain, "RubyPort done draining, signaling drain done\n");
348            drainManager->signalDrainDone();
349            // Clear the drain manager once we're done with it.
350            drainManager = NULL;
351        }
352    }
353}
354
355unsigned int
356RubyPort::getChildDrainCount(DrainManager *dm)
357{
358    int count = 0;
359
360    if (pio_port.isConnected()) {
361        count += pio_port.drain(dm);
362        DPRINTF(Config, "count after pio check %d\n", count);
363    }
364
365    for (CpuPortIter p = slave_ports.begin(); p != slave_ports.end(); ++p) {
366        count += (*p)->drain(dm);
367        DPRINTF(Config, "count after slave port check %d\n", count);
368    }
369
370    for (std::vector<PioPort*>::iterator p = master_ports.begin();
371         p != master_ports.end(); ++p) {
372        count += (*p)->drain(dm);
373        DPRINTF(Config, "count after master port check %d\n", count);
374    }
375
376    DPRINTF(Config, "final count %d\n", count);
377
378    return count;
379}
380
381unsigned int
382RubyPort::drain(DrainManager *dm)
383{
384    if (isDeadlockEventScheduled()) {
385        descheduleDeadlockEvent();
386    }
387
388    //
389    // If the RubyPort is not empty, then it needs to clear all outstanding
390    // requests before it should call drainManager->signalDrainDone()
391    //
392    DPRINTF(Config, "outstanding count %d\n", outstandingCount());
393    bool need_drain = outstandingCount() > 0;
394
395    //
396    // Also, get the number of child ports that will also need to clear
397    // their buffered requests before they call drainManager->signalDrainDone()
398    //
399    unsigned int child_drain_count = getChildDrainCount(dm);
400
401    // Set status
402    if (need_drain) {
403        drainManager = dm;
404
405        DPRINTF(Drain, "RubyPort not drained\n");
406        setDrainState(Drainable::Draining);
407        return child_drain_count + 1;
408    }
409
410    drainManager = NULL;
411    setDrainState(Drainable::Drained);
412    return child_drain_count;
413}
414
415void
416RubyPort::M5Port::hitCallback(PacketPtr pkt)
417{
418    bool needsResponse = pkt->needsResponse();
419
420    //
421    // Unless specified at configuraiton, all responses except failed SC
422    // and Flush operations access M5 physical memory.
423    //
424    bool accessPhysMem = access_phys_mem;
425
426    if (pkt->isLLSC()) {
427        if (pkt->isWrite()) {
428            if (pkt->req->getExtraData() != 0) {
429                //
430                // Successful SC packets convert to normal writes
431                //
432                pkt->convertScToWrite();
433            } else {
434                //
435                // Failed SC packets don't access physical memory and thus
436                // the RubyPort itself must convert it to a response.
437                //
438                accessPhysMem = false;
439            }
440        } else {
441            //
442            // All LL packets convert to normal loads so that M5 PhysMem does
443            // not lock the blocks.
444            //
445            pkt->convertLlToRead();
446        }
447    }
448
449    //
450    // Flush requests don't access physical memory
451    //
452    if (pkt->isFlush()) {
453        accessPhysMem = false;
454    }
455
456    DPRINTF(RubyPort, "Hit callback needs response %d\n", needsResponse);
457
458    if (accessPhysMem) {
459        ruby_port->system->getPhysMem().access(pkt);
460    } else if (needsResponse) {
461        pkt->makeResponse();
462    }
463
464    // turn packet around to go back to requester if response expected
465    if (needsResponse) {
466        DPRINTF(RubyPort, "Sending packet back over port\n");
467        // send next cycle
468        schedTimingResp(pkt, curTick() + g_system_ptr->clockPeriod());
469    } else {
470        delete pkt;
471    }
472    DPRINTF(RubyPort, "Hit callback done!\n");
473}
474
475AddrRangeList
476RubyPort::M5Port::getAddrRanges() const
477{
478    // at the moment the assumption is that the master does not care
479    AddrRangeList ranges;
480    return ranges;
481}
482
483bool
484RubyPort::M5Port::isPhysMemAddress(Addr addr)
485{
486    return ruby_port->system->isMemAddr(addr);
487}
488
489unsigned
490RubyPort::M5Port::deviceBlockSize() const
491{
492    return (unsigned) RubySystem::getBlockSizeBytes();
493}
494
495void
496RubyPort::ruby_eviction_callback(const Address& address)
497{
498    DPRINTF(RubyPort, "Sending invalidations.\n");
499    // This request is deleted in the stack-allocated packet destructor
500    // when this function exits
501    // TODO: should this really be using funcMasterId?
502    RequestPtr req =
503            new Request(address.getAddress(), 0, 0, Request::funcMasterId);
504    // Use a single packet to signal all snooping ports of the invalidation.
505    // This assumes that snooping ports do NOT modify the packet/request
506    Packet pkt(req, MemCmd::InvalidationReq);
507    for (CpuPortIter p = slave_ports.begin(); p != slave_ports.end(); ++p) {
508        // check if the connected master port is snooping
509        if ((*p)->isSnooping()) {
510            // send as a snoop request
511            (*p)->sendTimingSnoopReq(&pkt);
512        }
513    }
514}
515