110478SAndrew.Bardsley@arm.com/*
210478SAndrew.Bardsley@arm.com * Copyright (c) 2012-2014 ARM Limited
310478SAndrew.Bardsley@arm.com * All rights reserved
410478SAndrew.Bardsley@arm.com *
510478SAndrew.Bardsley@arm.com * The license below extends only to copyright in the software and shall
610478SAndrew.Bardsley@arm.com * not be construed as granting a license to any other intellectual
710478SAndrew.Bardsley@arm.com * property including but not limited to intellectual property relating
810478SAndrew.Bardsley@arm.com * to a hardware implementation of the functionality of the software
910478SAndrew.Bardsley@arm.com * licensed hereunder.  You may use the software subject to the license
1010478SAndrew.Bardsley@arm.com * terms below provided that you ensure that this notice is replicated
1110478SAndrew.Bardsley@arm.com * unmodified and in its entirety in all distributions of the software,
1210478SAndrew.Bardsley@arm.com * modified or unmodified, in source code or in binary form.
1310478SAndrew.Bardsley@arm.com *
1410478SAndrew.Bardsley@arm.com * Redistribution and use in source and binary forms, with or without
1510478SAndrew.Bardsley@arm.com * modification, are permitted provided that the following conditions are
1610478SAndrew.Bardsley@arm.com * met: redistributions of source code must retain the above copyright
1710478SAndrew.Bardsley@arm.com * notice, this list of conditions and the following disclaimer;
1810478SAndrew.Bardsley@arm.com * redistributions in binary form must reproduce the above copyright
1910478SAndrew.Bardsley@arm.com * notice, this list of conditions and the following disclaimer in the
2010478SAndrew.Bardsley@arm.com * documentation and/or other materials provided with the distribution;
2110478SAndrew.Bardsley@arm.com * neither the name of the copyright holders nor the names of its
2210478SAndrew.Bardsley@arm.com * contributors may be used to endorse or promote products derived from
2310478SAndrew.Bardsley@arm.com * this software without specific prior written permission.
2410478SAndrew.Bardsley@arm.com *
2510478SAndrew.Bardsley@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2610478SAndrew.Bardsley@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2710478SAndrew.Bardsley@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2810478SAndrew.Bardsley@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2910478SAndrew.Bardsley@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3010478SAndrew.Bardsley@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3110478SAndrew.Bardsley@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3210478SAndrew.Bardsley@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3310478SAndrew.Bardsley@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3410478SAndrew.Bardsley@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3510478SAndrew.Bardsley@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3610478SAndrew.Bardsley@arm.com *
3710478SAndrew.Bardsley@arm.com * Authors: Andrew Bardsley
3810478SAndrew.Bardsley@arm.com */
3910478SAndrew.Bardsley@arm.com
4011793Sbrandon.potter@amd.com#include "mem/external_slave.hh"
4111793Sbrandon.potter@amd.com
4210478SAndrew.Bardsley@arm.com#include <cctype>
4310478SAndrew.Bardsley@arm.com#include <iomanip>
4410478SAndrew.Bardsley@arm.com
4511800Sbrandon.potter@amd.com#include "base/trace.hh"
4610478SAndrew.Bardsley@arm.com#include "debug/ExternalPort.hh"
4710478SAndrew.Bardsley@arm.com
4810478SAndrew.Bardsley@arm.com/** Implement a `stub' port which just responds to requests by printing
4910478SAndrew.Bardsley@arm.com *  a message.  The stub port can be used to configure and test a system
5010478SAndrew.Bardsley@arm.com *  where the external port is used for a peripheral before connecting
5110478SAndrew.Bardsley@arm.com *  the external port */
5213784Sgabeblack@google.comclass StubSlavePort : public ExternalSlave::ExternalPort
5310478SAndrew.Bardsley@arm.com{
5410478SAndrew.Bardsley@arm.com  public:
5512128Sspwilson2@wisc.edu    void processResponseEvent();
5610478SAndrew.Bardsley@arm.com
5712128Sspwilson2@wisc.edu    EventFunctionWrapper responseEvent;
5810478SAndrew.Bardsley@arm.com
5910478SAndrew.Bardsley@arm.com    /** Stub can handle a single request at a time.  This will be
6010478SAndrew.Bardsley@arm.com     *  NULL when no packet is in flight */
6110478SAndrew.Bardsley@arm.com    PacketPtr responsePacket;
6210478SAndrew.Bardsley@arm.com
6310478SAndrew.Bardsley@arm.com    /** Received a new request while processing a first.  Need to ask for
6410478SAndrew.Bardsley@arm.com     *  a retry after completing this packet */
6510478SAndrew.Bardsley@arm.com    bool mustRetry;
6610478SAndrew.Bardsley@arm.com
6710478SAndrew.Bardsley@arm.com    StubSlavePort(const std::string &name_,
6810478SAndrew.Bardsley@arm.com        ExternalSlave &owner_) :
6913784Sgabeblack@google.com        ExternalSlave::ExternalPort(name_, owner_),
7012128Sspwilson2@wisc.edu        responseEvent([this]{ processResponseEvent(); }, name()),
7112128Sspwilson2@wisc.edu        responsePacket(NULL), mustRetry(false)
7210478SAndrew.Bardsley@arm.com    { }
7310478SAndrew.Bardsley@arm.com
7410478SAndrew.Bardsley@arm.com    Tick recvAtomic(PacketPtr packet);
7510478SAndrew.Bardsley@arm.com    void recvFunctional(PacketPtr packet);
7610478SAndrew.Bardsley@arm.com    bool recvTimingReq(PacketPtr packet);
7710478SAndrew.Bardsley@arm.com    bool recvTimingSnoopResp(PacketPtr packet);
7810713Sandreas.hansson@arm.com    void recvRespRetry();
7910478SAndrew.Bardsley@arm.com    void recvFunctionalSnoop(PacketPtr packet);
8010478SAndrew.Bardsley@arm.com};
8110478SAndrew.Bardsley@arm.com
8210478SAndrew.Bardsley@arm.comclass StubSlavePortHandler : public
8310478SAndrew.Bardsley@arm.com    ExternalSlave::Handler
8410478SAndrew.Bardsley@arm.com{
8510478SAndrew.Bardsley@arm.com  public:
8613784Sgabeblack@google.com    ExternalSlave::ExternalPort *getExternalPort(
8710478SAndrew.Bardsley@arm.com        const std::string &name_,
8810478SAndrew.Bardsley@arm.com        ExternalSlave &owner,
8910478SAndrew.Bardsley@arm.com        const std::string &port_data)
9010478SAndrew.Bardsley@arm.com    {
9110478SAndrew.Bardsley@arm.com        StringWrap name(name_);
9210478SAndrew.Bardsley@arm.com
9310478SAndrew.Bardsley@arm.com        DPRINTF(ExternalPort, "finding stub port '%s'\n", port_data);
9410478SAndrew.Bardsley@arm.com        return new StubSlavePort(name_, owner);
9510478SAndrew.Bardsley@arm.com    }
9610478SAndrew.Bardsley@arm.com};
9710478SAndrew.Bardsley@arm.com
9810478SAndrew.Bardsley@arm.comTick
9910478SAndrew.Bardsley@arm.comStubSlavePort::recvAtomic(PacketPtr packet)
10010478SAndrew.Bardsley@arm.com{
10110478SAndrew.Bardsley@arm.com    if (DTRACE(ExternalPort)) {
10210478SAndrew.Bardsley@arm.com        unsigned int M5_VAR_USED size = packet->getSize();
10310478SAndrew.Bardsley@arm.com
10410478SAndrew.Bardsley@arm.com        DPRINTF(ExternalPort, "StubSlavePort: recvAtomic a: 0x%x size: %d"
10510478SAndrew.Bardsley@arm.com            " data: ...\n", packet->getAddr(), size);
10610563Sandreas.hansson@arm.com        DDUMP(ExternalPort, packet->getConstPtr<uint8_t>(), size);
10710478SAndrew.Bardsley@arm.com    }
10810478SAndrew.Bardsley@arm.com
10910478SAndrew.Bardsley@arm.com    return 0;
11010478SAndrew.Bardsley@arm.com}
11110478SAndrew.Bardsley@arm.com
11210478SAndrew.Bardsley@arm.comvoid
11310478SAndrew.Bardsley@arm.comStubSlavePort::recvFunctional(PacketPtr packet)
11410478SAndrew.Bardsley@arm.com{
11510478SAndrew.Bardsley@arm.com    recvAtomic(packet);
11610478SAndrew.Bardsley@arm.com}
11710478SAndrew.Bardsley@arm.com
11810478SAndrew.Bardsley@arm.comvoid
11912128Sspwilson2@wisc.eduStubSlavePort::processResponseEvent()
12010478SAndrew.Bardsley@arm.com{
12112128Sspwilson2@wisc.edu    responsePacket->makeResponse();
12212128Sspwilson2@wisc.edu    responsePacket->headerDelay = 0;
12312128Sspwilson2@wisc.edu    responsePacket->payloadDelay = 0;
12410478SAndrew.Bardsley@arm.com
12512128Sspwilson2@wisc.edu    if (sendTimingResp(responsePacket)) {
12612128Sspwilson2@wisc.edu        responsePacket = NULL;
12710478SAndrew.Bardsley@arm.com
12812128Sspwilson2@wisc.edu        if (mustRetry)
12912128Sspwilson2@wisc.edu            sendRetryReq();
13012128Sspwilson2@wisc.edu        mustRetry = false;
13110478SAndrew.Bardsley@arm.com    }
13210478SAndrew.Bardsley@arm.com}
13310478SAndrew.Bardsley@arm.com
13410478SAndrew.Bardsley@arm.combool
13510478SAndrew.Bardsley@arm.comStubSlavePort::recvTimingReq(PacketPtr packet)
13610478SAndrew.Bardsley@arm.com{
13710478SAndrew.Bardsley@arm.com    if (responsePacket) {
13810478SAndrew.Bardsley@arm.com        mustRetry = true;
13910478SAndrew.Bardsley@arm.com
14010478SAndrew.Bardsley@arm.com        return false;
14110478SAndrew.Bardsley@arm.com    } else {
14210478SAndrew.Bardsley@arm.com        recvAtomic(packet);
14310478SAndrew.Bardsley@arm.com
14410478SAndrew.Bardsley@arm.com        responsePacket = packet;
14510478SAndrew.Bardsley@arm.com        owner.schedule(responseEvent, curTick());
14610478SAndrew.Bardsley@arm.com
14710478SAndrew.Bardsley@arm.com        return true;
14810478SAndrew.Bardsley@arm.com    }
14910478SAndrew.Bardsley@arm.com}
15010478SAndrew.Bardsley@arm.com
15110478SAndrew.Bardsley@arm.combool
15210478SAndrew.Bardsley@arm.comStubSlavePort::recvTimingSnoopResp(PacketPtr packet)
15310478SAndrew.Bardsley@arm.com{
15410478SAndrew.Bardsley@arm.com    fatal("StubSlavePort: function: %s\n", __func__);
15510478SAndrew.Bardsley@arm.com    return false;
15610478SAndrew.Bardsley@arm.com}
15710478SAndrew.Bardsley@arm.com
15810478SAndrew.Bardsley@arm.comvoid
15910713Sandreas.hansson@arm.comStubSlavePort::recvRespRetry()
16010478SAndrew.Bardsley@arm.com{
16110478SAndrew.Bardsley@arm.com    assert(responsePacket);
16210478SAndrew.Bardsley@arm.com    /* Stub handles only one response at a time so responseEvent should never
16310478SAndrew.Bardsley@arm.com     *  be scheduled at this point.  Retrys shouldn't need to schedule, we
16410478SAndrew.Bardsley@arm.com     *  can safely send the response here */
16510478SAndrew.Bardsley@arm.com    responseEvent.process();
16610478SAndrew.Bardsley@arm.com}
16710478SAndrew.Bardsley@arm.com
16810478SAndrew.Bardsley@arm.comvoid
16910478SAndrew.Bardsley@arm.comStubSlavePort::recvFunctionalSnoop(PacketPtr packet)
17010478SAndrew.Bardsley@arm.com{
17110478SAndrew.Bardsley@arm.com    fatal("StubSlavePort: unimplemented function: %s\n", __func__);
17210478SAndrew.Bardsley@arm.com}
17310478SAndrew.Bardsley@arm.com
17410478SAndrew.Bardsley@arm.comstd::map<std::string, ExternalSlave::Handler *>
17510478SAndrew.Bardsley@arm.com    ExternalSlave::portHandlers;
17610478SAndrew.Bardsley@arm.com
17710478SAndrew.Bardsley@arm.comAddrRangeList
17813784Sgabeblack@google.comExternalSlave::ExternalPort::getAddrRanges() const
17910478SAndrew.Bardsley@arm.com{
18010478SAndrew.Bardsley@arm.com    return owner.addrRanges;
18110478SAndrew.Bardsley@arm.com}
18210478SAndrew.Bardsley@arm.com
18310478SAndrew.Bardsley@arm.comExternalSlave::ExternalSlave(ExternalSlaveParams *params) :
18413892Sgabeblack@google.com    SimObject(params),
18510478SAndrew.Bardsley@arm.com    externalPort(NULL),
18610478SAndrew.Bardsley@arm.com    portName(params->name + ".port"),
18710478SAndrew.Bardsley@arm.com    portType(params->port_type),
18810478SAndrew.Bardsley@arm.com    portData(params->port_data),
18910478SAndrew.Bardsley@arm.com    addrRanges(params->addr_ranges.begin(), params->addr_ranges.end())
19010478SAndrew.Bardsley@arm.com{
19110478SAndrew.Bardsley@arm.com    /* Register the stub handler if it hasn't already been registered */
19210478SAndrew.Bardsley@arm.com    if (portHandlers.find("stub") == portHandlers.end())
19310478SAndrew.Bardsley@arm.com        registerHandler("stub", new StubSlavePortHandler);
19410478SAndrew.Bardsley@arm.com}
19510478SAndrew.Bardsley@arm.com
19613784Sgabeblack@google.comPort &
19713784Sgabeblack@google.comExternalSlave::getPort(const std::string &if_name, PortID idx)
19810478SAndrew.Bardsley@arm.com{
19910478SAndrew.Bardsley@arm.com    if (if_name == "port") {
20010478SAndrew.Bardsley@arm.com        DPRINTF(ExternalPort, "Trying to bind external port: %s %s\n",
20110478SAndrew.Bardsley@arm.com            portType, portName);
20210478SAndrew.Bardsley@arm.com
20310478SAndrew.Bardsley@arm.com        if (!externalPort) {
20410478SAndrew.Bardsley@arm.com            auto handlerIter = portHandlers.find(portType);
20510478SAndrew.Bardsley@arm.com
20610478SAndrew.Bardsley@arm.com            if (handlerIter == portHandlers.end())
20710478SAndrew.Bardsley@arm.com                fatal("Can't find port handler type '%s'\n", portType);
20810478SAndrew.Bardsley@arm.com
20910478SAndrew.Bardsley@arm.com            externalPort = portHandlers[portType]->getExternalPort(portName,
21010478SAndrew.Bardsley@arm.com                *this, portData);
21110478SAndrew.Bardsley@arm.com
21210478SAndrew.Bardsley@arm.com            if (!externalPort) {
21310478SAndrew.Bardsley@arm.com                fatal("%s: Can't find external port type: %s"
21410478SAndrew.Bardsley@arm.com                    " port_data: '%s'\n", portName, portType, portData);
21510478SAndrew.Bardsley@arm.com            }
21610478SAndrew.Bardsley@arm.com        }
21710478SAndrew.Bardsley@arm.com        return *externalPort;
21810478SAndrew.Bardsley@arm.com    } else {
21913892Sgabeblack@google.com        return SimObject::getPort(if_name, idx);
22010478SAndrew.Bardsley@arm.com    }
22110478SAndrew.Bardsley@arm.com}
22210478SAndrew.Bardsley@arm.com
22310478SAndrew.Bardsley@arm.comvoid
22410478SAndrew.Bardsley@arm.comExternalSlave::init()
22510478SAndrew.Bardsley@arm.com{
22610478SAndrew.Bardsley@arm.com    if (!externalPort) {
22710478SAndrew.Bardsley@arm.com        fatal("ExternalSlave %s: externalPort not set!\n", name());
22810478SAndrew.Bardsley@arm.com    } else if (!externalPort->isConnected()) {
22910478SAndrew.Bardsley@arm.com        fatal("ExternalSlave %s is unconnected!\n", name());
23010478SAndrew.Bardsley@arm.com    } else {
23110478SAndrew.Bardsley@arm.com        externalPort->sendRangeChange();
23210478SAndrew.Bardsley@arm.com    }
23310478SAndrew.Bardsley@arm.com}
23410478SAndrew.Bardsley@arm.com
23510478SAndrew.Bardsley@arm.comExternalSlave *
23610478SAndrew.Bardsley@arm.comExternalSlaveParams::create()
23710478SAndrew.Bardsley@arm.com{
23810478SAndrew.Bardsley@arm.com    return new ExternalSlave(this);
23910478SAndrew.Bardsley@arm.com}
24010478SAndrew.Bardsley@arm.com
24110478SAndrew.Bardsley@arm.comvoid
24210478SAndrew.Bardsley@arm.comExternalSlave::registerHandler(const std::string &handler_name,
24310478SAndrew.Bardsley@arm.com    Handler *handler)
24410478SAndrew.Bardsley@arm.com{
24510478SAndrew.Bardsley@arm.com    portHandlers[handler_name] = handler;
24610478SAndrew.Bardsley@arm.com}
247