1/*
2 * Copyright 2019 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Copyright (c) 2015, University of Kaiserslautern
28 * Copyright (c) 2016, Dresden University of Technology (TU Dresden)
29 * All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions are
33 * met:
34 *
35 * 1. Redistributions of source code must retain the above copyright notice,
36 *    this list of conditions and the following disclaimer.
37 *
38 * 2. Redistributions in binary form must reproduce the above copyright
39 *    notice, this list of conditions and the following disclaimer in the
40 *    documentation and/or other materials provided with the distribution.
41 *
42 * 3. Neither the name of the copyright holder nor the names of its
43 *    contributors may be used to endorse or promote products derived from
44 *    this software without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
47 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
50 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
51 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
52 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
53 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
54 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
55 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
56 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 *
58 * Authors: Gabe Black
59 *          Matthias Jung
60 *          Christian Menard
61 */
62
63#ifndef __SYSTEMC_TLM_BRIDGE_GEM5_TO_TLM_HH__
64#define __SYSTEMC_TLM_BRIDGE_GEM5_TO_TLM_HH__
65
66#include <string>
67
68#include "mem/port.hh"
69#include "params/Gem5ToTlmBridgeBase.hh"
70#include "sim/system.hh"
71#include "systemc/ext/core/sc_module.hh"
72#include "systemc/ext/core/sc_module_name.hh"
73#include "systemc/ext/tlm_core/2/generic_payload/gp.hh"
74#include "systemc/ext/tlm_utils/simple_initiator_socket.h"
75#include "systemc/tlm_bridge/sc_peq.hh"
76#include "systemc/tlm_port_wrapper.hh"
77
78namespace sc_gem5
79{
80
81tlm::tlm_generic_payload *packet2payload(PacketPtr packet);
82
83class Gem5ToTlmBridgeBase : public sc_core::sc_module
84{
85  protected:
86    using sc_core::sc_module::sc_module;
87};
88
89template <unsigned int BITWIDTH>
90class Gem5ToTlmBridge : public Gem5ToTlmBridgeBase
91{
92  private:
93    class BridgeSlavePort : public SlavePort
94    {
95      protected:
96        Gem5ToTlmBridge<BITWIDTH> &bridge;
97
98        AddrRangeList
99        getAddrRanges() const override
100        {
101            return bridge.getAddrRanges();
102        }
103        Tick
104        recvAtomic(PacketPtr pkt) override
105        {
106            return bridge.recvAtomic(pkt);
107        }
108        Tick
109        recvAtomicBackdoor(PacketPtr pkt, MemBackdoorPtr &backdoor) override
110        {
111            return bridge.recvAtomicBackdoor(pkt, backdoor);
112        }
113        void
114        recvFunctional(PacketPtr pkt) override
115        {
116            return bridge.recvFunctional(pkt);
117        }
118        bool
119        recvTimingReq(PacketPtr pkt) override
120        {
121            return bridge.recvTimingReq(pkt);
122        }
123        bool
124        tryTiming(PacketPtr pkt) override
125        {
126            return bridge.tryTiming(pkt);
127        }
128        bool
129        recvTimingSnoopResp(PacketPtr pkt) override
130        {
131            return bridge.recvTimingSnoopResp(pkt);
132        }
133        void recvRespRetry() override { bridge.recvRespRetry(); }
134
135      public:
136        BridgeSlavePort(const std::string &name_,
137                        Gem5ToTlmBridge<BITWIDTH> &bridge_) :
138            SlavePort(name_, nullptr), bridge(bridge_)
139        {}
140    };
141
142    BridgeSlavePort bsp;
143    tlm_utils::simple_initiator_socket<
144        Gem5ToTlmBridge<BITWIDTH>, BITWIDTH> socket;
145    sc_gem5::TlmInitiatorWrapper<BITWIDTH> wrapper;
146
147    System *system;
148
149    /**
150     * A transaction after BEGIN_REQ has been sent but before END_REQ, which
151     * is blocking the request channel (Exlusion Rule, see IEEE1666)
152     */
153    tlm::tlm_generic_payload *blockingRequest;
154
155    /**
156     * Did another gem5 request arrive while currently blocked?
157     * This variable is needed when a retry should happen
158     */
159    bool needToSendRequestRetry;
160
161    /**
162     * A response which has been asked to retry by gem5 and so is blocking
163     * the response channel
164     */
165    tlm::tlm_generic_payload *blockingResponse;
166
167    AddrRangeList addrRanges;
168
169  protected:
170    void pec(Gem5SystemC::PayloadEvent<Gem5ToTlmBridge<BITWIDTH>> *pe,
171             tlm::tlm_generic_payload &trans, const tlm::tlm_phase &phase);
172
173    MemBackdoorPtr getBackdoor(tlm::tlm_generic_payload &trans);
174    AddrRangeMap<MemBackdoorPtr> backdoorMap;
175
176    // The gem5 port interface.
177    Tick recvAtomic(PacketPtr packet);
178    Tick recvAtomicBackdoor(PacketPtr pkt, MemBackdoorPtr &backdoor);
179    void recvFunctional(PacketPtr packet);
180    bool recvTimingReq(PacketPtr packet);
181    bool tryTiming(PacketPtr packet);
182    bool recvTimingSnoopResp(PacketPtr packet);
183    void recvRespRetry();
184    void recvFunctionalSnoop(PacketPtr packet);
185    AddrRangeList getAddrRanges() const { return addrRanges; }
186
187    // The TLM initiator interface.
188    tlm::tlm_sync_enum nb_transport_bw(tlm::tlm_generic_payload &trans,
189                                       tlm::tlm_phase &phase,
190                                       sc_core::sc_time &t);
191    void invalidate_direct_mem_ptr(
192            sc_dt::uint64 start_range, sc_dt::uint64 end_range);
193
194  public:
195    ::Port &gem5_getPort(const std::string &if_name, int idx=-1) override;
196
197    typedef Gem5ToTlmBridgeBaseParams Params;
198    Gem5ToTlmBridge(Params *p, const sc_core::sc_module_name &mn);
199
200    tlm_utils::simple_initiator_socket<Gem5ToTlmBridge<BITWIDTH>, BITWIDTH> &
201    getSocket()
202    {
203        return socket;
204    }
205
206    void before_end_of_elaboration() override;
207};
208
209} // namespace sc_gem5
210
211#endif // __SYSTEMC_TLM_BRIDGE_GEM5_TO_TLM_HH__
212