| 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 arm32boot
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief Memory management used while booting the kernel.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <typedefs.h>
|
|---|
| 37 | #include <arch/asm.h>
|
|---|
| 38 | #include <arch/mm.h>
|
|---|
| 39 |
|
|---|
| 40 | /** Disable the MMU */
|
|---|
| 41 | static void disable_paging(void)
|
|---|
| 42 | {
|
|---|
| 43 | asm volatile (
|
|---|
| 44 | "mrc p15, 0, r0, c1, c0, 0\n"
|
|---|
| 45 | "bic r0, r0, #1\n"
|
|---|
| 46 | "mcr p15, 0, r0, c1, c0, 0\n"
|
|---|
| 47 | ::: "r0"
|
|---|
| 48 | );
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /** Check if caching can be enabled for a given memory section.
|
|---|
| 52 | *
|
|---|
| 53 | * Memory areas used for I/O are excluded from caching.
|
|---|
| 54 | * At the moment caching is enabled only on GTA02.
|
|---|
| 55 | *
|
|---|
| 56 | * @param section The section number.
|
|---|
| 57 | *
|
|---|
| 58 | * @return 1 if the given section can be mapped as cacheable, 0 otherwise.
|
|---|
| 59 | */
|
|---|
| 60 | static inline int section_cacheable(pfn_t section)
|
|---|
| 61 | {
|
|---|
| 62 | #ifdef MACHINE_gta02
|
|---|
| 63 | unsigned long address = section << PTE_SECTION_SHIFT;
|
|---|
| 64 |
|
|---|
| 65 | if (address >= GTA02_IOMEM_START && address < GTA02_IOMEM_END)
|
|---|
| 66 | return 0;
|
|---|
| 67 | else
|
|---|
| 68 | return 1;
|
|---|
| 69 | #elif defined MACHINE_beagleboardxm
|
|---|
| 70 | const unsigned long address = section << PTE_SECTION_SHIFT;
|
|---|
| 71 | if (address >= BBXM_RAM_START && address < BBXM_RAM_END)
|
|---|
| 72 | return 1;
|
|---|
| 73 | #elif defined MACHINE_beaglebone
|
|---|
| 74 | const unsigned long address = section << PTE_SECTION_SHIFT;
|
|---|
| 75 | if (address >= AM335x_RAM_START && address < AM335x_RAM_END)
|
|---|
| 76 | return 1;
|
|---|
| 77 | #elif defined MACHINE_raspberrypi
|
|---|
| 78 | const unsigned long address = section << PTE_SECTION_SHIFT;
|
|---|
| 79 | if (address < BCM2835_RAM_END)
|
|---|
| 80 | return 1;
|
|---|
| 81 | #endif
|
|---|
| 82 | return 0;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /** Initialize "section" page table entry.
|
|---|
| 86 | *
|
|---|
| 87 | * Will be readable/writable by kernel with no access from user mode.
|
|---|
| 88 | * Will belong to domain 0. No cache or buffering is enabled.
|
|---|
| 89 | *
|
|---|
| 90 | * @param pte Section entry to initialize.
|
|---|
| 91 | * @param frame First frame in the section (frame number).
|
|---|
| 92 | *
|
|---|
| 93 | * @note If frame is not 1 MB aligned, first lower 1 MB aligned frame will be
|
|---|
| 94 | * used.
|
|---|
| 95 | *
|
|---|
| 96 | */
|
|---|
| 97 | static void init_ptl0_section(pte_level0_section_t* pte,
|
|---|
| 98 | pfn_t frame)
|
|---|
| 99 | {
|
|---|
| 100 | pte->descriptor_type = PTE_DESCRIPTOR_SECTION;
|
|---|
| 101 | pte->bufferable = 1;
|
|---|
| 102 | pte->cacheable = section_cacheable(frame);
|
|---|
| 103 | pte->xn = 0;
|
|---|
| 104 | pte->domain = 0;
|
|---|
| 105 | pte->should_be_zero_1 = 0;
|
|---|
| 106 | pte->access_permission_0 = PTE_AP_USER_NO_KERNEL_RW;
|
|---|
| 107 | pte->tex = 0;
|
|---|
| 108 | pte->access_permission_1 = 0;
|
|---|
| 109 | pte->shareable = 0;
|
|---|
| 110 | pte->non_global = 0;
|
|---|
| 111 | pte->should_be_zero_2 = 0;
|
|---|
| 112 | pte->non_secure = 0;
|
|---|
| 113 | pte->section_base_addr = frame;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /** Initialize page table used while booting the kernel. */
|
|---|
| 117 | static void init_boot_pt(void)
|
|---|
| 118 | {
|
|---|
| 119 | #if defined MACHINE_raspberrypi
|
|---|
| 120 | const pfn_t split_page = 2048;
|
|---|
| 121 | #else
|
|---|
| 122 | const pfn_t split_page = PTL0_ENTRIES;
|
|---|
| 123 | #endif
|
|---|
| 124 |
|
|---|
| 125 | /* Create 1:1 virtual-physical mapping (in lower 2 GB). */
|
|---|
| 126 | pfn_t page;
|
|---|
| 127 | for (page = 0; page < split_page; page++)
|
|---|
| 128 | init_ptl0_section(&boot_pt[page], page);
|
|---|
| 129 |
|
|---|
| 130 | #if defined MACHINE_raspberrypi
|
|---|
| 131 | for (; page < PTL0_ENTRIES; page++)
|
|---|
| 132 | init_ptl0_section(&boot_pt[page], page - split_page);
|
|---|
| 133 | #endif
|
|---|
| 134 | asm volatile (
|
|---|
| 135 | "mcr p15, 0, %[pt], c2, c0, 0\n"
|
|---|
| 136 | :: [pt] "r" (boot_pt)
|
|---|
| 137 | );
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | static void enable_paging()
|
|---|
| 141 | {
|
|---|
| 142 | /* c3 - each two bits controls access to the one of domains (16)
|
|---|
| 143 | * 0b01 - behave as a client (user) of a domain
|
|---|
| 144 | */
|
|---|
| 145 | asm volatile (
|
|---|
| 146 | /* Behave as a client of domains */
|
|---|
| 147 | "ldr r0, =0x55555555\n"
|
|---|
| 148 | "mcr p15, 0, r0, c3, c0, 0\n"
|
|---|
| 149 |
|
|---|
| 150 | /* Current settings */
|
|---|
| 151 | "mrc p15, 0, r0, c1, c0, 0\n"
|
|---|
| 152 |
|
|---|
| 153 | /* Enable ICache, DCache, BPredictors and MMU,
|
|---|
| 154 | * we disable caches before jumping to kernel
|
|---|
| 155 | * so this is safe for all archs.
|
|---|
| 156 | */
|
|---|
| 157 | "ldr r1, =0x00001805\n"
|
|---|
| 158 |
|
|---|
| 159 | "orr r0, r0, r1\n"
|
|---|
| 160 |
|
|---|
| 161 | /* Invalidate the TLB content before turning on the MMU.
|
|---|
| 162 | * ARMv7-A Reference manual, B3.10.3
|
|---|
| 163 | */
|
|---|
| 164 | "mcr p15, 0, r0, c8, c7, 0\n"
|
|---|
| 165 |
|
|---|
| 166 | /* Store settings, enable the MMU */
|
|---|
| 167 | "mcr p15, 0, r0, c1, c0, 0\n"
|
|---|
| 168 | ::: "r0", "r1"
|
|---|
| 169 | );
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /** Start the MMU - initialize page table and enable paging. */
|
|---|
| 173 | void mmu_start() {
|
|---|
| 174 | disable_paging();
|
|---|
| 175 | init_boot_pt();
|
|---|
| 176 | enable_paging();
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /** @}
|
|---|
| 180 | */
|
|---|