481 lines
11 KiB
C
481 lines
11 KiB
C
/*----------------------------------------------------------------------------
|
|
* Tencent is pleased to support the open source community by making TencentOS
|
|
* available.
|
|
*
|
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
* If you have downloaded a copy of the TencentOS binary from Tencent, please
|
|
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
|
|
*
|
|
* If you have downloaded a copy of the TencentOS source code from Tencent,
|
|
* please note that TencentOS source code is licensed under the BSD 3-Clause
|
|
* License, except for the third-party components listed below which are
|
|
* subject to different license terms. Your integration of TencentOS into your
|
|
* own projects may require compliance with the BSD 3-Clause License, as well
|
|
* as the other licenses applicable to the third-party components included
|
|
* within TencentOS.
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
#include "tos_k.h"
|
|
#if (USE_SMP ==1u)
|
|
|
|
extern uint8_t ucPrimaryCoreNum ;
|
|
__KNL__ static void prvDisableInterruptsAndPortStartSchedulerOnCore( void )
|
|
{
|
|
TOS_CPU_CPSR_ALLOC();
|
|
TOS_CPU_INT_DISABLE();
|
|
port_smp_init_kernel();
|
|
tos_knl_start();
|
|
}
|
|
|
|
__KNL__ void smp_init_core0(void){
|
|
/* No one else should use these! */
|
|
spin_lock_claim( configSMP_SPINLOCK_0 );
|
|
spin_lock_claim( configSMP_SPINLOCK_1 );
|
|
#if (USE_SMP == 1u)
|
|
ucPrimaryCoreNum = configTICK_CORE;
|
|
configASSERT( get_core_num() == 0) ; // we must be started on core 0
|
|
#else
|
|
ucPrimaryCoreNum = get_core_num();
|
|
#endif
|
|
//主核初始化
|
|
port_smp_init_kernel();
|
|
}
|
|
|
|
__API__ k_err_t tos_knl_smp_init(void)
|
|
|
|
{
|
|
k_err_t err;
|
|
err=tos_knl_init();
|
|
TOS_CPU_CPSR_ALLOC();
|
|
//关全局中断
|
|
TOS_CPU_INT_DISABLE();
|
|
smp_init_core0();
|
|
//开全局中断
|
|
TOS_CPU_INT_ENABLE();
|
|
return err;
|
|
}
|
|
#if ( configUSE_CORE_AFFINITY == 1u )
|
|
|
|
__API__ void tos_TaskCoreAffinitySet( k_task_t * Task, BaseType_t CoreAffinityMask ){
|
|
|
|
int flag=0;
|
|
//ensure the core is valid
|
|
CoreAffinityMask &= ( ( 1 << configNUM_CORES ) - 1 );
|
|
TOS_CPU_CPSR_ALLOC();
|
|
TOS_CPU_INT_DISABLE();
|
|
portGET_TASK_LOCK();
|
|
Task->CoreAffinityMask=CoreAffinityMask;
|
|
if(!tos_knl_is_running()){
|
|
portRELEASE_TASK_LOCK();
|
|
TOS_CPU_INT_ENABLE();
|
|
return;
|
|
}
|
|
if (knl_is_sched_locked()) {
|
|
portRELEASE_TASK_LOCK();
|
|
TOS_CPU_INT_ENABLE();
|
|
return;
|
|
}
|
|
|
|
BaseType_t xCoreID = ( BaseType_t ) Task->RunningOnCore;
|
|
if(xCoreID!=-1&&(CoreAffinityMask & ( 1 << xCoreID ) ) == 0){
|
|
if(xCoreID==port_GET_CORE_ID()){
|
|
flag=1;
|
|
}
|
|
else{
|
|
portSCHED_CORE (xCoreID);
|
|
}
|
|
}
|
|
portRELEASE_TASK_LOCK();
|
|
TOS_CPU_INT_ENABLE();
|
|
if(flag){
|
|
knl_sched();
|
|
}
|
|
return ;
|
|
}
|
|
__API__ BaseType_t vTaskCoreAffinityGet( const k_task_t * Task )
|
|
{
|
|
|
|
BaseType_t uxCoreAffinityMask;
|
|
TOS_CPU_CPSR_ALLOC();
|
|
TOS_CPU_INT_DISABLE();
|
|
portGET_TASK_LOCK();
|
|
uxCoreAffinityMask = Task->CoreAffinityMask;
|
|
portRELEASE_TASK_LOCK();
|
|
TOS_CPU_INT_ENABLE();
|
|
return uxCoreAffinityMask;
|
|
}
|
|
#endif
|
|
|
|
#endif
|
|
__API__ k_err_t tos_knl_init(void)
|
|
{
|
|
k_err_t err;
|
|
|
|
cpu_init();
|
|
|
|
readyqueue_init();
|
|
|
|
#if TOS_CFG_MMHEAP_EN > 0
|
|
#if TOS_CFG_MMHEAP_DEFAULT_POOL_EN > 0u
|
|
err = mmheap_init_with_pool(k_mmheap_default_pool, TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE);
|
|
#else
|
|
err = mmheap_init();
|
|
#endif
|
|
if (err != K_ERR_NONE) {
|
|
return err;
|
|
}
|
|
#endif
|
|
|
|
err = knl_idle_init();
|
|
if (err != K_ERR_NONE) {
|
|
return err;
|
|
}
|
|
|
|
#if TOS_CFG_TIMER_EN > 0
|
|
err = soft_timer_init();
|
|
if (err != K_ERR_NONE) {
|
|
return err;
|
|
}
|
|
#endif
|
|
|
|
#if TOS_CFG_PWR_MGR_EN > 0U
|
|
pm_init();
|
|
#endif
|
|
|
|
#if TOS_CFG_TICKLESS_EN > 0u
|
|
tickless_init();
|
|
#endif
|
|
|
|
return K_ERR_NONE;
|
|
}
|
|
|
|
__API__ void tos_knl_irq_enter(void)
|
|
{
|
|
if (!tos_knl_is_running()) {
|
|
return;
|
|
}
|
|
#if (USE_SMP == 1u)
|
|
if (unlikely(k_irq_nest_cnts[ port_GET_CORE_ID()] >= K_NESTING_LIMIT_IRQ)) {
|
|
return;
|
|
}
|
|
++k_irq_nest_cnts[ port_GET_CORE_ID()];
|
|
#else
|
|
if (unlikely(k_irq_nest_cnt >= K_NESTING_LIMIT_IRQ)) {
|
|
return;
|
|
}
|
|
|
|
++k_irq_nest_cnt;
|
|
#endif
|
|
}
|
|
|
|
__API__ void tos_knl_irq_leave(void)
|
|
{
|
|
TOS_CPU_CPSR_ALLOC();
|
|
|
|
if (!tos_knl_is_running()) {
|
|
return;
|
|
}
|
|
|
|
TOS_CPU_INT_DISABLE();
|
|
portGET_TASK_LOCK();
|
|
if (!knl_is_inirq()) {
|
|
portRELEASE_TASK_LOCK();
|
|
TOS_CPU_INT_ENABLE();
|
|
return;
|
|
}
|
|
#if (USE_SMP == 1u)
|
|
--k_irq_nest_cnts[ port_GET_CORE_ID()];
|
|
#else
|
|
--k_irq_nest_cnt;
|
|
#endif
|
|
if (knl_is_inirq()) {
|
|
portRELEASE_TASK_LOCK();
|
|
TOS_CPU_INT_ENABLE();
|
|
return;
|
|
}
|
|
|
|
if (knl_is_sched_locked()) {
|
|
portRELEASE_TASK_LOCK();
|
|
TOS_CPU_INT_ENABLE();
|
|
return;
|
|
}
|
|
|
|
k_next_task = readyqueue_highest_ready_task_get();
|
|
#if ( USE_SMP == 1u)
|
|
k_next_tasks[port_GET_CORE_ID()]=k_next_task;
|
|
#endif
|
|
if (knl_is_self(k_next_task)) {
|
|
portRELEASE_TASK_LOCK();
|
|
TOS_CPU_INT_ENABLE();
|
|
return;
|
|
}
|
|
|
|
cpu_irq_context_switch();
|
|
portRELEASE_TASK_LOCK();
|
|
TOS_CPU_INT_ENABLE();
|
|
}
|
|
|
|
__API__ k_err_t tos_knl_sched_lock(void)
|
|
{
|
|
TOS_CPU_CPSR_ALLOC();
|
|
|
|
TOS_IN_IRQ_CHECK();
|
|
|
|
if (!tos_knl_is_running()) {
|
|
return K_ERR_KNL_NOT_RUNNING;
|
|
}
|
|
|
|
if (k_sched_lock_nest_cnt >= K_NESTING_LIMIT_SCHED_LOCK) {
|
|
return K_ERR_LOCK_NESTING_OVERFLOW;
|
|
}
|
|
|
|
TOS_CPU_INT_DISABLE();
|
|
portGET_TASK_LOCK();
|
|
++k_sched_lock_nest_cnt;
|
|
portRELEASE_TASK_LOCK();
|
|
TOS_CPU_INT_ENABLE();
|
|
return K_ERR_NONE;
|
|
}
|
|
|
|
__API__ k_err_t tos_knl_sched_unlock(void)
|
|
{
|
|
TOS_CPU_CPSR_ALLOC();
|
|
|
|
TOS_IN_IRQ_CHECK();
|
|
|
|
if (!tos_knl_is_running()) {
|
|
return K_ERR_KNL_NOT_RUNNING;
|
|
}
|
|
|
|
if (!knl_is_sched_locked()) {
|
|
return K_ERR_SCHED_NOT_LOCKED;
|
|
}
|
|
|
|
TOS_CPU_INT_DISABLE();
|
|
portGET_TASK_LOCK();
|
|
--k_sched_lock_nest_cnt;
|
|
portRELEASE_TASK_LOCK();
|
|
TOS_CPU_INT_ENABLE();
|
|
|
|
knl_sched();
|
|
return K_ERR_NONE;
|
|
}
|
|
|
|
__API__ k_err_t tos_knl_start_smp(void)
|
|
{
|
|
//启动应用核心
|
|
port_multicore_launch(prvDisableInterruptsAndPortStartSchedulerOnCore);
|
|
//启动主核心调度
|
|
if(port_GET_CORE_ID()==ucPrimaryCoreNum){
|
|
return tos_knl_start();
|
|
}
|
|
return K_ERR_NONE;
|
|
}
|
|
__API__ k_err_t tos_knl_start(void)
|
|
{
|
|
if (unlikely(tos_knl_is_running())) {
|
|
return K_ERR_KNL_RUNNING;
|
|
}
|
|
|
|
#if( USE_SMP == 1u )
|
|
TOS_CPU_CPSR_ALLOC();
|
|
//关全局中断
|
|
TOS_CPU_INT_DISABLE();
|
|
portGET_TASK_LOCK();
|
|
k_next_task = readyqueue_highest_ready_task_get();
|
|
k_next_tasks[port_GET_CORE_ID()]=k_next_task;
|
|
k_curr_tasks[port_GET_CORE_ID()]=k_next_task;
|
|
portRELEASE_TASK_LOCK();
|
|
//开全局中断
|
|
TOS_CPU_INT_ENABLE();
|
|
k_knl_states[port_GET_CORE_ID()] = KNL_STATE_RUNNING;
|
|
#else
|
|
|
|
k_next_task = readyqueue_highest_ready_task_get();
|
|
k_curr_task = k_next_task;
|
|
k_knl_state = KNL_STATE_RUNNING;
|
|
|
|
#endif
|
|
cpu_sched_start();
|
|
return K_ERR_NONE;
|
|
}
|
|
|
|
__API__ int tos_knl_is_running(void)
|
|
{
|
|
#if( USE_SMP == 1u )
|
|
return k_knl_states[port_GET_CORE_ID()] == KNL_STATE_RUNNING;
|
|
#else
|
|
return k_knl_state == KNL_STATE_RUNNING;
|
|
#endif
|
|
}
|
|
|
|
#if TOS_CFG_TICKLESS_EN > 0u
|
|
|
|
/**
|
|
* @brief Get the remain ticks of the first oncoming task.
|
|
*
|
|
* @return The remian ticks of the first oncoming task to be scheduled.
|
|
*/
|
|
__KNL__ k_tick_t knl_next_expires_get(void)
|
|
{
|
|
k_tick_t tick_next_expires;
|
|
#if TOS_CFG_TIMER_EN > 0u
|
|
k_tick_t timer_next_expires;
|
|
#endif
|
|
|
|
tick_next_expires = tick_next_expires_get();
|
|
|
|
#if TOS_CFG_TIMER_EN > 0u
|
|
timer_next_expires = soft_timer_next_expires_get();
|
|
#endif
|
|
|
|
#if TOS_CFG_TIMER_EN > 0u
|
|
return tick_next_expires < timer_next_expires ? tick_next_expires : timer_next_expires;
|
|
#else
|
|
return tick_next_expires;
|
|
#endif
|
|
}
|
|
|
|
#endif
|
|
__KNL__ void knl_sched(void)
|
|
{
|
|
TOS_CPU_CPSR_ALLOC();
|
|
|
|
if (unlikely(!tos_knl_is_running())) {
|
|
return;
|
|
}
|
|
|
|
if (knl_is_inirq()) {
|
|
return;
|
|
}
|
|
|
|
if (knl_is_sched_locked()) {
|
|
return;
|
|
}
|
|
|
|
TOS_CPU_INT_DISABLE();
|
|
portGET_TASK_LOCK(); /* Must always acquire the task lock first */
|
|
k_next_task = readyqueue_highest_ready_task_get();
|
|
#if (USE_SMP == 1u )
|
|
k_next_tasks[port_GET_CORE_ID()]=k_next_task;
|
|
#endif
|
|
if (knl_is_self(k_next_task)) {
|
|
portRELEASE_TASK_LOCK();
|
|
TOS_CPU_INT_ENABLE();
|
|
return;
|
|
}
|
|
cpu_context_switch();
|
|
portRELEASE_TASK_LOCK();
|
|
|
|
TOS_CPU_INT_ENABLE();
|
|
}
|
|
|
|
__KNL__ int knl_is_sched_locked(void)
|
|
{
|
|
return k_sched_lock_nest_cnt > 0u;
|
|
}
|
|
|
|
__KNL__ int knl_is_inirq(void)
|
|
{
|
|
#if (USE_SMP == 1u )
|
|
//return portCHECK_IF_IN_ISR() ;
|
|
return k_irq_nest_cnts[port_GET_CORE_ID()] > 0u;
|
|
#else
|
|
return k_irq_nest_cnt > 0u;
|
|
#endif
|
|
}
|
|
|
|
__KNL__ int knl_is_idle_pre(k_task_t *task)
|
|
{
|
|
int x=0;
|
|
for(x;x<configNUM_CORES;x++){
|
|
if(task==&k_idle_tasks[x])
|
|
return 1;
|
|
}
|
|
return 0;
|
|
|
|
}
|
|
|
|
__KNL__ int knl_is_idle(k_task_t *task)
|
|
{
|
|
#if (USE_SMP == 1u )
|
|
return task->IsIDLE==1;
|
|
#else
|
|
return task == &k_idle_task;
|
|
#endif
|
|
}
|
|
|
|
__KNL__ int knl_is_self(k_task_t *task)
|
|
{
|
|
return task == k_curr_task;
|
|
}
|
|
|
|
__STATIC__ void knl_idle1_entry(void *arg)
|
|
{
|
|
arg = arg; // make compiler happy
|
|
while (K_TRUE) {
|
|
knl_sched();
|
|
}
|
|
}
|
|
__STATIC__ void knl_idle_entry(void *arg)
|
|
{
|
|
arg = arg; // make compiler happy
|
|
while (K_TRUE) {
|
|
#if TOS_CFG_OBJ_DYNAMIC_CREATE_EN > 0u
|
|
task_free_all();
|
|
#endif
|
|
#if TOS_CFG_PWR_MGR_EN > 0u
|
|
pm_power_manager();
|
|
#endif
|
|
if(k_rdyq.highest_prio<K_TASK_PRIO_IDLE){
|
|
knl_sched();
|
|
}
|
|
}
|
|
}
|
|
|
|
__KNL__ k_err_t knl_idle_init(void)
|
|
{
|
|
#if USE_SMP ==1u
|
|
k_err_t xReturn=K_ERR_NONE;
|
|
char cIdleName[ configMAX_TASK_NAME_LEN ];
|
|
BaseType_t x;
|
|
BaseType_t xCoreID;
|
|
for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUM_CORES; xCoreID++ ){
|
|
if( xReturn !=K_ERR_NONE ){
|
|
break;
|
|
}
|
|
for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ ){
|
|
cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
|
|
if( cIdleName[ x ] == ( char ) 0x00 )
|
|
break;
|
|
}
|
|
if( x < configMAX_TASK_NAME_LEN ){
|
|
cIdleName[ x++ ] = xCoreID + '0';
|
|
|
|
/* And append a null character if there is space */
|
|
if( x < configMAX_TASK_NAME_LEN )
|
|
cIdleName[ x ] = '\0';
|
|
|
|
}
|
|
|
|
|
|
xReturn=tos_task_create(&k_idle_tasks[xCoreID] , cIdleName,
|
|
knl_idle_entry, K_NULL,
|
|
K_TASK_PRIO_IDLE,
|
|
k_idle_task_stk_addrs[xCoreID],
|
|
k_idle_task_stk_size,
|
|
0);
|
|
k_idle_tasks[xCoreID].IsIDLE=1;
|
|
|
|
}
|
|
#else
|
|
return tos_task_create(&k_idle_task, "idle",
|
|
knl_idle_entry, K_NULL,
|
|
K_TASK_PRIO_IDLE,
|
|
k_idle_task_stk_addr,
|
|
k_idle_task_stk_size,
|
|
0);
|
|
#endif
|
|
}
|
|
|