]> begriffs open source - cmsis/blob - CMSIS/CoreValidation/Tests/config/core_a/mmu.c
Pack: Bump version numbers and histories for release 5.5.1.
[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 #include "mem.h"
106
107 #if   __CORTEX_A == 5
108 #define VE_MP_FLASH_BASE0                  VE_A5_MP_FLASH_BASE0
109 #define VE_MP_FLASH_BASE1                  VE_A5_MP_FLASH_BASE1
110 #define VE_MP_PERIPH_BASE                  VE_A5_MP_PERIPH_BASE
111 #define VE_MP_SRAM_BASE                    VE_A5_MP_SRAM_BASE
112 #define VE_MP_DRAM_BASE                    VE_A5_MP_DRAM_BASE
113 #define VE_MP_VRAM_BASE                    VE_A5_MP_VRAM_BASE
114 #define VE_MP_ETHERNET_BASE                VE_A5_MP_ETHERNET_BASE
115 #define VE_MP_USB_BASE                     VE_A5_MP_USB_BASE
116 #define VE_MP_DAP_BASE                     VE_A5_MP_DAP_BASE
117 #define VE_MP_SYSTEM_REG_BASE              VE_A5_MP_SYSTEM_REG_BASE
118 #define VE_MP_SERIAL_BASE                  VE_A5_MP_SERIAL_BASE
119 #define VE_MP_AACI_BASE                    VE_A5_MP_AACI_BASE
120 #define VE_MP_MMCI_BASE                    VE_A5_MP_MMCI_BASE
121 #define VE_MP_KMI0_BASE                    VE_A5_MP_KMI0_BASE
122 #define VE_MP_UART_BASE                    VE_A5_MP_UART_BASE
123 #define VE_MP_WDT_BASE                     VE_A5_MP_WDT_BASE
124 #define VE_MP_TIMER_BASE                   VE_A5_MP_TIMER_BASE
125 #define VE_MP_DVI_BASE                     VE_A5_MP_DVI_BASE
126 #define VE_MP_RTC_BASE                     VE_A5_MP_RTC_BASE
127 #define VE_MP_UART4_BASE                   VE_A5_MP_UART4_BASE
128 #define VE_MP_CLCD_BASE                    VE_A5_MP_CLCD_BASE
129 #define VE_MP_GIC_DISTRIBUTOR_BASE         VE_A5_MP_GIC_DISTRIBUTOR_BASE
130 #define VE_MP_GIC_INTERFACE_BASE           VE_A5_MP_GIC_INTERFACE_BASE
131 #define VE_MP_PRIVATE_TIMER                VE_A5_MP_PRIVATE_TIMER
132 #elif __CORTEX_A == 7
133 #define VE_MP_FLASH_BASE0                  VE_A7_MP_FLASH_BASE0
134 #define VE_MP_FLASH_BASE1                  VE_A7_MP_FLASH_BASE1
135 #define VE_MP_PERIPH_BASE                  VE_A7_MP_PERIPH_BASE
136 #define VE_MP_SRAM_BASE                    VE_A7_MP_SRAM_BASE
137 #define VE_MP_DRAM_BASE                    VE_A7_MP_DRAM_BASE
138 #define VE_MP_VRAM_BASE                    VE_A7_MP_VRAM_BASE
139 #define VE_MP_ETHERNET_BASE                VE_A7_MP_ETHERNET_BASE
140 #define VE_MP_USB_BASE                     VE_A7_MP_USB_BASE
141 #define VE_MP_DAP_BASE                     VE_A7_MP_DAP_BASE
142 #define VE_MP_SYSTEM_REG_BASE              VE_A7_MP_SYSTEM_REG_BASE
143 #define VE_MP_SERIAL_BASE                  VE_A7_MP_SERIAL_BASE
144 #define VE_MP_AACI_BASE                    VE_A7_MP_AACI_BASE
145 #define VE_MP_MMCI_BASE                    VE_A7_MP_MMCI_BASE
146 #define VE_MP_KMI0_BASE                    VE_A7_MP_KMI0_BASE
147 #define VE_MP_UART_BASE                    VE_A7_MP_UART_BASE
148 #define VE_MP_WDT_BASE                     VE_A7_MP_WDT_BASE
149 #define VE_MP_TIMER_BASE                   VE_A7_MP_TIMER_BASE
150 #define VE_MP_DVI_BASE                     VE_A7_MP_DVI_BASE
151 #define VE_MP_RTC_BASE                     VE_A7_MP_RTC_BASE
152 #define VE_MP_UART4_BASE                   VE_A7_MP_UART4_BASE
153 #define VE_MP_CLCD_BASE                    VE_A7_MP_CLCD_BASE
154 #define VE_MP_GIC_DISTRIBUTOR_BASE         VE_A7_MP_GIC_DISTRIBUTOR_BASE
155 #define VE_MP_GIC_INTERFACE_BASE           VE_A7_MP_GIC_INTERFACE_BASE
156 #define VE_MP_PRIVATE_TIMER                VE_A7_MP_PRIVATE_TIMER
157 #elif __CORTEX_A == 9
158 #define VE_MP_FLASH_BASE0                  VE_A9_MP_FLASH_BASE0
159 #define VE_MP_FLASH_BASE1                  VE_A9_MP_FLASH_BASE1
160 #define VE_MP_PERIPH_BASE                  VE_A9_MP_PERIPH_BASE
161 #define VE_MP_SRAM_BASE                    VE_A9_MP_SRAM_BASE
162 #define VE_MP_DRAM_BASE                    VE_A9_MP_DRAM_BASE
163 #define VE_MP_VRAM_BASE                    VE_A9_MP_VRAM_BASE
164 #define VE_MP_ETHERNET_BASE                VE_A9_MP_ETHERNET_BASE
165 #define VE_MP_USB_BASE                     VE_A9_MP_USB_BASE
166 #define VE_MP_DAP_BASE                     VE_A9_MP_DAP_BASE
167 #define VE_MP_SYSTEM_REG_BASE              VE_A9_MP_SYSTEM_REG_BASE
168 #define VE_MP_SERIAL_BASE                  VE_A9_MP_SERIAL_BASE
169 #define VE_MP_AACI_BASE                    VE_A9_MP_AACI_BASE
170 #define VE_MP_MMCI_BASE                    VE_A9_MP_MMCI_BASE
171 #define VE_MP_KMI0_BASE                    VE_A9_MP_KMI0_BASE
172 #define VE_MP_UART_BASE                    VE_A9_MP_UART_BASE
173 #define VE_MP_WDT_BASE                     VE_A9_MP_WDT_BASE
174 #define VE_MP_TIMER_BASE                   VE_A9_MP_TIMER_BASE
175 #define VE_MP_DVI_BASE                     VE_A9_MP_DVI_BASE
176 #define VE_MP_RTC_BASE                     VE_A9_MP_RTC_BASE
177 #define VE_MP_UART4_BASE                   VE_A9_MP_UART4_BASE
178 #define VE_MP_CLCD_BASE                    VE_A9_MP_CLCD_BASE
179 #define VE_MP_GIC_DISTRIBUTOR_BASE         VE_A9_MP_GIC_DISTRIBUTOR_BASE
180 #define VE_MP_GIC_INTERFACE_BASE           VE_A9_MP_GIC_INTERFACE_BASE
181 #define VE_MP_PRIVATE_TIMER                VE_A9_MP_PRIVATE_TIMER
182 #endif
183
184 // L2 table pointers
185 //----------------------------------------
186 #define PRIVATE_TABLE_L2_BASE_4k       (0x80504000) //Map 4k Private Address space
187 #define SYNC_FLAGS_TABLE_L2_BASE_4k    (0x80504C00) //Map 4k Flag synchronization
188 #define PERIPHERAL_A_TABLE_L2_BASE_64k (0x80504400) //Map 64k Peripheral #1 0x1C000000 - 0x1C00FFFFF
189 #define PERIPHERAL_B_TABLE_L2_BASE_64k (0x80504800) //Map 64k Peripheral #2 0x1C100000 - 0x1C1FFFFFF
190
191 //--------------------- PERIPHERALS -------------------
192 #define PERIPHERAL_A_FAULT (0x00000000 + 0x1c000000) //0x1C000000-0x1C00FFFF (1M)
193 #define PERIPHERAL_B_FAULT (0x00100000 + 0x1c000000) //0x1C100000-0x1C10FFFF (1M)
194
195 //--------------------- SYNC FLAGS --------------------
196 #define FLAG_SYNC     0xFFFFF000
197 #define F_SYNC_BASE   0xFFF00000  //1M aligned
198
199 static uint32_t Sect_Normal;     //outer & inner wb/wa, non-shareable, executable, rw, domain 0, base addr 0
200 static uint32_t Sect_Normal_Cod; //outer & inner wb/wa, non-shareable, executable, ro, domain 0, base addr 0
201 static uint32_t Sect_Normal_RO;  //as Sect_Normal_Cod, but not executable
202 static uint32_t Sect_Normal_RW;  //as Sect_Normal_Cod, but writeable and not executable
203 static uint32_t Sect_Device_RO;  //device, non-shareable, non-executable, ro, domain 0, base addr 0
204 static uint32_t Sect_Device_RW;  //as Sect_Device_RO, but writeable
205
206 /* Define global descriptors */
207 static uint32_t Page_L1_4k  = 0x0;  //generic
208 static uint32_t Page_L1_64k = 0x0;  //generic
209 static uint32_t Page_4k_Device_RW;  //Shared device, not executable, rw, domain 0
210 static uint32_t Page_64k_Device_RW; //Shared device, not executable, rw, domain 0
211
212 void MMU_CreateTranslationTable(void)
213 {
214     mmu_region_attributes_Type region;
215
216     /*
217      * Generate descriptors. Refer to core_ca.h to get information about attributes
218      *
219      */
220     //Create descriptors for Vectors, RO, RW, ZI sections
221     section_normal(Sect_Normal, region);
222     section_normal_cod(Sect_Normal_Cod, region);
223     section_normal_ro(Sect_Normal_RO, region);
224     section_normal_rw(Sect_Normal_RW, region);
225     //Create descriptors for peripherals
226     section_device_ro(Sect_Device_RO, region);
227     section_device_rw(Sect_Device_RW, region);
228     //Create descriptors for 64k pages
229     page64k_device_rw(Page_L1_64k, Page_64k_Device_RW, region);
230     //Create descriptors for 4k pages
231     page4k_device_rw(Page_L1_4k, Page_4k_Device_RW, region);
232
233
234     /*
235      *  Define MMU flat-map regions and attributes
236      *
237      */
238     static uint32_t *const ttb = (uint32_t*)__TTB_BASE;
239      
240     //Create 4GB of normal entries
241     MMU_TTSection (ttb, 0, 4096, Sect_Normal);
242
243     //Define Image
244     MMU_TTSection (ttb, __ROM_BASE, __ROM_SIZE/0x00100000, Sect_Normal_Cod);
245     MMU_TTSection (ttb, __RAM_BASE, __RAM_SIZE/0x00100000, Sect_Normal_RW);
246
247     //--------------------- PERIPHERALS -------------------
248     MMU_TTSection (ttb, VE_MP_FLASH_BASE0    , 64, Sect_Device_RO);
249     MMU_TTSection (ttb, VE_MP_FLASH_BASE1    , 64, Sect_Device_RO);
250     MMU_TTSection (ttb, VE_MP_SRAM_BASE      , 64, Sect_Device_RW);
251     MMU_TTSection (ttb, VE_MP_VRAM_BASE      , 32, Sect_Device_RW);
252     MMU_TTSection (ttb, VE_MP_ETHERNET_BASE  , 16, Sect_Device_RW);
253     MMU_TTSection (ttb, VE_MP_USB_BASE       , 16, Sect_Device_RW);
254
255     // Create (16 * 64k)=1MB faulting entries to cover peripheral range 0x1C000000-0x1C00FFFF
256     MMU_TTPage64k(ttb, PERIPHERAL_A_FAULT      , 16, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, DESCRIPTOR_FAULT);
257     // Define peripheral range 0x1C000000-0x1C00FFFF
258     MMU_TTPage64k(ttb, VE_MP_DAP_BASE       ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
259     MMU_TTPage64k(ttb, VE_MP_SYSTEM_REG_BASE,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
260     MMU_TTPage64k(ttb, VE_MP_SERIAL_BASE    ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
261     MMU_TTPage64k(ttb, VE_MP_AACI_BASE      ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
262     MMU_TTPage64k(ttb, VE_MP_MMCI_BASE      ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
263     MMU_TTPage64k(ttb, VE_MP_KMI0_BASE      ,  2, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
264     MMU_TTPage64k(ttb, VE_MP_UART_BASE      ,  4, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
265     MMU_TTPage64k(ttb, VE_MP_WDT_BASE       ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
266
267     // Create (16 * 64k)=1MB faulting entries to cover peripheral range 0x1C100000-0x1C10FFFF
268     MMU_TTPage64k(ttb, PERIPHERAL_B_FAULT      , 16, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, DESCRIPTOR_FAULT);
269     // Define peripheral range 0x1C100000-0x1C10FFFF
270     MMU_TTPage64k(ttb, VE_MP_TIMER_BASE     ,  2, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
271     MMU_TTPage64k(ttb, VE_MP_DVI_BASE       ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
272     MMU_TTPage64k(ttb, VE_MP_RTC_BASE       ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
273     MMU_TTPage64k(ttb, VE_MP_UART4_BASE     ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
274     MMU_TTPage64k(ttb, VE_MP_CLCD_BASE      ,  1, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
275
276     // Create (256 * 4k)=1MB faulting entries to cover private address space. Needs to be marked as Device memory
277     MMU_TTPage4k (ttb, __get_CBAR()            ,256,  Page_L1_4k, (uint32_t *)PRIVATE_TABLE_L2_BASE_4k, DESCRIPTOR_FAULT);
278     // Define private address space entry.
279     MMU_TTPage4k (ttb, __get_CBAR()            ,  3,  Page_L1_4k, (uint32_t *)PRIVATE_TABLE_L2_BASE_4k, Page_4k_Device_RW);
280     // Define L2CC entry.  Uncomment if PL310 is present
281     //    MMU_TTPage4k (ttb, VE_A9_MP_PL310_BASE     ,  1,  Page_L1_4k, (uint32_t *)PRIVATE_TABLE_L2_BASE_4k, Page_4k_Device_RW);
282
283     // Create (256 * 4k)=1MB faulting entries to synchronization space (Useful if some non-cacheable DMA agent is present in the SoC)
284     MMU_TTPage4k (ttb, F_SYNC_BASE , 256, Page_L1_4k, (uint32_t *)SYNC_FLAGS_TABLE_L2_BASE_4k, DESCRIPTOR_FAULT);
285     // Define synchronization space entry.
286     MMU_TTPage4k (ttb, FLAG_SYNC   ,   1, Page_L1_4k, (uint32_t *)SYNC_FLAGS_TABLE_L2_BASE_4k, Page_4k_Device_RW);
287
288     /* Set location of level 1 page table
289     ; 31:14 - Translation table base addr (31:14-TTBCR.N, TTBCR.N is 0 out of reset)
290     ; 13:7  - 0x0
291     ; 6     - IRGN[0] 0x1  (Inner WB WA)
292     ; 5     - NOS     0x0  (Non-shared)
293     ; 4:3   - RGN     0x01 (Outer WB WA)
294     ; 2     - IMP     0x0  (Implementation Defined)
295     ; 1     - S       0x0  (Non-shared)
296     ; 0     - IRGN[1] 0x0  (Inner WB WA) */
297     __set_TTBR0(__TTB_BASE | 0x48);
298     __ISB();
299
300     /* Set up domain access control register
301     ; We set domain 0 to Client and all other domains to No Access.
302     ; All translation table entries specify domain 0 */
303     __set_DACR(1);
304     __ISB();
305 }