The HIP09 is a chip developed by HiSilicon, and we use it on the Kunpeng platform. Add a new device_dt driver that provides reusable helpers for HiSilicon platform devices to be discovered and initialized from the device tree. The device tree information for the d06 and HIP09 platform has been supplemented. Signed-off-by: zhaozheng <zhaozheng30@huawei.com> --- core/arch/arm/dts/HIP09.dts | 61 +++++++++++ core/arch/arm/dts/d06.dts | 44 ++++++++ core/arch/arm/plat-kunpeng/conf.mk | 58 ++++++++++ core/arch/arm/plat-kunpeng/core_pos_a64.S | 65 +++++++++++ core/arch/arm/plat-kunpeng/main.c | 23 ++++ core/arch/arm/plat-kunpeng/platform_config.h | 23 ++++ core/arch/arm/plat-kunpeng/sub.mk | 3 + core/drivers/device_dt.c | 109 +++++++++++++++++++ core/drivers/hisi_trng.c | 25 ++++- core/drivers/sub.mk | 1 + core/include/drivers/device_dt.h | 23 ++++ 11 files changed, 429 insertions(+), 6 deletions(-) create mode 100644 core/arch/arm/dts/HIP09.dts create mode 100644 core/arch/arm/dts/d06.dts create mode 100644 core/arch/arm/plat-kunpeng/conf.mk create mode 100644 core/arch/arm/plat-kunpeng/core_pos_a64.S create mode 100644 core/arch/arm/plat-kunpeng/main.c create mode 100644 core/arch/arm/plat-kunpeng/platform_config.h create mode 100644 core/arch/arm/plat-kunpeng/sub.mk create mode 100644 core/drivers/device_dt.c create mode 100644 core/include/drivers/device_dt.h diff --git a/core/arch/arm/dts/HIP09.dts b/core/arch/arm/dts/HIP09.dts new file mode 100644 index 000000000..072116a45 --- /dev/null +++ b/core/arch/arm/dts/HIP09.dts @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) +/* + * Copyright (C) Huawei Technologies Co., Ltd. 2023. All rights reserved. + */ + +/dts-v1/; + +/ { + compatible = "huawei,HIP09"; + #address-cells = <2>; + #size-cells = <2>; + + secure-memory { + compatible ="secure-memory"; + reg = <0x00000821 0x00000000 0x00000000 0x80000000>; + }; + + share-memory { + compatible ="share-memory"; + reg = <0x00000821 0x80000000 0x00000000 0x10000000>; + }; + + device-regions { + compatible = "arm,ffa-manifest-device-regions"; + + uart { + compatible = "uart"; + base-address = <0x000000c6 0x14080000>; + pages-count = <0x1>; /* page size is 4KB */ + }; + + hisi-sec1 { + compatible = "hisi-sec"; + base-address = <0x00000001 0x60000000>; + pages-count = <0x400>; /* page size is 4KB */ + }; + + hisi-sec2 { + compatible = "hisi-sec"; + base-address = <0x00000081 0x60000000>; + pages-count = <0x400>; /* page size is 4KB */ + }; + + hisi-hpre1 { + compatible = "hisi-hpre"; + base-address = <0x00000001 0x50000000>; + pages-count = <0x400>; /* page size is 4KB */ + }; + hisi-hpre2 { + compatible = "hisi-hpre"; + base-address = <0x00000081 0x50000000>; + pages-count = <0x400>; /* page size is 4KB */ + }; + + hisi-trng { + compatible = "hisi-trng"; + base-address = <0x00000001 0x000E0000>; + pages-count = <0x1>; /* page size is 4KB */ + }; + }; +}; \ No newline at end of file diff --git a/core/arch/arm/dts/d06.dts b/core/arch/arm/dts/d06.dts new file mode 100644 index 000000000..a82196022 --- /dev/null +++ b/core/arch/arm/dts/d06.dts @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) +/* + * Copyright (C) Huawei Technologies Co., Ltd. 2023. All rights reserved. + */ + +/dts-v1/; + +/ { + compatible = "huawei,d06"; + #address-cells = <2>; + #size-cells = <2>; + + secure-memory { + compatible ="secure-memory"; + reg = <0x00000020 0xC0000000 0x00000000 0x32000000>; + }; + + share-memory { + compatible ="share-memory"; + reg = <0x00000000 0x50000000 0x00000000 0x04000000>; + }; + + device-regions { + compatible = "arm,ffa-manifest-device-regions"; + + uart { + compatible = "uart"; + base-address = <0x00000002 0x01190000>; + pages-count = <0x1000>; /* page size is 4KB */ + }; + + hisi-sec { + compatible = "hisi-sec"; + base-address = <0x00000001 0x41800000>; + pages-count = <0x400>; /* page size is 4KB */ + }; + + hisi-trng { + compatible = "hisi-trng"; + base-address = <0x00000002 0x010C0000>; + pages-count = <0x1>; /* page size is 4KB */ + }; + }; +}; \ No newline at end of file diff --git a/core/arch/arm/plat-kunpeng/conf.mk b/core/arch/arm/plat-kunpeng/conf.mk new file mode 100644 index 000000000..8913309ec --- /dev/null +++ b/core/arch/arm/plat-kunpeng/conf.mk @@ -0,0 +1,58 @@ +# common config +include core/arch/arm/cpu/cortex-armv8-0.mk + +CFG_TEE_CORE_EMBED_INTERNAL_TESTS ?= y +CFG_TZDRAM_START ?= 0x2000000000 +PLATFORM_FLAVOR ?= HIP09 +CFG_WITH_SOFTWARE_PRNG ?= n +CFG_HISILICON_PLAT_ACC ?= y +CFG_TEE_CORE_LOG_LEVEL ?= 2 +CFG_CRYPTO_WITH_CE ?= y +CFG_WITH_PAGER ?= n +CFG_WITH_STATS ?= y +CFG_EMBED_DTB_SOURCE_FILE ?= $(PLATFORM_FLAVOR).dts +CFG_CRYPTO_PBKDF2 ?= y +CFG_CRYPTO_HW_PBKDF2 ?= y +CFG_HISILICON_CRYPTO_DRIVER ?= y + +# By default, the BIOS does not support parameter transfer through BTD. +CFG_BIOS_DTB ?= n + +$(call force,CFG_SECURE_TIME_SOURCE_CNTPCT,y) +$(call force,CFG_CORE_IRQ_IS_NATIVE_INTR,y) +$(call force,CFG_CORE_UNMAP_CORE_AT_EL0,n) +$(call force,CFG_LPAE_ADDR_SPACE_BITS,48) +$(call force,CFG_CORE_PHYS_RELOCATABLE,y) +$(call force,CFG_WITH_ARM_TRUSTED_FW,y) +$(call force,CFG_ATTESTATION_PTA,y) +$(call force,CFG_ARM64_core,y) +$(call force,CFG_DYN_CONFIG,y) +$(call force,CFG_WITH_LPAE,y) +$(call force,CFG_ARM_GICV3,y) +$(call force,CFG_EMBED_DTB,y) +$(call force,CFG_PL011,y) +$(call force,CFG_DT,y) +CFG_TEE_DYN_VASPACE_SIZE ?= (1024 * 1024 * 40) + +ifeq ($(PLATFORM_FLAVOR),HIP09) +CFG_CORE_ARM64_PA_BITS ?= 48 +CFG_TEE_CORE_NB_CORE ?= 128 +CFG_NUM_THREADS ?= 128 +ifneq (,$(filter $(CFG_TEE_CORE_NB_CORE),128 256 512)) +CFG_CPUS_PER_DIE ?= 32 +CFG_CPUS_PER_SOCKET ?= 128 +else ifneq (,$(filter $(CFG_TEE_CORE_NB_CORE),160 320 640)) +CFG_CPUS_PER_DIE ?= 40 +CFG_CPUS_PER_SOCKET ?= 160 +else ifneq (,$(filter $(CFG_TEE_CORE_NB_CORE),96)) +CFG_CPUS_PER_DIE ?= 24 +CFG_CPUS_PER_SOCKET ?= 96 +endif +CFG_HISILICON_ACC_V3 ?= y +CFG_CORE_HEAP_SIZE ?= 0x8000000 +CFG_TEE_RAM_VA_SIZE ?= 0xC000000 +CFG_TZDRAM_START ?= 0x82100000000 +CFG_TZDRAM_SIZE ?= 0x80000000 +CFG_SHMEM_START ?= 0x82180000000 +CFG_SHMEM_SIZE ?= 0x10000000 +endif # PLATFORM_FLAVOR=HIP09 diff --git a/core/arch/arm/plat-kunpeng/core_pos_a64.S b/core/arch/arm/plat-kunpeng/core_pos_a64.S new file mode 100644 index 000000000..d0e47f8c5 --- /dev/null +++ b/core/arch/arm/plat-kunpeng/core_pos_a64.S @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: BSD-2-Clause +/* + * Copyright (c) 2023, HiSilicon Technologies Co., Ltd. + * All rights reserved. + */ + +#include <asm.S> +#include <arm.h> +#include <arm64_macros.S> + +/* + * MPIDR Format + * [39:32] aff3, {Skt_id[7:3], die_id[2:0]} + * [30] U bit, tie 0 + * [24] MT bit, tie 1 + * [23:16] aff2, cluster_id + * [15:8] aff1, core_id + * [7:0] aff0, thread_id + * each socket has 2 dies: TB(die_id: 3) + TA(die_id: 1), + * each die has max 10 clusters, each cluster has 4 cpus, + * each cpu has 2 threads. + * HIP09: + * in one socket TB's cpu index < TA's cpu index + * 128 cores: socketid * 128 + (3 - die_id) * 32 + cluster_id * 8 + + * core_id * 2 + thread_id + * 160 cores: socketid * 160 + (3 - die_id) * 40 + cluster_id * 8 + + * core_id * 2 + thread_id + */ +FUNC get_core_pos_mpidr , : + /* init x2~x8 */ + mov x2, #0 + mov x3, #0 + mov x4, #0 + mov x5, #0 + mov x6, #0 + mov x7, #0 + mov x8, #0 + +#if defined(PLATFORM_FLAVOR_HIP09) + lsr x1, x0, #35 + and x2, x1, #0x1f + lsr x1, x0, #32 + and x3, x1, #0x7 + mov x4, #3 + sub x3, x4, x3 + lsr x1, x0, #16 + and x4, x1, #0xff + lsl x4, x4, #3 + lsr x1, x0, #8 + and x5, x1, #0xff + lsl x5, x5, #1 + and x6, x0, #0xff +#endif + mov x0, #0 + mov x7, #CFG_CPUS_PER_SOCKET + mov x8, #CFG_CPUS_PER_DIE + /* calculate cpu index */ + mul x3, x3, x8 + mul x0, x2, x7 + add x0, x0, x3 + add x0, x0, x4 + add x0, x0, x5 + add x0, x0, x6 + ret +END_FUNC get_core_pos_mpidr \ No newline at end of file diff --git a/core/arch/arm/plat-kunpeng/main.c b/core/arch/arm/plat-kunpeng/main.c new file mode 100644 index 000000000..4d4563617 --- /dev/null +++ b/core/arch/arm/plat-kunpeng/main.c @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: BSD-2-Clause +/* + * Copyright (c) 2023, HiSilicon Technologies Co., Ltd. + * All rights reserved. + */ + +#include <console.h> +#include <drivers/pl011.h> +#include <kernel/panic.h> +#include <mm/tee_pager.h> +#include <mm/core_memprot.h> +#include <platform_config.h> +#include <stdint.h> + +static struct pl011_data console_data __nex_bss; + +void plat_console_init(void) +{ + pl011_init(&console_data, UART_BASE, CONSOLE_UART_CLK_IN_HZ, + CONSOLE_BAUDRATE); + + register_serial_console(&console_data.chip); +} \ No newline at end of file diff --git a/core/arch/arm/plat-kunpeng/platform_config.h b/core/arch/arm/plat-kunpeng/platform_config.h new file mode 100644 index 000000000..24dec0c6e --- /dev/null +++ b/core/arch/arm/plat-kunpeng/platform_config.h @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: BSD-2-Clause +/* + * Copyright (c) 2023, HiSilicon Technologies Co., Ltd. + * All rights reserved. + */ + +#ifndef PLATFORM_CONFIG_H +#define PLATFORM_CONFIG_H + +#include <mm/generic_ram_layout.h> + +/* Make stacks aligned to data cache line length */ +#define STACK_ALIGNMENT 64 + +/* UART */ +#if defined(PLATFORM_FLAVOR_HIP09) +#define UART_BASE 0xc614080000 +#define UART_SIZE 0x1000 +#define CONSOLE_BAUDRATE 115200 +#define CONSOLE_UART_CLK_IN_HZ 0 +#endif + +#endif /* PLATFORM_CONFIG_H */ \ No newline at end of file diff --git a/core/arch/arm/plat-kunpeng/sub.mk b/core/arch/arm/plat-kunpeng/sub.mk new file mode 100644 index 000000000..8ca4ac0ea --- /dev/null +++ b/core/arch/arm/plat-kunpeng/sub.mk @@ -0,0 +1,3 @@ +global-incdirs-y += . +srcs-y += main.c +srcs-y += core_pos_a64.S \ No newline at end of file diff --git a/core/drivers/device_dt.c b/core/drivers/device_dt.c new file mode 100644 index 000000000..9ad4d77c4 --- /dev/null +++ b/core/drivers/device_dt.c @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 HiSilicon Limited. */ + +#include <drivers/device_dt.h> + +static void *get_fdt(void) +{ + void *fdt = NULL; + +#if defined(CFG_BIOS_DTB) + enum teecore_memtypes mtype; + mtype = core_mmu_get_type_by_pa((paddr_t)bios_dtb); + fdt = phys_to_virt((paddr_t)bios_dtb, mtype, + sizeof(BIOS_DTB_SIZE)); +#else + fdt = get_dt(); +#endif + + return fdt; +} + +TEE_Result set_device_info_by_node(uintptr_t *io_base, uint32_t *io_size, + void *fdt, int node) +{ + const fdt64_t *p64 = NULL; + const fdt32_t *p32 = NULL; + uint32_t pages_cnt = 0; + uintptr_t base = 0; + int len = 0; + + /* base-address: 64-bit property, big-endian in the blob */ + p64 = fdt_getprop(fdt, node, "base-address", &len); + if (!p64 || len != sizeof(*p64)) { + EMSG("Fail to get field base-address\n"); + return TEE_ERROR_BAD_FORMAT; + } + base = fdt64_ld(p64); + + /* pages-count: 32-bit property, big-endian in the blob */ + p32 = fdt_getprop(fdt, node, "pages-count", &len); + if (!p32 || len != sizeof(*p32)) { + EMSG("Fail to get field pages-count\n"); + return TEE_ERROR_BAD_FORMAT; + } + pages_cnt = fdt32_ld(p32); + + *io_base = base; + *io_size = pages_cnt * SMALL_PAGE_SIZE; + + return TEE_SUCCESS; +} + +TEE_Result set_device_info_by_dt(uintptr_t *io_base, uint32_t *io_size, + const char *dt_dev_match) +{ + void *fdt = NULL; + int node = 0; + + fdt = get_fdt(); + if (!fdt) { + EMSG("Fail to get valid dtb"); + return TEE_ERROR_NOT_SUPPORTED; + } + + node = fdt_node_offset_by_compatible(fdt, -1, dt_dev_match); + if (node < 0) { + EMSG("Fail to find %s compatible node", dt_dev_match); + return TEE_ERROR_NOT_SUPPORTED; + } + + return set_device_info_by_node(io_base, io_size, fdt, node); +} + +TEE_Result device_init_by_dt(TEE_Result (*device_probe)(void *fdt, int node), + const char *dt_dev_match) +{ + void *fdt = NULL; + int parent = 0; + int node = 0; + int cnt = 0; + + fdt = get_fdt(); + if (!fdt) { + EMSG("Fail to get valid dtb"); + return TEE_ERROR_NOT_SUPPORTED; + } + + parent = fdt_node_offset_by_compatible(fdt, -1, + "arm,ffa-manifest-device-regions"); + if (parent < 0) { + EMSG("Fail to find device-regions compatible node"); + return TEE_ERROR_BAD_FORMAT; + } + + fdt_for_each_subnode(node, fdt, parent) { + if (fdt_node_check_compatible(fdt, node, dt_dev_match)) + continue; + + if (device_probe(fdt, node) != TEE_SUCCESS) { + EMSG("Fail to init device %s", + fdt_get_name(fdt, node, NULL)); + continue; + } + + cnt++; + } + + return (cnt ? TEE_SUCCESS : TEE_ERROR_NOT_IMPLEMENTED); +} diff --git a/core/drivers/hisi_trng.c b/core/drivers/hisi_trng.c index 59c066f1e..a77dd1676 100644 --- a/core/drivers/hisi_trng.c +++ b/core/drivers/hisi_trng.c @@ -8,6 +8,8 @@ #include <mm/core_memprot.h> #include <platform_config.h> #include <rng_support.h> +#include <drivers/device_dt.h> +#include <kernel/delay.h> #include <string.h> #define HTRNG_RANDATA_REG 0xF0 @@ -17,7 +19,8 @@ #define POLL_TIMEOUT 1000 struct hisi_trng { - vaddr_t base; + uintptr_t base; + uint32_t size; }; static unsigned int trng_lock = SPINLOCK_UNLOCK; @@ -71,15 +74,25 @@ TEE_Result hw_get_random_bytes(void *buf, size_t len) static TEE_Result trng_init(void) { - DMSG("TRNG driver init start"); - trng_dev = calloc(1, sizeof(struct hisi_trng)); + const char *dt_dev_match = "hisi-trng"; + TEE_Result ret = TEE_ERROR_GENERIC; + + IMSG("TRNG driver init start"); + trng_dev = (struct hisi_trng *)calloc(1, sizeof(struct hisi_trng)); if (!trng_dev) { EMSG("Fail to calloc trng device"); return TEE_ERROR_OUT_OF_MEMORY; } - trng_dev->base = (vaddr_t)phys_to_virt_io(HISI_TRNG_BASE, - HISI_TRNG_SIZE); + ret = set_device_info_by_dt(&trng_dev->base, &trng_dev->size, dt_dev_match); + if (ret) { + free(trng_dev); + trng_dev = NULL; + return ret; + } + + trng_dev->base = (vaddr_t)phys_to_virt_io(trng_dev->base, + trng_dev->size); if (!trng_dev->base) { EMSG("Fail to get trng io_base"); free(trng_dev); @@ -87,7 +100,7 @@ static TEE_Result trng_init(void) return TEE_ERROR_ACCESS_DENIED; } - DMSG("TRNG driver init done"); + IMSG("TRNG driver init done"); return TEE_SUCCESS; } diff --git a/core/drivers/sub.mk b/core/drivers/sub.mk index de6bb20d4..2277721c1 100644 --- a/core/drivers/sub.mk +++ b/core/drivers/sub.mk @@ -97,6 +97,7 @@ srcs-$(CFG_RISCV_IMSIC) += imsic.c srcs-$(CFG_RISCV_ZKR_RNG) += riscv_zkr_rng.c srcs-$(CFG_ROCKCHIP_OTP) += rockchip_otp.c srcs-$(CFG_SIFIVE_UART) += sifive_uart.c +srcs-$(CFG_HISILICON_CRYPTO_DRIVER) += device_dt.c srcs-$(CFG_HISILICON_CRYPTO_DRIVER) += hisi_trng.c srcs-$(CFG_WIDEVINE_HUK) += widevine_huk.c srcs-$(CFG_SEMIHOSTING_CONSOLE) += semihosting_console.c diff --git a/core/include/drivers/device_dt.h b/core/include/drivers/device_dt.h new file mode 100644 index 000000000..9d8b1d765 --- /dev/null +++ b/core/include/drivers/device_dt.h @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (c) 2023-2026, HiSilicon Technologies Co., Ltd. + */ + +#ifndef __HISI_DT_H__ +#define __HISI_DT_H__ + +#include <stdint.h> +#include <kernel/boot.h> +#include <kernel/dt.h> +#include <libfdt.h> +#include <io.h> +#include <mm/core_memprot.h> + +TEE_Result set_device_info_by_dt(uintptr_t *io_base, uint32_t *io_size, + const char *dt_dev_match); +TEE_Result set_device_info_by_node(uintptr_t *io_base, uint32_t *io_size, + void *fdt, int node); +TEE_Result device_init_by_dt(TEE_Result (*device_probe)(void *fdt, int node), + const char *dt_dev_match); + +#endif /* __HISI_DT_H__ */ -- 2.33.0