A SystemC productivity library for virtual platform development utilizing SCV and TLM2.0 https://www.minres.com/#opensource

target_mixin.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*******************************************************************************
  2. * Copyright 2016, 2018 MINRES Technologies GmbH
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *******************************************************************************/
  16. #ifndef __TARGET_MIXIN_H__
  17. #define __TARGET_MIXIN_H__
  18. #ifndef SC_INCLUDE_DYNAMIC_PROCESSES // needed for sc_spawn
  19. #define SC_INCLUDE_DYNAMIC_PROCESSES
  20. #endif
  21. #include "utilities.h"
  22. #include <sstream>
  23. #include <tlm>
  24. #include <tlm_utils/peq_with_get.h>
  25. namespace scc {
  26. /**
  27. * an target socket mixin adding default implementation of callback functions similar to tlm::simple_target_socket
  28. */
  29. template <typename BASE_TYPE, typename TYPES = tlm::tlm_base_protocol_types> class target_mixin : public BASE_TYPE {
  30. // friend class fw_process;
  31. // friend class bw_process;
  32. public:
  33. using transaction_type = typename TYPES::tlm_payload_type;
  34. using phase_type = typename TYPES::tlm_phase_type;
  35. using sync_enum_type = tlm::tlm_sync_enum;
  36. using fw_interface_type = tlm::tlm_fw_transport_if<TYPES>;
  37. using bw_interface_type = tlm::tlm_bw_transport_if<TYPES>;
  38. public:
  39. /**
  40. * default constructor
  41. */
  42. target_mixin()
  43. : target_mixin(sc_core::sc_gen_unique_name("target_mixin_socket")) {}
  44. /**
  45. * constructor with explicit instance name
  46. *
  47. * @param n
  48. */
  49. explicit target_mixin(const sc_core::sc_module_name &n)
  50. : BASE_TYPE(n)
  51. , m_fw_process(this)
  52. , m_bw_process(this)
  53. , m_current_transaction(nullptr) {
  54. bind(m_fw_process);
  55. }
  56. //! make bind of base class available
  57. using BASE_TYPE::bind;
  58. /**
  59. * return the bw_process interface
  60. *
  61. * @return
  62. */
  63. tlm::tlm_bw_transport_if<TYPES> *operator->() { return &m_bw_process; }
  64. /**
  65. * register a non-blocking forward path callback function
  66. *
  67. * @param cb
  68. */
  69. void
  70. register_nb_transport_fw(std::function<sync_enum_type(transaction_type &, phase_type &, sc_core::sc_time &)> cb) {
  71. assert(!sc_core::sc_get_curr_simcontext()->elaboration_done());
  72. m_fw_process.set_nb_transport_ptr(cb);
  73. }
  74. /**
  75. * register a blocking forward path callback function
  76. *
  77. * @param cb
  78. */
  79. void register_b_transport(std::function<void(transaction_type &, sc_core::sc_time &)> cb) {
  80. assert(!sc_core::sc_get_curr_simcontext()->elaboration_done());
  81. m_fw_process.set_b_transport_ptr(cb);
  82. }
  83. /**
  84. *
  85. * @param cb
  86. */
  87. void register_transport_dbg(std::function<unsigned int(transaction_type &)> cb) {
  88. assert(!sc_core::sc_get_curr_simcontext()->elaboration_done());
  89. m_fw_process.set_transport_dbg_ptr(cb);
  90. }
  91. /**
  92. * register a DMI callback function
  93. *
  94. * @param cb
  95. */
  96. void register_get_direct_mem_ptr(std::function<bool(transaction_type &, tlm::tlm_dmi &)> cb) {
  97. assert(!sc_core::sc_get_curr_simcontext()->elaboration_done());
  98. m_fw_process.set_get_direct_mem_ptr(cb);
  99. }
  100. private:
  101. // make call on bw path.
  102. sync_enum_type bw_nb_transport(transaction_type &trans, phase_type &phase, sc_core::sc_time &t) {
  103. return BASE_TYPE::operator->()->nb_transport_bw(trans, phase, t);
  104. }
  105. void bw_invalidate_direct_mem_ptr(sc_dt::uint64 s, sc_dt::uint64 e) {
  106. BASE_TYPE::operator->()->invalidate_direct_mem_ptr(s, e);
  107. }
  108. // Helper class to handle bw path calls
  109. // Needed to detect transaction end when called from b_transport.
  110. class bw_process : public tlm::tlm_bw_transport_if<TYPES> {
  111. public:
  112. bw_process(target_mixin *p_own)
  113. : m_owner(p_own) {}
  114. sync_enum_type nb_transport_bw(transaction_type &trans, phase_type &phase, sc_core::sc_time &t) {
  115. typename std::map<transaction_type *, sc_core::sc_event *>::iterator it;
  116. it = m_owner->m_pending_trans.find(&trans);
  117. if (it == m_owner->m_pending_trans.end()) {
  118. // Not a blocking call, forward.
  119. return m_owner->bw_nb_transport(trans, phase, t);
  120. } else {
  121. if (phase == tlm::END_REQ) {
  122. m_owner->m_end_request.notify(sc_core::SC_ZERO_TIME);
  123. return tlm::TLM_ACCEPTED;
  124. } else if (phase == tlm::BEGIN_RESP) {
  125. if (m_owner->m_current_transaction == &trans) {
  126. m_owner->m_end_request.notify(sc_core::SC_ZERO_TIME);
  127. }
  128. // TODO: add response-accept delay?
  129. it->second->notify(t);
  130. m_owner->m_pending_trans.erase(it);
  131. return tlm::TLM_COMPLETED;
  132. } else {
  133. assert(false);
  134. exit(1);
  135. }
  136. // return tlm::TLM_COMPLETED; //Should not reach here
  137. }
  138. }
  139. void invalidate_direct_mem_ptr(sc_dt::uint64 s, sc_dt::uint64 e) {
  140. m_owner->bw_invalidate_direct_mem_ptr(s, e);
  141. }
  142. private:
  143. target_mixin *m_owner;
  144. };
  145. class fw_process : public tlm::tlm_fw_transport_if<TYPES>, public tlm::tlm_mm_interface {
  146. public:
  147. using NBTransportPtr = std::function<sync_enum_type(transaction_type &, phase_type &, sc_core::sc_time &)>;
  148. using BTransportPtr = std::function<void(transaction_type &, sc_core::sc_time &)>;
  149. using TransportDbgPtr = std::function<unsigned int(transaction_type &)>;
  150. using GetDirectMemPtr = std::function<bool(transaction_type &, tlm::tlm_dmi &)>;
  151. fw_process(target_mixin *p_own)
  152. : m_name(p_own->name())
  153. , m_owner(p_own)
  154. , m_nb_transport_ptr(0)
  155. , m_b_transport_ptr(0)
  156. , m_transport_dbg_ptr(0)
  157. , m_get_direct_mem_ptr(0)
  158. , m_peq(sc_core::sc_gen_unique_name("m_peq"))
  159. , m_response_in_progress(false) {
  160. sc_core::sc_spawn_options opts;
  161. opts.set_sensitivity(&m_peq.get_event());
  162. sc_core::sc_spawn(sc_bind(&fw_process::b2nb_thread, this), sc_core::sc_gen_unique_name("b2nb_thread"),
  163. &opts);
  164. }
  165. void set_nb_transport_ptr(NBTransportPtr p) {
  166. if (m_nb_transport_ptr) {
  167. std::stringstream s;
  168. s << m_name << ": non-blocking callback allready registered";
  169. SC_REPORT_WARNING("/OSCI_TLM-2/simple_socket", s.str().c_str());
  170. } else {
  171. m_nb_transport_ptr = p;
  172. }
  173. }
  174. void set_b_transport_ptr(BTransportPtr p) {
  175. if (m_b_transport_ptr) {
  176. std::stringstream s;
  177. s << m_name << ": blocking callback allready registered";
  178. SC_REPORT_WARNING("/OSCI_TLM-2/simple_socket", s.str().c_str());
  179. } else {
  180. m_b_transport_ptr = p;
  181. }
  182. }
  183. void set_transport_dbg_ptr(TransportDbgPtr p) {
  184. if (m_transport_dbg_ptr) {
  185. std::stringstream s;
  186. s << m_name << ": debug callback allready registered";
  187. SC_REPORT_WARNING("/OSCI_TLM-2/simple_socket", s.str().c_str());
  188. } else {
  189. m_transport_dbg_ptr = p;
  190. }
  191. }
  192. void set_get_direct_mem_ptr(GetDirectMemPtr p) {
  193. if (m_get_direct_mem_ptr) {
  194. std::stringstream s;
  195. s << m_name << ": get DMI pointer callback allready registered";
  196. SC_REPORT_WARNING("/OSCI_TLM-2/simple_socket", s.str().c_str());
  197. } else {
  198. m_get_direct_mem_ptr = p;
  199. }
  200. }
  201. // Interface implementation
  202. sync_enum_type nb_transport_fw(transaction_type &trans, phase_type &phase, sc_core::sc_time &t) {
  203. if (m_nb_transport_ptr) {
  204. // forward call
  205. return m_nb_transport_ptr(trans, phase, t);
  206. } else if (m_b_transport_ptr) {
  207. if (phase == tlm::BEGIN_REQ) {
  208. // prepare thread to do blocking call
  209. process_handle_class *ph = m_process_handle.get_handle(&trans);
  210. if (!ph) { // create new dynamic process
  211. ph = new process_handle_class(&trans);
  212. m_process_handle.put_handle(ph);
  213. sc_core::sc_spawn_options opts;
  214. opts.dont_initialize();
  215. opts.set_sensitivity(&ph->m_e);
  216. sc_core::sc_spawn(sc_bind(&fw_process::nb2b_thread, this, ph),
  217. sc_core::sc_gen_unique_name("nb2b_thread"), &opts);
  218. }
  219. ph->m_e.notify(t);
  220. return tlm::TLM_ACCEPTED;
  221. } else if (phase == tlm::END_RESP) {
  222. m_response_in_progress = false;
  223. m_end_response.notify(t);
  224. return tlm::TLM_COMPLETED;
  225. } else {
  226. assert(false);
  227. exit(1);
  228. // return tlm::TLM_COMPLETED; ///< unreachable code
  229. }
  230. } else {
  231. std::stringstream s;
  232. s << m_name << ": no non-blocking transport callback registered";
  233. SC_REPORT_ERROR("/OSCI_TLM-2/simple_socket", s.str().c_str());
  234. }
  235. return tlm::TLM_ACCEPTED; ///< unreachable code
  236. }
  237. void b_transport(transaction_type &trans, sc_core::sc_time &t) {
  238. if (m_b_transport_ptr) {
  239. // forward call
  240. m_b_transport_ptr(trans, t);
  241. return;
  242. } else if (m_nb_transport_ptr) {
  243. m_peq.notify(trans, t);
  244. t = sc_core::SC_ZERO_TIME;
  245. mm_end_event_ext mm_ext;
  246. const bool mm_added = !trans.has_mm();
  247. if (mm_added) {
  248. trans.set_mm(this);
  249. trans.set_auto_extension(&mm_ext);
  250. trans.acquire();
  251. }
  252. // wait until transaction is finished
  253. sc_core::sc_event end_event;
  254. m_owner->m_pending_trans[&trans] = &end_event;
  255. sc_core::wait(end_event);
  256. if (mm_added) {
  257. // release will not delete the transaction, it will notify mm_ext.done
  258. trans.release();
  259. if (trans.get_ref_count()) {
  260. sc_core::wait(mm_ext.done);
  261. }
  262. trans.set_mm(0);
  263. }
  264. } else {
  265. std::stringstream s;
  266. s << m_name << ": no blocking transport callback registered";
  267. SC_REPORT_ERROR("/OSCI_TLM-2/simple_socket", s.str().c_str());
  268. }
  269. }
  270. unsigned int transport_dbg(transaction_type &trans) {
  271. if (m_transport_dbg_ptr) {
  272. // forward call
  273. return m_transport_dbg_ptr(trans);
  274. } else {
  275. // No debug support
  276. return 0;
  277. }
  278. }
  279. bool get_direct_mem_ptr(transaction_type &trans, tlm::tlm_dmi &dmi_data) {
  280. if (m_get_direct_mem_ptr) {
  281. // forward call
  282. return m_get_direct_mem_ptr(trans, dmi_data);
  283. } else {
  284. // No DMI support
  285. dmi_data.allow_read_write();
  286. dmi_data.set_start_address(0x0);
  287. dmi_data.set_end_address((sc_dt::uint64)-1);
  288. return false;
  289. }
  290. }
  291. private:
  292. // dynamic process handler for nb2b conversion
  293. class process_handle_class {
  294. public:
  295. explicit process_handle_class(transaction_type *trans)
  296. : m_trans(trans)
  297. , m_suspend(false) {}
  298. transaction_type *m_trans;
  299. sc_core::sc_event m_e;
  300. bool m_suspend;
  301. };
  302. class process_handle_list {
  303. public:
  304. process_handle_list() = default;
  305. ~process_handle_list() {
  306. for (typename std::vector<process_handle_class *>::iterator it = v.begin(), end = v.end(); it != end;
  307. ++it)
  308. delete *it;
  309. }
  310. process_handle_class *get_handle(transaction_type *trans) {
  311. typename std::vector<process_handle_class *>::iterator it;
  312. for (it = v.begin(); it != v.end(); it++) {
  313. if ((*it)->m_suspend) { // found suspended dynamic process, re-use it
  314. (*it)->m_trans = trans; // replace to new one
  315. (*it)->m_suspend = false;
  316. return *it;
  317. }
  318. }
  319. return NULL; // no suspended process
  320. }
  321. void put_handle(process_handle_class *ph) { v.push_back(ph); }
  322. private:
  323. std::vector<process_handle_class *> v;
  324. };
  325. process_handle_list m_process_handle;
  326. void nb2b_thread(process_handle_class *h) {
  327. while (true) {
  328. transaction_type *trans = h->m_trans;
  329. sc_core::sc_time t = sc_core::SC_ZERO_TIME;
  330. // forward call
  331. m_b_transport_ptr(*trans, t);
  332. sc_core::wait(t);
  333. // return path
  334. while (m_response_in_progress) {
  335. sc_core::wait(m_end_response);
  336. }
  337. t = sc_core::SC_ZERO_TIME;
  338. phase_type phase = tlm::BEGIN_RESP;
  339. sync_enum_type sync = m_owner->bw_nb_transport(*trans, phase, t);
  340. if (!(sync == tlm::TLM_COMPLETED || (sync == tlm::TLM_UPDATED && phase == tlm::END_RESP))) {
  341. m_response_in_progress = true;
  342. }
  343. // suspend until next transaction
  344. h->m_suspend = true;
  345. sc_core::wait();
  346. }
  347. }
  348. void b2nb_thread() {
  349. while (true) {
  350. sc_core::wait(m_peq.get_event());
  351. transaction_type *trans;
  352. while ((trans = m_peq.get_next_transaction()) != 0) {
  353. assert(m_nb_transport_ptr);
  354. phase_type phase = tlm::BEGIN_REQ;
  355. sc_core::sc_time t = sc_core::SC_ZERO_TIME;
  356. switch (m_nb_transport_ptr(*trans, phase, t)) {
  357. case tlm::TLM_COMPLETED: {
  358. // notify transaction is finished
  359. typename std::map<transaction_type *, sc_core::sc_event *>::iterator it =
  360. m_owner->m_pending_trans.find(trans);
  361. assert(it != m_owner->m_pending_trans.end());
  362. it->second->notify(t);
  363. m_owner->m_pending_trans.erase(it);
  364. break;
  365. }
  366. case tlm::TLM_ACCEPTED:
  367. case tlm::TLM_UPDATED:
  368. switch (phase) {
  369. case tlm::BEGIN_REQ:
  370. m_owner->m_current_transaction = trans;
  371. sc_core::wait(m_owner->m_end_request);
  372. m_owner->m_current_transaction = 0;
  373. break;
  374. case tlm::END_REQ:
  375. sc_core::wait(t);
  376. break;
  377. case tlm::BEGIN_RESP: {
  378. phase = tlm::END_RESP;
  379. sc_core::wait(t); // This line is a bug fix added in TLM-2.0.2
  380. t = sc_core::SC_ZERO_TIME;
  381. m_nb_transport_ptr(*trans, phase, t);
  382. // notify transaction is finished
  383. typename std::map<transaction_type *, sc_core::sc_event *>::iterator it =
  384. m_owner->m_pending_trans.find(trans);
  385. assert(it != m_owner->m_pending_trans.end());
  386. it->second->notify(t);
  387. m_owner->m_pending_trans.erase(it);
  388. break;
  389. }
  390. default:
  391. assert(false);
  392. exit(1);
  393. };
  394. break;
  395. default:
  396. assert(false);
  397. exit(1);
  398. };
  399. }
  400. }
  401. }
  402. void free(tlm::tlm_generic_payload *trans) {
  403. mm_end_event_ext *ext = trans->template get_extension<mm_end_event_ext>();
  404. assert(ext);
  405. // notif event first before freeing extensions (reset)
  406. ext->done.notify();
  407. trans->reset();
  408. }
  409. private:
  410. class mm_end_event_ext : public tlm::tlm_extension<mm_end_event_ext> {
  411. public:
  412. tlm::tlm_extension_base *clone() const { return NULL; }
  413. void free() {}
  414. void copy_from(tlm::tlm_extension_base const &) {}
  415. sc_core::sc_event done;
  416. };
  417. private:
  418. const std::string m_name;
  419. target_mixin *m_owner;
  420. NBTransportPtr m_nb_transport_ptr;
  421. BTransportPtr m_b_transport_ptr;
  422. TransportDbgPtr m_transport_dbg_ptr;
  423. GetDirectMemPtr m_get_direct_mem_ptr;
  424. tlm_utils::peq_with_get<transaction_type> m_peq;
  425. bool m_response_in_progress;
  426. sc_core::sc_event m_end_response;
  427. };
  428. private:
  429. fw_process m_fw_process;
  430. bw_process m_bw_process;
  431. std::map<transaction_type *, sc_core::sc_event *> m_pending_trans;
  432. sc_core::sc_event m_end_request;
  433. transaction_type *m_current_transaction;
  434. };
  435. }
  436. #endif //__TARGET_MIXIN_H__