Skip to content

Commit d8601f8

Browse files
committed
feat(hal): Add CPU_APM support for ESP32-C61
1 parent 37733a8 commit d8601f8

File tree

7 files changed

+335
-37
lines changed

7 files changed

+335
-37
lines changed

components/hal/apm_hal.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,82 @@ void apm_hal_enable_region_filter(apm_ctrl_module_t ctrl_mod, uint32_t regn_num,
218218
}
219219
}
220220

221+
void apm_hal_set_region_start_addr(apm_ctrl_module_t ctrl_mod, uint32_t regn_num, uint32_t addr)
222+
{
223+
switch (ctrl_mod) {
224+
case APM_CTRL_HP_APM:
225+
apm_ll_hp_apm_set_region_start_addr(regn_num, addr);
226+
break;
227+
#if SOC_APM_LP_APM0_SUPPORTED
228+
case APM_CTRL_LP_APM0:
229+
apm_ll_lp_apm0_set_region_start_addr(regn_num, addr);
230+
break;
231+
#endif
232+
case APM_CTRL_LP_APM:
233+
apm_ll_lp_apm_set_region_start_addr(regn_num, addr);
234+
break;
235+
#if SOC_APM_CPU_APM_SUPPORTED
236+
case APM_CTRL_CPU_APM:
237+
apm_ll_cpu_apm_set_region_start_addr(regn_num, addr);
238+
break;
239+
#endif
240+
default:
241+
break;
242+
}
243+
}
244+
245+
void apm_hal_set_region_end_addr(apm_ctrl_module_t ctrl_mod, uint32_t regn_num, uint32_t addr)
246+
{
247+
switch (ctrl_mod) {
248+
case APM_CTRL_HP_APM:
249+
apm_ll_hp_apm_set_region_end_addr(regn_num, addr);
250+
break;
251+
#if SOC_APM_LP_APM0_SUPPORTED
252+
case APM_CTRL_LP_APM0:
253+
apm_ll_lp_apm0_set_region_end_addr(regn_num, addr);
254+
break;
255+
#endif
256+
case APM_CTRL_LP_APM:
257+
apm_ll_lp_apm_set_region_end_addr(regn_num, addr);
258+
break;
259+
#if SOC_APM_CPU_APM_SUPPORTED
260+
case APM_CTRL_CPU_APM:
261+
apm_ll_cpu_apm_set_region_end_addr(regn_num, addr);
262+
break;
263+
#endif
264+
default:
265+
break;
266+
}
267+
}
268+
269+
void apm_hal_set_sec_mode_region_attr(apm_ctrl_module_t ctrl_mod, uint32_t regn_num, apm_security_mode_t mode, uint32_t regn_pms)
270+
{
271+
switch (ctrl_mod) {
272+
case APM_CTRL_HP_APM:
273+
apm_ll_hp_apm_set_sec_mode_region_attr(regn_num, mode, regn_pms);
274+
break;
275+
#if SOC_APM_LP_APM0_SUPPORTED
276+
case APM_CTRL_LP_APM0:
277+
apm_ll_lp_apm0_set_sec_mode_region_attr(regn_num, mode, regn_pms);
278+
break;
279+
#endif
280+
case APM_CTRL_LP_APM:
281+
apm_ll_lp_apm_set_sec_mode_region_attr(regn_num, mode, regn_pms);
282+
break;
283+
#if SOC_APM_CPU_APM_SUPPORTED
284+
case APM_CTRL_CPU_APM:
285+
apm_ll_cpu_apm_set_sec_mode_region_attr(regn_num, mode, regn_pms);
286+
break;
287+
#endif
288+
default:
289+
break;
290+
}
291+
}
292+
221293
void apm_hal_set_region_filter_cfg(apm_ctrl_module_t ctrl_mod, apm_security_mode_t mode, const apm_hal_ctrl_region_cfg_t *regn_cfg)
222294
{
223295
HAL_ASSERT(regn_cfg);
296+
HAL_ASSERT(mode != APM_SEC_MODE_TEE);
224297

225298
switch (ctrl_mod) {
226299
case APM_CTRL_HP_APM:

components/hal/esp32c61/include/hal/apm_ll.h

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "soc/hp_apm_struct.h"
1515
#include "soc/lp_apm_reg.h"
1616
#include "soc/lp_apm_struct.h"
17+
#include "soc/cpu_apm_reg.h"
18+
#include "soc/cpu_apm_struct.h"
1719

1820
#include "soc/pcr_reg.h"
1921
#include "soc/interrupts.h"
@@ -448,6 +450,195 @@ static inline int apm_ll_lp_apm_get_ctrl_intr_src(apm_ctrl_access_path_t path)
448450
return ETS_LP_APM_M0_INTR_SOURCE;
449451
}
450452

453+
/**
454+
* @brief Enable/disable controller filter for specific path in CPU-APM
455+
*
456+
* @param path Access path
457+
* @param enable True to enable, false to disable
458+
*/
459+
static inline void apm_ll_cpu_apm_enable_ctrl_filter(apm_ctrl_access_path_t path, bool enable)
460+
{
461+
if (enable) {
462+
REG_SET_BIT(CPU_APM_FUNC_CTRL_REG, BIT(path));
463+
} else {
464+
REG_CLR_BIT(CPU_APM_FUNC_CTRL_REG, BIT(path));
465+
}
466+
}
467+
468+
/**
469+
* @brief Enable/disable all controller filters in CPU-APM
470+
*
471+
* @param enable True to enable, false to disable
472+
*/
473+
static inline void apm_ll_cpu_apm_enable_ctrl_filter_all(bool enable)
474+
{
475+
REG_WRITE(CPU_APM_FUNC_CTRL_REG, enable ? UINT32_MAX : 0);
476+
}
477+
478+
/**
479+
* @brief Enable/disable region filter in CPU-APM
480+
*
481+
* @param regn_num Region number
482+
* @param enable True to enable, false to disable
483+
*/
484+
static inline void apm_ll_cpu_apm_enable_region_filter(uint32_t regn_num, bool enable)
485+
{
486+
if (enable) {
487+
REG_SET_BIT(CPU_APM_REGION_FILTER_EN_REG, BIT(regn_num));
488+
} else {
489+
REG_CLR_BIT(CPU_APM_REGION_FILTER_EN_REG, BIT(regn_num));
490+
}
491+
}
492+
493+
/**
494+
* @brief Set region start address in CPU-APM
495+
*
496+
* @param regn_num Region number
497+
* @param addr Start address
498+
*/
499+
static inline void apm_ll_cpu_apm_set_region_start_addr(uint32_t regn_num, uint32_t addr)
500+
{
501+
REG_WRITE(CPU_APM_REGION0_ADDR_START_REG + APM_REGION_ADDR_OFFSET * regn_num, addr);
502+
}
503+
504+
/**
505+
* @brief Set region end address in CPU-APM
506+
*
507+
* @param regn_num Region number
508+
* @param addr End address
509+
*/
510+
static inline void apm_ll_cpu_apm_set_region_end_addr(uint32_t regn_num, uint32_t addr)
511+
{
512+
REG_WRITE(CPU_APM_REGION0_ADDR_END_REG + APM_REGION_ADDR_OFFSET * regn_num, addr);
513+
}
514+
515+
/**
516+
* @brief Set security mode region attributes in CPU-APM
517+
*
518+
* @param regn_num Region number
519+
* @param mode Security mode
520+
* @param regn_pms Region PMS attributes
521+
*/
522+
static inline void apm_ll_cpu_apm_set_sec_mode_region_attr(uint32_t regn_num, apm_security_mode_t mode, uint32_t regn_pms)
523+
{
524+
uint32_t reg = CPU_APM_REGION0_ATTR_REG + APM_REGION_ATTR_OFFSET * regn_num;
525+
uint32_t val = REG_READ(reg);
526+
val &= ~APM_REGION_PMS_MASK(mode);
527+
val |= APM_REGION_PMS_FIELD(mode, regn_pms);
528+
REG_WRITE(reg, val);
529+
}
530+
531+
/**
532+
* @brief Lock security mode region attributes in CPU-APM
533+
*
534+
* @param regn_num Region number
535+
*/
536+
static inline void apm_ll_cpu_apm_lock_sec_mode_region_attr(uint32_t regn_num)
537+
{
538+
REG_SET_BIT(CPU_APM_REGION0_ATTR_REG + APM_REGION_ATTR_OFFSET * regn_num, APM_REGION_LOCK_BIT);
539+
}
540+
541+
/**
542+
* @brief Get exception data (regn, master, security mode) from CPU-APM
543+
*
544+
* @param path Access path
545+
* @return Exception data
546+
*/
547+
static inline uint32_t apm_ll_cpu_apm_get_excp_data(apm_ctrl_access_path_t path)
548+
{
549+
return REG_READ(CPU_APM_M0_EXCEPTION_INFO0_REG + APM_EXCP_INFO_OFFSET * path);
550+
}
551+
552+
/**
553+
* @brief Get exception status from CPU-APM
554+
*
555+
* @param path Access path
556+
* @return Exception type
557+
*/
558+
static inline uint32_t apm_ll_cpu_apm_get_excp_type(apm_ctrl_access_path_t path)
559+
{
560+
return REG_READ(CPU_APM_M0_STATUS_REG + APM_EXCP_INFO_OFFSET * path);
561+
}
562+
563+
/**
564+
* @brief Get exception address from CPU-APM
565+
*
566+
* @param path Access path
567+
* @return Exception address
568+
*/
569+
static inline uint32_t apm_ll_cpu_apm_get_excp_addr(apm_ctrl_access_path_t path)
570+
{
571+
return REG_READ(CPU_APM_M0_EXCEPTION_INFO1_REG + APM_EXCP_INFO_OFFSET * path);
572+
}
573+
574+
/**
575+
* @brief Get exception information from CPU-APM
576+
*
577+
* @param path Access path
578+
* @param info Pointer to store exception information
579+
*/
580+
static inline void apm_ll_cpu_apm_get_excp_info(apm_ctrl_access_path_t path, apm_ctrl_exception_info_t *info)
581+
{
582+
cpu_apm_m0_exception_info0_reg_t reg;
583+
reg.val = apm_ll_cpu_apm_get_excp_data(path);
584+
info->regn = reg.apm_m0_exception_region;
585+
info->mode = reg.apm_m0_exception_mode;
586+
info->id = reg.apm_m0_exception_id;
587+
588+
info->type = apm_ll_cpu_apm_get_excp_type(path);
589+
info->addr = apm_ll_cpu_apm_get_excp_addr(path);
590+
}
591+
592+
/**
593+
* @brief Clear controller exception status in CPU-APM
594+
*
595+
* @param path Access path
596+
*/
597+
static inline void apm_ll_cpu_apm_clear_ctrl_excp_status(apm_ctrl_access_path_t path)
598+
{
599+
REG_SET_BIT(CPU_APM_M0_STATUS_CLR_REG + APM_EXCP_INFO_OFFSET * path, APM_EXCP_STATUS_CLR_BIT);
600+
}
601+
602+
/**
603+
* @brief Enable/disable controller interrupt in CPU-APM
604+
*
605+
* @param path Access path
606+
* @param enable True to enable, false to disable
607+
*/
608+
static inline void apm_ll_cpu_apm_enable_ctrl_intr(apm_ctrl_access_path_t path, bool enable)
609+
{
610+
if (enable) {
611+
REG_SET_BIT(CPU_APM_INT_EN_REG, BIT(path));
612+
} else {
613+
REG_CLR_BIT(CPU_APM_INT_EN_REG, BIT(path));
614+
}
615+
}
616+
617+
/**
618+
* @brief Enable/disable controller clock gating in CPU-APM
619+
*
620+
* @param enable True to enable, false to disable
621+
*/
622+
static inline void apm_ll_cpu_apm_enable_ctrl_clk_gating(bool enable)
623+
{
624+
if (enable) {
625+
REG_CLR_BIT(CPU_APM_CLOCK_GATE_REG, CPU_APM_CLK_EN);
626+
} else {
627+
REG_SET_BIT(CPU_APM_CLOCK_GATE_REG, CPU_APM_CLK_EN);
628+
}
629+
}
630+
631+
/**
632+
* @brief Get controller interrupt source number from CPU-APM
633+
*
634+
* @param path Access path
635+
* @return Interrupt source number
636+
*/
637+
static inline int apm_ll_cpu_apm_get_ctrl_intr_src(apm_ctrl_access_path_t path)
638+
{
639+
return ETS_CPU_APM_M0_INTR_SOURCE + path;
640+
}
641+
451642
/**
452643
* @brief Enable/disable APM reset event bypass
453644
*

components/hal/include/hal/apm_hal.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,33 @@ void apm_hal_enable_ctrl_filter_all(bool enable);
253253
*/
254254
void apm_hal_enable_region_filter(apm_ctrl_module_t ctrl_mod, uint32_t regn_num, bool enable);
255255

256+
/**
257+
* @brief Set the start address for the given region
258+
*
259+
* @param ctrl_mod APM controller module
260+
* @param regn_num Region number
261+
* @param addr Address
262+
*/
263+
void apm_hal_set_region_start_addr(apm_ctrl_module_t ctrl_mod, uint32_t regn_num, uint32_t addr);
264+
265+
/**
266+
* @brief Set the end address for the given region
267+
*
268+
* @param ctrl_mod APM controller module
269+
* @param regn_num Region number
270+
* @param addr Address
271+
*/
272+
void apm_hal_set_region_end_addr(apm_ctrl_module_t ctrl_mod, uint32_t regn_num, uint32_t addr);
273+
274+
/**
275+
* @brief Set the permissions for the specified security mode for the given region
276+
*
277+
* @param ctrl_mod APM controller module
278+
* @param regn_num Region number
279+
* @param mode Security mode
280+
*/
281+
void apm_hal_set_sec_mode_region_attr(apm_ctrl_module_t ctrl_mod, uint32_t regn_num, apm_security_mode_t mode, uint32_t regn_pms);
282+
256283
/**
257284
* @brief Set region filter configuration
258285
*

components/soc/esp32c61/include/soc/Kconfig.soc_caps.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,10 @@ config SOC_APM_CTRL_FILTER_SUPPORTED
999999
bool
10001000
default y
10011001

1002+
config SOC_APM_CPU_APM_SUPPORTED
1003+
bool
1004+
default y
1005+
10021006
config SOC_APM_SUPPORT_CTRL_CFG_LOCK
10031007
bool
10041008
default y

components/soc/esp32c61/include/soc/apm_defs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ extern "C" {
1515
/* Number of paths for each supported APM controller */
1616
#define APM_CTRL_HP_APM_PATH_NUM (4)
1717
#define APM_CTRL_LP_APM_PATH_NUM (1)
18+
#define APM_CTRL_CPU_APM_PATH_NUM (2)
1819
/* Number of regions for each supported APM controller */
1920
#define APM_CTRL_HP_APM_REGION_NUM (16)
2021
#define APM_CTRL_LP_APM_REGION_NUM (4)
22+
#define APM_CTRL_CPU_APM_REGION_NUM (8)
2123

2224
/* Register offset for TEE mode control */
2325
#define APM_TEE_MODE_CTRL_OFFSET (0x04)

components/soc/esp32c61/include/soc/soc_caps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@
412412

413413
/*-------------------------- APM CAPS ----------------------------------------*/
414414
#define SOC_APM_CTRL_FILTER_SUPPORTED 1 /*!< Support for APM control filter */
415+
#define SOC_APM_CPU_APM_SUPPORTED 1 /*!< Support for CPU APM control filter */
415416
#define SOC_APM_SUPPORT_CTRL_CFG_LOCK 1 /*!< Support for APM controller configuration lock */
416417

417418
/*------------------------ Anti DPA (Security) CAPS --------------------------*/

0 commit comments

Comments
 (0)