| /***************************************************************************** |
| * Copyright Statement: |
| * -------------------- |
| * This software is protected by Copyright and the information contained |
| * herein is confidential. The software may not be copied and the information |
| * contained herein may not be used or disclosed except with the written |
| * permission of MediaTek Inc. (C) 2018 |
| * |
| * BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES |
| * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE") |
| * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON |
| * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, |
| * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF |
| * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. |
| * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE |
| * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR |
| * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH |
| * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO |
| * NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S |
| * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM. |
| * |
| * BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE |
| * LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, |
| * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, |
| * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO |
| * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. |
| * |
| * THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE |
| * WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF |
| * LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND |
| * RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER |
| * THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC). |
| * |
| *****************************************************************************/ |
| |
| /***************************************************************************** |
| * |
| * Filename: |
| * --------- |
| * mer_kernel_task.h |
| * |
| * Project: |
| * -------- |
| * MERTOS |
| * |
| * Description: |
| * ------------ |
| * Declare task related API header. |
| * |
| * Author: |
| * ------- |
| * ------- |
| * |
| *****************************************************************************/ |
| #ifndef _MER_KERNEL_TASK_H_ |
| #define _MER_KERNEL_TASK_H_ |
| |
| #include "mer_kernel_config_public.h" |
| #include "mer_kernel_scheduler.h" |
| #include "mer_service_task.h" |
| #include "mer_kernel_switch_target.h" |
| #include "mer_kernel_lock_target.h" |
| #include "mer_config_public.h" |
| #include <stddef.h> |
| |
| /////////////////////////////////////////////////////////////////////////////// |
| // Porting variable |
| /////////////////////////////////////////////////////////////////////////////// |
| typedef mer_service_task_cb mer_kernel_task_cb; |
| |
| /* Make sure the sp is the first element in the control block */ |
| MER_CASSERT(offsetof(mer_kernel_task_cb, stack_ptr) == 0); |
| |
| /* Make sure the offset of stack_end in the control block matches with ASM */ |
| #if defined(__MD97__) || defined(__MD97P__) |
| #include "mer_kernel_switch_target_shaolin.inc" |
| MER_CASSERT(offsetof(mer_kernel_task_cb, last_execution_vpe) == MER_KERNEL_SWITCH_LAST_EXE_VPE_OFFSET); |
| #endif /* defined(__MD97__) || defined(__MD97P__) */ |
| |
| |
| /////////////////////////////////////////////////////////////////////////////// |
| // Macro |
| /////////////////////////////////////////////////////////////////////////////// |
| #define MER_KERNEL_TASK_LEVEL_BIT 31 |
| #define MER_KERNEL_TASK_LEVEL_MASK (1 << MER_KERNEL_TASK_LEVEL_BIT) |
| #define MER_KERNEL_TASK_APPEND_PRIORITY(task_id) ((1 << MER_KERNEL_TASK_LEVEL_BIT) | (task_id)) |
| #define MER_KERNEL_TASK_REMOVE_PRIORITY(task_id) ((~(1 << MER_KERNEL_TASK_LEVEL_BIT)) & (task_id)) |
| #define MER_KERNEL_TASK_NUM mer_kernel_task_num |
| #define MER_KERNEL_TASK_MASK_NUM mer_kernel_task_mask_num |
| #define MER_KERNEL_TASK_TOP_MASK_NUM mer_kernel_task_top_mask_num |
| |
| /* context means it is either preempted by ISR or running */ |
| /* For task, it is either bit 31 or bit 29 */ |
| #define MER_KERNEL_TASK_CONTEXT_MASK (MER_KERNEL_TASK_LEVEL_MASK | (1 << (MER_KERNEL_TASK_LEVEL_BIT-2))) |
| |
| |
| /////////////////////////////////////////////////////////////////////////////// |
| // Enum |
| /////////////////////////////////////////////////////////////////////////////// |
| |
| |
| |
| /////////////////////////////////////////////////////////////////////////////// |
| // Structure |
| /////////////////////////////////////////////////////////////////////////////// |
| typedef struct { |
| mer_uint32 *mask; |
| } mer_kernel_task_top_mask; |
| |
| typedef struct { |
| mer_uint32 *mask; |
| } mer_kernel_task_mask; |
| |
| typedef mer_uint32 mer_kernel_task_id; |
| /////////////////////////////////////////////////////////////////////////////// |
| // External variable |
| /////////////////////////////////////////////////////////////////////////////// |
| |
| extern mer_kernel_task_cb mer_kernel_task_info_table[]; |
| extern mer_uint8 mer_kernel_task_dummy_stack[]; |
| |
| extern mer_kernel_task_top_mask mer_kernel_task_top_ready_mask[MIPS_HW_VPE_NUM]; |
| extern mer_kernel_task_mask mer_kernel_task_ready_mask[MIPS_HW_VPE_NUM]; |
| |
| extern mer_uint32 mer_kernel_task_info_table_begin; |
| extern mer_uint32 mer_kernel_task_info_table_end; |
| |
| extern mer_uint32 mer_kernel_task_idle_priority_begin; |
| |
| extern const mer_uint32 mer_kernel_task_num; |
| extern const mer_uint32 mer_kernel_task_mask_num; |
| extern const mer_uint32 mer_kernel_task_top_mask_num; |
| |
| /////////////////////////////////////////////////////////////////////////////// |
| // External functions |
| /////////////////////////////////////////////////////////////////////////////// |
| void mer_kernel_task_remove(mer_kernel_task_id); |
| void mer_kernel_task_insert(mer_kernel_task_id); |
| void mer_kernel_task_set_running_state(mer_kernel_task_id task_id); |
| void mer_kernel_task_update_ready_job(mer_uint32 vpe_id); |
| void mer_kernel_task_suspend(mer_kernel_task_id task_id, mer_service_task_state suspend_reason); |
| void mer_kernel_task_resume(mer_kernel_task_cb* cb, mer_service_task_state suspend_reason); |
| void mer_kernel_task_initialization(); |
| void mer_kernel_task_default_main(); |
| void mer_kernel_task_set_priority(mer_kernel_task_id source, mer_kernel_task_id target_priority); |
| void mer_kernel_task_change_current_affinity(mer_uint32 affinity_mask); |
| void mer_kernel_task_set_affinity_to_current_vpe(); |
| void mer_kernel_task_resume_and_return(mer_kernel_task_cb* cb, mer_service_task_state suspend_reason); |
| |
| #if defined(__MER_CONFIG_USE_DYNAMIC_ALLOCATION__) |
| void mer_kernel_task_stack_allocation(); |
| #endif /* __MER_CONFIG_USE_DYNAMIC_ALLOCATION__ */ |
| |
| /////////////////////////////////////////////////////////////////////////////// |
| // Static inline functions |
| /////////////////////////////////////////////////////////////////////////////// |
| /** |
| * Return the task priority by cb |
| * Note that this function assume the schedule lock is taken |
| * |
| * Time complexity O(1) |
| * |
| * @param[out] The task priority |
| * @param[in] cb |
| * |
| */ |
| static inline mer_kernel_task_id mer_kernel_task_get_priority(mer_kernel_task_cb *cb){ |
| |
| return cb->priority; |
| } |
| |
| /** |
| * Return the inheritor task index by cb |
| * Note that this function assume the schedule lock is taken |
| * |
| * Time complexity O(1) |
| * |
| * @param[out] The inheritor task index |
| * @param[in] cb |
| * |
| */ |
| static inline mer_kernel_task_id mer_kernel_task_get_inheritor_task_index(mer_kernel_task_cb *cb){ |
| |
| return cb->inheritor_task_index; |
| } |
| |
| /** |
| * Return the task index with cb pointer |
| * |
| * |
| * Time complexity O(1) |
| * |
| * @param[out] Index of input cb pointer |
| * @param[in] The task pointer |
| * |
| */ |
| static inline mer_uint32 mer_kernel_task_get_index(mer_kernel_task_cb *cb){ |
| |
| //return (mer_uint32)(cb - mer_kernel_task_info_table); |
| return (mer_uint32) cb->job_index; |
| } |
| |
| /** |
| * Return the current running task priority |
| * Note that this function assume the schedule lock is taken |
| * |
| * If we have private memory, no need lock anymore |
| * |
| * Time complexity O(1) |
| * |
| * @param[out] The current task id |
| * @param[in] vpe_id |
| * |
| */ |
| static inline mer_kernel_task_id mer_kernel_task_get_current_priority(){ |
| #if defined(__MER_KERNEL_USE_MIPS_CP0_CONTEXT_REG__) |
| mer_kernel_task_cb *cb = (mer_kernel_task_cb *)(MER_KERNEL_GET_CURRENT_CONTROL_BLOCK()); |
| return cb->priority; |
| #else |
| mer_uint32 current_vpe = mer_kernel_utility_get_current_vpe_id(); |
| return MER_KERNEL_TASK_REMOVE_PRIORITY(mer_kernel_scheduler_running_job[current_vpe].priority); |
| #endif |
| } |
| /** |
| * Return the current running task index |
| * Note that this function assume the schedule lock is taken |
| * |
| * If we have private memory, no need lock anymore |
| * |
| * Time complexity O(1) |
| * |
| * @param[out] The current task id |
| * @param[in] vpe_id |
| * |
| */ |
| __attribute__((always_inline)) static inline mer_kernel_task_id mer_kernel_task_get_current_index(){ |
| |
| #if defined(__MER_KERNEL_USE_MIPS_CP0_CONTEXT_REG__) |
| mer_kernel_task_cb *cb = (mer_kernel_task_cb *)(MER_KERNEL_GET_CURRENT_CONTROL_BLOCK()); |
| return mer_kernel_task_get_index(cb); |
| #else |
| mer_uint32 current_vpe = mer_kernel_utility_get_current_vpe_id(); |
| return mer_kernel_scheduler_running_job[current_vpe].job_index; |
| #endif /* __MER_KERNEL_USE_MIPS_CP0_CONTEXT_REG__ */ |
| } |
| |
| /** |
| * Return the current running task cb pointer |
| * Note that this function assume the schedule lock is taken |
| * |
| * If we have private memory, no need lock anymore |
| * |
| * Time complexity O(1) |
| * |
| * @param[out] cb pointer |
| * @param[in] vpe_id |
| * |
| */ |
| static inline mer_kernel_task_cb * mer_kernel_task_get_current_cb(){ |
| #if defined(__MER_KERNEL_USE_MIPS_CP0_CONTEXT_REG__) |
| return (mer_kernel_task_cb *)(MER_KERNEL_GET_CURRENT_CONTROL_BLOCK()); |
| #else |
| mer_uint32 current_vpe = mer_kernel_utility_get_current_vpe_id(); |
| mer_uint32 job_index = mer_kernel_scheduler_running_job[current_vpe].job_index; |
| return &mer_kernel_task_info_table[job_index]; |
| #endif /* __MER_KERNEL_USE_MIPS_CP0_CONTEXT_REG__ */ |
| } |
| |
| /** |
| * Return the state of specified task control block |
| * |
| * |
| * Time complexity O(1) |
| * |
| * @param[out] State of input cb pointer |
| * @param[in] The task pointer |
| * |
| */ |
| static inline mer_service_task_state mer_kernel_task_get_state(mer_kernel_task_cb *cb){ |
| |
| return cb->state; |
| } |
| |
| #endif /* _MER_KERNEL_TASK_H_ */ |
| |