tlm_to_gem5.hh revision 13821:f9252f27ded7
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) 2016, Dresden University of Technology (TU Dresden)
28 * All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions are
32 * met:
33 *
34 * 1. Redistributions of source code must retain the above copyright notice,
35 *    this list of conditions and the following disclaimer.
36 *
37 * 2. Redistributions in binary form must reproduce the above copyright
38 *    notice, this list of conditions and the following disclaimer in the
39 *    documentation and/or other materials provided with the distribution.
40 *
41 * 3. Neither the name of the copyright holder nor the names of its
42 *    contributors may be used to endorse or promote products derived from
43 *    this software without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
49 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
52 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
53 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
54 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 *
57 * Authors: Gabe Black
58 *          Christian Menard
59 */
60
61#ifndef __SYSTEMC_TLM_BRIDGE_TLM_TO_GEM5_HH__
62#define __SYSTEMC_TLM_BRIDGE_TLM_TO_GEM5_HH__
63
64#include "mem/port.hh"
65#include "params/TlmToGem5Bridge.hh"
66#include "systemc/ext/core/sc_module.hh"
67#include "systemc/ext/core/sc_module_name.hh"
68#include "systemc/ext/tlm_core/2/generic_payload/gp.hh"
69#include "systemc/ext/tlm_utils/peq_with_cb_and_phase.h"
70#include "systemc/ext/tlm_utils/simple_target_socket.h"
71#include "systemc/tlm_bridge/sc_ext.hh"
72#include "systemc/tlm_port_wrapper.hh"
73
74namespace sc_gem5
75{
76
77class TlmToGem5Bridge : public sc_core::sc_module
78{
79  private:
80    struct TlmSenderState : public Packet::SenderState
81    {
82        tlm::tlm_generic_payload &trans;
83        TlmSenderState(tlm::tlm_generic_payload &trans) : trans(trans) {}
84    };
85
86    class BridgeMasterPort : public MasterPort
87    {
88      protected:
89        TlmToGem5Bridge &bridge;
90
91        bool
92        recvTimingResp(PacketPtr pkt) override
93        {
94            return bridge.recvTimingResp(pkt);
95        }
96        void recvReqRetry() override { bridge.recvReqRetry(); }
97        void recvRangeChange() override { bridge.recvRangeChange(); }
98
99      public:
100        BridgeMasterPort(const std::string &name_, TlmToGem5Bridge &bridge_) :
101            MasterPort(name_, nullptr), bridge(bridge_)
102        {}
103    };
104
105    tlm_utils::peq_with_cb_and_phase<TlmToGem5Bridge> peq;
106
107    bool waitForRetry;
108    tlm::tlm_generic_payload *pendingRequest;
109    PacketPtr pendingPacket;
110
111    bool needToSendRetry;
112
113    bool responseInProgress;
114
115    BridgeMasterPort bmp;
116    tlm_utils::simple_target_socket<TlmToGem5Bridge, 64> socket;
117    sc_gem5::TlmTargetWrapper<64> wrapper;
118
119    System *system;
120
121    void sendEndReq(tlm::tlm_generic_payload &trans);
122    void sendBeginResp(tlm::tlm_generic_payload &trans,
123                       sc_core::sc_time &delay);
124
125    void handleBeginReq(tlm::tlm_generic_payload &trans);
126    void handleEndResp(tlm::tlm_generic_payload &trans);
127
128    PacketPtr generatePacket(tlm::tlm_generic_payload &trans);
129    void destroyPacket(PacketPtr pkt);
130
131    void checkTransaction(tlm::tlm_generic_payload &trans);
132
133  protected:
134    // payload event call back
135    void peq_cb(tlm::tlm_generic_payload &trans, const tlm::tlm_phase &phase);
136
137    // The TLM target interface
138    tlm::tlm_sync_enum nb_transport_fw(tlm::tlm_generic_payload &trans,
139                                       tlm::tlm_phase &phase,
140                                       sc_core::sc_time &t);
141    void b_transport(tlm::tlm_generic_payload &trans, sc_core::sc_time &t);
142    unsigned int transport_dbg(tlm::tlm_generic_payload &trans);
143    bool get_direct_mem_ptr(tlm::tlm_generic_payload &trans,
144                            tlm::tlm_dmi &dmi_data);
145
146    // Gem5 port interface.
147    bool recvTimingResp(PacketPtr pkt);
148    void recvReqRetry();
149    void recvRangeChange();
150
151  public:
152    ::Port &gem5_getPort(const std::string &if_name, int idx=-1) override;
153
154    typedef TlmToGem5BridgeParams Params;
155    TlmToGem5Bridge(Params *p, const sc_core::sc_module_name &mn);
156
157    tlm_utils::simple_target_socket<TlmToGem5Bridge, 64> &
158    getSocket()
159    {
160        return socket;
161    }
162
163    void before_end_of_elaboration() override;
164
165    const MasterID masterId;
166};
167
168} // namespace sc_gem5
169
170#endif // __SYSTEMC_TLM_BRIDGE_TLM_TO_GEM5_HH__
171