rpmsg_env.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * Copyright (c) 2014, Mentor Graphics Corporation
  3. * Copyright (c) 2015 Xilinx, Inc.
  4. * Copyright (c) 2016 Freescale Semiconductor, Inc.
  5. * Copyright 2016-2022 NXP
  6. * Copyright 2021 ACRIOS Systems s.r.o.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holder nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /**************************************************************************
  34. * FILE NAME
  35. *
  36. * rpmsg_env.h
  37. *
  38. * COMPONENT
  39. *
  40. * OpenAMP stack.
  41. *
  42. * DESCRIPTION
  43. *
  44. * This file defines abstraction layer for OpenAMP stack. The implementor
  45. * must provide definition of all the functions.
  46. *
  47. * DATA STRUCTURES
  48. *
  49. * none
  50. *
  51. * FUNCTIONS
  52. *
  53. * env_allocate_memory
  54. * env_free_memory
  55. * env_memset
  56. * env_memcpy
  57. * env_strncpy
  58. * env_print
  59. * env_map_vatopa
  60. * env_map_patova
  61. * env_mb
  62. * env_rmb
  63. * env_wmb
  64. * env_create_mutex
  65. * env_delete_mutex
  66. * env_lock_mutex
  67. * env_unlock_mutex
  68. * env_sleep_msec
  69. * env_disable_interrupt
  70. * env_enable_interrupt
  71. * env_create_queue
  72. * env_delete_queue
  73. * env_put_queue
  74. * env_get_queue
  75. * env_wait_for_link_up
  76. * env_tx_callback
  77. *
  78. **************************************************************************/
  79. #ifndef RPMSG_ENV_H_
  80. #define RPMSG_ENV_H_
  81. #include <stdint.h>
  82. #include "rpmsg_default_config.h"
  83. #include "rpmsg_env_specific.h"
  84. #include "rpmsg_platform.h"
  85. /*!
  86. * env_init
  87. *
  88. * Initializes OS/BM environment.
  89. *
  90. * @param env_context Pointer to preallocated environment context data
  91. * @param env_init_data Initialization data for the environment layer
  92. *
  93. * @returns - execution status
  94. */
  95. #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
  96. int32_t env_init(void **env_context, void *env_init_data);
  97. #else
  98. int32_t env_init(void);
  99. #endif
  100. /*!
  101. * env_deinit
  102. *
  103. * Uninitializes OS/BM environment.
  104. *
  105. * @param env_context Pointer to environment context data
  106. *
  107. * @returns - execution status
  108. */
  109. #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
  110. int32_t env_deinit(void *env_context);
  111. #else
  112. int32_t env_deinit(void);
  113. #endif
  114. /*!
  115. * -------------------------------------------------------------------------
  116. *
  117. * Dynamic memory management functions. The parameters
  118. * are similar to standard c functions.
  119. *
  120. *-------------------------------------------------------------------------
  121. **/
  122. /*!
  123. * env_allocate_memory
  124. *
  125. * Allocates memory with the given size.
  126. *
  127. * @param size - size of memory to allocate
  128. *
  129. * @return - pointer to allocated memory
  130. */
  131. void *env_allocate_memory(uint32_t size);
  132. /*!
  133. * env_free_memory
  134. *
  135. * Frees memory pointed by the given parameter.
  136. *
  137. * @param ptr - pointer to memory to free
  138. */
  139. void env_free_memory(void *ptr);
  140. /*!
  141. * -------------------------------------------------------------------------
  142. *
  143. * RTL Functions
  144. *
  145. *-------------------------------------------------------------------------
  146. */
  147. void env_memset(void *ptr, int32_t value, uint32_t size);
  148. void env_memcpy(void *dst, void const *src, uint32_t len);
  149. int32_t env_strcmp(const char *dst, const char *src);
  150. void env_strncpy(char *dest, const char *src, uint32_t len);
  151. int32_t env_strncmp(char *dest, const char *src, uint32_t len);
  152. #ifdef MCUXPRESSO_SDK
  153. /* MCUXpresso_SDK's PRINTF used in SDK examples */
  154. #include "fsl_debug_console.h"
  155. #if defined SDK_DEBUGCONSOLE && (SDK_DEBUGCONSOLE != DEBUGCONSOLE_DISABLE)
  156. #define env_print(...) (void)PRINTF(__VA_ARGS__)
  157. #else
  158. #define env_print(...)
  159. #endif
  160. #else
  161. /* When RPMsg_Lite being used outside of MCUXpresso_SDK use your own env_print
  162. implemenetation to avoid conflict with Misra 21.6 rule */
  163. #include <stdio.h>
  164. #define env_print(...) (void)printf(__VA_ARGS__)
  165. #endif /* MCUXPRESSO_SDK */
  166. /*!
  167. *-----------------------------------------------------------------------------
  168. *
  169. * Functions to convert physical address to virtual address and vice versa.
  170. *
  171. *-----------------------------------------------------------------------------
  172. */
  173. /*!
  174. * env_map_vatopa
  175. *
  176. * Converts logical address to physical address
  177. *
  178. * @param env Pointer to environment context data
  179. * @param address Pointer to logical address
  180. *
  181. * @return - physical address
  182. */
  183. #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
  184. uint32_t env_map_vatopa(void *env, void *address);
  185. #else
  186. uint32_t env_map_vatopa(void *address);
  187. #endif
  188. /*!
  189. * env_map_patova
  190. *
  191. * Converts physical address to logical address
  192. *
  193. * @param env_context Pointer to environment context data
  194. * @param address Pointer to physical address
  195. *
  196. * @return - logical address
  197. *
  198. */
  199. #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
  200. void *env_map_patova(void *env, uint32_t address);
  201. #else
  202. void *env_map_patova(uint32_t address);
  203. #endif
  204. /*!
  205. *-----------------------------------------------------------------------------
  206. *
  207. * Abstractions for memory barrier instructions.
  208. *
  209. *-----------------------------------------------------------------------------
  210. */
  211. /*!
  212. * env_mb
  213. *
  214. * Inserts memory barrier.
  215. */
  216. void env_mb(void);
  217. /*!
  218. * env_rmb
  219. *
  220. * Inserts read memory barrier
  221. */
  222. void env_rmb(void);
  223. /*!
  224. * env_wmb
  225. *
  226. * Inserts write memory barrier
  227. */
  228. void env_wmb(void);
  229. /*!
  230. *-----------------------------------------------------------------------------
  231. *
  232. * Abstractions for OS lock primitives.
  233. *
  234. *-----------------------------------------------------------------------------
  235. */
  236. /*!
  237. * env_create_mutex
  238. *
  239. * Creates a mutex with given initial count.
  240. *
  241. * @param lock - pointer to created mutex
  242. * @param count - initial count 0 or 1
  243. * @param context - context for mutex
  244. *
  245. * @return - status of function execution
  246. */
  247. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  248. int32_t env_create_mutex(void **lock, int32_t count, void *context);
  249. #else
  250. int32_t env_create_mutex(void **lock, int32_t count);
  251. #endif
  252. /*!
  253. * env_delete_mutex
  254. *
  255. * Deletes the given lock.
  256. *
  257. * @param lock - mutex to delete
  258. */
  259. void env_delete_mutex(void *lock);
  260. /*!
  261. * env_lock_mutex
  262. *
  263. * Tries to acquire the lock, if lock is not available then call to
  264. * this function will suspend.
  265. *
  266. * @param lock - mutex to lock
  267. *
  268. */
  269. void env_lock_mutex(void *lock);
  270. /*!
  271. * env_unlock_mutex
  272. *
  273. * Releases the given lock.
  274. *
  275. * @param lock - mutex to unlock
  276. */
  277. void env_unlock_mutex(void *lock);
  278. /*!
  279. * env_create_sync_lock
  280. *
  281. * Creates a synchronization lock primitive. It is used
  282. * when signal has to be sent from the interrupt context to main
  283. * thread context.
  284. *
  285. * @param lock - pointer to created sync lock object
  286. * @param state - initial state , lock or unlocked
  287. * @param context - context for lock
  288. *
  289. * @returns - status of function execution
  290. */
  291. #define LOCKED 0
  292. #define UNLOCKED 1
  293. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  294. int32_t env_create_sync_lock(void **lock, int32_t state, void *context);
  295. #else
  296. int32_t env_create_sync_lock(void **lock, int32_t state);
  297. #endif
  298. /*!
  299. * env_create_sync_lock
  300. *
  301. * Deletes given sync lock object.
  302. *
  303. * @param lock - sync lock to delete.
  304. *
  305. */
  306. void env_delete_sync_lock(void *lock);
  307. /*!
  308. * env_acquire_sync_lock
  309. *
  310. * Tries to acquire the sync lock.
  311. *
  312. * @param lock - sync lock to acquire.
  313. */
  314. void env_acquire_sync_lock(void *lock);
  315. /*!
  316. * env_release_sync_lock
  317. *
  318. * Releases synchronization lock.
  319. *
  320. * @param lock - sync lock to release.
  321. */
  322. void env_release_sync_lock(void *lock);
  323. /*!
  324. * env_sleep_msec
  325. *
  326. * Suspends the calling thread for given time in msecs.
  327. *
  328. * @param num_msec - delay in msecs
  329. */
  330. void env_sleep_msec(uint32_t num_msec);
  331. /*!
  332. * env_register_isr
  333. *
  334. * Registers interrupt handler data for the given interrupt vector.
  335. *
  336. * @param env Pointer to environment context data
  337. * @param vector_id Virtual interrupt vector number
  338. * @param data Interrupt handler data (virtqueue)
  339. */
  340. #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
  341. void env_register_isr(void *env, uint32_t vector_id, void *data);
  342. #else
  343. void env_register_isr(uint32_t vector_id, void *data);
  344. #endif
  345. /*!
  346. * env_unregister_isr
  347. *
  348. * Unregisters interrupt handler data for the given interrupt vector.
  349. *
  350. * @param env Pointer to environment context data
  351. * @param vector_id Virtual interrupt vector number
  352. */
  353. #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
  354. void env_unregister_isr(void *env, uint32_t vector_id);
  355. #else
  356. void env_unregister_isr(uint32_t vector_id);
  357. #endif
  358. /*!
  359. * env_enable_interrupt
  360. *
  361. * Enables the given interrupt
  362. *
  363. * @param env Pointer to environment context data
  364. * @param vector_id Virtual interrupt vector number
  365. */
  366. #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
  367. void env_enable_interrupt(void *env, uint32_t vector_id);
  368. #else
  369. void env_enable_interrupt(uint32_t vector_id);
  370. #endif
  371. /*!
  372. * env_disable_interrupt
  373. *
  374. * Disables the given interrupt.
  375. *
  376. * @param env Pointer to environment context data
  377. * @param vector_id Virtual interrupt vector number
  378. */
  379. #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
  380. void env_disable_interrupt(void *env, uint32_t vector_id);
  381. #else
  382. void env_disable_interrupt(uint32_t vector_id);
  383. #endif
  384. /*!
  385. * env_map_memory
  386. *
  387. * Enables memory mapping for given memory region.
  388. *
  389. * @param pa - physical address of memory
  390. * @param va - logical address of memory
  391. * @param size - memory size
  392. * param flags - flags for cache/uncached and access type
  393. *
  394. * Currently only first byte of flag parameter is used and bits mapping is defined as follow;
  395. *
  396. * Cache bits
  397. * 0x0000_0001 = No cache
  398. * 0x0000_0010 = Write back
  399. * 0x0000_0100 = Write through
  400. * 0x0000_x000 = Not used
  401. *
  402. * Memory types
  403. *
  404. * 0x0001_xxxx = Memory Mapped
  405. * 0x0010_xxxx = IO Mapped
  406. * 0x0100_xxxx = Shared
  407. * 0x1000_xxxx = TLB
  408. */
  409. /* Macros for caching scheme used by the shared memory */
  410. #define UNCACHED (1 << 0)
  411. #define WB_CACHE (1 << 1)
  412. #define WT_CACHE (1 << 2)
  413. /* Memory Types */
  414. #define MEM_MAPPED (1 << 4)
  415. #define IO_MAPPED (1 << 5)
  416. #define SHARED_MEM (1 << 6)
  417. #define TLB_MEM (1 << 7)
  418. void env_map_memory(uint32_t pa, uint32_t va, uint32_t size, uint32_t flags);
  419. /*!
  420. * env_get_timestamp
  421. *
  422. * Returns a 64 bit time stamp.
  423. *
  424. *
  425. */
  426. uint64_t env_get_timestamp(void);
  427. /*!
  428. * env_disable_cache
  429. *
  430. * Disables system caches.
  431. *
  432. */
  433. void env_disable_cache(void);
  434. typedef void LOCK;
  435. /*!
  436. * env_create_queue
  437. *
  438. * Creates a message queue.
  439. *
  440. * @param queue Pointer to created queue
  441. * @param length Maximum number of elements in the queue
  442. * @param item_size Queue element size in bytes
  443. * @param queue_static_storage Pointer to queue static storage buffer
  444. * @param queue_static_context Pointer to queue static context
  445. *
  446. * @return - status of function execution
  447. */
  448. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  449. int32_t env_create_queue(void **queue,
  450. int32_t length,
  451. int32_t element_size,
  452. uint8_t *queue_static_storage,
  453. rpmsg_static_queue_ctxt *queue_static_context);
  454. #else
  455. int32_t env_create_queue(void **queue, int32_t length, int32_t element_size);
  456. #endif
  457. /*!
  458. * env_delete_queue
  459. *
  460. * Deletes the message queue.
  461. *
  462. * @param queue Queue to delete
  463. */
  464. void env_delete_queue(void *queue);
  465. /*!
  466. * env_put_queue
  467. *
  468. * Put an element in a queue.
  469. *
  470. * @param queue Queue to put element in
  471. * @param msg Pointer to the message to be put into the queue
  472. * @param timeout_ms Timeout in ms
  473. *
  474. * @return - status of function execution
  475. */
  476. int32_t env_put_queue(void *queue, void *msg, uintptr_t timeout_ms);
  477. /*!
  478. * env_get_queue
  479. *
  480. * Get an element out of a queue.
  481. *
  482. * @param queue Queue to get element from
  483. * @param msg Pointer to a memory to save the message
  484. * @param timeout_ms Timeout in ms
  485. *
  486. * @return - status of function execution
  487. */
  488. int32_t env_get_queue(void *queue, void *msg, uintptr_t timeout_ms);
  489. /*!
  490. * env_get_current_queue_size
  491. *
  492. * Get current queue size.
  493. *
  494. * @param queue Queue pointer
  495. *
  496. * @return - Number of queued items in the queue
  497. */
  498. int32_t env_get_current_queue_size(void *queue);
  499. /*!
  500. * env_isr
  501. *
  502. * Invoke RPMSG/IRQ callback
  503. *
  504. * @param env Pointer to environment context data
  505. * @param vector RPMSG IRQ vector ID.
  506. */
  507. #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
  508. void env_isr(void *env, uint32_t vector);
  509. #else
  510. void env_isr(uint32_t vector);
  511. #endif
  512. #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
  513. /*!
  514. * env_get_platform_context
  515. *
  516. * Get the platform layer context from the environment platform context
  517. *
  518. * @param env Pointer to environment context data
  519. *
  520. * @return Pointer to platform context data
  521. */
  522. void *env_get_platform_context(void *env_context);
  523. /*!
  524. * env_init_interrupt
  525. *
  526. * Initialize the ISR data for given virtqueue interrupt
  527. *
  528. * @param env Pointer to environment context data
  529. * @param vq_id Virtqueue ID
  530. * @param isr_data Pointer to initial ISR data
  531. *
  532. * @return Execution status, 0 on success
  533. */
  534. int32_t env_init_interrupt(void *env, int32_t vq_id, void *isr_data);
  535. /*!
  536. * env_deinit_interrupt
  537. *
  538. * Deinitialize the ISR data for given virtqueue interrupt
  539. *
  540. * @param env Pointer to environment context data
  541. * @param vq_id Virtqueue ID
  542. *
  543. * @return Execution status, 0 on success
  544. */
  545. int32_t env_deinit_interrupt(void *env, int32_t vq_id);
  546. #endif
  547. /*!
  548. * env_wait_for_link_up
  549. *
  550. * Env. specific implementation of rpmsg_lite_wait_for_link_up function with the usage
  551. * of RTOS sync. primitives to avoid busy loop. Returns once the link is up.
  552. *
  553. * @param link_state Pointer to the link_state parameter of the rpmsg_lite_instance structure
  554. * @param link_id Link ID used to define the rpmsg-lite instance, see rpmsg_platform.h
  555. * @param timeout_ms Timeout in ms
  556. *
  557. * @return RL_TRUE when link up, RL_FALSE when timeout.
  558. *
  559. */
  560. uint32_t env_wait_for_link_up(volatile uint32_t *link_state, uint32_t link_id, uint32_t timeout_ms);
  561. /*!
  562. * env_tx_callback
  563. *
  564. * Called from rpmsg_lite_tx_callback() to allow unblocking of env_wait_for_link_up()
  565. *
  566. * @param link_id Link ID used to define the rpmsg-lite instance, see rpmsg_platform.h
  567. */
  568. void env_tx_callback(uint32_t link_id);
  569. #endif /* RPMSG_ENV_H_ */