2012-03-17 17:32:52 -07:00
|
|
|
/*
|
2015-06-03 17:44:46 -07:00
|
|
|
* Copyright (c) 2012-2015 Travis Geiselbrecht
|
2012-03-17 17:32:52 -07:00
|
|
|
*
|
2019-07-05 17:22:23 -07:00
|
|
|
* Use of this source code is governed by a MIT-style
|
|
|
|
|
* license that can be found in the LICENSE file or at
|
|
|
|
|
* https://opensource.org/licenses/MIT
|
2012-03-17 17:32:52 -07:00
|
|
|
*/
|
2019-06-17 18:28:51 -07:00
|
|
|
#include <lk/debug.h>
|
2012-03-17 17:32:52 -07:00
|
|
|
#include <arch.h>
|
|
|
|
|
#include <arch/ops.h>
|
|
|
|
|
#include <arch/arm.h>
|
2013-03-10 17:41:10 -07:00
|
|
|
#include <kernel/thread.h>
|
2013-07-19 18:52:28 -07:00
|
|
|
#include <kernel/debug.h>
|
2012-03-17 17:32:52 -07:00
|
|
|
#include <platform.h>
|
2012-11-11 20:08:50 -08:00
|
|
|
#include <arch/arm/cm.h>
|
2015-12-14 13:15:42 -08:00
|
|
|
#include <target.h>
|
2012-04-10 14:19:16 -07:00
|
|
|
|
|
|
|
|
extern void *vectab;
|
2012-03-17 17:32:52 -07:00
|
|
|
|
2014-03-05 12:51:34 -08:00
|
|
|
#if ARM_CM_DYNAMIC_PRIORITY_SIZE
|
2013-03-10 17:19:53 -07:00
|
|
|
unsigned int arm_cm_num_irq_pri_bits;
|
|
|
|
|
unsigned int arm_cm_irq_pri_mask;
|
2012-10-27 16:31:28 -07:00
|
|
|
#endif
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2021-02-13 20:17:42 -08:00
|
|
|
/* if otherwise not set externally, load the VTOR for all armv7m+ cpus.
|
|
|
|
|
* dynamic VTOR setting is optional on armv6m cores.
|
|
|
|
|
*/
|
|
|
|
|
#ifndef ARM_CM_SET_VTOR
|
|
|
|
|
#if (__CORTEX_M >= 0x03) || (CORTEX_SC >= 300)
|
|
|
|
|
#define ARM_CM_SET_VTOR 1
|
|
|
|
|
#else
|
|
|
|
|
#define ARM_CM_SET_VTOR 0
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
void arch_early_init(void) {
|
2014-03-05 12:51:34 -08:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
arch_disable_ints();
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2021-02-13 20:17:42 -08:00
|
|
|
#if ARM_CM_SET_VTOR
|
2015-11-09 14:27:09 -08:00
|
|
|
/* set the vector table base */
|
|
|
|
|
SCB->VTOR = (uint32_t)&vectab;
|
2021-02-13 20:17:42 -08:00
|
|
|
#endif
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2021-02-13 20:17:42 -08:00
|
|
|
#if (__CORTEX_M >= 0x03) || (CORTEX_SC >= 300)
|
|
|
|
|
uint i;
|
2014-03-05 12:51:34 -08:00
|
|
|
#if ARM_CM_DYNAMIC_PRIORITY_SIZE
|
2015-11-09 14:27:09 -08:00
|
|
|
/* number of priorities */
|
|
|
|
|
for (i=0; i < 7; i++) {
|
|
|
|
|
__set_BASEPRI(1 << i);
|
|
|
|
|
if (__get_BASEPRI() != 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
arm_cm_num_irq_pri_bits = 8 - i;
|
|
|
|
|
arm_cm_irq_pri_mask = ~((1 << i) - 1) & 0xff;
|
2012-10-27 16:31:28 -07:00
|
|
|
#endif
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
/* clear any pending interrupts and set all the vectors to medium priority */
|
|
|
|
|
uint groups = (SCnSCB->ICTR & 0xf) + 1;
|
|
|
|
|
for (i = 0; i < groups; i++) {
|
|
|
|
|
NVIC->ICER[i] = 0xffffffff;
|
|
|
|
|
NVIC->ICPR[i] = 0xffffffff;
|
|
|
|
|
for (uint j = 0; j < 32; j++) {
|
|
|
|
|
NVIC_SetPriority(i*32 + j, arm_cm_medium_priority());
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-05 12:51:34 -08:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
/* leave BASEPRI at 0 */
|
|
|
|
|
__set_BASEPRI(0);
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
/* set priority grouping to 0 */
|
|
|
|
|
NVIC_SetPriorityGrouping(0);
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
/* enable certain faults */
|
|
|
|
|
SCB->SHCSR |= (SCB_SHCSR_USGFAULTENA_Msk | SCB_SHCSR_BUSFAULTENA_Msk | SCB_SHCSR_MEMFAULTENA_Msk);
|
2021-02-13 20:17:42 -08:00
|
|
|
#endif
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
/* set the svc and pendsv priority level to pretty low */
|
|
|
|
|
NVIC_SetPriority(SVCall_IRQn, arm_cm_lowest_priority());
|
|
|
|
|
NVIC_SetPriority(PendSV_IRQn, arm_cm_lowest_priority());
|
2012-05-10 18:59:29 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
/* set systick and debugmonitor to medium priority */
|
|
|
|
|
NVIC_SetPriority(SysTick_IRQn, arm_cm_medium_priority());
|
2016-01-18 11:07:31 -08:00
|
|
|
|
|
|
|
|
#if (__CORTEX_M >= 0x03)
|
2015-11-09 14:27:09 -08:00
|
|
|
NVIC_SetPriority(DebugMonitor_IRQn, arm_cm_medium_priority());
|
2016-01-18 11:07:31 -08:00
|
|
|
#endif
|
2015-07-10 00:50:05 -07:00
|
|
|
|
2015-09-22 14:39:11 -07:00
|
|
|
/* FPU settings ------------------------------------------------------------*/
|
|
|
|
|
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
|
|
|
|
|
SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-07-10 00:50:05 -07:00
|
|
|
#if ARM_WITH_CACHE
|
2024-05-09 19:28:56 -07:00
|
|
|
arch_enable_cache(ARCH_CACHE_FLAG_UCACHE);
|
2015-07-10 00:50:05 -07:00
|
|
|
#endif
|
2012-03-17 17:32:52 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
void arch_init(void) {
|
2012-05-30 20:25:51 -07:00
|
|
|
#if ENABLE_CYCLE_COUNTER
|
2019-06-12 14:25:39 -07:00
|
|
|
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
|
|
|
|
|
DWT->CYCCNT = 0;
|
|
|
|
|
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; // enable cycle counter
|
2016-03-28 19:57:17 -07:00
|
|
|
#endif
|
2012-03-17 17:32:52 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
void arch_quiesce(void) {
|
2016-03-09 15:21:04 -08:00
|
|
|
#if ARM_WITH_CACHE
|
2024-05-09 19:28:56 -07:00
|
|
|
arch_disable_cache(ARCH_CACHE_FLAG_UCACHE);
|
2016-03-09 15:21:04 -08:00
|
|
|
#endif
|
2012-03-17 17:32:52 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
void arch_idle(void) {
|
2015-11-09 14:27:09 -08:00
|
|
|
__asm__ volatile("wfi");
|
2012-03-17 17:32:52 -07:00
|
|
|
}
|
|
|
|
|
|
2016-01-12 11:19:33 -08:00
|
|
|
#if (__CORTEX_M >= 0x03) || (CORTEX_SC >= 300)
|
|
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
void _arm_cm_set_irqpri(uint32_t pri) {
|
2015-11-09 14:27:09 -08:00
|
|
|
if (pri == 0) {
|
|
|
|
|
__disable_irq(); // cpsid i
|
|
|
|
|
__set_BASEPRI(0);
|
|
|
|
|
} else if (pri >= 256) {
|
|
|
|
|
__set_BASEPRI(0);
|
|
|
|
|
__enable_irq();
|
|
|
|
|
} else {
|
|
|
|
|
uint32_t _pri = pri & arm_cm_irq_pri_mask;
|
|
|
|
|
|
|
|
|
|
if (_pri == 0)
|
|
|
|
|
__set_BASEPRI(1 << (8 - arm_cm_num_irq_pri_bits));
|
|
|
|
|
else
|
|
|
|
|
__set_BASEPRI(_pri);
|
|
|
|
|
__enable_irq(); // cpsie i
|
|
|
|
|
}
|
2012-04-10 14:19:16 -07:00
|
|
|
}
|
2016-01-12 11:19:33 -08:00
|
|
|
#endif
|
|
|
|
|
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
void arm_cm_irq_entry(void) {
|
2015-11-09 14:27:09 -08:00
|
|
|
// Set PRIMASK to 1
|
|
|
|
|
// This is so that later calls to arch_ints_disabled() returns true while we're inside the int handler
|
|
|
|
|
// Note: this will probably screw up future efforts to stack higher priority interrupts since we're setting
|
|
|
|
|
// the cpu to essentially max interrupt priority here. Will have to rethink it then.
|
|
|
|
|
__disable_irq();
|
|
|
|
|
|
|
|
|
|
THREAD_STATS_INC(interrupts);
|
|
|
|
|
KEVLOG_IRQ_ENTER(__get_IPSR());
|
2015-12-14 13:15:42 -08:00
|
|
|
|
|
|
|
|
target_set_debug_led(1, true);
|
2013-03-10 17:41:10 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
void arm_cm_irq_exit(bool reschedule) {
|
2015-12-14 13:15:42 -08:00
|
|
|
target_set_debug_led(1, false);
|
|
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
if (reschedule)
|
2023-05-14 23:11:05 +00:00
|
|
|
thread_preempt();
|
2013-03-10 17:41:10 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
KEVLOG_IRQ_EXIT(__get_IPSR());
|
2015-06-03 17:44:46 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
__enable_irq(); // clear PRIMASK
|
2013-03-10 17:41:10 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
void arch_chain_load(void *entry, ulong arg0, ulong arg1, ulong arg2, ulong arg3) {
|
2016-03-09 15:21:04 -08:00
|
|
|
#if (__CORTEX_M >= 0x03)
|
|
|
|
|
|
2020-07-25 16:46:34 -07:00
|
|
|
uint32_t *entry_vector = (uint32_t *)entry;
|
2016-03-09 15:21:04 -08:00
|
|
|
|
|
|
|
|
__asm__ volatile(
|
|
|
|
|
"mov r0, %[arg0]; "
|
|
|
|
|
"mov r1, %[arg1]; "
|
|
|
|
|
"mov r2, %[arg2]; "
|
|
|
|
|
"mov r3, %[arg3]; "
|
|
|
|
|
"mov sp, %[SP]; "
|
|
|
|
|
"bx %[entry]; "
|
|
|
|
|
:
|
|
|
|
|
: [arg0]"r"(arg0),
|
2019-06-19 20:54:28 -07:00
|
|
|
[arg1]"r"(arg1),
|
|
|
|
|
[arg2]"r"(arg2),
|
|
|
|
|
[arg3]"r"(arg3),
|
2020-07-25 16:46:34 -07:00
|
|
|
[SP]"r"(entry_vector[0]),
|
|
|
|
|
[entry]"r"(entry_vector[1])
|
2016-03-09 15:21:04 -08:00
|
|
|
: "r0", "r1", "r2", "r3"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
__UNREACHABLE;
|
|
|
|
|
#else
|
2014-08-12 16:10:16 -07:00
|
|
|
PANIC_UNIMPLEMENTED;
|
2016-03-09 15:21:04 -08:00
|
|
|
#endif
|
2014-08-12 16:10:16 -07:00
|
|
|
}
|