| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774 |
- /*
- * Copyright 2020-2023 NXP
- * All rights reserved.
- *
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
- /**************************************************************************
- * FILE NAME
- *
- * rpmsg_env_threadx.c
- *
- *
- * DESCRIPTION
- *
- * This file is ThreadX Implementation of env layer for RPMsg_Lite.
- *
- *
- **************************************************************************/
- #include "rpmsg_compiler.h"
- #include "rpmsg_env.h"
- #include "rpmsg_lite.h"
- #include "tx_api.h"
- #include "tx_event_flags.h"
- #include "rpmsg_platform.h"
- #include "fsl_common.h"
- #include "fsl_component_mem_manager.h"
- #include <stdlib.h>
- #include <string.h>
- #include "virtqueue.h"
- static int32_t env_init_counter = 0;
- static TX_SEMAPHORE env_sema = {0};
- static TX_EVENT_FLAGS_GROUP event_group = {0};
- /* RL_ENV_MAX_MUTEX_COUNT is an arbitrary count greater than 'count'
- if the inital count is 1, this function behaves as a mutex
- if it is greater than 1, it acts as a "resource allocator" with
- the maximum of 'count' resources available.
- Currently, only the first use-case is applicable/applied in RPMsg-Lite.
- */
- #define RL_ENV_MAX_MUTEX_COUNT (10)
- /* Max supported ISR counts */
- #define ISR_COUNT (32U)
- /*!
- * Structure to keep track of registered ISR's.
- */
- struct isr_info
- {
- void *data;
- };
- static struct isr_info isr_table[ISR_COUNT];
- #if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
- #error "This RPMsg-Lite port requires RL_USE_ENVIRONMENT_CONTEXT set to 0"
- #endif
- /*!
- * env_in_isr
- *
- * @returns - true, if currently in ISR
- *
- */
- static int32_t env_in_isr(void)
- {
- return platform_in_isr();
- }
- /*!
- * env_wait_for_link_up
- *
- * Wait until the link_state parameter of the rpmsg_lite_instance is set.
- * Utilize events to avoid busy loop implementation.
- *
- */
- uint32_t env_wait_for_link_up(volatile uint32_t *link_state, uint32_t link_id, uint32_t timeout_ms)
- {
- ULONG actual_events;
- if (*link_state != 1U)
- {
- if (RL_BLOCK == timeout_ms)
- {
- if (TX_SUCCESS ==
- tx_event_flags_get(&event_group, (1UL << link_id), TX_AND, &actual_events, TX_WAIT_FOREVER))
- {
- return 1U;
- }
- }
- else
- {
- if (TX_SUCCESS == tx_event_flags_get(&event_group, (1UL << link_id), TX_AND, &actual_events,
- ((timeout_ms * TX_TIMER_TICKS_PER_SECOND) / 1000)))
- {
- return 1U;
- }
- }
- return 0U;
- }
- else
- {
- return 1U;
- }
- }
- /*!
- * env_tx_callback
- *
- * Set event to notify task waiting in env_wait_for_link_up().
- *
- */
- void env_tx_callback(uint32_t link_id)
- {
- tx_event_flags_set(&event_group, (1UL << link_id), TX_OR);
- }
- /*!
- * env_init
- *
- * Initializes ThreadX environment.
- *
- */
- int32_t env_init(void)
- {
- int32_t retval;
- uint32_t regPrimask = DisableGlobalIRQ(); /* stop scheduler */
- /* verify 'env_init_counter' */
- RL_ASSERT(env_init_counter >= 0);
- if (env_init_counter < 0)
- {
- EnableGlobalIRQ(regPrimask); /* re-enable scheduler */
- return -1;
- }
- env_init_counter++;
- /* multiple call of 'env_init' - return ok */
- if (env_init_counter == 1)
- {
- /* first call */
- if (TX_SUCCESS != tx_semaphore_create((TX_SEMAPHORE *)&env_sema, NULL, 0))
- {
- EnableGlobalIRQ(regPrimask);
- return -1;
- }
- (void)tx_event_flags_create(&event_group, NULL);
- (void)memset(isr_table, 0, sizeof(isr_table));
- EnableGlobalIRQ(regPrimask);
- retval = platform_init();
- tx_semaphore_put((TX_SEMAPHORE *)&env_sema);
- return retval;
- }
- else
- {
- EnableGlobalIRQ(regPrimask);
- /* Get the semaphore and then return it,
- * this allows for platform_init() to block
- * if needed and other tasks to wait for the
- * blocking to be done.
- * This is in ENV layer as this is ENV specific.*/
- if (TX_SUCCESS == tx_semaphore_get((TX_SEMAPHORE *)&env_sema, TX_WAIT_FOREVER))
- {
- tx_semaphore_put((TX_SEMAPHORE *)&env_sema);
- }
- return 0;
- }
- }
- /*!
- * env_deinit
- *
- * Uninitializes ThreadX environment.
- *
- * @returns - execution status
- */
- int32_t env_deinit(void)
- {
- int32_t retval;
- uint32_t regPrimask = DisableGlobalIRQ(); /* stop scheduler */
- /* verify 'env_init_counter' */
- RL_ASSERT(env_init_counter > 0);
- if (env_init_counter <= 0)
- {
- EnableGlobalIRQ(regPrimask); /* re-enable scheduler */
- return -1;
- }
- /* counter on zero - call platform deinit */
- env_init_counter--;
- /* multiple call of 'env_deinit' - return ok */
- if (env_init_counter <= 0)
- {
- /* last call */
- (void)memset(isr_table, 0, sizeof(isr_table));
- retval = platform_deinit();
- (void)tx_event_flags_delete(&event_group);
- (void)memset(&event_group, 0, sizeof(event_group));
- (void)tx_semaphore_delete((TX_SEMAPHORE *)&env_sema);
- (void)memset(&env_sema, 0, sizeof(env_sema));
- EnableGlobalIRQ(regPrimask);
- return retval;
- }
- else
- {
- EnableGlobalIRQ(regPrimask);
- return 0;
- }
- }
- /*!
- * env_allocate_memory - implementation
- *
- * @param size
- */
- void *env_allocate_memory(uint32_t size)
- {
- return (MEM_BufferAlloc(size));
- }
- /*!
- * env_free_memory - implementation
- *
- * @param ptr
- */
- void env_free_memory(void *ptr)
- {
- if (ptr != ((void *)0))
- {
- MEM_BufferFree(ptr);
- }
- }
- /*!
- *
- * env_memset - implementation
- *
- * @param ptr
- * @param value
- * @param size
- */
- void env_memset(void *ptr, int32_t value, uint32_t size)
- {
- (void)memset(ptr, value, size);
- }
- /*!
- *
- * env_memcpy - implementation
- *
- * @param dst
- * @param src
- * @param len
- */
- void env_memcpy(void *dst, void const *src, uint32_t len)
- {
- (void)memcpy(dst, src, len);
- }
- /*!
- *
- * env_strcmp - implementation
- *
- * @param dst
- * @param src
- */
- int32_t env_strcmp(const char *dst, const char *src)
- {
- return (strcmp(dst, src));
- }
- /*!
- *
- * env_strncpy - implementation
- *
- * @param dest
- * @param src
- * @param len
- */
- void env_strncpy(char *dest, const char *src, uint32_t len)
- {
- (void)strncpy(dest, src, len);
- }
- /*!
- *
- * env_strncmp - implementation
- *
- * @param dest
- * @param src
- * @param len
- */
- int32_t env_strncmp(char *dest, const char *src, uint32_t len)
- {
- return (strncmp(dest, src, len));
- }
- /*!
- *
- * env_mb - implementation
- *
- */
- void env_mb(void)
- {
- MEM_BARRIER();
- }
- /*!
- * env_rmb - implementation
- */
- void env_rmb(void)
- {
- MEM_BARRIER();
- }
- /*!
- * env_wmb - implementation
- */
- void env_wmb(void)
- {
- MEM_BARRIER();
- }
- /*!
- * env_map_vatopa - implementation
- *
- * @param address
- */
- uint32_t env_map_vatopa(void *address)
- {
- return platform_vatopa(address);
- }
- /*!
- * env_map_patova - implementation
- *
- * @param address
- */
- void *env_map_patova(uint32_t address)
- {
- return platform_patova(address);
- }
- /*!
- * env_create_mutex
- *
- * Creates a mutex with the given initial count.
- *
- */
- #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
- int32_t env_create_mutex(void **lock, int32_t count, void *context)
- #else
- int32_t env_create_mutex(void **lock, int32_t count)
- #endif
- {
- TX_SEMAPHORE *semaphore_ptr;
- if (count > RL_ENV_MAX_MUTEX_COUNT)
- {
- return -1;
- }
- #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
- semaphore_ptr = (TX_SEMAPHORE *)context;
- #else
- semaphore_ptr = (TX_SEMAPHORE *)env_allocate_memory(sizeof(TX_SEMAPHORE));
- #endif
- if (semaphore_ptr == ((void *)0))
- {
- return -1;
- }
- if (TX_SUCCESS == tx_semaphore_create((TX_SEMAPHORE *)semaphore_ptr, NULL, count))
- {
- *lock = (void *)semaphore_ptr;
- return 0;
- }
- else
- {
- #if !(defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1))
- env_free_memory(semaphore_ptr);
- #endif
- return -1;
- }
- }
- /*!
- * env_delete_mutex
- *
- * Deletes the given lock
- *
- */
- void env_delete_mutex(void *lock)
- {
- (void)tx_semaphore_delete((TX_SEMAPHORE *)lock);
- #if !(defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1))
- env_free_memory(lock);
- #endif
- }
- /*!
- * env_lock_mutex
- *
- * Tries to acquire the lock, if lock is not available then call to
- * this function will suspend.
- */
- void env_lock_mutex(void *lock)
- {
- if (env_in_isr() == 0)
- {
- (void)tx_semaphore_get((TX_SEMAPHORE *)lock, TX_WAIT_FOREVER);
- }
- }
- /*!
- * env_unlock_mutex
- *
- * Releases the given lock.
- */
- void env_unlock_mutex(void *lock)
- {
- if (env_in_isr() == 0)
- {
- (void)tx_semaphore_put((TX_SEMAPHORE *)lock);
- }
- }
- /*!
- * env_create_sync_lock
- *
- * Creates a synchronization lock primitive. It is used
- * when signal has to be sent from the interrupt context to main
- * thread context.
- */
- #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
- int32_t env_create_sync_lock(void **lock, int32_t state, void *context)
- {
- return env_create_mutex(lock, state, context); /* state=1 .. initially free */
- }
- #else
- int32_t env_create_sync_lock(void **lock, int32_t state)
- {
- return env_create_mutex(lock, state); /* state=1 .. initially free */
- }
- #endif
- /*!
- * env_delete_sync_lock
- *
- * Deletes the given lock
- *
- */
- void env_delete_sync_lock(void *lock)
- {
- if (lock != ((void *)0))
- {
- env_delete_mutex(lock);
- }
- }
- /*!
- * env_acquire_sync_lock
- *
- * Tries to acquire the lock, if lock is not available then call to
- * this function waits for lock to become available.
- */
- void env_acquire_sync_lock(void *lock)
- {
- if (lock != ((void *)0))
- {
- env_lock_mutex(lock);
- }
- }
- /*!
- * env_release_sync_lock
- *
- * Releases the given lock.
- */
- void env_release_sync_lock(void *lock)
- {
- if (lock != ((void *)0))
- {
- env_unlock_mutex(lock);
- }
- }
- /*!
- * env_sleep_msec
- *
- * Suspends the calling thread for given time , in msecs.
- */
- void env_sleep_msec(uint32_t num_msec)
- {
- (void)tx_thread_sleep((num_msec * TX_TIMER_TICKS_PER_SECOND) / 1000);
- }
- /*!
- * env_register_isr
- *
- * Registers interrupt handler data for the given interrupt vector.
- *
- * @param vector_id - virtual interrupt vector number
- * @param data - interrupt handler data (virtqueue)
- */
- void env_register_isr(uint32_t vector_id, void *data)
- {
- RL_ASSERT(vector_id < ISR_COUNT);
- if (vector_id < ISR_COUNT)
- {
- isr_table[vector_id].data = data;
- }
- }
- /*!
- * env_unregister_isr
- *
- * Unregisters interrupt handler data for the given interrupt vector.
- *
- * @param vector_id - virtual interrupt vector number
- */
- void env_unregister_isr(uint32_t vector_id)
- {
- RL_ASSERT(vector_id < ISR_COUNT);
- if (vector_id < ISR_COUNT)
- {
- isr_table[vector_id].data = ((void *)0);
- }
- }
- /*!
- * env_enable_interrupt
- *
- * Enables the given interrupt
- *
- * @param vector_id - virtual interrupt vector number
- */
- void env_enable_interrupt(uint32_t vector_id)
- {
- (void)platform_interrupt_enable(vector_id);
- }
- /*!
- * env_disable_interrupt
- *
- * Disables the given interrupt
- *
- * @param vector_id - virtual interrupt vector number
- */
- void env_disable_interrupt(uint32_t vector_id)
- {
- (void)platform_interrupt_disable(vector_id);
- }
- /*!
- * env_map_memory
- *
- * Enables memory mapping for given memory region.
- *
- * @param pa - physical address of memory
- * @param va - logical address of memory
- * @param size - memory size
- * param flags - flags for cache/uncached and access type
- */
- void env_map_memory(uint32_t pa, uint32_t va, uint32_t size, uint32_t flags)
- {
- platform_map_mem_region(va, pa, size, flags);
- }
- /*!
- * env_disable_cache
- *
- * Disables system caches.
- *
- */
- void env_disable_cache(void)
- {
- platform_cache_all_flush_invalidate();
- platform_cache_disable();
- }
- /*!
- *
- * env_get_timestamp
- *
- * Returns a 64 bit time stamp.
- *
- *
- */
- uint64_t env_get_timestamp(void)
- {
- return tx_time_get();
- }
- /*========================================================= */
- /* Util data / functions */
- void env_isr(uint32_t vector)
- {
- struct isr_info *info;
- RL_ASSERT(vector < ISR_COUNT);
- if (vector < ISR_COUNT)
- {
- info = &isr_table[vector];
- virtqueue_notification((struct virtqueue *)info->data);
- }
- }
- /*
- * env_create_queue
- *
- * Creates a message queue.
- *
- * @param queue - pointer to created queue
- * @param length - maximum number of elements in the queue
- * @param element_size - queue element size in bytes
- * @param queue_static_storage - pointer to queue static storage buffer
- * @param queue_static_context - pointer to queue static context
- *
- * @return - status of function execution
- */
- #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
- int32_t env_create_queue(void **queue,
- int32_t length,
- int32_t element_size,
- uint8_t *queue_static_storage,
- rpmsg_static_queue_ctxt *queue_static_context)
- #else
- int32_t env_create_queue(void **queue, int32_t length, int32_t element_size)
- #endif
- {
- struct TX_QUEUE *queue_ptr = ((void *)0);
- char *msgq_buffer_ptr = ((void *)0);
- #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
- queue_ptr = (struct TX_QUEUE *)queue_static_context;
- msgq_buffer_ptr = (char *)queue_static_storage;
- #else
- queue_ptr = (struct TX_QUEUE *)env_allocate_memory(sizeof(TX_QUEUE));
- msgq_buffer_ptr = (char *)env_allocate_memory(length * element_size);
- #endif
- if ((queue_ptr == ((void *)0)) || (msgq_buffer_ptr == ((void *)0)))
- {
- return -1;
- }
- if (TX_SUCCESS == tx_queue_create((TX_QUEUE *)queue_ptr, NULL, (element_size / RL_WORD_SIZE),
- (VOID *)msgq_buffer_ptr, (length * element_size)))
- {
- *queue = (void *)queue_ptr;
- return 0;
- }
- else
- {
- #if !(defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1))
- env_free_memory(msgq_buffer_ptr);
- env_free_memory(queue_ptr);
- #endif
- return -1;
- }
- }
- /*!
- * env_delete_queue
- *
- * Deletes the message queue.
- *
- * @param queue - queue to delete
- */
- void env_delete_queue(void *queue)
- {
- tx_queue_delete((TX_QUEUE *)(queue));
- #if !(defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1))
- env_free_memory(((TX_QUEUE *)(queue))->tx_queue_start);
- env_free_memory(queue);
- #endif
- }
- /*!
- * env_put_queue
- *
- * Put an element in a queue.
- *
- * @param queue - queue to put element in
- * @param msg - pointer to the message to be put into the queue
- * @param timeout_ms - timeout in ms
- *
- * @return - status of function execution
- */
- int32_t env_put_queue(void *queue, void *msg, uintptr_t timeout_ms)
- {
- if (RL_BLOCK == timeout_ms)
- {
- if (TX_SUCCESS == tx_queue_send((TX_QUEUE *)(queue), msg, TX_WAIT_FOREVER))
- {
- return 1;
- }
- }
- else
- {
- if (TX_SUCCESS == tx_queue_send((TX_QUEUE *)(queue), msg, ((timeout_ms * TX_TIMER_TICKS_PER_SECOND) / 1000)))
- {
- return 1;
- }
- }
- return 0;
- }
- /*!
- * env_get_queue
- *
- * Get an element out of a queue.
- *
- * @param queue - queue to get element from
- * @param msg - pointer to a memory to save the message
- * @param timeout_ms - timeout in ms
- *
- * @return - status of function execution
- */
- int32_t env_get_queue(void *queue, void *msg, uintptr_t timeout_ms)
- {
- if (RL_BLOCK == timeout_ms)
- {
- if (TX_SUCCESS == tx_queue_receive((TX_QUEUE *)(queue), msg, TX_WAIT_FOREVER))
- {
- return 1;
- }
- }
- else
- {
- if (TX_SUCCESS == tx_queue_receive((TX_QUEUE *)(queue), msg, ((timeout_ms * TX_TIMER_TICKS_PER_SECOND) / 1000)))
- {
- return 1;
- }
- }
- return 0;
- }
- /*!
- * env_get_current_queue_size
- *
- * Get current queue size.
- *
- * @param queue - queue pointer
- *
- * @return - Number of queued items in the queue
- */
- int32_t env_get_current_queue_size(void *queue)
- {
- int32_t enqueued;
- ULONG available_storage;
- TX_THREAD *first_suspended;
- ULONG suspended_count;
- TX_QUEUE *next_queue;
- if (TX_SUCCESS == tx_queue_info_get((TX_QUEUE *)(queue), NULL, (ULONG *)&enqueued, &available_storage,
- &first_suspended, &suspended_count, &next_queue))
- {
- return enqueued;
- }
- else
- {
- return -1;
- }
- }
|