Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <arch.h> |
| 32 | #include <arch_helpers.h> |
| 33 | #include <assert.h> |
| 34 | #include <cassert.h> |
| 35 | #include <common_def.h> |
| 36 | #include <debug.h> |
| 37 | #include <errno.h> |
| 38 | #include <platform_def.h> |
| 39 | #include <string.h> |
| 40 | #include <types.h> |
| 41 | #include <utils.h> |
| 42 | #include <xlat_tables_v2.h> |
| 43 | #ifdef AARCH32 |
| 44 | # include "aarch32/xlat_tables_arch.h" |
| 45 | #else |
| 46 | # include "aarch64/xlat_tables_arch.h" |
| 47 | #endif |
| 48 | #include "xlat_tables_private.h" |
| 49 | |
| 50 | /* |
| 51 | * Private variables used by the TF |
| 52 | */ |
| 53 | static mmap_region_t tf_mmap[MAX_MMAP_REGIONS + 1]; |
| 54 | |
| 55 | static uint64_t tf_xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES] |
| 56 | __aligned(XLAT_TABLE_SIZE) __section("xlat_table"); |
| 57 | |
| 58 | static uint64_t tf_base_xlat_table[NUM_BASE_LEVEL_ENTRIES] |
| 59 | __aligned(NUM_BASE_LEVEL_ENTRIES * sizeof(uint64_t)); |
| 60 | |
| 61 | static mmap_region_t tf_mmap[MAX_MMAP_REGIONS + 1]; |
| 62 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 63 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 64 | static int xlat_tables_mapped_regions[MAX_XLAT_TABLES]; |
| 65 | #endif /* PLAT_XLAT_TABLES_DYNAMIC */ |
| 66 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 67 | xlat_ctx_t tf_xlat_ctx = { |
| 68 | |
| 69 | .pa_max_address = PLAT_PHY_ADDR_SPACE_SIZE - 1, |
| 70 | .va_max_address = PLAT_VIRT_ADDR_SPACE_SIZE - 1, |
| 71 | |
| 72 | .mmap = tf_mmap, |
| 73 | .mmap_num = MAX_MMAP_REGIONS, |
| 74 | |
| 75 | .tables = tf_xlat_tables, |
| 76 | .tables_num = MAX_XLAT_TABLES, |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 77 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 78 | .tables_mapped_regions = xlat_tables_mapped_regions, |
| 79 | #endif /* PLAT_XLAT_TABLES_DYNAMIC */ |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 80 | |
| 81 | .base_table = tf_base_xlat_table, |
| 82 | .base_table_entries = NUM_BASE_LEVEL_ENTRIES, |
| 83 | |
| 84 | .max_pa = 0, |
| 85 | .max_va = 0, |
| 86 | |
| 87 | .next_table = 0, |
| 88 | |
| 89 | .base_level = XLAT_TABLE_LEVEL_BASE, |
| 90 | |
| 91 | .initialized = 0 |
| 92 | }; |
| 93 | |
| 94 | void mmap_add_region(unsigned long long base_pa, uintptr_t base_va, |
| 95 | size_t size, unsigned int attr) |
| 96 | { |
| 97 | mmap_region_t mm = { |
| 98 | .base_va = base_va, |
| 99 | .base_pa = base_pa, |
| 100 | .size = size, |
| 101 | .attr = attr, |
| 102 | }; |
| 103 | mmap_add_region_ctx(&tf_xlat_ctx, (mmap_region_t *)&mm); |
| 104 | } |
| 105 | |
| 106 | void mmap_add(const mmap_region_t *mm) |
| 107 | { |
| 108 | while (mm->size) { |
| 109 | mmap_add_region_ctx(&tf_xlat_ctx, (mmap_region_t *)mm); |
| 110 | mm++; |
| 111 | } |
| 112 | } |
| 113 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 114 | #if PLAT_XLAT_TABLES_DYNAMIC |
| 115 | |
| 116 | int mmap_add_dynamic_region(unsigned long long base_pa, |
| 117 | uintptr_t base_va, size_t size, unsigned int attr) |
| 118 | { |
| 119 | mmap_region_t mm = { |
| 120 | .base_va = base_va, |
| 121 | .base_pa = base_pa, |
| 122 | .size = size, |
| 123 | .attr = attr, |
| 124 | }; |
| 125 | return mmap_add_dynamic_region_ctx(&tf_xlat_ctx, &mm); |
| 126 | } |
| 127 | |
| 128 | int mmap_remove_dynamic_region(uintptr_t base_va, size_t size) |
| 129 | { |
| 130 | return mmap_remove_dynamic_region_ctx(&tf_xlat_ctx, base_va, size); |
| 131 | } |
| 132 | |
| 133 | #endif /* PLAT_XLAT_TABLES_DYNAMIC */ |
| 134 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 135 | void init_xlat_tables(void) |
| 136 | { |
| 137 | assert(!is_mmu_enabled()); |
| 138 | assert(!tf_xlat_ctx.initialized); |
| 139 | print_mmap(tf_xlat_ctx.mmap); |
| 140 | init_xlation_table(&tf_xlat_ctx); |
| 141 | xlat_tables_print(&tf_xlat_ctx); |
| 142 | |
| 143 | assert(tf_xlat_ctx.max_va <= PLAT_VIRT_ADDR_SPACE_SIZE - 1); |
| 144 | assert(tf_xlat_ctx.max_pa <= PLAT_PHY_ADDR_SPACE_SIZE - 1); |
| 145 | |
| 146 | init_xlat_tables_arch(tf_xlat_ctx.max_pa); |
| 147 | } |
| 148 | |
| 149 | #ifdef AARCH32 |
| 150 | |
| 151 | void enable_mmu_secure(unsigned int flags) |
| 152 | { |
| 153 | enable_mmu_arch(flags, tf_xlat_ctx.base_table); |
| 154 | } |
| 155 | |
| 156 | #else |
| 157 | |
| 158 | void enable_mmu_el1(unsigned int flags) |
| 159 | { |
| 160 | enable_mmu_arch(flags, tf_xlat_ctx.base_table); |
| 161 | } |
| 162 | |
| 163 | void enable_mmu_el3(unsigned int flags) |
| 164 | { |
| 165 | enable_mmu_arch(flags, tf_xlat_ctx.base_table); |
| 166 | } |
| 167 | |
| 168 | #endif /* AARCH32 */ |