From 97e8e9ce129eef09851ed9f87b72d38b730373a4 Mon Sep 17 00:00:00 2001 From: Hou Zhiqiang Date: Wed, 24 Jan 2024 14:51:43 +0800 Subject: [PATCH 1/5] arm64: linker: create region and section from zephyr,memory-region In arm64 linker script, create a memory region and section for each device tree node with compatible string "zephyr,memory-region". Signed-off-by: Hou Zhiqiang --- include/zephyr/arch/arm64/scripts/linker.ld | 4 ++++ tests/drivers/syscon/linker_arm64_reserved.ld | 11 ----------- .../lib/shared_multi_heap/linker_arm64_shared_pool.ld | 11 ----------- 3 files changed, 4 insertions(+), 22 deletions(-) diff --git a/include/zephyr/arch/arm64/scripts/linker.ld b/include/zephyr/arch/arm64/scripts/linker.ld index 5a8e1404a98a..c24e38606eaa 100644 --- a/include/zephyr/arch/arm64/scripts/linker.ld +++ b/include/zephyr/arch/arm64/scripts/linker.ld @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -66,6 +67,7 @@ MEMORY { FLASH (rx) : ORIGIN = ROM_ADDR, LENGTH = ROM_SIZE RAM (wx) : ORIGIN = RAM_ADDR, LENGTH = RAM_SIZE + LINKER_DT_REGIONS() /* Used by and documented in include/linker/intlist.ld */ IDT_LIST (wx) : ORIGIN = 0xFFFF8000, LENGTH = 32K } @@ -329,6 +331,8 @@ SECTIONS /DISCARD/ : { *(.note.GNU-stack) } + /* Sections generated from 'zephyr,memory-region' nodes */ + LINKER_DT_SECTIONS() /* Must be last in romable region */ SECTION_PROLOGUE(.last_section,,) diff --git a/tests/drivers/syscon/linker_arm64_reserved.ld b/tests/drivers/syscon/linker_arm64_reserved.ld index dc373b11ff15..2e250bca2883 100644 --- a/tests/drivers/syscon/linker_arm64_reserved.ld +++ b/tests/drivers/syscon/linker_arm64_reserved.ld @@ -5,19 +5,8 @@ #include #include -#include #include #include -MEMORY -{ - LINKER_DT_REGIONS() -} - -SECTIONS -{ - LINKER_DT_SECTIONS() -} - #include diff --git a/tests/lib/shared_multi_heap/linker_arm64_shared_pool.ld b/tests/lib/shared_multi_heap/linker_arm64_shared_pool.ld index 3970daae911d..2831f1a953c2 100644 --- a/tests/lib/shared_multi_heap/linker_arm64_shared_pool.ld +++ b/tests/lib/shared_multi_heap/linker_arm64_shared_pool.ld @@ -6,19 +6,8 @@ #include #include -#include #include #include -MEMORY -{ - LINKER_DT_REGIONS() -} - -SECTIONS -{ - LINKER_DT_SECTIONS() -} - #include From 937193b941e0ffe638e87b23bd2aa3a76f46d289 Mon Sep 17 00:00:00 2001 From: Hou Zhiqiang Date: Mon, 29 Jan 2024 00:57:37 +0800 Subject: [PATCH 2/5] soc: imx9: a55: create region and section from the zephyr,memory-region It has been added in the arm64 common linker script, so also update the one of mimx9 SoCs. Signed-off-by: Hou Zhiqiang --- soc/nxp/imx/imx9/a55/linker.ld | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/soc/nxp/imx/imx9/a55/linker.ld b/soc/nxp/imx/imx9/a55/linker.ld index 102d9f1b0445..4fa6e0b075c7 100644 --- a/soc/nxp/imx/imx9/a55/linker.ld +++ b/soc/nxp/imx/imx9/a55/linker.ld @@ -1,5 +1,5 @@ /* - * Copyright 2023 NXP + * Copyright 2023-2024 NXP * Copyright (c) 2013-2014 Wind River Systems, Inc. * * SPDX-License-Identifier: Apache-2.0 @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -60,6 +61,7 @@ MEMORY { FLASH (rx) : ORIGIN = ROM_ADDR, LENGTH = ROM_SIZE RAM (wx) : ORIGIN = RAM_ADDR, LENGTH = RAM_SIZE + LINKER_DT_REGIONS() /* Used by and documented in include/linker/intlist.ld */ IDT_LIST (wx) : ORIGIN = 0xFFFFF7FF, LENGTH = 2K } @@ -336,6 +338,8 @@ SECTIONS /DISCARD/ : { *(.note.GNU-stack) } + /* Sections generated from 'zephyr,memory-region' nodes */ + LINKER_DT_SECTIONS() /* Must be last in romable region */ SECTION_PROLOGUE(.last_section,,) From 543e828355f2e51f9d6eb2f7b8d121475468a096 Mon Sep 17 00:00:00 2001 From: Hou Zhiqiang Date: Tue, 19 Mar 2024 15:28:25 +0800 Subject: [PATCH 3/5] driver: ram_console: leave one byte from the defined buffer size Leave one byte from the CONFIG_RAM_CONSOLE_BUFFER_SIZE to ensure the NULL-termination. Signed-off-by: Hou Zhiqiang --- drivers/console/Kconfig | 6 ++++-- drivers/console/ram_console.c | 6 +++--- lib/open-amp/resource_table.c | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/drivers/console/Kconfig b/drivers/console/Kconfig index 28dcf9fbc16b..a658744a025a 100644 --- a/drivers/console/Kconfig +++ b/drivers/console/Kconfig @@ -109,8 +109,10 @@ config RAM_CONSOLE_BUFFER_SIZE default 1024 depends on RAM_CONSOLE help - Size of the RAM console buffer. Messages will wrap around if the - length is exceeded. + Total size of the RAM console buffer, to ensure it's always + NULL-terminated leave one byte unused, the actual length is + one byte less. Messages will wrap around if the actual length + is exceeded. config RTT_CONSOLE bool "Use RTT console" diff --git a/drivers/console/ram_console.c b/drivers/console/ram_console.c index 4ace56f96304..6d8031ed476d 100644 --- a/drivers/console/ram_console.c +++ b/drivers/console/ram_console.c @@ -15,14 +15,14 @@ extern void __printk_hook_install(int (*fn)(int)); extern void __stdout_hook_install(int (*fn)(int)); -/* Extra byte to ensure we're always NULL-terminated */ -char ram_console[CONFIG_RAM_CONSOLE_BUFFER_SIZE + 1]; +char ram_console[CONFIG_RAM_CONSOLE_BUFFER_SIZE]; static int pos; static int ram_console_out(int character) { ram_console[pos] = (char)character; - pos = (pos + 1) % CONFIG_RAM_CONSOLE_BUFFER_SIZE; + /* Leave one byte to ensure we're always NULL-terminated */ + pos = (pos + 1) % (CONFIG_RAM_CONSOLE_BUFFER_SIZE - 1); return character; } diff --git a/lib/open-amp/resource_table.c b/lib/open-amp/resource_table.c index 2d58beb4b928..63339f23dec4 100644 --- a/lib/open-amp/resource_table.c +++ b/lib/open-amp/resource_table.c @@ -66,7 +66,7 @@ static struct fw_resource_table __resource resource_table = { #if defined(CONFIG_RAM_CONSOLE) .cm_trace = { RSC_TRACE, - (uint32_t)ram_console, CONFIG_RAM_CONSOLE_BUFFER_SIZE + 1, 0, + (uint32_t)ram_console, CONFIG_RAM_CONSOLE_BUFFER_SIZE, 0, "Zephyr_log", }, #endif From 3be610bd42f8b6b3cd5a62a0004b706275bf569c Mon Sep 17 00:00:00 2001 From: Hou Zhiqiang Date: Wed, 24 Jan 2024 15:18:25 +0800 Subject: [PATCH 4/5] drivers: ram_console: add option to link console buffer to given section Currently, the ram_console buffer is defined as a global var, its address is determined during the building and may be changed when code update. This is not a problem if the ram_console is just used in debug purpose. While in the heterogeneous SoCs, there can be multiple MPU Cores and several MCU cores, it can run multiple OS/bare-metal instances on these cores, but the UART ports may be not enough, so the ram_console can be leveraged. To make it easy to use, it's better make the console buffer fixed and predefined. This patch adds a option to link the console buffer to a given section, through the "zephyr,memory-region" device tree node, then the address can be known from the device tree node and easy to check from other cores running Linux/U-Boot. To use this option, the chosen property 'zephyr,ram-console' must be added, the following is a example: chosen { zephyr,ram-console = &ram_console; }; ram_console: memory@93d00000 { compatible = "zephyr,memory-region"; reg = <0x93d00000 DT_SIZE_K(4)>; zephyr,memory-region = "RAM_CONSOLE"; }; Signed-off-by: Hou Zhiqiang --- drivers/console/Kconfig | 14 ++++++++++++++ drivers/console/ram_console.c | 27 ++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/drivers/console/Kconfig b/drivers/console/Kconfig index a658744a025a..c7bfe5c6639f 100644 --- a/drivers/console/Kconfig +++ b/drivers/console/Kconfig @@ -96,16 +96,30 @@ config JAILHOUSE_DEBUG_CONSOLE Useful in board bring-up if there aren't any working serial drivers. +# Workaround for not being able to have commas in macro arguments +DT_CHOSEN_Z_RAM_CONSOLE := zephyr,ram-console + config RAM_CONSOLE bool "Use RAM console" select CONSOLE_HAS_DRIVER + select RAM_CONSOLE_BUFFER_SECTION if $(dt_chosen_enabled,$(DT_CHOSEN_Z_RAM_CONSOLE)) help Emit console messages to a RAM buffer "ram_console" which can be examined at runtime with a debugger. Useful in board bring-up if there aren't any working serial drivers. +config RAM_CONSOLE_BUFFER_SECTION + bool "Use dedicated section as RAM console buffer" + depends on RAM_CONSOLE && $(dt_chosen_enabled,$(DT_CHOSEN_Z_RAM_CONSOLE)) + select KERNEL_DIRECT_MAP if MMU + help + Use a dedicated section as the RAM console buffer, whose address is + known before build so that the console output messages can be easily + examined by a debugger or software tool from a parallel-running OS. + config RAM_CONSOLE_BUFFER_SIZE int "Ram Console buffer size" + default $(dt_chosen_reg_size_int,$(DT_CHOSEN_Z_RAM_CONSOLE)) if RAM_CONSOLE_BUFFER_SECTION default 1024 depends on RAM_CONSOLE help diff --git a/drivers/console/ram_console.c b/drivers/console/ram_console.c index 6d8031ed476d..fdb71b8bda0a 100644 --- a/drivers/console/ram_console.c +++ b/drivers/console/ram_console.c @@ -2,6 +2,7 @@ /* * Copyright (c) 2015 Intel Corporation + * Copyright 2024 NXP * * SPDX-License-Identifier: Apache-2.0 */ @@ -11,11 +12,26 @@ #include #include #include +#include + +#ifdef CONFIG_RAM_CONSOLE_BUFFER_SECTION +#if !DT_HAS_CHOSEN(zephyr_ram_console) +#error "Lack of chosen property zephyr,ram_console!" +#elif (CONFIG_RAM_CONSOLE_BUFFER_SIZE > DT_REG_SIZE(DT_CHOSEN(zephyr_ram_console))) +#error "Custom RAM console buffer exceeds the section size!" +#endif + +#define RAM_CONSOLE_BUF_ATTR \ + __attribute__((__section__(LINKER_DT_NODE_REGION_NAME(DT_CHOSEN(zephyr_ram_console))))) +#else +#define RAM_CONSOLE_BUF_ATTR +#endif extern void __printk_hook_install(int (*fn)(int)); extern void __stdout_hook_install(int (*fn)(int)); -char ram_console[CONFIG_RAM_CONSOLE_BUFFER_SIZE]; +char ram_console_buf[CONFIG_RAM_CONSOLE_BUFFER_SIZE] RAM_CONSOLE_BUF_ATTR; +char *ram_console; static int pos; static int ram_console_out(int character) @@ -28,6 +44,15 @@ static int ram_console_out(int character) static int ram_console_init(void) { +#ifdef CONFIG_RAM_CONSOLE_BUFFER_SECTION + mm_reg_t ram_console_va; + + device_map((mm_reg_t *)&ram_console_va, DT_REG_ADDR(DT_CHOSEN(zephyr_ram_console)), + CONFIG_RAM_CONSOLE_BUFFER_SIZE, K_MEM_CACHE_NONE | K_MEM_DIRECT_MAP); + ram_console = (char *)ram_console_va, +#else + ram_console = ram_console_buf, +#endif __printk_hook_install(ram_console_out); __stdout_hook_install(ram_console_out); From 47d631acc5865800bf48c18c25b14d638feed3bb Mon Sep 17 00:00:00 2001 From: Hou Zhiqiang Date: Tue, 19 Mar 2024 13:07:08 +0800 Subject: [PATCH 5/5] snippets: add ram-console snippet support The ram-console snippet disabled UART console and enabled the RAM console with the option link the RAM console buffer to a dedicate section in a new added memory-region. Signed-off-by: Hou Zhiqiang --- snippets/ram-console/README.rst | 37 +++++++++++++++++++ .../boards/imx8mm_evk_mimx8mm6_a53.overlay | 17 +++++++++ .../boards/imx8mn_evk_mimx8mn6_a53.overlay | 17 +++++++++ .../boards/imx8mp_evk_mimx8ml8_a53.overlay | 17 +++++++++ .../boards/imx93_evk_mimx9352_a55.overlay | 17 +++++++++ snippets/ram-console/ram-console.conf | 9 +++++ snippets/ram-console/snippet.yml | 32 ++++++++++++++++ 7 files changed, 146 insertions(+) create mode 100644 snippets/ram-console/README.rst create mode 100644 snippets/ram-console/boards/imx8mm_evk_mimx8mm6_a53.overlay create mode 100644 snippets/ram-console/boards/imx8mn_evk_mimx8mn6_a53.overlay create mode 100644 snippets/ram-console/boards/imx8mp_evk_mimx8ml8_a53.overlay create mode 100644 snippets/ram-console/boards/imx93_evk_mimx9352_a55.overlay create mode 100644 snippets/ram-console/ram-console.conf create mode 100644 snippets/ram-console/snippet.yml diff --git a/snippets/ram-console/README.rst b/snippets/ram-console/README.rst new file mode 100644 index 000000000000..1abef50eb4a7 --- /dev/null +++ b/snippets/ram-console/README.rst @@ -0,0 +1,37 @@ +.. _snippet-ram-console: + +RAM Console Snippet (ram-console) +################################# + +.. code-block:: console + + west build -S ram-console [...] + +Overview +******** + +This snippet redirects console output to a RAM buffer. The RAM console +buffer is a global array located in RAM region by default, whose address +is unknown before building. The RAM console driver also supports using +a dedicated section for the RAM console buffer with prefined address. + +How to enable RAM console buffer section +**************************************** + +Add board dts overlay to this snippet to add property ``zephyr,ram-console`` +in the chosen node and memory-region node with compatible string +:dtcompatible:`zephyr,memory-region` as the following: + +.. code-block:: DTS + + / { + chosen { + zephyr,ram-console = &snippet_ram_console; + }; + + snippet_ram_console: memory@93d00000 { + compatible = "zephyr,memory-region"; + reg = <0x93d00000 DT_SIZE_K(4)>; + zephyr,memory-region = "RAM_CONSOLE"; + }; + }; diff --git a/snippets/ram-console/boards/imx8mm_evk_mimx8mm6_a53.overlay b/snippets/ram-console/boards/imx8mm_evk_mimx8mm6_a53.overlay new file mode 100644 index 000000000000..1d0a7a296e77 --- /dev/null +++ b/snippets/ram-console/boards/imx8mm_evk_mimx8mm6_a53.overlay @@ -0,0 +1,17 @@ +/* + * Copyright 2024 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + chosen { + zephyr,ram-console = &snippet_ram_console; + }; + + snippet_ram_console: memory@93d00000 { + compatible = "zephyr,memory-region"; + reg = <0x93d00000 DT_SIZE_K(4)>; + zephyr,memory-region = "RAM_CONSOLE"; + }; +}; diff --git a/snippets/ram-console/boards/imx8mn_evk_mimx8mn6_a53.overlay b/snippets/ram-console/boards/imx8mn_evk_mimx8mn6_a53.overlay new file mode 100644 index 000000000000..1d0a7a296e77 --- /dev/null +++ b/snippets/ram-console/boards/imx8mn_evk_mimx8mn6_a53.overlay @@ -0,0 +1,17 @@ +/* + * Copyright 2024 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + chosen { + zephyr,ram-console = &snippet_ram_console; + }; + + snippet_ram_console: memory@93d00000 { + compatible = "zephyr,memory-region"; + reg = <0x93d00000 DT_SIZE_K(4)>; + zephyr,memory-region = "RAM_CONSOLE"; + }; +}; diff --git a/snippets/ram-console/boards/imx8mp_evk_mimx8ml8_a53.overlay b/snippets/ram-console/boards/imx8mp_evk_mimx8ml8_a53.overlay new file mode 100644 index 000000000000..243dea43766d --- /dev/null +++ b/snippets/ram-console/boards/imx8mp_evk_mimx8ml8_a53.overlay @@ -0,0 +1,17 @@ +/* + * Copyright 2024 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + chosen { + zephyr,ram-console = &snippet_ram_console; + }; + + snippet_ram_console: memory@c0100000 { + compatible = "zephyr,memory-region"; + reg = <0xc0100000 DT_SIZE_K(4)>; + zephyr,memory-region = "RAM_CONSOLE"; + }; +}; diff --git a/snippets/ram-console/boards/imx93_evk_mimx9352_a55.overlay b/snippets/ram-console/boards/imx93_evk_mimx9352_a55.overlay new file mode 100644 index 000000000000..8d2619efe53b --- /dev/null +++ b/snippets/ram-console/boards/imx93_evk_mimx9352_a55.overlay @@ -0,0 +1,17 @@ +/* + * Copyright 2024 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + chosen { + zephyr,ram-console = &snippet_ram_console; + }; + + snippet_ram_console: memory@d0100000 { + compatible = "zephyr,memory-region"; + reg = <0xd0100000 DT_SIZE_K(4)>; + zephyr,memory-region = "RAM_CONSOLE"; + }; +}; diff --git a/snippets/ram-console/ram-console.conf b/snippets/ram-console/ram-console.conf new file mode 100644 index 000000000000..a20439dbda75 --- /dev/null +++ b/snippets/ram-console/ram-console.conf @@ -0,0 +1,9 @@ +# +# Copyright 2024 NXP +# +# SPDX-License-Identifier: Apache-2.0 + +# Disable UART Console +CONFIG_UART_CONSOLE=n +# Enable RAM Console +CONFIG_RAM_CONSOLE=y diff --git a/snippets/ram-console/snippet.yml b/snippets/ram-console/snippet.yml new file mode 100644 index 000000000000..47e154a9c764 --- /dev/null +++ b/snippets/ram-console/snippet.yml @@ -0,0 +1,32 @@ +# +# Copyright 2024 NXP +# +# SPDX-License-Identifier: Apache-2.0 +# + +name: ram-console +append: + EXTRA_CONF_FILE: ram-console.conf + +boards: + imx8mm_evk/mimx8mm6/a53: + append: + EXTRA_DTC_OVERLAY_FILE: boards/imx8mm_evk_mimx8mm6_a53.overlay + imx8mm_evk/mimx8mm6/a53/smp: + append: + EXTRA_DTC_OVERLAY_FILE: boards/imx8mm_evk_mimx8mm6_a53.overlay + imx8mn_evk/mimx8mn6/a53: + append: + EXTRA_DTC_OVERLAY_FILE: boards/imx8mn_evk_mimx8mn6_a53.overlay + imx8mn_evk/mimx8mn6/a53/smp: + append: + EXTRA_DTC_OVERLAY_FILE: boards/imx8mn_evk_mimx8mn6_a53.overlay + imx8mp_evk/mimx8ml8/a53: + append: + EXTRA_DTC_OVERLAY_FILE: boards/imx8mp_evk_mimx8ml8_a53.overlay + imx8mp_evk/mimx8ml8/a53/smp: + append: + EXTRA_DTC_OVERLAY_FILE: boards/imx8mp_evk_mimx8ml8_a53.overlay + imx93_evk/mimx9352/a55: + append: + EXTRA_DTC_OVERLAY_FILE: boards/imx93_evk_mimx9352_a55.overlay