| [4872160] | 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 |
|
|---|
| [b5a3b50] | 40 | /** Check if caching can be enabled for a given memory section.
|
|---|
| 41 | *
|
|---|
| 42 | * Memory areas used for I/O are excluded from caching.
|
|---|
| 43 | * At the moment caching is enabled only on GTA02.
|
|---|
| 44 | *
|
|---|
| 45 | * @param section The section number.
|
|---|
| 46 | *
|
|---|
| 47 | * @return 1 if the given section can be mapped as cacheable, 0 otherwise.
|
|---|
| 48 | */
|
|---|
| 49 | static inline int section_cacheable(pfn_t section)
|
|---|
| 50 | {
|
|---|
| 51 | #ifdef MACHINE_gta02
|
|---|
| 52 | unsigned long address = section << PTE_SECTION_SHIFT;
|
|---|
| 53 |
|
|---|
| 54 | if (address >= GTA02_IOMEM_START && address < GTA02_IOMEM_END)
|
|---|
| 55 | return 0;
|
|---|
| 56 | else
|
|---|
| 57 | return 1;
|
|---|
| [0acd339] | 58 | #elif defined MACHINE_beagleboardxm
|
|---|
| 59 | const unsigned long address = section << PTE_SECTION_SHIFT;
|
|---|
| 60 | if (address >= BBXM_RAM_START && address < BBXM_RAM_END)
|
|---|
| [bfb6576] | 61 | return 1;
|
|---|
| [b5a3b50] | 62 | #endif
|
|---|
| [0acd339] | 63 | return 0;
|
|---|
| [b5a3b50] | 64 | }
|
|---|
| 65 |
|
|---|
| [4872160] | 66 | /** Initialize "section" page table entry.
|
|---|
| 67 | *
|
|---|
| 68 | * Will be readable/writable by kernel with no access from user mode.
|
|---|
| 69 | * Will belong to domain 0. No cache or buffering is enabled.
|
|---|
| 70 | *
|
|---|
| 71 | * @param pte Section entry to initialize.
|
|---|
| 72 | * @param frame First frame in the section (frame number).
|
|---|
| 73 | *
|
|---|
| 74 | * @note If frame is not 1 MB aligned, first lower 1 MB aligned frame will be
|
|---|
| 75 | * used.
|
|---|
| 76 | *
|
|---|
| 77 | */
|
|---|
| 78 | static void init_ptl0_section(pte_level0_section_t* pte,
|
|---|
| 79 | pfn_t frame)
|
|---|
| 80 | {
|
|---|
| 81 | pte->descriptor_type = PTE_DESCRIPTOR_SECTION;
|
|---|
| [4d02595] | 82 | pte->bufferable = 1;
|
|---|
| [b5a3b50] | 83 | pte->cacheable = section_cacheable(frame);
|
|---|
| [4d02595] | 84 | pte->xn = 0;
|
|---|
| [4872160] | 85 | pte->domain = 0;
|
|---|
| 86 | pte->should_be_zero_1 = 0;
|
|---|
| [4d02595] | 87 | pte->access_permission_0 = PTE_AP_USER_NO_KERNEL_RW;
|
|---|
| 88 | pte->tex = 0;
|
|---|
| 89 | pte->access_permission_1 = 0;
|
|---|
| 90 | pte->non_global = 0;
|
|---|
| [4872160] | 91 | pte->should_be_zero_2 = 0;
|
|---|
| [4d02595] | 92 | pte->non_secure = 0;
|
|---|
| [4872160] | 93 | pte->section_base_addr = frame;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /** Initialize page table used while booting the kernel. */
|
|---|
| 97 | static void init_boot_pt(void)
|
|---|
| 98 | {
|
|---|
| [fcc6224] | 99 | const pfn_t split_page = PTL0_ENTRIES;
|
|---|
| [4872160] | 100 | /* Create 1:1 virtual-physical mapping (in lower 2 GB). */
|
|---|
| 101 | pfn_t page;
|
|---|
| 102 | for (page = 0; page < split_page; page++)
|
|---|
| 103 | init_ptl0_section(&boot_pt[page], page);
|
|---|
| 104 |
|
|---|
| 105 | /*
|
|---|
| 106 | * Create 1:1 virtual-physical mapping in kernel space
|
|---|
| 107 | * (upper 2 GB), physical addresses start from 0.
|
|---|
| 108 | */
|
|---|
| [c6b601b] | 109 | /* BeagleBoard-xM (DM37x) memory starts at 2GB border,
|
|---|
| [4d02595] | 110 | * thus mapping only lower 2GB is not not enough.
|
|---|
| 111 | * Map entire AS 1:1 instead and hope it works. */
|
|---|
| [4872160] | 112 | for (page = split_page; page < PTL0_ENTRIES; page++)
|
|---|
| [4d02595] | 113 | #ifndef MACHINE_beagleboardxm
|
|---|
| [4872160] | 114 | init_ptl0_section(&boot_pt[page], page - split_page);
|
|---|
| [4d02595] | 115 | #else
|
|---|
| 116 | init_ptl0_section(&boot_pt[page], page);
|
|---|
| 117 | #endif
|
|---|
| [4872160] | 118 |
|
|---|
| 119 | asm volatile (
|
|---|
| 120 | "mcr p15, 0, %[pt], c2, c0, 0\n"
|
|---|
| 121 | :: [pt] "r" (boot_pt)
|
|---|
| 122 | );
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | static void enable_paging()
|
|---|
| 126 | {
|
|---|
| 127 | /* c3 - each two bits controls access to the one of domains (16)
|
|---|
| 128 | * 0b01 - behave as a client (user) of a domain
|
|---|
| 129 | */
|
|---|
| 130 | asm volatile (
|
|---|
| 131 | /* Behave as a client of domains */
|
|---|
| 132 | "ldr r0, =0x55555555\n"
|
|---|
| [6b3ee0c5] | 133 | "mcr p15, 0, r0, c3, c0, 0\n"
|
|---|
| [4872160] | 134 |
|
|---|
| 135 | /* Current settings */
|
|---|
| 136 | "mrc p15, 0, r0, c1, c0, 0\n"
|
|---|
| 137 |
|
|---|
| [df334ca] | 138 | /* Enable ICache, DCache, BPredictors and MMU,
|
|---|
| 139 | * we disable caches before jumping to kernel
|
|---|
| 140 | * so this is safe for all archs.
|
|---|
| 141 | */
|
|---|
| 142 | "ldr r1, =0x00001805\n"
|
|---|
| 143 |
|
|---|
| [4872160] | 144 | "orr r0, r0, r1\n"
|
|---|
| 145 |
|
|---|
| 146 | /* Store settings */
|
|---|
| 147 | "mcr p15, 0, r0, c1, c0, 0\n"
|
|---|
| 148 | ::: "r0", "r1"
|
|---|
| 149 | );
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /** Start the MMU - initialize page table and enable paging. */
|
|---|
| 153 | void mmu_start() {
|
|---|
| 154 | init_boot_pt();
|
|---|
| 155 | enable_paging();
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /** @}
|
|---|
| 159 | */
|
|---|