multi_passthrough_initiator_socket.h revision 13511:dc5864c73df3
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 TLM_UTILS_MULTI_PASSTHROUGH_INITIATOR_SOCKET_H_INCLUDED_
20#define TLM_UTILS_MULTI_PASSTHROUGH_INITIATOR_SOCKET_H_INCLUDED_
21
22#include "multi_socket_bases.h"
23
24namespace tlm_utils {
25
26/*
27This class implements a trivial multi initiator socket.
28The triviality refers to the fact that the socket does not
29do blocking to non-blocking or non-blocking to blocking conversions.
30
31It allows to connect multiple targets to this socket.
32The user has to register callbacks for the bw interface methods
33he likes to use. The callbacks are basically equal to the bw interface
34methods but carry an additional integer that indicates to which
35index of this socket the calling target is connected.
36*/
37template <typename MODULE,
38          unsigned int BUSWIDTH = 32,
39          typename TYPES = tlm::tlm_base_protocol_types,
40          unsigned int N=0, sc_core::sc_port_policy POL = sc_core::SC_ONE_OR_MORE_BOUND>
41class multi_passthrough_initiator_socket
42  : public multi_init_base< BUSWIDTH, TYPES, N, POL>
43{
44public:
45
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)(int,
54                                         transaction_type&,
55                                         phase_type&,
56                                         sc_core::sc_time&);
57  typedef void (MODULE::*dmi_cb)(int, sc_dt::uint64, sc_dt::uint64);
58
59  typedef multi_init_base<BUSWIDTH, TYPES, N, POL> base_type;
60
61  typedef typename base_type::base_target_socket_type base_target_socket_type;
62
63  static const char* default_name()
64    { return sc_core::sc_gen_unique_name("multi_passthrough_initiator_socket"); }
65
66  //CTOR
67  explicit multi_passthrough_initiator_socket(const char* name = default_name())
68      : base_type(name)
69      , m_hierarch_bind(0)
70      , m_beoe_disabled(false)
71      , m_dummy(this,42)
72  {
73  }
74
75  ~multi_passthrough_initiator_socket(){
76    //clean up everything allocated by 'new'
77    for (unsigned int i=0; i<m_binders.size(); i++) delete m_binders[i];
78  }
79
80  //register callback for nb transport of bw interface
81  void register_nb_transport_bw(MODULE* mod,
82                              sync_enum_type (MODULE::*cb)(int,
83                                                           transaction_type&,
84                                                           phase_type&,
85                                                           sc_core::sc_time&))
86  {
87    //warn if there already is a callback
88    if (m_nb_f.is_valid()){
89      display_warning("NBTransport_bw callback already registered.");
90      return;
91    }
92
93    //set the functor
94    m_nb_f.set_function(mod, cb);
95  }
96
97  //register callback for dmi function of bw interface
98  void register_invalidate_direct_mem_ptr(MODULE* mod,
99                             void (MODULE::*cb)(int, sc_dt::uint64, sc_dt::uint64))
100  {
101    //warn if there already is a callback
102    if (m_dmi_f.is_valid()){
103      display_warning("InvalidateDMI callback already registered.");
104      return;
105    }
106
107    //set the functor
108    m_dmi_f.set_function(mod, cb);
109  }
110
111  //Override virtual functions of the tlm_initiator_socket:
112  // this function is called whenever an sc_port (as part of a target socket)
113  //  wants to bind to the export of the underlying tlm_initiator_socket
114  //At this time a callback binder is created an returned to the sc_port
115  // of the target socket, so that it binds to the callback binder
116  virtual tlm::tlm_bw_transport_if<TYPES>& get_base_interface()
117  {
118    m_binders.push_back(new callback_binder_bw<TYPES>(this, m_binders.size()));
119    return *m_binders[m_binders.size()-1];
120  }
121
122  // const overload not allowed for multi-sockets
123  virtual const tlm::tlm_bw_transport_if<TYPES>& get_base_interface() const
124  {
125    display_error("'get_base_interface()' const not allowed for multi-sockets.");
126    return base_type::get_base_interface();
127  }
128
129  //Override virtual functions of the tlm_initiator_socket:
130  // this function is called whenever an sc_export (as part of a initiator socket)
131  //  wants to bind to the export of the underlying tlm_initiator_socket
132  //   i.e. a hierarchical bind takes place
133  virtual sc_core::sc_export<tlm::tlm_bw_transport_if<TYPES> >& get_base_export()
134  {
135    if (!m_beoe_disabled) //we are not bound hierarchically
136      base_type::m_export.bind(m_dummy);  //so we bind the dummy to avoid a SystemC error
137    return base_type::get_base_export(); //and then return our own export so that the hierarchical binding is set up properly
138  }
139
140  virtual const sc_core::sc_export<tlm::tlm_bw_transport_if<TYPES> >& get_base_export() const
141  {
142    return base_type::get_base_export();
143  }
144
145  //bind against a target socket
146  virtual void bind(base_target_socket_type& s)
147  {
148    //error if this socket is already bound hierarchically
149    if (m_hierarch_bind) {
150      display_error("Already hierarchically bound.");
151      return;
152    }
153
154    base_type::bind(s); //satisfy systemC, leads to a call to get_base_interface()
155
156    //try to cast the target socket into a fw interface
157    sc_core::sc_export<tlm::tlm_fw_transport_if<TYPES> >* p_ex_s=dynamic_cast<sc_core::sc_export<tlm::tlm_fw_transport_if<TYPES> >*>(&s);
158    if (!p_ex_s) {
159      display_error("Multi socket not bound to tlm_socket.");
160      return;
161    }
162
163    //try a cast into a multi sockets
164    multi_to_multi_bind_base<TYPES>* test=dynamic_cast<multi_to_multi_bind_base<TYPES>*> (p_ex_s);
165    if (test) //did we just do a multi-multi bind??
166      //if that is the case the multi target socket must have just created a callback binder
167      // which we want to get from it.
168      //Moreover, we also just created one, which we will pass to it.
169      m_sockets.push_back(test->get_last_binder(m_binders[m_binders.size()-1]));
170    else{  // if not just bind normally
171      sc_core::sc_export<tlm::tlm_fw_transport_if<TYPES> >& ex_s=*p_ex_s;
172      m_sockets.push_back(&((tlm::tlm_fw_transport_if<TYPES>&)ex_s)); //store the interface we are bound against
173    }
174  }
175
176  //operator notation for direct bind
177  void operator() (base_target_socket_type& s)
178  {
179    bind(s);
180  }
181
182  //SystemC standard callback before end of elaboration
183  void before_end_of_elaboration(){
184    //if our export hasn't been bound yet (due to a hierarch binding)
185    // we bind it now to avoid a SystemC error.
186    //We must do that, because it is legal not to register a callback on this socket
187    // as the user might only use b_transport
188    if (!base_type::m_export.get_interface()){
189      base_type::m_export.bind(m_dummy);
190    }
191
192    //'break' here if the socket was told not to do callback binding
193    if (m_beoe_disabled) return;
194
195    //get the callback binders of the top of the hierachical bind chain
196    // NOTE: this could be the same socket if there is no hierachical bind
197    std::vector<callback_binder_bw<TYPES>* >& binders=get_hierarch_bind()->get_binders();
198
199    //get the interfaces bound to the top of the hierachical bind chain
200    // NOTE: this could be the same socket if there is no hierachical bind
201    m_used_sockets=get_hierarch_bind()->get_sockets();
202
203    //register the callbacks of this socket with the callback binders
204    // we just got from the top of the hierachical bind chain
205    for (unsigned int i=0; i<binders.size(); i++) {
206      binders[i]->set_callbacks(m_nb_f, m_dmi_f);
207    }
208  }
209
210  //
211  // Bind multi initiator socket to multi initiator socket (hierarchical bind)
212  //
213  virtual void bind(base_type& s)
214  {
215    if (m_binders.size()) {
216      //a multi socket is either bound hierarchically or directly
217      display_error("Socket already directly bound.");
218      return;
219    }
220    if (m_hierarch_bind){
221      display_warning("Socket already bound hierarchically. Bind attempt ignored.");
222      return;
223    }
224
225    //remember to which socket we are hierarchically bound and disable it,
226    // so that it won't try to register callbacks itself
227    s.disable_cb_bind();
228    m_hierarch_bind=&s;
229    base_type::bind(s); //satisfy SystemC
230  }
231
232  //operator notation for hierarchical bind
233  void operator() (base_type& s)
234  {
235    bind(s);
236  }
237
238  //get access to sub port
239  tlm::tlm_fw_transport_if<TYPES>* operator[](int i){return m_used_sockets[i];}
240
241  //get the number of bound targets
242  // NOTE: this is only valid at end of elaboration!
243  unsigned int size() {return get_hierarch_bind()->get_sockets().size();}
244
245protected:
246  using base_type::display_warning;
247  using base_type::display_error;
248
249  //implementation of base class interface
250  base_type* get_hierarch_bind(){if (m_hierarch_bind) return m_hierarch_bind->get_hierarch_bind(); else return this;}
251  void disable_cb_bind(){ m_beoe_disabled=true;}
252  std::vector<callback_binder_bw<TYPES>* >& get_binders(){return m_binders;}
253  std::vector<tlm::tlm_fw_transport_if<TYPES>*>& get_sockets(){return m_sockets;}
254  //vector of connected sockets
255  std::vector<tlm::tlm_fw_transport_if<TYPES>*> m_sockets;
256  std::vector<tlm::tlm_fw_transport_if<TYPES>*> m_used_sockets;
257  //vector of binders that convert untagged interface into tagged interface
258  std::vector<callback_binder_bw<TYPES>*> m_binders;
259
260  base_type*  m_hierarch_bind; //pointer to hierarchical bound multi port
261  bool m_beoe_disabled;  // bool that remembers whether this socket shall bind callbacks or not
262  callback_binder_bw<TYPES> m_dummy; //a callback binder that is bound to the underlying export
263                                     // in case there was no real bind
264
265  //callbacks as functors
266  // (allows to pass the callback to another socket that does not know the type of the module that owns
267  //  the callbacks)
268  typename callback_binder_bw<TYPES>::nb_func_type  m_nb_f;
269  typename callback_binder_bw<TYPES>::dmi_func_type m_dmi_f;
270};
271
272template <typename MODULE,
273          unsigned int BUSWIDTH = 32,
274          typename TYPES = tlm::tlm_base_protocol_types,
275          unsigned int N=0>
276class multi_passthrough_initiator_socket_optional
277  : public multi_passthrough_initiator_socket<MODULE,BUSWIDTH,TYPES,N,sc_core::SC_ZERO_OR_MORE_BOUND>
278{
279  typedef multi_passthrough_initiator_socket<MODULE,BUSWIDTH,TYPES,N,sc_core::SC_ZERO_OR_MORE_BOUND> socket_b;
280public:
281  multi_passthrough_initiator_socket_optional() : socket_b() {}
282  explicit multi_passthrough_initiator_socket_optional(const char* name) : socket_b(name) {}
283};
284
285} // namespace tlm_utils
286#endif // TLM_UTILS_MULTI_PASSTHROUGH_INITIATOR_SOCKET_H_INCLUDED_
287