| 1 | /*
|
|---|
| 2 | * Copyright (c) 2007 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 Bootstrap.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <arch/main.h>
|
|---|
| 37 | #include <arch/types.h>
|
|---|
| 38 | #include <arch/asm.h>
|
|---|
| 39 | #include <arch/mm.h>
|
|---|
| 40 | #include <halt.h>
|
|---|
| 41 | #include <printf.h>
|
|---|
| 42 | #include <memstr.h>
|
|---|
| 43 | #include <version.h>
|
|---|
| 44 | #include <macros.h>
|
|---|
| 45 | #include <align.h>
|
|---|
| 46 | #include <stdbool.h>
|
|---|
| 47 | #include <str.h>
|
|---|
| 48 | #include <errno.h>
|
|---|
| 49 | #include <inflate.h>
|
|---|
| 50 | #include <arch/cp15.h>
|
|---|
| 51 | #include <payload.h>
|
|---|
| 52 | #include <kernel.h>
|
|---|
| 53 |
|
|---|
| 54 | static void clean_dcache_poc(void *address, size_t size)
|
|---|
| 55 | {
|
|---|
| 56 | const uintptr_t addr = (uintptr_t) address;
|
|---|
| 57 |
|
|---|
| 58 | #if !defined(PROCESSOR_ARCH_armv7_a)
|
|---|
| 59 | bool sep;
|
|---|
| 60 | if (MIDR_read() != CTR_read()) {
|
|---|
| 61 | sep = (CTR_read() & CTR_SEP_FLAG) == CTR_SEP_FLAG;
|
|---|
| 62 | } else {
|
|---|
| 63 | printf("Unknown cache type.\n");
|
|---|
| 64 | halt();
|
|---|
| 65 | }
|
|---|
| 66 | #endif
|
|---|
| 67 |
|
|---|
| 68 | for (uintptr_t a = ALIGN_DOWN(addr, CP15_C7_MVA_ALIGN); a < addr + size;
|
|---|
| 69 | a += CP15_C7_MVA_ALIGN) {
|
|---|
| 70 | #if defined(PROCESSOR_ARCH_armv7_a)
|
|---|
| 71 | DCCMVAC_write(a);
|
|---|
| 72 | #else
|
|---|
| 73 | if (sep)
|
|---|
| 74 | DCCMVA_write(a);
|
|---|
| 75 | else
|
|---|
| 76 | CCMVA_write(a);
|
|---|
| 77 | #endif
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | static bootinfo_t bootinfo;
|
|---|
| 82 |
|
|---|
| 83 | void bootstrap(void)
|
|---|
| 84 | {
|
|---|
| 85 | /* Enable MMU and caches */
|
|---|
| 86 | mmu_start();
|
|---|
| 87 | version_print();
|
|---|
| 88 |
|
|---|
| 89 | printf("Boot loader: %p -> %p\n", loader_start, loader_end);
|
|---|
| 90 | printf("\nMemory statistics\n");
|
|---|
| 91 | printf(" %p|%p: bootstrap stack\n", &boot_stack, &boot_stack);
|
|---|
| 92 | printf(" %p|%p: bootstrap page table\n", &boot_pt, &boot_pt);
|
|---|
| 93 | printf(" %p|%p: boot info structure\n", &bootinfo, &bootinfo);
|
|---|
| 94 | printf(" %p|%p: kernel entry point\n",
|
|---|
| 95 | (void *) PA2KA(BOOT_OFFSET), (void *) BOOT_OFFSET);
|
|---|
| 96 |
|
|---|
| 97 | // FIXME: Use the correct value.
|
|---|
| 98 | uint8_t *kernel_dest = (uint8_t *) BOOT_OFFSET;
|
|---|
| 99 | uint8_t *ram_end = kernel_dest + (1 << 24);
|
|---|
| 100 |
|
|---|
| 101 | extract_payload(&bootinfo.taskmap, kernel_dest, ram_end,
|
|---|
| 102 | PA2KA(kernel_dest), clean_dcache_poc);
|
|---|
| 103 |
|
|---|
| 104 | /* Flush PT too. We need this if we disable caches later */
|
|---|
| 105 | clean_dcache_poc(boot_pt, PTL0_ENTRIES * PTL0_ENTRY_SIZE);
|
|---|
| 106 |
|
|---|
| 107 | uintptr_t entry = check_kernel((void *) PA2KA(BOOT_OFFSET));
|
|---|
| 108 |
|
|---|
| 109 | printf("Booting the kernel...\n");
|
|---|
| 110 | jump_to_kernel((void *) entry, &bootinfo);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /** @}
|
|---|
| 114 | */
|
|---|