source: mainline/boot/arch/arm32/src/mm.c@ accdbd83

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since accdbd83 was accdbd83, checked in by Jakub Jermar <jakub@…>, 7 years ago

Create mappings also for the kernel

If needed, create also dedicated mappings for the kernel with proper
memory region attributes. At the same time, stop making assumptions
about various mirrors of the physical memory and consider only the
layout of the physical address space.

This commit fixes boot on RaspberryPi which stopped working due to
the switch to LDREX/STREX-based atomics, which do not work in memory
regions with wrong attributes.

Tested on: RaspberryPi, GTA02, bbone, bbxm and Integrator/CP

  • Property mode set to 100644
File size: 8.3 KB
Line 
1/*
2 * Copyright (c) 2007 Pavel Jancik, Michal Kebrt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup boot_arm32
30 * @{
31 */
32/** @file
33 * @brief Memory management used while booting the kernel.
34 */
35
36#include <stdint.h>
37#include <arch/asm.h>
38#include <arch/mm.h>
39#include <arch/cp15.h>
40#include <arch/types.h>
41
42#ifdef PROCESSOR_ARCH_armv7_a
43static unsigned log2(unsigned val)
44{
45 unsigned log = 0;
46 while (val >> log++)
47 ;
48 return log - 2;
49}
50
51static void dcache_invalidate_level(unsigned level)
52{
53 CSSELR_write(level << 1);
54 const uint32_t ccsidr = CCSIDR_read();
55 const unsigned sets = CCSIDR_SETS(ccsidr);
56 const unsigned ways = CCSIDR_WAYS(ccsidr);
57 const unsigned line_log = CCSIDR_LINESIZE_LOG(ccsidr);
58 const unsigned set_shift = line_log;
59 const unsigned way_shift = 32 - log2(ways);
60
61 for (unsigned k = 0; k < ways; ++k)
62 for (unsigned j = 0; j < sets; ++j) {
63 const uint32_t val = (level << 1) |
64 (j << set_shift) | (k << way_shift);
65 DCISW_write(val);
66 }
67}
68
69/** invalidate all dcaches -- armv7 */
70static void cache_invalidate(void)
71{
72 const uint32_t cinfo = CLIDR_read();
73 for (unsigned i = 0; i < 7; ++i) {
74 switch (CLIDR_CACHE(i, cinfo)) {
75 case CLIDR_DCACHE_ONLY:
76 case CLIDR_SEP_CACHE:
77 case CLIDR_UNI_CACHE:
78 dcache_invalidate_level(i);
79 }
80 }
81 asm volatile ("dsb\n");
82 ICIALLU_write(0);
83 asm volatile ("isb\n");
84}
85#endif
86
87/** Disable the MMU */
88static void disable_paging(void)
89{
90 asm volatile (
91 "mrc p15, 0, r0, c1, c0, 0\n"
92 "bic r0, r0, #1\n"
93 "mcr p15, 0, r0, c1, c0, 0\n"
94 ::: "r0"
95 );
96}
97
98/** Check if caching can be enabled for a given memory section.
99 *
100 * Memory areas used for I/O are excluded from caching.
101 *
102 * @param section The section number.
103 *
104 * @return 1 if the given section can be mapped as cacheable, 0 otherwise.
105 */
106static inline int section_cacheable(pfn_t section)
107{
108 const unsigned long address = section << PTE_SECTION_SHIFT;
109#ifdef MACHINE_gta02
110 if (address < GTA02_IOMEM_START || address >= GTA02_IOMEM_END)
111 return 1;
112#elif defined MACHINE_beagleboardxm
113 if (address >= BBXM_RAM_START && address < BBXM_RAM_END)
114 return 1;
115#elif defined MACHINE_beaglebone
116 if (address >= AM335x_RAM_START && address < AM335x_RAM_END)
117 return 1;
118#elif defined MACHINE_raspberrypi
119 if (address < BCM2835_RAM_END)
120 return 1;
121#endif
122 return address * 0;
123}
124
125/** Initialize "section" page table entry.
126 *
127 * Will be readable/writable by kernel with no access from user mode.
128 * Will belong to domain 0. No cache or buffering is enabled.
129 *
130 * @param pte Section entry to initialize.
131 * @param frame First frame in the section (frame number).
132 *
133 * @note If frame is not 1 MB aligned, first lower 1 MB aligned frame will be
134 * used.
135 *
136 */
137static void init_ptl0_section(pte_level0_section_t *pte,
138 pfn_t frame)
139{
140 pte->descriptor_type = PTE_DESCRIPTOR_SECTION;
141 pte->xn = 0;
142 pte->domain = 0;
143 pte->should_be_zero_1 = 0;
144 pte->access_permission_0 = PTE_AP_USER_NO_KERNEL_RW;
145#if defined(PROCESSOR_ARCH_armv6) || defined(PROCESSOR_ARCH_armv7_a)
146 /*
147 * Keeps this setting in sync with memory type attributes in:
148 * init_boot_pt (boot/arch/arm32/src/mm.c)
149 * set_pt_level1_flags (kernel/arch/arm32/include/arch/mm/page_armv6.h)
150 * set_ptl0_addr (kernel/arch/arm32/include/arch/mm/page.h)
151 */
152 pte->tex = section_cacheable(frame) ? 5 : 0;
153 pte->cacheable = section_cacheable(frame) ? 0 : 0;
154 pte->bufferable = section_cacheable(frame) ? 1 : 1;
155#else
156 pte->bufferable = section_cacheable(frame);
157 pte->cacheable = section_cacheable(frame);
158 pte->tex = 0;
159#endif
160 pte->access_permission_1 = 0;
161 pte->shareable = 0;
162 pte->non_global = 0;
163 pte->should_be_zero_2 = 0;
164 pte->non_secure = 0;
165 pte->section_base_addr = frame;
166}
167
168/** Initialize page table used while booting the kernel. */
169static void init_boot_pt(void)
170{
171 /*
172 * Our goal is to create page tables that serve two purposes:
173 *
174 * 1. Allow the loader to run in identity-mapped virtual memory and use
175 * I/O devices (e.g. an UART for logging).
176 * 2. Allow the kernel to start running in virtual memory from addresses
177 * above 2G.
178 *
179 * The matters are slightly complicated by the different locations of
180 * physical memory and I/O devices on the various boards that we
181 * support. We see two cases (but other are still possible):
182 *
183 * a) Both RAM and I/O is in memory below 2G
184 * For instance, this is the case of GTA02, Integrator/CP
185 * and RaspberryPi
186 * b) RAM starts at 2G and I/O devices are below 2G
187 * For example, this is the case of BeagleBone and BeagleBoard XM
188 *
189 * This leads to two possible setups of boot page tables:
190 *
191 * A) To arrange for a), split the virtual address space into two
192 * halves, both identity-mapping the first 2G of physical address
193 * space.
194 * B) To accommodate b), create one larger virtual address space
195 * identity-mapping the entire physical address space.
196 */
197
198 for (pfn_t page = 0; page < PTL0_ENTRIES; page++) {
199 pfn_t frame = page;
200 if (BOOT_BASE < 0x80000000UL && page >= PTL0_ENTRIES / 2)
201 frame -= PTL0_ENTRIES / 2;
202 init_ptl0_section(&boot_pt[page], frame);
203 }
204
205 /*
206 * Tell MMU page might be cached. Keeps this setting in sync
207 * with memory type attributes in:
208 * init_ptl0_section (boot/arch/arm32/src/mm.c)
209 * set_pt_level1_flags (kernel/arch/arm32/include/arch/mm/page_armv6.h)
210 * set_ptl0_addr (kernel/arch/arm32/include/arch/mm/page.h)
211 */
212 uint32_t val = (uint32_t)boot_pt & TTBR_ADDR_MASK;
213#if defined(PROCESSOR_ARCH_armv6) || defined(PROCESSOR_ARCH_armv7_a)
214 // FIXME: TTBR_RGN_WBWA_CACHE is unpredictable on ARMv6
215 val |= TTBR_RGN_WBWA_CACHE | TTBR_C_FLAG;
216#endif
217 TTBR0_write(val);
218}
219
220static void enable_paging(void)
221{
222 /*
223 * c3 - each two bits controls access to the one of domains (16)
224 * 0b01 - behave as a client (user) of a domain
225 */
226 asm volatile (
227 /* Behave as a client of domains */
228 "ldr r0, =0x55555555\n"
229 "mcr p15, 0, r0, c3, c0, 0\n"
230
231 /* Current settings */
232 "mrc p15, 0, r0, c1, c0, 0\n"
233
234 /*
235 * Enable ICache, DCache, BPredictors and MMU,
236 * we disable caches before jumping to kernel
237 * so this is safe for all archs.
238 * Enable VMSAv6 the bit (23) is only writable on ARMv6.
239 * (and QEMU)
240 */
241#ifdef PROCESSOR_ARCH_armv6
242 "ldr r1, =0x00801805\n"
243#else
244 "ldr r1, =0x00001805\n"
245#endif
246
247 "orr r0, r0, r1\n"
248
249 /*
250 * Invalidate the TLB content before turning on the MMU.
251 * ARMv7-A Reference manual, B3.10.3
252 */
253 "mcr p15, 0, r0, c8, c7, 0\n"
254
255 /* Store settings, enable the MMU */
256 "mcr p15, 0, r0, c1, c0, 0\n"
257 ::: "r0", "r1"
258 );
259}
260
261/** Start the MMU - initialize page table and enable paging. */
262void mmu_start(void)
263{
264 disable_paging();
265#ifdef PROCESSOR_ARCH_armv7_a
266 /*
267 * Make sure we run in memory code when caches are enabled,
268 * make sure we read memory data too. This part is ARMv7 specific as
269 * ARMv7 no longer invalidates caches on restart.
270 * See chapter B2.2.2 of ARM Architecture Reference Manual p. B2-1263
271 */
272 cache_invalidate();
273#endif
274 init_boot_pt();
275 enable_paging();
276}
277
278/** @}
279 */
Note: See TracBrowser for help on using the repository browser.