From b5b0fc99a223f56a512a58cecec9688e6bd32c41 Mon Sep 17 00:00:00 2001 From: L Lakshmanan Date: Thu, 6 Jul 2023 16:28:17 +0530 Subject: [PATCH] drivers: mm: Add RAT support using system_mm API Integrates get local address functionality required by the RAT driver using sys_mm_drv_page_phys_get. Signed-off-by: L Lakshmanan --- drivers/mm/CMakeLists.txt | 2 +- drivers/mm/Kconfig | 12 ++++--- drivers/mm/{rat/ti_rat.c => mm_drv_ti_rat.c} | 36 +++++++++++++------- drivers/mm/rat/CMakeLists.txt | 8 ----- drivers/mm/rat/Kconfig | 13 ------- include/zephyr/drivers/mm/rat.h | 7 ++-- include/zephyr/sys/device_mmio.h | 6 +++- include/zephyr/sys/mem_manage.h | 20 ----------- kernel/mmu.c | 10 ------ 9 files changed, 39 insertions(+), 75 deletions(-) rename drivers/mm/{rat/ti_rat.c => mm_drv_ti_rat.c} (74%) delete mode 100644 drivers/mm/rat/CMakeLists.txt delete mode 100644 drivers/mm/rat/Kconfig diff --git a/drivers/mm/CMakeLists.txt b/drivers/mm/CMakeLists.txt index 94906b5a0b36898..62736e972867090 100644 --- a/drivers/mm/CMakeLists.txt +++ b/drivers/mm/CMakeLists.txt @@ -16,4 +16,4 @@ zephyr_sources_ifdef( mm_drv_intel_adsp_mtl_tlb.c ) -add_subdirectory_ifdef(CONFIG_TI_RAT rat) +zephyr_sources_ifdef(CONFIG_TI_RAT mm_drv_ti_rat.c) diff --git a/drivers/mm/Kconfig b/drivers/mm/Kconfig index 68e38ac0112ee8f..3b0fb2fdcd6040a 100644 --- a/drivers/mm/Kconfig +++ b/drivers/mm/Kconfig @@ -44,9 +44,7 @@ config MM_DRV_INTEL_ADSP_TLB Driver for the translation lookup buffer on Intel Audio DSP hardware. -endif # MM_DRV - -menuconfig EXTERNAL_ADDRESS_TRANSLATION +config EXTERNAL_ADDRESS_TRANSLATION bool "Support for external address translation modules if necessary" help This config is intended to support an external address @@ -55,7 +53,13 @@ menuconfig EXTERNAL_ADDRESS_TRANSLATION if EXTERNAL_ADDRESS_TRANSLATION -source "drivers/mm/rat/Kconfig" +config TI_RAT + bool "Texas Instruments RAT module" + depends on EXTERNAL_ADDRESS_TRANSLATION + help + Enables Region based address translation + support functions specific to TI SoCs. endif # EXTERNAL_ADDRESS_TRANSLATION +endif # MM_DRV diff --git a/drivers/mm/rat/ti_rat.c b/drivers/mm/mm_drv_ti_rat.c similarity index 74% rename from drivers/mm/rat/ti_rat.c rename to drivers/mm/mm_drv_ti_rat.c index e1510350dd53ea3..2af4f5f90fb1bf6 100644 --- a/drivers/mm/rat/ti_rat.c +++ b/drivers/mm/mm_drv_ti_rat.c @@ -6,6 +6,7 @@ */ #include +#include #include address_trans_params translate_config; @@ -53,7 +54,7 @@ void address_trans_init(address_trans_params *params) } } -void RAT_init(void *region_config, uint64_t rat_base_addr, uint8_t translate_regions) +void mm_drv_ti_rat_init(void *region_config, uint64_t rat_base_addr, uint8_t translate_regions) { translate_config.num_regions = translate_regions; translate_config.rat_base_addr = rat_base_addr; @@ -66,46 +67,55 @@ void RAT_deinit(void) { } -void *z_get_local_addr(uint64_t system_addr) +int sys_mm_drv_page_phys_get(void *virt, uintptr_t *phys) { - return rat_get_local_addr(system_addr); -} + if (virt == NULL) { + return -EINVAL; + } + uint64_t pa = ((uint64_t) (virt)); + uintptr_t *va = phys; -void *rat_get_local_addr(uint64_t system_addr) -{ uint32_t found, regionId; - void *local_addr; __ASSERT(translate_config.num_regions < address_trans_MAX_REGIONS, "Exceeding maximum number of regions"); found = 0; - uint32_t x = translate_config.num_regions; for (regionId = 0; regionId < translate_config.num_regions; regionId++) { uint64_t start_addr, end_addr; uint32_t size_mask; + /* we assume translate_config.region_config[] address and size is aligned */ size_mask = ((uint32_t)(((uint64_t)1 << translate_config.region_config[regionId].size) - 1)); start_addr = translate_config.region_config[regionId].system_addr; + /* calculate end address */ end_addr = start_addr + size_mask; - if (system_addr >= start_addr && system_addr <= end_addr) { + /* see if input address falls in this region */ + if (pa >= start_addr && pa <= end_addr) { + /* yes, input address falls in this region, break from loop */ found = 1; break; } } if (found) { + /* translate input address to output address */ uint32_t offset = - system_addr - translate_config.region_config[regionId].system_addr; + pa - translate_config.region_config[regionId].system_addr; - local_addr = (void *)(translate_config.region_config[regionId].local_addr + offset); + *va = (void *)(translate_config.region_config[regionId].local_addr + offset); } else { - local_addr = (void *)system_addr; + /* no mapping found, set output = input with 32b truncation */ + *va = (void *)pa; + } + + if (va == NULL) { + return -EFAULT; } - return local_addr; + return 0; } diff --git a/drivers/mm/rat/CMakeLists.txt b/drivers/mm/rat/CMakeLists.txt deleted file mode 100644 index d44d1b261abc32a..000000000000000 --- a/drivers/mm/rat/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -# Copyright (c) 2023 Texas Instruments Incorporated -# Copyright (c) 2023 L Lakshmanan -# -# SPDX-License-Identifier: Apache-2.0 - -zephyr_library() - -zephyr_library_sources(ti_rat.c) diff --git a/drivers/mm/rat/Kconfig b/drivers/mm/rat/Kconfig deleted file mode 100644 index 5a1ae7e2a2be13c..000000000000000 --- a/drivers/mm/rat/Kconfig +++ /dev/null @@ -1,13 +0,0 @@ -# Region Address Translation configuration options -# -# Copyright (c) 2023 Texas Instruments Incorporated -# Copyright (c) 2023 L Lakshmanan -# -# SPDX-License-Identifier: Apache-2.0 - -config TI_RAT - bool "Texas Instruments RAT module" - depends on EXTERNAL_ADDRESS_TRANSLATION - help - Enables Region based address translation - support functions specific to TI SoCs. diff --git a/include/zephyr/drivers/mm/rat.h b/include/zephyr/drivers/mm/rat.h index ebc812a63dfb6a3..94882dca4002ce9 100644 --- a/include/zephyr/drivers/mm/rat.h +++ b/include/zephyr/drivers/mm/rat.h @@ -85,11 +85,8 @@ typedef struct address_trans_params_s { } address_trans_params; void address_trans_init(address_trans_params *params); -void RAT_init(void *region_config, uint64_t rat_base_addr, uint8_t translate_regions); -void RAT_deinit(void); -void *rat_get_local_addr(uint64_t system_addr); -/* Generic abstraction function to override weakly defined function */ -void *z_get_local_addr(uint64_t system_addr); +void mm_drv_ti_rat_init(void *region_config, uint64_t rat_base_addr, uint8_t translate_regions); +void mm_drv_ti_rat_deinit(void); /** @} */ diff --git a/include/zephyr/sys/device_mmio.h b/include/zephyr/sys/device_mmio.h index 5fd0878799c0472..eb48cb67d974949 100644 --- a/include/zephyr/sys/device_mmio.h +++ b/include/zephyr/sys/device_mmio.h @@ -37,6 +37,10 @@ #define DEVICE_MMIO_IS_IN_RAM #endif +#if defined(CONFIG_MM_DRV) +#include +#endif + #ifndef _ASMLANGUAGE #include #include @@ -102,7 +106,7 @@ static inline void device_map(mm_reg_t *virt_addr, uintptr_t phys_addr, ARG_UNUSED(size); ARG_UNUSED(flags); #ifdef CONFIG_EXTERNAL_ADDRESS_TRANSLATION - *virt_addr = (mm_reg_t *) z_get_local_addr((uint64_t) phys_addr); + sys_mm_drv_page_phys_get((void *) phys_addr, virt_addr); #else *virt_addr = phys_addr; #endif /* CONFIG_EXTERNAL_ADDRESS_TRANSLATION */ diff --git a/include/zephyr/sys/mem_manage.h b/include/zephyr/sys/mem_manage.h index afb6bf154a25514..3f0c0f4b5fdf539 100644 --- a/include/zephyr/sys/mem_manage.h +++ b/include/zephyr/sys/mem_manage.h @@ -261,26 +261,6 @@ void z_phys_map(uint8_t **virt_ptr, uintptr_t phys, size_t size, */ void z_phys_unmap(uint8_t *virt, size_t size); -#ifdef CONFIG_EXTERNAL_ADDRESS_TRANSLATION -/** - * Get a local address using a custom memory mapping function. - * - * This API is only available if CONFIG_EXTERNAL_ADDRESS_TRANSLATION - * is enabled. - * - * This function is meant to be overridden by custom address mapppings - * defined in drivers. - * - * This API is part of infrastructure still under development and may - * change. - * - * @param phys_addr Physical address that is to be translated by the - * external module. - */ - -__weak void *z_get_local_addr(uint64_t phys_addr); -#endif /* CONFIG_EXTERNAL_ADDRESS_TRANSLATION */ - /* * k_mem_map() control flags */ diff --git a/kernel/mmu.c b/kernel/mmu.c index 80481e721b0145d..09af04a49f4e118 100644 --- a/kernel/mmu.c +++ b/kernel/mmu.c @@ -952,16 +952,6 @@ void z_mem_manage_boot_finish(void) #endif } -#ifdef CONFIG_EXTERNAL_ADDRESS_TRANSLATION - -__weak void *z_get_local_addr(uint64_t phys_addr) -{ - /* To be overridden by external address translation in drivers */ - return (void *) phys_addr; -} - -#endif /* CONFIG_EXTERNAL_ADDRESS_TRANSLATION */ - #ifdef CONFIG_DEMAND_PAGING #ifdef CONFIG_DEMAND_PAGING_STATS