Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <debug.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <compiler.h> |
| 26 | #include <arch.h> |
| 27 | #include <arch/arm.h> |
| 28 | #include <arch/arm/mmu.h> |
| 29 | |
| 30 | #if ARM_WITH_MMU |
| 31 | |
| 32 | #define MB (1024*1024) |
| 33 | |
| 34 | /* the location of the table may be brought in from outside */ |
| 35 | #if WITH_EXTERNAL_TRANSLATION_TABLE |
| 36 | #if !defined(MMU_TRANSLATION_TABLE_ADDR) |
| 37 | #error must set MMU_TRANSLATION_TABLE_ADDR in the make configuration |
| 38 | #endif |
| 39 | static uint32_t *tt = (void *)MMU_TRANSLATION_TABLE_ADDR; |
| 40 | #else |
| 41 | /* the main translation table */ |
| 42 | static uint32_t tt[4096] __ALIGNED(16384); |
| 43 | #endif |
| 44 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 45 | void arm_mmu_map_section(addr_t paddr, addr_t vaddr, uint flags) |
| 46 | { |
| 47 | int index; |
Brian Swetland | eceda41 | 2008-09-07 02:48:41 -0700 | [diff] [blame] | 48 | |
Amol Jadi | 9ef9b73 | 2011-05-23 16:00:17 -0700 | [diff] [blame] | 49 | /* Get the index into the translation table */ |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 50 | index = vaddr / MB; |
Amol Jadi | 9ef9b73 | 2011-05-23 16:00:17 -0700 | [diff] [blame] | 51 | |
| 52 | /* Set the entry value: |
| 53 | * (2<<0): Section entry |
| 54 | * (0<<5): Domain = 0 |
| 55 | * flags: TEX, CB and AP bit settings provided by the caller. |
| 56 | */ |
| 57 | tt[index] = (paddr & ~(MB-1)) | (0<<5) | (2<<0) | flags; |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 58 | |
| 59 | arm_invalidate_tlb(); |
| 60 | } |
| 61 | |
| 62 | void arm_mmu_init(void) |
| 63 | { |
| 64 | int i; |
| 65 | |
Amol Jadi | 9ef9b73 | 2011-05-23 16:00:17 -0700 | [diff] [blame] | 66 | /* set some mmu specific control bits: |
| 67 | * access flag disabled, TEX remap disabled, mmu disabled |
| 68 | */ |
| 69 | arm_write_cr1(arm_read_cr1() & ~((1<<29)|(1<<28)|(1<<0))); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 70 | |
Deepa Dinamani | 20cb9e5 | 2012-10-18 12:20:47 -0700 | [diff] [blame] | 71 | if (platform_use_identity_mmu_mappings()) |
| 72 | { |
| 73 | /* set up an identity-mapped translation table with |
| 74 | * strongly ordered memory type and read/write access. |
| 75 | */ |
| 76 | for (i=0; i < 4096; i++) { |
| 77 | arm_mmu_map_section(i * MB, |
| 78 | i * MB, |
| 79 | MMU_MEMORY_TYPE_STRONGLY_ORDERED | MMU_MEMORY_AP_READ_WRITE); |
| 80 | } |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Neeti Desai | 13e688d | 2012-08-22 16:30:55 -0700 | [diff] [blame] | 83 | platform_init_mmu_mappings(); |
| 84 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 85 | /* set up the translation table base */ |
| 86 | arm_write_ttbr((uint32_t)tt); |
| 87 | |
| 88 | /* set up the domain access register */ |
| 89 | arm_write_dacr(0x00000001); |
| 90 | |
| 91 | /* turn on the mmu */ |
| 92 | arm_write_cr1(arm_read_cr1() | 0x1); |
| 93 | } |
| 94 | |
| 95 | void arch_disable_mmu(void) |
| 96 | { |
Amol Jadi | 9ef9b73 | 2011-05-23 16:00:17 -0700 | [diff] [blame] | 97 | arm_write_cr1(arm_read_cr1() & ~(1<<0)); |
Deepa Dinamani | 28c0ffe | 2012-09-24 11:45:21 -0700 | [diff] [blame] | 98 | arm_invalidate_tlb(); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | #endif // ARM_WITH_MMU |