113819Sgabeblack@google.com/*
213819Sgabeblack@google.com * Copyright 2018 Google, Inc.
313819Sgabeblack@google.com *
413819Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
513819Sgabeblack@google.com * modification, are permitted provided that the following conditions are
613819Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
713819Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
813819Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
913819Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1013819Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1113819Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1213819Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1313819Sgabeblack@google.com * this software without specific prior written permission.
1413819Sgabeblack@google.com *
1513819Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1613819Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1713819Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1813819Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1913819Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2013819Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2113819Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2213819Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2313819Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2413819Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2513819Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2613819Sgabeblack@google.com *
2713819Sgabeblack@google.com * Authors: Gabe Black
2813819Sgabeblack@google.com */
2913819Sgabeblack@google.com
3013819Sgabeblack@google.com#ifndef __SYSTEMC_TLM_PORT_WRAPPER_HH__
3113819Sgabeblack@google.com#define __SYSTEMC_TLM_PORT_WRAPPER_HH__
3213819Sgabeblack@google.com
3313819Sgabeblack@google.com#include "base/logging.hh"
3413819Sgabeblack@google.com#include "sim/port.hh"
3513819Sgabeblack@google.com#include "systemc/ext/tlm_core/2/sockets/sockets.hh"
3613819Sgabeblack@google.com
3713819Sgabeblack@google.comnamespace sc_gem5
3813819Sgabeblack@google.com{
3913819Sgabeblack@google.com
4014276Sgabeblack@google.comtemplate <unsigned int BUSWIDTH, typename FW_IF, typename BW_IF, int N,
4114276Sgabeblack@google.com          sc_core::sc_port_policy POL>
4214276Sgabeblack@google.comclass TlmInitiatorBaseWrapper;
4313819Sgabeblack@google.com
4414276Sgabeblack@google.comtemplate <unsigned int BUSWIDTH, typename FW_IF, typename BW_IF, int N,
4514276Sgabeblack@google.com          sc_core::sc_port_policy POL>
4614276Sgabeblack@google.comclass TlmTargetBaseWrapper;
4713819Sgabeblack@google.com
4814276Sgabeblack@google.comtemplate <unsigned int BUSWIDTH, typename FW_IF, typename BW_IF, int N,
4913819Sgabeblack@google.com          sc_core::sc_port_policy POL>
5014276Sgabeblack@google.comclass TlmInitiatorBaseWrapper : public ::Port
5113819Sgabeblack@google.com{
5213819Sgabeblack@google.com  public:
5314276Sgabeblack@google.com    typedef tlm::tlm_base_initiator_socket<BUSWIDTH, FW_IF, BW_IF, N, POL>
5413819Sgabeblack@google.com        InitiatorSocket;
5513819Sgabeblack@google.com    typedef typename InitiatorSocket::base_target_socket_type TargetSocket;
5614276Sgabeblack@google.com    typedef TlmTargetBaseWrapper<BUSWIDTH, FW_IF, BW_IF, N, POL> TargetWrapper;
5713819Sgabeblack@google.com
5813819Sgabeblack@google.com    InitiatorSocket &initiator() { return _initiator; }
5913819Sgabeblack@google.com
6014276Sgabeblack@google.com    TlmInitiatorBaseWrapper(
6113819Sgabeblack@google.com            InitiatorSocket &i, const std::string &_name, PortID _id) :
6213819Sgabeblack@google.com        Port(_name, _id), _initiator(i)
6313819Sgabeblack@google.com    {}
6413819Sgabeblack@google.com
6513819Sgabeblack@google.com    void
6613819Sgabeblack@google.com    bind(::Port &peer) override
6713819Sgabeblack@google.com    {
6813819Sgabeblack@google.com        auto *target = dynamic_cast<TargetWrapper *>(&peer);
6913819Sgabeblack@google.com        fatal_if(!target, "Attempt to bind TLM initiator socket %s to "
7013819Sgabeblack@google.com                "incompatible port %s.", name(), peer.name());
7113819Sgabeblack@google.com
7213819Sgabeblack@google.com        initiator().bind(target->target());
7314189Sgabeblack@google.com        Port::bind(peer);
7413819Sgabeblack@google.com    }
7513819Sgabeblack@google.com
7613819Sgabeblack@google.com    void
7713819Sgabeblack@google.com    unbind() override
7813819Sgabeblack@google.com    {
7913819Sgabeblack@google.com        panic("TLM sockets can't be unbound.");
8013819Sgabeblack@google.com    }
8113819Sgabeblack@google.com
8213819Sgabeblack@google.com  private:
8313819Sgabeblack@google.com    InitiatorSocket &_initiator;
8413819Sgabeblack@google.com};
8513819Sgabeblack@google.com
8614276Sgabeblack@google.comtemplate <unsigned int BUSWIDTH, typename FW_IF, typename BW_IF, int N,
8713819Sgabeblack@google.com          sc_core::sc_port_policy POL>
8814276Sgabeblack@google.comclass TlmTargetBaseWrapper : public ::Port
8913819Sgabeblack@google.com{
9013819Sgabeblack@google.com  public:
9114276Sgabeblack@google.com    typedef tlm::tlm_base_target_socket<BUSWIDTH, FW_IF, BW_IF, N, POL>
9213819Sgabeblack@google.com        TargetSocket;
9313819Sgabeblack@google.com
9413819Sgabeblack@google.com    TargetSocket &target() { return _target; }
9513819Sgabeblack@google.com
9614276Sgabeblack@google.com    TlmTargetBaseWrapper(TargetSocket &t, const std::string &_name,
9714276Sgabeblack@google.com                         PortID _id) :
9813819Sgabeblack@google.com        Port(_name, _id), _target(t)
9913819Sgabeblack@google.com    {}
10013819Sgabeblack@google.com
10113819Sgabeblack@google.com    void
10213819Sgabeblack@google.com    bind(::Port &peer) override
10313819Sgabeblack@google.com    {
10413819Sgabeblack@google.com        // Ignore attempts to bind a target socket. The initiator will
10513819Sgabeblack@google.com        // handle it.
10614189Sgabeblack@google.com        Port::bind(peer);
10713819Sgabeblack@google.com    }
10813819Sgabeblack@google.com
10913819Sgabeblack@google.com    void
11013819Sgabeblack@google.com    unbind() override
11113819Sgabeblack@google.com    {
11213819Sgabeblack@google.com        panic("TLM sockets can't be unbound.");
11313819Sgabeblack@google.com    }
11413819Sgabeblack@google.com
11513819Sgabeblack@google.com  private:
11613819Sgabeblack@google.com    TargetSocket &_target;
11713819Sgabeblack@google.com};
11813819Sgabeblack@google.com
11914276Sgabeblack@google.comtemplate <unsigned int BUSWIDTH=32,
12014276Sgabeblack@google.com          typename TYPES=tlm::tlm_base_protocol_types, int N=1,
12114276Sgabeblack@google.com          sc_core::sc_port_policy POL=sc_core::SC_ONE_OR_MORE_BOUND>
12214276Sgabeblack@google.comusing TlmInitiatorWrapper =
12314276Sgabeblack@google.com    TlmInitiatorBaseWrapper<BUSWIDTH, tlm::tlm_fw_transport_if<TYPES>,
12414276Sgabeblack@google.com                            tlm::tlm_bw_transport_if<TYPES>, N, POL>;
12514276Sgabeblack@google.com
12614276Sgabeblack@google.comtemplate <unsigned int BUSWIDTH=32,
12714276Sgabeblack@google.com          typename TYPES=tlm::tlm_base_protocol_types, int N=1,
12814276Sgabeblack@google.com          sc_core::sc_port_policy POL=sc_core::SC_ONE_OR_MORE_BOUND>
12914276Sgabeblack@google.comusing TlmTargetWrapper =
13014276Sgabeblack@google.com    TlmTargetBaseWrapper<BUSWIDTH, tlm::tlm_fw_transport_if<TYPES>,
13114276Sgabeblack@google.com                         tlm::tlm_bw_transport_if<TYPES>, N, POL>;
13214276Sgabeblack@google.com
13313819Sgabeblack@google.com} // namespace sc_gem5
13413819Sgabeblack@google.com
13513819Sgabeblack@google.com#endif  //__SYSTEMC_TLM_PORT_WRAPPER_HH__
136