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