1 /*
2  * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @Backgrounds
9  *
10  * This file contains 2 parts:
11  * 1. Feature: Copy Flash content to PSRAM. Related APIs are private:
12  *    - mmu_config_psram_text_segment()
13  *    - mmu_config_psram_rodata_segment()
14  *
15  * 2. Private APIs used by `flash_mmap.c` and `cache_utils.c`
16  *    APIs in 2 are due to lack of MMU driver. There will be an MMU driver to maintain vaddr range.
17  *    APIs in 2 will be refactored when MMU driver is ready
18  */
19 
20 #pragma once
21 
22 #include <sys/param.h>
23 #include "esp_err.h"
24 #include "sdkconfig.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #if CONFIG_IDF_TARGET_ESP32
31 #define MMU_PAGE_SIZE                   0x8000
32 #else
33 #define MMU_PAGE_SIZE                   0x10000
34 #define MMU_PAGE_TO_BYTES(page_id)      ((page_id) * MMU_PAGE_SIZE)
35 #define BYTES_TO_MMU_PAGE(bytes)        ((bytes) / MMU_PAGE_SIZE)
36 #endif
37 
38 /*----------------------------------------------------------------------------
39                     Part 1 APIs (See @Backgrounds on top of this file)
40 -------------------------------------------------------------------------------*/
41 #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
42 /**
43  * @brief Copy Flash texts to PSRAM
44  *
45  * @param[in]  start_page    PSRAM physical start page
46  * @param[in]  psram_size    PSRAM available size
47  * @param[out] out_page      Used pages
48  */
49 esp_err_t mmu_config_psram_text_segment(uint32_t start_page, uint32_t psram_size, uint32_t *out_page);
50 #endif  //#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
51 
52 #if CONFIG_SPIRAM_RODATA
53 /**
54  * @brief Copy Flash rodata to PSRAM
55  *
56  * @param[in]  start_page    PSRAM physical start page
57  * @param[in]  psram_size    PSRAM available size
58  * @param[out] out_page      Used pages
59  */
60 esp_err_t mmu_config_psram_rodata_segment(uint32_t start_page, uint32_t psram_size, uint32_t *out_page);
61 #endif  //#if CONFIG_SPIRAM_RODATA
62 
63 
64 /*----------------------------------------------------------------------------
65                     Part 2 APIs (See @Backgrounds on top of this file)
66 -------------------------------------------------------------------------------*/
67 #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
68 /**
69  * @brief Init other file requested MMU variables
70  *
71  * - These logics are abstracted from the PSRAM driver
72  * - These functions are only required by `flash_mmap.c` for converting paddr to vaddr, and vice versa
73  * - The `flash_mmpa.c` will be rewritten into MMU driver
74  *
75  * Therefore, keep the APIs here for now
76  */
77 void instruction_flash_page_info_init(uint32_t psram_start_physical_page);
78 
79 /**
80  * @brief Get the start page number of the instruction in SPI flash
81  *
82  * @return start page number
83  */
84 uint32_t instruction_flash_start_page_get(void);
85 
86 /**
87  * @brief Get the end page number of the instruction in SPI flash
88  *
89  * @return end page number
90  */
91 uint32_t instruction_flash_end_page_get(void);
92 
93 /**
94  * @brief Get the offset of instruction from SPI flash to SPI RAM
95  *
96  * @return instruction offset
97  */
98 int instruction_flash2spiram_offset(void);
99 #endif  // #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
100 
101 #if CONFIG_SPIRAM_RODATA
102 /**
103  * @brief Init other file requested MMU variables
104  *
105  * - These logics are abstracted from the PSRAM driver
106  * - These functions are only required by `flash_mmap.c` for converting paddr to vaddr, and vice versa
107  * - The `flash_mmpa.c` will be rewritten into MMU driver
108  *
109  * Therefore, keep the APIs here for now
110  */
111 void rodata_flash_page_info_init(uint32_t psram_start_physical_page);
112 
113 /**
114  * @brief Get the start page number of the rodata in SPI flash
115  *
116  * @return start page number
117  */
118 uint32_t rodata_flash_start_page_get(void);
119 
120 /**
121  * @brief Get the end page number of the rodata in SPI flash
122  *
123  * @return end page number
124  */
125 uint32_t rodata_flash_end_page_get(void);
126 
127 /**
128  * @brief Get the offset number of rodata from SPI flash to SPI RAM
129  *
130  * @return rodata offset
131  */
132 int rodata_flash2spiram_offset(void);
133 #endif  // #if CONFIG_SPIRAM_RODATA
134 
135 
136 #ifdef __cplusplus
137 }
138 #endif
139