113821Sgabeblack@google.com/*
213821Sgabeblack@google.com * Copyright 2019 Google, Inc.
313821Sgabeblack@google.com *
413821Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
513821Sgabeblack@google.com * modification, are permitted provided that the following conditions are
613821Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
713821Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
813821Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
913821Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1013821Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1113821Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1213821Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1313821Sgabeblack@google.com * this software without specific prior written permission.
1413821Sgabeblack@google.com *
1513821Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1613821Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1713821Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1813821Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1913821Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2013821Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2113821Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2213821Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2313821Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2413821Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2513821Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2613821Sgabeblack@google.com *
2713821Sgabeblack@google.com * Copyright (c) 2015, University of Kaiserslautern
2813821Sgabeblack@google.com * Copyright (c) 2016, Dresden University of Technology (TU Dresden)
2913821Sgabeblack@google.com * All rights reserved.
3013821Sgabeblack@google.com *
3113821Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
3213821Sgabeblack@google.com * modification, are permitted provided that the following conditions are
3313821Sgabeblack@google.com * met:
3413821Sgabeblack@google.com *
3513821Sgabeblack@google.com * 1. Redistributions of source code must retain the above copyright notice,
3613821Sgabeblack@google.com *    this list of conditions and the following disclaimer.
3713821Sgabeblack@google.com *
3813821Sgabeblack@google.com * 2. Redistributions in binary form must reproduce the above copyright
3913821Sgabeblack@google.com *    notice, this list of conditions and the following disclaimer in the
4013821Sgabeblack@google.com *    documentation and/or other materials provided with the distribution.
4113821Sgabeblack@google.com *
4213821Sgabeblack@google.com * 3. Neither the name of the copyright holder nor the names of its
4313821Sgabeblack@google.com *    contributors may be used to endorse or promote products derived from
4413821Sgabeblack@google.com *    this software without specific prior written permission.
4513821Sgabeblack@google.com *
4613821Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4713821Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4813821Sgabeblack@google.com * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4913821Sgabeblack@google.com * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
5013821Sgabeblack@google.com * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5113821Sgabeblack@google.com * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5213821Sgabeblack@google.com * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5313821Sgabeblack@google.com * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5413821Sgabeblack@google.com * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5513821Sgabeblack@google.com * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5613821Sgabeblack@google.com * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5713821Sgabeblack@google.com *
5813821Sgabeblack@google.com * Authors: Gabe Black
5913821Sgabeblack@google.com *          Matthias Jung
6013821Sgabeblack@google.com *          Christian Menard
6113821Sgabeblack@google.com */
6213821Sgabeblack@google.com
6313821Sgabeblack@google.com#ifndef __SYSTEMC_TLM_BRIDGE_GEM5_TO_TLM_HH__
6413821Sgabeblack@google.com#define __SYSTEMC_TLM_BRIDGE_GEM5_TO_TLM_HH__
6513821Sgabeblack@google.com
6613821Sgabeblack@google.com#include <string>
6713821Sgabeblack@google.com
6813821Sgabeblack@google.com#include "mem/port.hh"
6913823Sgabeblack@google.com#include "params/Gem5ToTlmBridgeBase.hh"
7013821Sgabeblack@google.com#include "sim/system.hh"
7113821Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh"
7213821Sgabeblack@google.com#include "systemc/ext/core/sc_module_name.hh"
7313821Sgabeblack@google.com#include "systemc/ext/tlm_core/2/generic_payload/gp.hh"
7413821Sgabeblack@google.com#include "systemc/ext/tlm_utils/simple_initiator_socket.h"
7513821Sgabeblack@google.com#include "systemc/tlm_bridge/sc_peq.hh"
7613821Sgabeblack@google.com#include "systemc/tlm_port_wrapper.hh"
7713821Sgabeblack@google.com
7813821Sgabeblack@google.comnamespace sc_gem5
7913821Sgabeblack@google.com{
8013821Sgabeblack@google.com
8114275Sgabeblack@google.comtlm::tlm_generic_payload *packet2payload(PacketPtr packet);
8214275Sgabeblack@google.com
8313823Sgabeblack@google.comclass Gem5ToTlmBridgeBase : public sc_core::sc_module
8413823Sgabeblack@google.com{
8513823Sgabeblack@google.com  protected:
8613823Sgabeblack@google.com    using sc_core::sc_module::sc_module;
8713823Sgabeblack@google.com};
8813823Sgabeblack@google.com
8913823Sgabeblack@google.comtemplate <unsigned int BITWIDTH>
9013823Sgabeblack@google.comclass Gem5ToTlmBridge : public Gem5ToTlmBridgeBase
9113821Sgabeblack@google.com{
9213821Sgabeblack@google.com  private:
9313821Sgabeblack@google.com    class BridgeSlavePort : public SlavePort
9413821Sgabeblack@google.com    {
9513821Sgabeblack@google.com      protected:
9613823Sgabeblack@google.com        Gem5ToTlmBridge<BITWIDTH> &bridge;
9713821Sgabeblack@google.com
9813821Sgabeblack@google.com        AddrRangeList
9913821Sgabeblack@google.com        getAddrRanges() const override
10013821Sgabeblack@google.com        {
10113821Sgabeblack@google.com            return bridge.getAddrRanges();
10213821Sgabeblack@google.com        }
10313821Sgabeblack@google.com        Tick
10413821Sgabeblack@google.com        recvAtomic(PacketPtr pkt) override
10513821Sgabeblack@google.com        {
10613821Sgabeblack@google.com            return bridge.recvAtomic(pkt);
10713821Sgabeblack@google.com        }
10813846Sgabeblack@google.com        Tick
10913846Sgabeblack@google.com        recvAtomicBackdoor(PacketPtr pkt, MemBackdoorPtr &backdoor) override
11013846Sgabeblack@google.com        {
11113846Sgabeblack@google.com            return bridge.recvAtomicBackdoor(pkt, backdoor);
11213846Sgabeblack@google.com        }
11313821Sgabeblack@google.com        void
11413821Sgabeblack@google.com        recvFunctional(PacketPtr pkt) override
11513821Sgabeblack@google.com        {
11613821Sgabeblack@google.com            return bridge.recvFunctional(pkt);
11713821Sgabeblack@google.com        }
11813821Sgabeblack@google.com        bool
11913821Sgabeblack@google.com        recvTimingReq(PacketPtr pkt) override
12013821Sgabeblack@google.com        {
12113821Sgabeblack@google.com            return bridge.recvTimingReq(pkt);
12213821Sgabeblack@google.com        }
12313821Sgabeblack@google.com        bool
12413821Sgabeblack@google.com        tryTiming(PacketPtr pkt) override
12513821Sgabeblack@google.com        {
12613821Sgabeblack@google.com            return bridge.tryTiming(pkt);
12713821Sgabeblack@google.com        }
12813821Sgabeblack@google.com        bool
12913821Sgabeblack@google.com        recvTimingSnoopResp(PacketPtr pkt) override
13013821Sgabeblack@google.com        {
13113821Sgabeblack@google.com            return bridge.recvTimingSnoopResp(pkt);
13213821Sgabeblack@google.com        }
13313821Sgabeblack@google.com        void recvRespRetry() override { bridge.recvRespRetry(); }
13413821Sgabeblack@google.com
13513821Sgabeblack@google.com      public:
13613823Sgabeblack@google.com        BridgeSlavePort(const std::string &name_,
13713823Sgabeblack@google.com                        Gem5ToTlmBridge<BITWIDTH> &bridge_) :
13813821Sgabeblack@google.com            SlavePort(name_, nullptr), bridge(bridge_)
13913821Sgabeblack@google.com        {}
14013821Sgabeblack@google.com    };
14113821Sgabeblack@google.com
14213821Sgabeblack@google.com    BridgeSlavePort bsp;
14313823Sgabeblack@google.com    tlm_utils::simple_initiator_socket<
14413823Sgabeblack@google.com        Gem5ToTlmBridge<BITWIDTH>, BITWIDTH> socket;
14513823Sgabeblack@google.com    sc_gem5::TlmInitiatorWrapper<BITWIDTH> wrapper;
14613821Sgabeblack@google.com
14713821Sgabeblack@google.com    System *system;
14813821Sgabeblack@google.com
14913821Sgabeblack@google.com    /**
15013821Sgabeblack@google.com     * A transaction after BEGIN_REQ has been sent but before END_REQ, which
15113821Sgabeblack@google.com     * is blocking the request channel (Exlusion Rule, see IEEE1666)
15213821Sgabeblack@google.com     */
15313821Sgabeblack@google.com    tlm::tlm_generic_payload *blockingRequest;
15413821Sgabeblack@google.com
15513821Sgabeblack@google.com    /**
15613821Sgabeblack@google.com     * Did another gem5 request arrive while currently blocked?
15713821Sgabeblack@google.com     * This variable is needed when a retry should happen
15813821Sgabeblack@google.com     */
15913821Sgabeblack@google.com    bool needToSendRequestRetry;
16013821Sgabeblack@google.com
16113821Sgabeblack@google.com    /**
16213821Sgabeblack@google.com     * A response which has been asked to retry by gem5 and so is blocking
16313821Sgabeblack@google.com     * the response channel
16413821Sgabeblack@google.com     */
16513821Sgabeblack@google.com    tlm::tlm_generic_payload *blockingResponse;
16613821Sgabeblack@google.com
16713821Sgabeblack@google.com    AddrRangeList addrRanges;
16813821Sgabeblack@google.com
16913821Sgabeblack@google.com  protected:
17013823Sgabeblack@google.com    void pec(Gem5SystemC::PayloadEvent<Gem5ToTlmBridge<BITWIDTH>> *pe,
17113821Sgabeblack@google.com             tlm::tlm_generic_payload &trans, const tlm::tlm_phase &phase);
17213821Sgabeblack@google.com
17313846Sgabeblack@google.com    MemBackdoorPtr getBackdoor(tlm::tlm_generic_payload &trans);
17413846Sgabeblack@google.com    AddrRangeMap<MemBackdoorPtr> backdoorMap;
17513846Sgabeblack@google.com
17613821Sgabeblack@google.com    // The gem5 port interface.
17713821Sgabeblack@google.com    Tick recvAtomic(PacketPtr packet);
17813846Sgabeblack@google.com    Tick recvAtomicBackdoor(PacketPtr pkt, MemBackdoorPtr &backdoor);
17913821Sgabeblack@google.com    void recvFunctional(PacketPtr packet);
18013821Sgabeblack@google.com    bool recvTimingReq(PacketPtr packet);
18113821Sgabeblack@google.com    bool tryTiming(PacketPtr packet);
18213821Sgabeblack@google.com    bool recvTimingSnoopResp(PacketPtr packet);
18313821Sgabeblack@google.com    void recvRespRetry();
18413821Sgabeblack@google.com    void recvFunctionalSnoop(PacketPtr packet);
18513821Sgabeblack@google.com    AddrRangeList getAddrRanges() const { return addrRanges; }
18613821Sgabeblack@google.com
18713821Sgabeblack@google.com    // The TLM initiator interface.
18813821Sgabeblack@google.com    tlm::tlm_sync_enum nb_transport_bw(tlm::tlm_generic_payload &trans,
18913821Sgabeblack@google.com                                       tlm::tlm_phase &phase,
19013821Sgabeblack@google.com                                       sc_core::sc_time &t);
19113846Sgabeblack@google.com    void invalidate_direct_mem_ptr(
19213846Sgabeblack@google.com            sc_dt::uint64 start_range, sc_dt::uint64 end_range);
19313821Sgabeblack@google.com
19413821Sgabeblack@google.com  public:
19513821Sgabeblack@google.com    ::Port &gem5_getPort(const std::string &if_name, int idx=-1) override;
19613821Sgabeblack@google.com
19713823Sgabeblack@google.com    typedef Gem5ToTlmBridgeBaseParams Params;
19813821Sgabeblack@google.com    Gem5ToTlmBridge(Params *p, const sc_core::sc_module_name &mn);
19913821Sgabeblack@google.com
20013823Sgabeblack@google.com    tlm_utils::simple_initiator_socket<Gem5ToTlmBridge<BITWIDTH>, BITWIDTH> &
20113821Sgabeblack@google.com    getSocket()
20213821Sgabeblack@google.com    {
20313821Sgabeblack@google.com        return socket;
20413821Sgabeblack@google.com    }
20513821Sgabeblack@google.com
20613821Sgabeblack@google.com    void before_end_of_elaboration() override;
20713821Sgabeblack@google.com};
20813821Sgabeblack@google.com
20913821Sgabeblack@google.com} // namespace sc_gem5
21013821Sgabeblack@google.com
21113821Sgabeblack@google.com#endif // __SYSTEMC_TLM_BRIDGE_GEM5_TO_TLM_HH__
212