alif/se_services: Add SE services interface.

Includes services to get random numbers, reset SoC, get unique-id, dump SoC
info, and CPU control services.

Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
This commit is contained in:
Damien George
2024-01-26 12:00:18 +11:00
parent 53b7c14836
commit 4e62ade442
5 changed files with 309 additions and 0 deletions

View File

@@ -42,6 +42,7 @@ INC += -I$(BUILD)
INC += -I$(BOARD_DIR)
INC += -I$(CMSIS_DIR)
INC += -I$(ALIF_DFP_REL_HERE)/drivers/include/
INC += -I$(ALIF_DFP_REL_HERE)/se_services/include/
INC += -I$(ALIF_DFP_REL_HERE)/ospi_xip/source/ospi
INC += -I$(ALIF_DFP_REL_HERE)/Device/common/config/
INC += -I$(ALIF_DFP_REL_HERE)/Device/common/include/
@@ -120,6 +121,7 @@ SRC_C = \
ospi_flash.c \
pendsv.c \
system_tick.c \
se_services.c \
usbd.c \
$(wildcard $(BOARD_DIR)/*.c)
@@ -174,13 +176,23 @@ ALIF_SRC_C += $(addprefix $(ALIF_DFP_REL_TOP)/,\
Device/common/source/system_M55.c \
Device/common/source/system_utils.c \
Device/core/$(MCU_CORE)/source/startup_$(MCU_CORE).c \
drivers/source/mhu_driver.c \
drivers/source/mhu_receiver.c \
drivers/source/mhu_sender.c \
drivers/source/pinconf.c \
drivers/source/uart.c \
drivers/source/utimer.c \
ospi_xip/source/ospi/ospi_drv.c \
se_services/source/services_host_application.c \
se_services/source/services_host_boot.c \
se_services/source/services_host_clocks.c \
se_services/source/services_host_cryptocell.c \
se_services/source/services_host_handler.c \
se_services/source/services_host_system.c \
)
$(BUILD)/tinyusb_port/tusb_alif_dcd.o: CFLAGS += -Wno-unused-variable -DTUSB_ALIF_NO_IRQ_CFG=1
$(BUILD)/$(ALIF_DFP_REL_TOP)/se_services/source/services_host_boot.o: CFLAGS += -Wno-stringop-truncation
# List of sources for qstr extraction
SRC_QSTR += $(SRC_C) $(SHARED_SRC_C) $(GEN_PINS_SRC)

View File

@@ -40,6 +40,7 @@
#define NVIC_PRIORITYGROUP_7 ((uint32_t)0x00000000U)
#define IRQ_PRI_SYSTEM_TICK NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 0, 0)
#define IRQ_PRI_MHU NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 0, 0)
#define IRQ_PRI_QUIET_TIMING NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 1, 0)
#define IRQ_PRI_UART_REPL NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 1, 0)
#define IRQ_PRI_USB NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 5, 0)

View File

@@ -39,6 +39,7 @@
#include "mpuart.h"
#include "ospi_flash.h"
#include "pendsv.h"
#include "se_services.h"
#include "system_tick.h"
extern uint8_t __StackTop, __StackLimit;
@@ -60,6 +61,7 @@ void _start(void) {
MICROPY_BOARD_STARTUP();
pendsv_init();
se_services_init();
MICROPY_BOARD_EARLY_INIT();

247
ports/alif/se_services.c Normal file
View File

@@ -0,0 +1,247 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2024 OpenMV LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include "irq.h"
#include "se_services.h"
#include "mhu.h"
#include "services_lib_bare_metal.h"
#include "services_lib_protocol.h"
#include "py/mphal.h"
// MHU indices.
#define MHU_M55_SE_MHU0 0
#define MAX_MHU 1
// The following timeout is implemented in se_services_handle.c as a
// simple loop busy polling on a variable set from an IRQ.
#define TIMEOUT 10000000
typedef struct {
volatile unsigned int RST_CTRL; // 0x1A010318
volatile unsigned int RST_STAT; // 0x1A01031C
} CPU_Type;
// HE CPU register flags
#define RST_CTRL_CPUWAIT_MASK (1 << 0)
#define RST_CTRL_RST_REQ_MASK (1 << 1)
#define RST_STAT_RST_ACK_MASK (3 << 1)
#define HE_CPU ((CPU_Type *)0x1A010318)
static const uint32_t mhu_sender_base_address_list[MAX_MHU] = {
MHU_SESS_S_TX_BASE,
};
static const uint32_t mhu_receiver_base_address_list[MAX_MHU] = {
MHU_SESS_S_RX_BASE,
};
// Must be aligned as a uint32_t.
static uint32_t packet_buffer[SERVICES_MAX_PACKET_BUFFER_SIZE / sizeof(uint32_t)];
static mhu_driver_out_t mhu_driver_out;
static uint32_t se_services_handle;
void MHU_SESS_S_TX_IRQHandler(void) {
mhu_driver_out.sender_irq_handler(MHU_M55_SE_MHU0);
}
void MHU_SESS_S_RX_IRQHandler(void) {
mhu_driver_out.receiver_irq_handler(MHU_M55_SE_MHU0);
}
int dummy_printf(const char *fmt, ...) {
(void)fmt;
return 0;
}
void se_services_init(void) {
// Initialize MHU.
mhu_driver_in_t mhu_driver_in;
mhu_driver_in.sender_base_address_list = (uint32_t *)mhu_sender_base_address_list;
mhu_driver_in.receiver_base_address_list = (uint32_t *)mhu_receiver_base_address_list;
mhu_driver_in.mhu_count = MAX_MHU;
mhu_driver_in.send_msg_acked_callback = SERVICES_send_msg_acked_callback;
mhu_driver_in.rx_msg_callback = SERVICES_rx_msg_callback;
mhu_driver_in.debug_print = NULL; // not currently used by MHU_driver_initialize
MHU_driver_initialize(&mhu_driver_in, &mhu_driver_out);
// Initialize SE services.
services_lib_t services_init_params = {
.packet_buffer_address = (uint32_t)packet_buffer,
.fn_send_mhu_message = mhu_driver_out.send_message,
.fn_wait_ms = NULL, // not currently used by services_host_handler.c
.wait_timeout = TIMEOUT,
.fn_print_msg = dummy_printf,
};
SERVICES_initialize(&services_init_params);
// Create SE services channel for sending requests.
se_services_handle = SERVICES_register_channel(MHU_M55_SE_MHU0, 0);
// Enable MHU interrupts.
NVIC_ClearPendingIRQ(MHU_SESS_S_RX_IRQ_IRQn);
NVIC_SetPriority(MHU_SESS_S_RX_IRQ_IRQn, IRQ_PRI_MHU);
NVIC_EnableIRQ(MHU_SESS_S_RX_IRQ_IRQn);
NVIC_ClearPendingIRQ(MHU_SESS_S_TX_IRQ_IRQn);
NVIC_SetPriority(MHU_SESS_S_TX_IRQ_IRQn, IRQ_PRI_MHU);
NVIC_EnableIRQ(MHU_SESS_S_TX_IRQ_IRQn);
// Send heartbeat services requests until one succeeds.
SERVICES_synchronize_with_se(se_services_handle);
}
void se_services_dump_device_data(void) {
uint32_t error_code;
uint8_t revision[80];
SERVICES_get_se_revision(se_services_handle, revision, &error_code);
SERVICES_version_data_t data;
SERVICES_system_get_device_data(se_services_handle, &data, &error_code);
printf("SE revision: %s\n", revision);
printf("ALIF_PN: %s\n", data.ALIF_PN);
printf("Raw device data:\n");
for (int i = 0; i < sizeof(data); ++i) {
printf(" %02x", ((uint8_t *)&data)[i]);
if (i % 16 == 15) {
printf("\n");
}
}
printf("\n");
}
void se_services_get_unique_id(uint8_t id[5]) {
uint32_t error_code;
SERVICES_version_data_t data;
SERVICES_system_get_device_data(se_services_handle, &data, &error_code);
// The MfgData has 5 bytes of valid data, at least on REV_B2.
memcpy(id, data.MfgData, 5);
}
__attribute__((noreturn)) void se_services_reset_soc(void) {
SERVICES_boot_reset_soc(se_services_handle);
NVIC_SystemReset();
}
uint64_t se_services_rand64(void) {
// If the SE core is not ready then the return value can be
// SERVICES_REQ_NOT_ACKNOWLEDGE. So retry a few times.
for (int retry = 0; retry < 100; ++retry) {
uint64_t value;
int32_t error_code;
uint32_t ret = SERVICES_cryptocell_get_rnd(se_services_handle, sizeof(uint64_t), &value, &error_code);
if (ret == SERVICES_REQ_SUCCESS) {
return value;
}
}
// No random number available.
return 0;
}
uint32_t se_services_enable_clock(clock_enable_t clock, bool enable) {
uint32_t error_code;
SERVICES_clocks_enable_clock(se_services_handle, clock, enable, &error_code);
return error_code;
}
uint32_t se_services_select_pll_source(pll_source_t source, pll_target_t target) {
uint32_t error_code;
SERVICES_clocks_select_pll_source(se_services_handle, source, target, &error_code);
return error_code;
}
uint32_t se_services_get_run_profile(run_profile_t *profile) {
uint32_t error_code;
SERVICES_get_run_cfg(se_services_handle, profile, &error_code);
return error_code;
}
uint32_t se_services_set_run_profile(run_profile_t *profile) {
uint32_t error_code;
SERVICES_set_run_cfg(se_services_handle, profile, &error_code);
return error_code;
}
uint32_t se_services_get_off_profile(off_profile_t *profile) {
uint32_t error_code;
SERVICES_get_off_cfg(se_services_handle, profile, &error_code);
return error_code;
}
uint32_t se_services_set_off_profile(off_profile_t *profile) {
uint32_t error_code;
SERVICES_set_off_cfg(se_services_handle, profile, &error_code);
return error_code;
}
uint32_t se_services_boot_process_toc_entry(const uint8_t *image_id) {
uint32_t error_code;
SERVICES_boot_process_toc_entry(se_services_handle, image_id, &error_code);
return error_code;
}
uint32_t se_services_boot_cpu(uint32_t cpu_id, uint32_t address) {
uint32_t error_code;
SERVICES_boot_cpu(se_services_handle, cpu_id, address, &error_code);
return error_code;
}
uint32_t se_services_boot_reset_cpu(uint32_t cpu_id) {
uint32_t error_code;
if (HE_CPU->RST_CTRL & RST_CTRL_CPUWAIT_MASK) {
// CPU held in reset
return SERVICES_REQ_SUCCESS;
}
for (mp_uint_t start = mp_hal_ticks_ms(); ; mp_hal_delay_ms(1)) {
uint32_t ret = SERVICES_boot_reset_cpu(se_services_handle, cpu_id, &error_code);
if (ret != SERVICES_REQ_SUCCESS) {
return error_code;
}
if ((HE_CPU->RST_STAT & RST_STAT_RST_ACK_MASK) == 0x4) {
return SERVICES_REQ_SUCCESS;
}
if ((mp_hal_ticks_ms() - start) >= 100) {
return SERVICES_REQ_TIMEOUT;
}
}
return error_code;
}
uint32_t se_services_boot_release_cpu(uint32_t cpu_id) {
uint32_t error_code;
SERVICES_boot_release_cpu(se_services_handle, cpu_id, &error_code);
return error_code;
}

47
ports/alif/se_services.h Normal file
View File

@@ -0,0 +1,47 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2024 OpenMV LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ALIF_SE_SERVICES_H
#define MICROPY_INCLUDED_ALIF_SE_SERVICES_H
#include "services_lib_api.h"
void se_services_init(void);
void se_services_dump_device_data(void);
void se_services_get_unique_id(uint8_t id[5]);
__attribute__((noreturn)) void se_services_reset_soc(void);
uint64_t se_services_rand64(void);
uint32_t se_services_enable_clock(clock_enable_t clock, bool enable);
uint32_t se_services_select_pll_source(pll_source_t source, pll_target_t target);
uint32_t se_services_get_run_profile(run_profile_t *profile);
uint32_t se_services_set_run_profile(run_profile_t *profile);
uint32_t se_services_get_off_profile(off_profile_t *profile);
uint32_t se_services_set_off_profile(off_profile_t *profile);
uint32_t se_services_boot_process_toc_entry(const uint8_t *image_id);
uint32_t se_services_boot_cpu(uint32_t cpu_id, uint32_t address);
uint32_t se_services_boot_reset_cpu(uint32_t cpu_id);
uint32_t se_services_boot_release_cpu(uint32_t cpu_id);
#endif // MICROPY_INCLUDED_ALIF_SE_SERVICES_H