multi_passthrough_target_socket.h revision 13513
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19#ifndef __SYSTEMC_EXT_TLM_UTILS_MULTI_PASSTHROUGH_TARGET_SOCKET_H__
20#define __SYSTEMC_EXT_TLM_UTILS_MULTI_PASSTHROUGH_TARGET_SOCKET_H__
21
22#include "tlm_utils/multi_socket_bases.h"
23
24namespace tlm_utils
25{
26
27/*
28This class implements a trivial multi target socket.
29The triviality refers to the fact that the socket does not
30do blocking to non-blocking or non-blocking to blocking conversions.
31
32It allows to connect multiple initiators to this socket.
33The user has to register callbacks for the fw interface methods
34he likes to use. The callbacks are basically equal to the fw interface
35methods but carry an additional integer that indicates to which
36index of this socket the calling initiator is connected.
37*/
38template <typename MODULE, unsigned int BUSWIDTH=32,
39          typename TYPES=tlm::tlm_base_protocol_types, unsigned int N=0,
40          sc_core::sc_port_policy POL=sc_core::SC_ONE_OR_MORE_BOUND>
41class multi_passthrough_target_socket :
42    public multi_target_base< BUSWIDTH, TYPES, N, POL>,
43    public multi_to_multi_bind_base<TYPES>
44{
45  public:
46    //typedefs
47    //  tlm 2.0 types for nb_transport
48    typedef typename TYPES::tlm_payload_type transaction_type;
49    typedef typename TYPES::tlm_phase_type phase_type;
50    typedef tlm::tlm_sync_enum sync_enum_type;
51
52    //  typedefs to keep the fn ptr notations short
53    typedef sync_enum_type (MODULE::*nb_cb)(
54            int, transaction_type &, phase_type &, sc_core::sc_time &);
55    typedef void (MODULE::*b_cb)(int, transaction_type &, sc_core::sc_time &);
56    typedef unsigned int (MODULE::*dbg_cb)(int, transaction_type &txn);
57    typedef bool (MODULE::*dmi_cb)(
58            int, transaction_type &txn, tlm::tlm_dmi &dmi);
59
60    typedef multi_target_base<BUSWIDTH, TYPES, N, POL> base_type;
61
62    typedef typename base_type::base_initiator_socket_type
63        base_initiator_socket_type;
64
65    static const char *
66    default_name()
67    {
68        return sc_core::sc_gen_unique_name("multi_passthrough_target_socket");
69    }
70
71    explicit multi_passthrough_target_socket(const char *name=default_name()) :
72        base_type(name), m_hierarch_bind(0), m_eoe_disabled(false),
73        m_export_callback_created(false)
74    {}
75
76    ~multi_passthrough_target_socket()
77    {
78        // Clean up everything allocated by 'new'.
79        for (unsigned int i = 0; i < m_binders.size(); i++)
80            delete m_binders[i];
81    }
82
83    void
84    check_export_binding()
85    {
86        // If our export hasn't been bound yet (due to a hierarch binding)
87        // we bind it now. We do that here as the user of the target port HAS
88        // to bind at least on callback, otherwise the socket was useless.
89        // Nevertheless, the target socket may still stay unbound afterwards.
90        if (!sc_core::sc_export<tlm::tlm_fw_transport_if<TYPES>>::
91                get_interface()) {
92            // We bind to a callback_binder that will be used as the first
93            // interface i.e. calls to the sc_export will have the same ID as
94            // calls from the first initator socket bound.
95            callback_binder_fw<TYPES> *binder;
96
97            if (m_binders.size() == 0) {
98                binder = new callback_binder_fw<TYPES>(
99                        this, m_binders.size());
100                m_binders.push_back(binder);
101                m_export_callback_created = true;
102            } else {
103                binder = m_binders[0];
104            }
105
106            sc_core::sc_export<tlm::tlm_fw_transport_if<TYPES>>::bind(*binder);
107        }
108    }
109
110    //register callback for nb transport of fw interface
111    void
112    register_nb_transport_fw(MODULE *mod, nb_cb cb)
113    {
114        check_export_binding();
115
116        // Warn if there already is a callback.
117        if (m_nb_f.is_valid()) {
118            display_warning("NBTransport_bw callback already registered.");
119            return;
120        }
121
122        // Set the functor.
123        m_nb_f.set_function(mod, cb);
124    }
125
126    // Register callback for b transport of fw interface.
127    void
128    register_b_transport(MODULE *mod, b_cb cb)
129    {
130        check_export_binding();
131
132        // Warn if there already is a callback.
133        if (m_b_f.is_valid()) {
134            display_warning("BTransport callback already registered.");
135            return;
136        }
137
138        // Set the functor.
139        m_b_f.set_function(mod, cb);
140    }
141
142    // Register callback for debug transport of fw interface.
143    void
144    register_transport_dbg(MODULE *mod, dbg_cb cb)
145    {
146        check_export_binding();
147
148        // Warn if there already is a callback.
149        if (m_dbg_f.is_valid()) {
150            display_warning("DebugTransport callback already registered.");
151            return;
152        }
153
154        // Set the functor.
155        m_dbg_f.set_function(mod, cb);
156    }
157
158    // Register callback for DMI of fw interface.
159    void
160    register_get_direct_mem_ptr(MODULE *mod, dmi_cb cb)
161    {
162        check_export_binding();
163
164        // Warn if there already is a callback.
165        if (m_dmi_f.is_valid()) {
166            display_warning("DMI callback already registered.");
167            return;
168        }
169
170        // Set the functor.
171        m_dmi_f.set_function(mod, cb);
172    }
173
174
175    // Override virtual functions of the tlm_target_socket:
176    // this function is called whenever an sc_port (as part of a init socket)
177    // wants to bind to the export of the underlying tlm_target_socket
178    // At this time a callback binder is created an returned to the sc_port
179    // of the init socket, so that it binds to the callback binder.
180    virtual tlm::tlm_fw_transport_if<TYPES> &
181    get_base_interface()
182    {
183        // Error if this socket is already bound hierarchically.
184        if (m_hierarch_bind)
185            display_error("Socket already bound hierarchically.");
186
187        if (m_export_callback_created) {
188            // Consume binder created from the callback registration.
189            m_export_callback_created = false;
190        } else {
191            m_binders.push_back(
192                    new callback_binder_fw<TYPES>(this, m_binders.size()));
193        }
194
195        return *m_binders[m_binders.size()-1];
196    }
197
198    // Const overload not allowed for multi-sockets.
199    virtual const tlm::tlm_fw_transport_if<TYPES> &
200    get_base_interface() const
201    {
202        display_error("'get_base_interface() const'"
203                " not allowed for multi-sockets.");
204        return base_type::get_base_interface();
205    }
206
207    // Just return the export of the underlying tlm_target_socket in case of
208    // a hierarchical bind.
209    virtual sc_core::sc_export<tlm::tlm_fw_transport_if<TYPES>> &
210    get_base_export()
211    {
212        return *this;
213    }
214
215    // Just return the export of the underlying tlm_target_socket in case of
216    // a hierarchical bind.
217    virtual const sc_core::sc_export<tlm::tlm_fw_transport_if<TYPES>> &
218    get_base_export() const
219    {
220        return base_type::get_base_export();
221    }
222
223    // The standard end of elaboration callback.
224    void
225    end_of_elaboration()
226    {
227        // 'break' here if the socket was told not to do callback binding.
228        if (m_eoe_disabled)
229            return;
230
231        // Get the callback binders and the multi binds of the top of the
232        // hierachical bind chain.
233        // NOTE: this could be the same socket if there is no hierachical
234        // bind.
235        std::vector<callback_binder_fw<TYPES> *> &binders =
236            get_hierarch_bind()->get_binders();
237        std::map<unsigned int, tlm::tlm_bw_transport_if<TYPES> *> &
238            multi_binds = get_hierarch_bind()->get_multi_binds();
239
240        // Complete binding only if there has been a real bind.
241        bool unbound = (binders.size() == 1 && m_export_callback_created);
242        // No call to get_base_interface has consumed the export - ignore.
243        if (unbound)
244            return;
245
246        // Iterate over all binders.
247        for (unsigned int i = 0; i < binders.size(); i++) {
248            // Set the callbacks for the binder.
249            binders[i]->set_callbacks(m_nb_f, m_b_f, m_dmi_f, m_dbg_f);
250            // Check if this connection is multi-multi.
251            if (multi_binds.find(i) != multi_binds.end()) {
252                // If so remember the interface.
253                m_sockets.push_back(multi_binds[i]);
254            } else {
255                // If we are bound to a normal socket.
256                // Get the calling port and try to cast it into a tlm socket
257                // base.
258                base_initiator_socket_type *test =
259                    dynamic_cast<base_initiator_socket_type*>(
260                            binders[i]->get_other_side());
261                if (!test) {
262                    display_error("Not bound to tlm_socket.");
263                }
264                // Remember the interface.
265                m_sockets.push_back(&test->get_base_interface());
266            }
267        }
268    }
269
270    //
271    // Bind multi target socket to multi target socket (hierarchical bind)
272    //
273    virtual void
274    bind(base_type &s)
275    {
276        // Warn if already bound hierarchically.
277        if (m_eoe_disabled) {
278            display_warning("Socket already bound hierarchically. "
279                    "Bind attempt ignored.");
280            return;
281        }
282
283        // Disable our own end of elaboration call.
284        disable_cb_bind();
285
286        // Inform the bound target socket that it is bound
287        // hierarchically now.
288        s.set_hierarch_bind((base_type*)this);
289        base_type::bind(s); // Satisfy SystemC.
290    }
291
292    // Operator notation for hierarchical bind.
293    void operator () (base_type &s) { bind(s); }
294
295    // Get access to sub port.
296    tlm::tlm_bw_transport_if<TYPES> *
297    operator [] (int i)
298    {
299        return m_sockets[i];
300    }
301
302    // Get number of bound initiators.
303    // NOTE: this is only valid at end of elaboration!
304    unsigned int size() { return get_hierarch_bind()->get_binders().size(); }
305
306  protected:
307    using base_type::display_warning;
308    using base_type::display_error;
309
310    // Implementation of base class interface.
311    base_type *
312    get_hierarch_bind()
313    {
314        if (m_hierarch_bind)
315            return m_hierarch_bind->get_hierarch_bind();
316        else
317            return this;
318    }
319    std::map<unsigned int, tlm::tlm_bw_transport_if<TYPES> *> &
320    get_multi_binds()
321    {
322        return m_multi_binds;
323    }
324    void set_hierarch_bind(base_type* h) { m_hierarch_bind = h; }
325    tlm::tlm_fw_transport_if<TYPES> *
326    get_last_binder(tlm::tlm_bw_transport_if<TYPES> *other)
327    {
328        m_multi_binds[m_binders.size() - 1] = other;
329        return m_binders[m_binders.size() - 1];
330    }
331
332    // Map that stores to which index a multi init socket is connected
333    // and the interface of the multi init socket.
334    std::map<unsigned int, tlm::tlm_bw_transport_if<TYPES> *> m_multi_binds;
335
336    void disable_cb_bind() { m_eoe_disabled = true; }
337    std::vector<callback_binder_fw<TYPES> *> &
338    get_binders()
339    {
340        return m_binders;
341    }
342    // Vector of connected sockets.
343    std::vector<tlm::tlm_bw_transport_if<TYPES> *> m_sockets;
344    // Vector of binders that convert untagged interface into tagged
345    // interface.
346    std::vector<callback_binder_fw<TYPES> *> m_binders;
347
348    base_type *m_hierarch_bind; // Pointer to hierarchical bound multi port.
349    // bool that disables callback bindings at end of elaboration.
350    bool m_eoe_disabled;
351    // bool that indicates that a binder has been created from a callback
352    // registration.
353    bool m_export_callback_created;
354
355    // callbacks as functors
356    // (allows to pass the callback to another socket that does not know
357    // the type of the module that owns the callbacks).
358    typename callback_binder_fw<TYPES>::nb_func_type m_nb_f;
359    typename callback_binder_fw<TYPES>::b_func_type m_b_f;
360    typename callback_binder_fw<TYPES>::debug_func_type m_dbg_f;
361    typename callback_binder_fw<TYPES>::dmi_func_type m_dmi_f;
362};
363
364template <typename MODULE, unsigned int BUSWIDTH=32,
365          typename TYPES=tlm::tlm_base_protocol_types, unsigned int N=0>
366class multi_passthrough_target_socket_optional :
367    public multi_passthrough_target_socket<
368        MODULE, BUSWIDTH, TYPES, N, sc_core::SC_ZERO_OR_MORE_BOUND>
369{
370    typedef multi_passthrough_target_socket<
371        MODULE, BUSWIDTH, TYPES, N, sc_core::SC_ZERO_OR_MORE_BOUND> socket_b;
372  public:
373    multi_passthrough_target_socket_optional() : socket_b() {}
374    explicit multi_passthrough_target_socket_optional(const char *name) :
375        socket_b(name)
376    {}
377};
378
379} // namespace tlm_utils
380
381#endif /* __SYSTEMC_EXT_TLM_UTILS_MULTI_PASSTHROUGH_TARGET_SOCKET_H__ */
382