RubyPort.cc revision 9270:92aad0e984ff
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      drainEvent(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}
80
81MasterPort &
82RubyPort::getMasterPort(const std::string &if_name, int idx)
83{
84    if (if_name == "pio_port") {
85        return pio_port;
86    }
87
88    // used by the x86 CPUs to connect the interrupt PIO and interrupt slave
89    // port
90    if (if_name != "master") {
91        // pass it along to our super class
92        return MemObject::getMasterPort(if_name, idx);
93    } else {
94        if (idx >= static_cast<int>(master_ports.size())) {
95            panic("RubyPort::getMasterPort: unknown index %d\n", idx);
96        }
97
98        return *master_ports[idx];
99    }
100}
101
102SlavePort &
103RubyPort::getSlavePort(const std::string &if_name, int idx)
104{
105    // used by the CPUs to connect the caches to the interconnect, and
106    // for the x86 case also the interrupt master
107    if (if_name != "slave") {
108        // pass it along to our super class
109        return MemObject::getSlavePort(if_name, idx);
110    } else {
111        if (idx >= static_cast<int>(slave_ports.size())) {
112            panic("RubyPort::getSlavePort: unknown index %d\n", idx);
113        }
114
115        return *slave_ports[idx];
116    }
117}
118
119RubyPort::PioPort::PioPort(const std::string &_name,
120                           RubyPort *_port)
121    : QueuedMasterPort(_name, _port, queue), queue(*_port, *this),
122      ruby_port(_port)
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->senderState);
154    M5Port *port = senderState->port;
155    assert(port != NULL);
156
157    // pop the sender state from the packet
158    pkt->senderState = senderState->saved;
159    delete senderState;
160
161    port->sendTimingResp(pkt);
162
163    return true;
164}
165
166bool
167RubyPort::M5Port::recvTimingReq(PacketPtr pkt)
168{
169    DPRINTF(RubyPort,
170            "Timing access caught for address %#x\n", pkt->getAddr());
171
172    //dsm: based on SimpleTimingPort::recvTimingReq(pkt);
173
174    // The received packets should only be M5 requests, which should never
175    // get nacked.  There used to be code to hanldle nacks here, but
176    // I'm pretty sure it didn't work correctly with the drain code,
177    // so that would need to be fixed if we ever added it back.
178
179    if (pkt->memInhibitAsserted()) {
180        warn("memInhibitAsserted???");
181        // snooper will supply based on copy of packet
182        // still target's responsibility to delete packet
183        delete pkt;
184        return true;
185    }
186
187    // Save the port in the sender state object to be used later to
188    // route the response
189    pkt->senderState = new SenderState(this, pkt->senderState);
190
191    // Check for pio requests and directly send them to the dedicated
192    // pio port.
193    if (!isPhysMemAddress(pkt->getAddr())) {
194        assert(ruby_port->pio_port.isConnected());
195        DPRINTF(RubyPort,
196                "Request for address 0x%#x is assumed to be a pio request\n",
197                pkt->getAddr());
198
199        // send next cycle
200        ruby_port->pio_port.schedTimingReq(pkt,
201            curTick() + g_system_ptr->clockPeriod());
202        return true;
203    }
204
205    assert(Address(pkt->getAddr()).getOffset() + pkt->getSize() <=
206           RubySystem::getBlockSizeBytes());
207
208    // Submit the ruby request
209    RequestStatus requestStatus = ruby_port->makeRequest(pkt);
210
211    // If the request successfully issued then we should return true.
212    // Otherwise, we need to delete the senderStatus we just created and return
213    // false.
214    if (requestStatus == RequestStatus_Issued) {
215        DPRINTF(RubyPort, "Request %#x issued\n", pkt->getAddr());
216        return true;
217    }
218
219    //
220    // Unless one is using the ruby tester, record the stalled M5 port for
221    // later retry when the sequencer becomes free.
222    //
223    if (!ruby_port->m_usingRubyTester) {
224        ruby_port->addToRetryList(this);
225    }
226
227    DPRINTF(RubyPort,
228            "Request for address %#x did not issue because %s\n",
229            pkt->getAddr(), RequestStatus_to_string(requestStatus));
230
231    SenderState* senderState = safe_cast<SenderState*>(pkt->senderState);
232    pkt->senderState = senderState->saved;
233    delete senderState;
234    return false;
235}
236
237void
238RubyPort::M5Port::recvFunctional(PacketPtr pkt)
239{
240    DPRINTF(RubyPort, "Functional access caught for address %#x\n",
241                                                           pkt->getAddr());
242
243    // Check for pio requests and directly send them to the dedicated
244    // pio port.
245    if (!isPhysMemAddress(pkt->getAddr())) {
246        assert(ruby_port->pio_port.isConnected());
247        DPRINTF(RubyPort, "Request for address 0x%#x is a pio request\n",
248                                                           pkt->getAddr());
249        panic("RubyPort::PioPort::recvFunctional() not implemented!\n");
250    }
251
252    assert(pkt->getAddr() + pkt->getSize() <=
253                line_address(Address(pkt->getAddr())).getAddress() +
254                RubySystem::getBlockSizeBytes());
255
256    bool accessSucceeded = false;
257    bool needsResponse = pkt->needsResponse();
258
259    // Do the functional access on ruby memory
260    if (pkt->isRead()) {
261        accessSucceeded = ruby_system->functionalRead(pkt);
262    } else if (pkt->isWrite()) {
263        accessSucceeded = ruby_system->functionalWrite(pkt);
264    } else {
265        panic("RubyPort: unsupported functional command %s\n",
266              pkt->cmdString());
267    }
268
269    // Unless the requester explicitly said otherwise, generate an error if
270    // the functional request failed
271    if (!accessSucceeded && !pkt->suppressFuncError()) {
272        fatal("Ruby functional %s failed for address %#x\n",
273              pkt->isWrite() ? "write" : "read", pkt->getAddr());
274    }
275
276    if (access_phys_mem) {
277        // The attached physmem contains the official version of data.
278        // The following command performs the real functional access.
279        // This line should be removed once Ruby supplies the official version
280        // of data.
281        ruby_port->system->getPhysMem().functionalAccess(pkt);
282    }
283
284    // turn packet around to go back to requester if response expected
285    if (needsResponse) {
286        pkt->setFunctionalResponseStatus(accessSucceeded);
287
288        // @todo There should not be a reverse call since the response is
289        // communicated through the packet pointer
290        // DPRINTF(RubyPort, "Sending packet back over port\n");
291        // sendFunctional(pkt);
292    }
293    DPRINTF(RubyPort, "Functional access %s!\n",
294            accessSucceeded ? "successful":"failed");
295}
296
297void
298RubyPort::ruby_hit_callback(PacketPtr pkt)
299{
300    // Retrieve the request port from the sender State
301    RubyPort::SenderState *senderState =
302        safe_cast<RubyPort::SenderState *>(pkt->senderState);
303    M5Port *port = senderState->port;
304    assert(port != NULL);
305
306    // pop the sender state from the packet
307    pkt->senderState = senderState->saved;
308    delete senderState;
309
310    port->hitCallback(pkt);
311
312    //
313    // If we had to stall the M5Ports, wake them up because the sequencer
314    // likely has free resources now.
315    //
316    if (waitingOnSequencer) {
317        //
318        // Record the current list of ports to retry on a temporary list before
319        // calling sendRetry on those ports.  sendRetry will cause an
320        // immediate retry, which may result in the ports being put back on the
321        // list. Therefore we want to clear the retryList before calling
322        // sendRetry.
323        //
324        std::list<M5Port*> curRetryList(retryList);
325
326        retryList.clear();
327        waitingOnSequencer = false;
328
329        for (std::list<M5Port*>::iterator i = curRetryList.begin();
330             i != curRetryList.end(); ++i) {
331            DPRINTF(RubyPort,
332                    "Sequencer may now be free.  SendRetry to port %s\n",
333                    (*i)->name());
334            (*i)->onRetryList(false);
335            (*i)->sendRetry();
336        }
337    }
338
339    testDrainComplete();
340}
341
342void
343RubyPort::testDrainComplete()
344{
345    //If we weren't able to drain before, we might be able to now.
346    if (drainEvent != NULL) {
347        unsigned int drainCount = outstandingCount();
348        DPRINTF(Drain, "Drain count: %u\n", drainCount);
349        if (drainCount == 0) {
350            DPRINTF(Drain, "RubyPort done draining, processing drain event\n");
351            drainEvent->process();
352            // Clear the drain event once we're done with it.
353            drainEvent = NULL;
354        }
355    }
356}
357
358unsigned int
359RubyPort::getChildDrainCount(Event *de)
360{
361    int count = 0;
362
363    if (pio_port.isConnected()) {
364        count += pio_port.drain(de);
365        DPRINTF(Config, "count after pio check %d\n", count);
366    }
367
368    for (CpuPortIter p = slave_ports.begin(); p != slave_ports.end(); ++p) {
369        count += (*p)->drain(de);
370        DPRINTF(Config, "count after slave port check %d\n", count);
371    }
372
373    for (std::vector<PioPort*>::iterator p = master_ports.begin();
374         p != master_ports.end(); ++p) {
375        count += (*p)->drain(de);
376        DPRINTF(Config, "count after master port check %d\n", count);
377    }
378
379    DPRINTF(Config, "final count %d\n", count);
380
381    return count;
382}
383
384unsigned int
385RubyPort::drain(Event *de)
386{
387    if (isDeadlockEventScheduled()) {
388        descheduleDeadlockEvent();
389    }
390
391    //
392    // If the RubyPort is not empty, then it needs to clear all outstanding
393    // requests before it should call drainEvent->process()
394    //
395    DPRINTF(Config, "outstanding count %d\n", outstandingCount());
396    bool need_drain = outstandingCount() > 0;
397
398    //
399    // Also, get the number of child ports that will also need to clear
400    // their buffered requests before they call drainEvent->process()
401    //
402    unsigned int child_drain_count = getChildDrainCount(de);
403
404    // Set status
405    if (need_drain) {
406        drainEvent = de;
407
408        DPRINTF(Drain, "RubyPort not drained\n");
409        changeState(SimObject::Draining);
410        return child_drain_count + 1;
411    }
412
413    drainEvent = NULL;
414    changeState(SimObject::Drained);
415    return child_drain_count;
416}
417
418void
419RubyPort::M5Port::hitCallback(PacketPtr pkt)
420{
421    bool needsResponse = pkt->needsResponse();
422
423    //
424    // Unless specified at configuraiton, all responses except failed SC
425    // and Flush operations access M5 physical memory.
426    //
427    bool accessPhysMem = access_phys_mem;
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    //
453    // Flush requests don't access physical memory
454    //
455    if (pkt->isFlush()) {
456        accessPhysMem = false;
457    }
458
459    DPRINTF(RubyPort, "Hit callback needs response %d\n", needsResponse);
460
461    if (accessPhysMem) {
462        ruby_port->system->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 next cycle
471        schedTimingResp(pkt, curTick() + g_system_ptr->clockPeriod());
472    } else {
473        delete pkt;
474    }
475    DPRINTF(RubyPort, "Hit callback done!\n");
476}
477
478AddrRangeList
479RubyPort::M5Port::getAddrRanges() const
480{
481    // at the moment the assumption is that the master does not care
482    AddrRangeList ranges;
483    return ranges;
484}
485
486bool
487RubyPort::M5Port::isPhysMemAddress(Addr addr)
488{
489    return ruby_port->system->isMemAddr(addr);
490}
491
492unsigned
493RubyPort::M5Port::deviceBlockSize() const
494{
495    return (unsigned) RubySystem::getBlockSizeBytes();
496}
497
498void
499RubyPort::ruby_eviction_callback(const Address& address)
500{
501    DPRINTF(RubyPort, "Sending invalidations.\n");
502    // should this really be using funcMasterId?
503    Request req(address.getAddress(), 0, 0, Request::funcMasterId);
504    for (CpuPortIter p = slave_ports.begin(); p != slave_ports.end(); ++p) {
505        // check if the connected master port is snooping
506        if ((*p)->isSnooping()) {
507            Packet *pkt = new Packet(&req, MemCmd::InvalidationReq);
508            // send as a snoop request
509            (*p)->sendTimingSnoopReq(pkt);
510        }
511    }
512}
513