]> begriffs open source - cmsis/blob - CMSIS/CoreValidation/Tests/config/core_a/mmu.c
CoreValidation: Fixed MMU setup for Cortex-A5/-A7.
[cmsis] / CMSIS / CoreValidation / Tests / config / core_a / mmu.c
1 /**************************************************************************//**
2  * @file     mmu.c
3  * @brief    MMU Configuration for Arm Cortex-A Device Series
4  * @version  V1.00
5  * @date     10. January 2018
6  *
7  * @note
8  *
9  ******************************************************************************/
10 /*
11  * Copyright (c) 2009-2018 Arm Limited. All rights reserved.
12  *
13  * SPDX-License-Identifier: Apache-2.0
14  *
15  * Licensed under the Apache License, Version 2.0 (the License); you may
16  * not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
23  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  */
27
28 /* Memory map description from: DUI0447G_v2m_p1_trm.pdf 4.2.2 Arm Cortex-A Series memory map
29
30                                                      Memory Type
31 0xffffffff |--------------------------|             ------------
32            |       FLAG SYNC          |             Device Memory
33 0xfffff000 |--------------------------|             ------------
34            |         Fault            |                Fault
35 0xfff00000 |--------------------------|             ------------
36            |                          |                Normal
37            |                          |
38            |      Daughterboard       |
39            |         memory           |
40            |                          |
41 0x80505000 |--------------------------|             ------------
42            |TTB (L2 Sync Flags   ) 4k |                Normal
43 0x80504C00 |--------------------------|             ------------
44            |TTB (L2 Peripherals-B) 16k|                Normal
45 0x80504800 |--------------------------|             ------------
46            |TTB (L2 Peripherals-A) 16k|                Normal
47 0x80504400 |--------------------------|             ------------
48            |TTB (L2 Priv Periphs)  4k |                Normal
49 0x80504000 |--------------------------|             ------------
50            |    TTB (L1 Descriptors)  |                Normal
51 0x80500000 |--------------------------|             ------------
52            |           Heap           |                Normal
53            |--------------------------|             ------------
54            |          Stack           |                Normal
55 0x80400000 |--------------------------|             ------------
56            |         ZI Data          |                Normal
57 0x80300000 |--------------------------|             ------------
58            |         RW Data          |                Normal
59 0x80200000 |--------------------------|             ------------
60            |         RO Data          |                Normal
61            |--------------------------|             ------------
62            |         RO Code          |              USH Normal
63 0x80000000 |--------------------------|             ------------
64            |      Daughterboard       |                Fault
65            |      HSB AXI buses       |
66 0x40000000 |--------------------------|             ------------
67            |      Daughterboard       |                Fault
68            |  test chips peripherals  |
69 0x2c002000 |--------------------------|             ------------
70            |     Private Address      |            Device Memory
71 0x2c000000 |--------------------------|             ------------
72            |      Daughterboard       |                Fault
73            |  test chips peripherals  |
74 0x20000000 |--------------------------|             ------------
75            |       Peripherals        |           Device Memory RW/RO
76            |                          |              & Fault
77 0x00000000 |--------------------------|
78 */
79
80 // L1 Cache info and restrictions about architecture of the caches (CCSIR register):
81 // Write-Through support *not* available
82 // Write-Back support available.
83 // Read allocation support available.
84 // Write allocation support available.
85
86 //Note: You should use the Shareable attribute carefully.
87 //For cores without coherency logic (such as SCU) marking a region as shareable forces the processor to not cache that region regardless of the inner cache settings.
88 //Cortex-A versions of RTX use LDREX/STREX instructions relying on Local monitors. Local monitors will be used only when the region gets cached, regions that are not cached will use the Global Monitor.
89 //Some Cortex-A implementations do not include Global Monitors, so wrongly setting the attribute Shareable may cause STREX to fail.
90
91 //Recall: When the Shareable attribute is applied to a memory region that is not Write-Back, Normal memory, data held in this region is treated as Non-cacheable.
92 //When SMP bit = 0, Inner WB/WA Cacheable Shareable attributes are treated as Non-cacheable.
93 //When SMP bit = 1, Inner WB/WA Cacheable Shareable attributes are treated as Cacheable.
94
95
96 //Following MMU configuration is expected
97 //SCTLR.AFE == 1 (Simplified access permissions model - AP[2:1] define access permissions, AP[0] is an access flag)
98 //SCTLR.TRE == 0 (TEX remap disabled, so memory type and attributes are described directly by bits in the descriptor)
99 //Domain 0 is always the Client domain
100 //Descriptors should place all memory in domain 0
101
102 #include "RTE_Components.h"
103 #include CMSIS_device_header
104
105 #if   __CORTEX_A == 5
106 #define VE_MP_FLASH_BASE0                  VE_A5_MP_FLASH_BASE0
107 #define VE_MP_FLASH_BASE1                  VE_A5_MP_FLASH_BASE1
108 #define VE_MP_PERIPH_BASE                  VE_A5_MP_PERIPH_BASE
109 #define VE_MP_SRAM_BASE                    VE_A5_MP_SRAM_BASE
110 #define VE_MP_DRAM_BASE                    VE_A5_MP_DRAM_BASE
111 #define VE_MP_VRAM_BASE                    VE_A5_MP_VRAM_BASE
112 #define VE_MP_ETHERNET_BASE                VE_A5_MP_ETHERNET_BASE
113 #define VE_MP_USB_BASE                     VE_A5_MP_USB_BASE
114 #define VE_MP_DAP_BASE                     VE_A5_MP_DAP_BASE
115 #define VE_MP_SYSTEM_REG_BASE              VE_A5_MP_SYSTEM_REG_BASE
116 #define VE_MP_SERIAL_BASE                  VE_A5_MP_SERIAL_BASE
117 #define VE_MP_AACI_BASE                    VE_A5_MP_AACI_BASE
118 #define VE_MP_MMCI_BASE                    VE_A5_MP_MMCI_BASE
119 #define VE_MP_KMI0_BASE                    VE_A5_MP_KMI0_BASE
120 #define VE_MP_UART_BASE                    VE_A5_MP_UART_BASE
121 #define VE_MP_WDT_BASE                     VE_A5_MP_WDT_BASE
122 #define VE_MP_TIMER_BASE                   VE_A5_MP_TIMER_BASE
123 #define VE_MP_DVI_BASE                     VE_A5_MP_DVI_BASE
124 #define VE_MP_RTC_BASE                     VE_A5_MP_RTC_BASE
125 #define VE_MP_UART4_BASE                   VE_A5_MP_UART4_BASE
126 #define VE_MP_CLCD_BASE                    VE_A5_MP_CLCD_BASE
127 #define VE_MP_GIC_DISTRIBUTOR_BASE         VE_A5_MP_GIC_DISTRIBUTOR_BASE
128 #define VE_MP_GIC_INTERFACE_BASE           VE_A5_MP_GIC_INTERFACE_BASE
129 #define VE_MP_PRIVATE_TIMER                VE_A5_MP_PRIVATE_TIMER
130 #elif __CORTEX_A == 7
131 #define VE_MP_FLASH_BASE0                  VE_A7_MP_FLASH_BASE0
132 #define VE_MP_FLASH_BASE1                  VE_A7_MP_FLASH_BASE1
133 #define VE_MP_PERIPH_BASE                  VE_A7_MP_PERIPH_BASE
134 #define VE_MP_SRAM_BASE                    VE_A7_MP_SRAM_BASE
135 #define VE_MP_DRAM_BASE                    VE_A7_MP_DRAM_BASE
136 #define VE_MP_VRAM_BASE                    VE_A7_MP_VRAM_BASE
137 #define VE_MP_ETHERNET_BASE                VE_A7_MP_ETHERNET_BASE
138 #define VE_MP_USB_BASE                     VE_A7_MP_USB_BASE
139 #define VE_MP_DAP_BASE                     VE_A7_MP_DAP_BASE
140 #define VE_MP_SYSTEM_REG_BASE              VE_A7_MP_SYSTEM_REG_BASE
141 #define VE_MP_SERIAL_BASE                  VE_A7_MP_SERIAL_BASE
142 #define VE_MP_AACI_BASE                    VE_A7_MP_AACI_BASE
143 #define VE_MP_MMCI_BASE                    VE_A7_MP_MMCI_BASE
144 #define VE_MP_KMI0_BASE                    VE_A7_MP_KMI0_BASE
145 #define VE_MP_UART_BASE                    VE_A7_MP_UART_BASE
146 #define VE_MP_WDT_BASE                     VE_A7_MP_WDT_BASE
147 #define VE_MP_TIMER_BASE                   VE_A7_MP_TIMER_BASE
148 #define VE_MP_DVI_BASE                     VE_A7_MP_DVI_BASE
149 #define VE_MP_RTC_BASE                     VE_A7_MP_RTC_BASE
150 #define VE_MP_UART4_BASE                   VE_A7_MP_UART4_BASE
151 #define VE_MP_CLCD_BASE                    VE_A7_MP_CLCD_BASE
152 #define VE_MP_GIC_DISTRIBUTOR_BASE         VE_A7_MP_GIC_DISTRIBUTOR_BASE
153 #define VE_MP_GIC_INTERFACE_BASE           VE_A7_MP_GIC_INTERFACE_BASE
154 #define VE_MP_PRIVATE_TIMER                VE_A7_MP_PRIVATE_TIMER
155 #elif __CORTEX_A == 9
156 #define VE_MP_FLASH_BASE0                  VE_A9_MP_FLASH_BASE0
157 #define VE_MP_FLASH_BASE1                  VE_A9_MP_FLASH_BASE1
158 #define VE_MP_PERIPH_BASE                  VE_A9_MP_PERIPH_BASE
159 #define VE_MP_SRAM_BASE                    VE_A9_MP_SRAM_BASE
160 #define VE_MP_DRAM_BASE                    VE_A9_MP_DRAM_BASE
161 #define VE_MP_VRAM_BASE                    VE_A9_MP_VRAM_BASE
162 #define VE_MP_ETHERNET_BASE                VE_A9_MP_ETHERNET_BASE
163 #define VE_MP_USB_BASE                     VE_A9_MP_USB_BASE
164 #define VE_MP_DAP_BASE                     VE_A9_MP_DAP_BASE
165 #define VE_MP_SYSTEM_REG_BASE              VE_A9_MP_SYSTEM_REG_BASE
166 #define VE_MP_SERIAL_BASE                  VE_A9_MP_SERIAL_BASE
167 #define VE_MP_AACI_BASE                    VE_A9_MP_AACI_BASE
168 #define VE_MP_MMCI_BASE                    VE_A9_MP_MMCI_BASE
169 #define VE_MP_KMI0_BASE                    VE_A9_MP_KMI0_BASE
170 #define VE_MP_UART_BASE                    VE_A9_MP_UART_BASE
171 #define VE_MP_WDT_BASE                     VE_A9_MP_WDT_BASE
172 #define VE_MP_TIMER_BASE                   VE_A9_MP_TIMER_BASE
173 #define VE_MP_DVI_BASE                     VE_A9_MP_DVI_BASE
174 #define VE_MP_RTC_BASE                     VE_A9_MP_RTC_BASE
175 #define VE_MP_UART4_BASE                   VE_A9_MP_UART4_BASE
176 #define VE_MP_CLCD_BASE                    VE_A9_MP_CLCD_BASE
177 #define VE_MP_GIC_DISTRIBUTOR_BASE         VE_A9_MP_GIC_DISTRIBUTOR_BASE
178 #define VE_MP_GIC_INTERFACE_BASE           VE_A9_MP_GIC_INTERFACE_BASE
179 #define VE_MP_PRIVATE_TIMER                VE_A9_MP_PRIVATE_TIMER
180 #endif
181
182 // L2 table pointers
183 //----------------------------------------
184 #define PRIVATE_TABLE_L2_BASE_4k       (0x80504000) //Map 4k Private Address space
185 #define SYNC_FLAGS_TABLE_L2_BASE_4k    (0x80504C00) //Map 4k Flag synchronization
186 #define PERIPHERAL_A_TABLE_L2_BASE_64k (0x80504400) //Map 64k Peripheral #1 0x1C000000 - 0x1C00FFFFF
187 #define PERIPHERAL_B_TABLE_L2_BASE_64k (0x80504800) //Map 64k Peripheral #2 0x1C100000 - 0x1C1FFFFFF
188
189 //--------------------- PERIPHERALS -------------------
190 #define PERIPHERAL_A_FAULT (0x00000000 + 0x1c000000) //0x1C000000-0x1C00FFFF (1M)
191 #define PERIPHERAL_B_FAULT (0x00100000 + 0x1c000000) //0x1C100000-0x1C10FFFF (1M)
192
193 //--------------------- SYNC FLAGS --------------------
194 #define FLAG_SYNC     0xFFFFF000
195 #define F_SYNC_BASE   0xFFF00000  //1M aligned
196
197 //Import symbols from linker
198 extern uint32_t Image$$VECTORS$$Base;
199 extern uint32_t Image$$RW_DATA$$Base;
200 extern uint32_t Image$$ZI_DATA$$Base;
201 extern uint32_t Image$$TTB$$ZI$$Base;
202
203 static uint32_t Sect_Normal;     //outer & inner wb/wa, non-shareable, executable, rw, domain 0, base addr 0
204 static uint32_t Sect_Normal_Cod; //outer & inner wb/wa, non-shareable, executable, ro, domain 0, base addr 0
205 static uint32_t Sect_Normal_RO;  //as Sect_Normal_Cod, but not executable
206 static uint32_t Sect_Normal_RW;  //as Sect_Normal_Cod, but writeable and not executable
207 static uint32_t Sect_Device_RO;  //device, non-shareable, non-executable, ro, domain 0, base addr 0
208 static uint32_t Sect_Device_RW;  //as Sect_Device_RO, but writeable
209
210 /* Define global descriptors */
211 static uint32_t Page_L1_4k  = 0x0;  //generic
212 static uint32_t Page_L1_64k = 0x0;  //generic
213 static uint32_t Page_4k_Device_RW;  //Shared device, not executable, rw, domain 0
214 static uint32_t Page_64k_Device_RW; //Shared device, not executable, rw, domain 0
215
216 void MMU_CreateTranslationTable(void)
217 {
218     mmu_region_attributes_Type region;
219
220     //Create 4GB of faulting entries
221     MMU_TTSection (&Image$$TTB$$ZI$$Base, 0, 4096, DESCRIPTOR_FAULT);
222
223     /*
224      * Generate descriptors. Refer to core_ca.h to get information about attributes
225      *
226      */
227     //Create descriptors for Vectors, RO, RW, ZI sections
228     section_normal(Sect_Normal, region);
229     section_normal_cod(Sect_Normal_Cod, region);
230     section_normal_ro(Sect_Normal_RO, region);
231     section_normal_rw(Sect_Normal_RW, region);
232     //Create descriptors for peripherals
233     section_device_ro(Sect_Device_RO, region);
234     section_device_rw(Sect_Device_RW, region);
235     //Create descriptors for 64k pages
236     page64k_device_rw(Page_L1_64k, Page_64k_Device_RW, region);
237     //Create descriptors for 4k pages
238     page4k_device_rw(Page_L1_4k, Page_4k_Device_RW, region);
239
240
241     /*
242      *  Define MMU flat-map regions and attributes
243      *
244      */
245
246     //Define Image
247     MMU_TTSection (&Image$$TTB$$ZI$$Base, (uint32_t)&Image$$VECTORS$$Base, 1, Sect_Normal_Cod);
248     MMU_TTSection (&Image$$TTB$$ZI$$Base, (uint32_t)&Image$$RW_DATA$$Base, 1, Sect_Normal_RW);
249     MMU_TTSection (&Image$$TTB$$ZI$$Base, (uint32_t)&Image$$ZI_DATA$$Base, 1, Sect_Normal_RW);
250
251     //all DRAM executable, rw, cacheable - applications may choose to divide memory into ro executable
252     MMU_TTSection (&Image$$TTB$$ZI$$Base, (uint32_t)&Image$$TTB$$ZI$$Base, 2043, Sect_Normal);
253
254     //--------------------- PERIPHERALS -------------------
255     MMU_TTSection (&Image$$TTB$$ZI$$Base, VE_MP_FLASH_BASE0    , 64, Sect_Device_RO);
256     MMU_TTSection (&Image$$TTB$$ZI$$Base, VE_MP_FLASH_BASE1    , 64, Sect_Device_RO);
257     MMU_TTSection (&Image$$TTB$$ZI$$Base, VE_MP_SRAM_BASE      , 64, Sect_Device_RW);
258     MMU_TTSection (&Image$$TTB$$ZI$$Base, VE_MP_VRAM_BASE      , 32, Sect_Device_RW);
259     MMU_TTSection (&Image$$TTB$$ZI$$Base, VE_MP_ETHERNET_BASE  , 16, Sect_Device_RW);
260     MMU_TTSection (&Image$$TTB$$ZI$$Base, VE_MP_USB_BASE       , 16, Sect_Device_RW);
261
262     // Create (16 * 64k)=1MB faulting entries to cover peripheral range 0x1C000000-0x1C00FFFF
263     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, PERIPHERAL_A_FAULT      , 16, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, DESCRIPTOR_FAULT);
264     // Define peripheral range 0x1C000000-0x1C00FFFF
265     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_DAP_BASE       ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
266     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_SYSTEM_REG_BASE,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
267     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_SERIAL_BASE    ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
268     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_AACI_BASE      ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
269     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_MMCI_BASE      ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
270     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_KMI0_BASE      ,  2, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
271     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_UART_BASE      ,  4, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
272     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_WDT_BASE       ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
273
274     // Create (16 * 64k)=1MB faulting entries to cover peripheral range 0x1C100000-0x1C10FFFF
275     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, PERIPHERAL_B_FAULT      , 16, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, DESCRIPTOR_FAULT);
276     // Define peripheral range 0x1C100000-0x1C10FFFF
277     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_TIMER_BASE     ,  2, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
278     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_DVI_BASE       ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
279     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_RTC_BASE       ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
280     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_UART4_BASE     ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
281     MMU_TTPage64k(&Image$$TTB$$ZI$$Base, VE_MP_CLCD_BASE      ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
282
283     // Create (256 * 4k)=1MB faulting entries to cover private address space. Needs to be marked as Device memory
284     MMU_TTPage4k (&Image$$TTB$$ZI$$Base, __get_CBAR()            ,256,  Page_L1_4k, (uint32_t *)PRIVATE_TABLE_L2_BASE_4k, DESCRIPTOR_FAULT);
285     // Define private address space entry.
286     MMU_TTPage4k (&Image$$TTB$$ZI$$Base, __get_CBAR()            ,  2,  Page_L1_4k, (uint32_t *)PRIVATE_TABLE_L2_BASE_4k, Page_4k_Device_RW);
287     // Define L2CC entry.  Uncomment if PL310 is present
288     //    MMU_TTPage4k (&Image$$TTB$$ZI$$Base, VE_A9_MP_PL310_BASE     ,  1,  Page_L1_4k, (uint32_t *)PRIVATE_TABLE_L2_BASE_4k, Page_4k_Device_RW);
289
290     // Create (256 * 4k)=1MB faulting entries to synchronization space (Useful if some non-cacheable DMA agent is present in the SoC)
291     MMU_TTPage4k (&Image$$TTB$$ZI$$Base, F_SYNC_BASE , 256, Page_L1_4k, (uint32_t *)SYNC_FLAGS_TABLE_L2_BASE_4k, DESCRIPTOR_FAULT);
292     // Define synchronization space entry.
293     MMU_TTPage4k (&Image$$TTB$$ZI$$Base, FLAG_SYNC   ,   1, Page_L1_4k, (uint32_t *)SYNC_FLAGS_TABLE_L2_BASE_4k, Page_4k_Device_RW);
294
295     /* Set location of level 1 page table
296     ; 31:14 - Translation table base addr (31:14-TTBCR.N, TTBCR.N is 0 out of reset)
297     ; 13:7  - 0x0
298     ; 6     - IRGN[0] 0x1  (Inner WB WA)
299     ; 5     - NOS     0x0  (Non-shared)
300     ; 4:3   - RGN     0x01 (Outer WB WA)
301     ; 2     - IMP     0x0  (Implementation Defined)
302     ; 1     - S       0x0  (Non-shared)
303     ; 0     - IRGN[1] 0x0  (Inner WB WA) */
304     __set_TTBR0(((uint32_t)&Image$$TTB$$ZI$$Base) | 0x48);
305     __ISB();
306
307     /* Set up domain access control register
308     ; We set domain 0 to Client and all other domains to No Access.
309     ; All translation table entries specify domain 0 */
310     __set_DACR(1);
311     __ISB();
312 }