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