From 0f6aae8a6fd37ff88e631f1c171910a479a1dc93 Mon Sep 17 00:00:00 2001 From: Daniel DeGrasse Date: Tue, 27 Sep 2022 16:51:13 -0500 Subject: [PATCH 1/5] cmake: modules: add zephyr_library_relocate, to relocate zephyr library Add zephyr_library_relocate, a helper macro intended to allow entire Zephyr libraries to be relocated to a new memory region. This is implemented via a two step process, to ensure that sources added to a library after relocation is requested still are relocated: 1. zephyr_library_relocate adds a custom target property to the library with the relocation region and flags 2. the root level CMakeLists.txt captures this property while parsing all Zephyr libraries, and adds all library source files to the code relocation target Signed-off-by: Daniel DeGrasse --- CMakeLists.txt | 17 +++++++++++++++++ cmake/modules/extensions.cmake | 14 ++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cba6ee3a8bbd..c3fb4af34d6e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,6 +91,8 @@ set(PARSE_SYSCALLS_TARGET parse_syscalls_target) define_property(GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT BRIEF_DOCS " " FULL_DOCS " ") set_property( GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT elf32-little${ARCH}) # BFD format +define_property(GLOBAL PROPERTY PROPERTY_CODE_RELOCATION_TARGET + BRIEF_DOCS "Code relocation target location" FULL_DOCS " ") # "zephyr_interface" is a source-less library that encapsulates all the global # compiler options needed by all source files. All zephyr libraries, @@ -815,6 +817,21 @@ foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY}) list(REMOVE_ITEM ZEPHYR_LIBS_PROPERTY ${zephyr_lib}) continue() endif() + # In order to allow zephyr_library_relocate to be called prior to adding + # library sources, we must parse and generate relocation information + # once all libraries have been defined + get_property(relocation_target TARGET ${zephyr_lib} PROPERTY + PROPERTY_CODE_RELOCATION_TARGET) + if(NOT "${relocation_target}" STREQUAL "") + get_property(lib_source_dir TARGET ${zephyr_lib} PROPERTY SOURCE_DIR) + foreach(file ${source_list}) + if(NOT IS_ABSOLUTE ${file}) + set(file ${lib_source_dir}/${file}) + endif() + set_property(TARGET code_data_relocation_target APPEND PROPERTY + COMPILE_DEFINITIONS "${relocation_target}:${file}") + endforeach() + endif() endif() # TODO: Could this become an INTERFACE property of zephyr_interface? diff --git a/cmake/modules/extensions.cmake b/cmake/modules/extensions.cmake index dae67fff09466..04b8712a9a6fb 100644 --- a/cmake/modules/extensions.cmake +++ b/cmake/modules/extensions.cmake @@ -1281,6 +1281,20 @@ function(zephyr_linker_sources location) endforeach() endfunction(zephyr_linker_sources) +function(zephyr_library_relocate location) + set(options NOCOPY) + cmake_parse_arguments(CODE_REL "${options}" "" "" ${ARGN}) + if(NOT CODE_REL_NOCOPY) + set(copy_flag COPY) + else() + set(copy_flag NOCOPY) + endif() + # Set current library relocation property. This will be picked up + # in the root CMakeLists.txt file to relocate all sources added to + # this library + set_property(TARGET ${ZEPHYR_CURRENT_LIBRARY} + PROPERTY PROPERTY_CODE_RELOCATION_TARGET "${location}:${copy_flag}") +endfunction() # Helper function for CONFIG_CODE_DATA_RELOCATION # Call this function with 2 arguments file and then memory location. From 4f41fe4278d0b1bc154f2ae708a62785ea22eaf2 Mon Sep 17 00:00:00 2001 From: Daniel DeGrasse Date: Wed, 28 Sep 2022 14:29:01 -0500 Subject: [PATCH 2/5] scripts: build: remove KEEP() from symbols moved with zephyr_code_relocate The build script parsing files that will be relocated using the zephyr_code_relocate macro was adding KEEP() to all symbols. This resulted in unnecessary symbols being added to the build, and could result in a linker failure if symbols in the relocated file that the linker would otherwise discard referenced undefined symbols. This error was observed while relocating portions of the networking subsystem to SRAM, since the network management API was not being compiled but some unused functions in the network subsystem were using the API. Signed-off-by: Daniel DeGrasse --- scripts/build/gen_relocate_app.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/build/gen_relocate_app.py b/scripts/build/gen_relocate_app.py index 594dba1bd3c77..f5be96255a872 100644 --- a/scripts/build/gen_relocate_app.py +++ b/scripts/build/gen_relocate_app.py @@ -45,7 +45,7 @@ PRINT_TEMPLATE = """ - KEEP(*({0})) + *({0}) """ SECTION_LOAD_MEMORY_SEQ = """ @@ -495,7 +495,6 @@ def main(): mem_type = mem_type.split("|", 1)[0] code_generation = generate_memcpy_code(mem_type, list_of_sections, code_generation) - dump_header_file(args.output_code, code_generation) From bab5fe3950056869bb5349b7eb010f407ffc543e Mon Sep 17 00:00:00 2001 From: Daniel DeGrasse Date: Wed, 28 Sep 2022 15:20:54 -0500 Subject: [PATCH 3/5] net: ethernet: enable relocating network stack to RAM Introduce CONFIG_NET_CODE_RELOCATE, to relocate Zephyr networking subsystem to RAM. This RAM region defaults to SRAM, but platforms can change the default region based on what memory is present. Signed-off-by: Daniel DeGrasse --- subsys/net/CMakeLists.txt | 4 ++++ subsys/net/Kconfig | 32 ++++++++++++++++++++++++++++++++ subsys/net/ip/CMakeLists.txt | 4 ++++ 3 files changed, 40 insertions(+) diff --git a/subsys/net/CMakeLists.txt b/subsys/net/CMakeLists.txt index bbc680f74e7f0..8bd56de5ecda9 100644 --- a/subsys/net/CMakeLists.txt +++ b/subsys/net/CMakeLists.txt @@ -4,6 +4,10 @@ zephyr_library() zephyr_library_sources_ifdef(CONFIG_NET_BUF buf.c) zephyr_library_sources_ifdef(CONFIG_NET_HOSTNAME_ENABLE hostname.c) +if(CONFIG_NET_CODE_RELOCATE) + zephyr_library_relocate("${CONFIG_NET_CODE_RAM_NAME}_TEXT") +endif() + if(CONFIG_NETWORKING) add_subdirectory(l2) add_subdirectory(pkt_filter) diff --git a/subsys/net/Kconfig b/subsys/net/Kconfig index 001807526c534..cb517aca14fa3 100644 --- a/subsys/net/Kconfig +++ b/subsys/net/Kconfig @@ -70,6 +70,38 @@ config NETWORKING if NETWORKING +config NET_CODE_RELOCATE + bool "Relocate network subsystem code into RAM" + select CODE_DATA_RELOCATION + help + Relocate networking subsystem code into RAM using zephyr_code_relocate + macro. This may enable significant performance gains on some + platforms, depending on the speed of their RAM versus FLASH region. +if NET_CODE_RELOCATE + +choice NET_CODE_RAM + prompt "Location to relocate networking code to in RAM" + default NET_CODE_RAM_SRAM + help + Location to relocated networking code to. Defaults to SRAM unless + the platform selects a higher performance RAM region + +config NET_CODE_RAM_SRAM + bool "SRAM" + select CODE_DATA_RELOCATION_SRAM + help + Relocate networking subsystem code to SRAM + +endchoice + +config NET_CODE_RAM_NAME + string + default "SRAM" if NET_CODE_RAM_SRAM + help + Hidden option for RAM bank to relocate networking code into + +endif # NET_CODE_RELOCATE + # Such option should not be configured manually but by device drivers # which supports PM properly. config NET_POWER_MANAGEMENT diff --git a/subsys/net/ip/CMakeLists.txt b/subsys/net/ip/CMakeLists.txt index cba2dcd921d40..1b88720d19c15 100644 --- a/subsys/net/ip/CMakeLists.txt +++ b/subsys/net/ip/CMakeLists.txt @@ -13,6 +13,10 @@ zephyr_library_sources( utils.c ) +if(CONFIG_NET_CODE_RELOCATE) + zephyr_library_relocate("${CONFIG_NET_CODE_RAM_NAME}_TEXT") +endif() + if(CONFIG_NET_OFFLOAD) zephyr_library_sources(net_context.c net_pkt.c net_tc.c) endif() From 678c772935380ec39b61bd0b7559d8126aad9256 Mon Sep 17 00:00:00 2001 From: Daniel DeGrasse Date: Wed, 28 Sep 2022 15:23:13 -0500 Subject: [PATCH 4/5] drivers: eth_mcux: Set ITCM as default network stack RAM code location Change the default target RAM for the network stack code to be relocated to to ITCM for NXP Cortex M7 platforms using the Kinetis Ethernet peripheral. This will enable improved performance over the default relocation region of SRAM. Signed-off-by: Daniel DeGrasse --- drivers/ethernet/Kconfig.mcux | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/drivers/ethernet/Kconfig.mcux b/drivers/ethernet/Kconfig.mcux index 1ecf0db8dc97d..4bb68e7b35dd7 100644 --- a/drivers/ethernet/Kconfig.mcux +++ b/drivers/ethernet/Kconfig.mcux @@ -110,4 +110,30 @@ config ETH_MCUX_PTP_CLOCK_INIT_PRIO endif # PTP_CLOCK_MCUX + +# Add relocation options for networking code +if NET_CODE_RELOCATE + +if CPU_CORTEX_M7 + +# Relocate code to ITCM for M7 cores + +choice NET_CODE_RAM + default NET_CODE_RAM_ITCM + +config NET_CODE_RAM_ITCM + bool "ITCM" + help + Relocate networking subsystem code to ITCM + +endchoice + +config NET_CODE_RAM_NAME + string + default "ITCM" if NET_CODE_RAM_ITCM + +endif # CPU_CORTEX_M7 + +endif # NET_CODE_RELOCATE + endif # ETH_MCUX From 410a0486d8f0fffc1bc623f64494e22a533fe985 Mon Sep 17 00:00:00 2001 From: Daniel DeGrasse Date: Wed, 28 Sep 2022 17:35:50 -0500 Subject: [PATCH 5/5] samples: zperf: enable net code relocation for RT1050 and RT1060 Enable code relocation for RT1050 and RT1060 Signed-off-by: Daniel DeGrasse --- samples/net/zperf/boards/mimxrt1050_evk.conf | 1 + samples/net/zperf/boards/mimxrt1060_evk.conf | 1 + 2 files changed, 2 insertions(+) diff --git a/samples/net/zperf/boards/mimxrt1050_evk.conf b/samples/net/zperf/boards/mimxrt1050_evk.conf index 8f90b5bd833fa..b5d2942ed62e4 100644 --- a/samples/net/zperf/boards/mimxrt1050_evk.conf +++ b/samples/net/zperf/boards/mimxrt1050_evk.conf @@ -1,2 +1,3 @@ # Note: HW accleration does not support IPV6 CONFIG_ETH_MCUX_HW_ACCELERATION=y +CONFIG_NET_CODE_RELOCATE=y diff --git a/samples/net/zperf/boards/mimxrt1060_evk.conf b/samples/net/zperf/boards/mimxrt1060_evk.conf index 8f90b5bd833fa..b5d2942ed62e4 100644 --- a/samples/net/zperf/boards/mimxrt1060_evk.conf +++ b/samples/net/zperf/boards/mimxrt1060_evk.conf @@ -1,2 +1,3 @@ # Note: HW accleration does not support IPV6 CONFIG_ETH_MCUX_HW_ACCELERATION=y +CONFIG_NET_CODE_RELOCATE=y