Mark Kettenis | 003b657 | 2021-10-23 16:58:03 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2021 Mark Kettenis <kettenis@openbsd.org> |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <efi_loader.h> |
| 9 | |
| 10 | #include <asm/armv8/mmu.h> |
| 11 | #include <asm/global_data.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <asm/system.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Mark Kettenis | a89fea7 | 2022-02-08 22:00:09 +0100 | [diff] [blame] | 17 | /* Apple M1 */ |
| 18 | |
| 19 | static struct mm_region t8103_mem_map[] = { |
Mark Kettenis | 003b657 | 2021-10-23 16:58:03 +0200 | [diff] [blame] | 20 | { |
| 21 | /* I/O */ |
| 22 | .virt = 0x200000000, |
| 23 | .phys = 0x200000000, |
Mark Kettenis | a89fea7 | 2022-02-08 22:00:09 +0100 | [diff] [blame] | 24 | .size = 2UL * SZ_1G, |
| 25 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 26 | PTE_BLOCK_NON_SHARE | |
| 27 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 28 | }, { |
| 29 | /* I/O */ |
| 30 | .virt = 0x380000000, |
| 31 | .phys = 0x380000000, |
| 32 | .size = SZ_1G, |
Mark Kettenis | 003b657 | 2021-10-23 16:58:03 +0200 | [diff] [blame] | 33 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 34 | PTE_BLOCK_NON_SHARE | |
| 35 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 36 | }, { |
| 37 | /* I/O */ |
| 38 | .virt = 0x500000000, |
| 39 | .phys = 0x500000000, |
Mark Kettenis | a89fea7 | 2022-02-08 22:00:09 +0100 | [diff] [blame] | 40 | .size = SZ_1G, |
Mark Kettenis | 003b657 | 2021-10-23 16:58:03 +0200 | [diff] [blame] | 41 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 42 | PTE_BLOCK_NON_SHARE | |
| 43 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 44 | }, { |
| 45 | /* I/O */ |
| 46 | .virt = 0x680000000, |
| 47 | .phys = 0x680000000, |
| 48 | .size = SZ_512M, |
| 49 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 50 | PTE_BLOCK_NON_SHARE | |
| 51 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 52 | }, { |
| 53 | /* PCIE */ |
| 54 | .virt = 0x6a0000000, |
| 55 | .phys = 0x6a0000000, |
| 56 | .size = SZ_512M, |
| 57 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRE) | |
| 58 | PTE_BLOCK_INNER_SHARE | |
| 59 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 60 | }, { |
| 61 | /* PCIE */ |
| 62 | .virt = 0x6c0000000, |
| 63 | .phys = 0x6c0000000, |
| 64 | .size = SZ_1G, |
| 65 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRE) | |
| 66 | PTE_BLOCK_INNER_SHARE | |
| 67 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 68 | }, { |
| 69 | /* RAM */ |
| 70 | .virt = 0x800000000, |
| 71 | .phys = 0x800000000, |
| 72 | .size = 8UL * SZ_1G, |
| 73 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 74 | PTE_BLOCK_INNER_SHARE |
| 75 | }, { |
Mark Kettenis | a89fea7 | 2022-02-08 22:00:09 +0100 | [diff] [blame] | 76 | /* Framebuffer */ |
| 77 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL_NC) | |
| 78 | PTE_BLOCK_INNER_SHARE | |
| 79 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
Mark Kettenis | 003b657 | 2021-10-23 16:58:03 +0200 | [diff] [blame] | 80 | }, { |
| 81 | /* List terminator */ |
| 82 | 0, |
| 83 | } |
| 84 | }; |
| 85 | |
Mark Kettenis | a89fea7 | 2022-02-08 22:00:09 +0100 | [diff] [blame] | 86 | /* Apple M1 Pro/Max */ |
| 87 | |
| 88 | static struct mm_region t6000_mem_map[] = { |
| 89 | { |
| 90 | /* I/O */ |
| 91 | .virt = 0x280000000, |
| 92 | .phys = 0x280000000, |
| 93 | .size = SZ_1G, |
| 94 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 95 | PTE_BLOCK_NON_SHARE | |
| 96 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 97 | }, { |
| 98 | /* I/O */ |
| 99 | .virt = 0x380000000, |
| 100 | .phys = 0x380000000, |
| 101 | .size = SZ_1G, |
| 102 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 103 | PTE_BLOCK_NON_SHARE | |
| 104 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 105 | }, { |
| 106 | /* I/O */ |
| 107 | .virt = 0x580000000, |
| 108 | .phys = 0x580000000, |
| 109 | .size = SZ_512M, |
| 110 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 111 | PTE_BLOCK_NON_SHARE | |
| 112 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 113 | }, { |
| 114 | /* PCIE */ |
| 115 | .virt = 0x5a0000000, |
| 116 | .phys = 0x5a0000000, |
| 117 | .size = SZ_512M, |
| 118 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRE) | |
| 119 | PTE_BLOCK_INNER_SHARE | |
| 120 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 121 | }, { |
| 122 | /* PCIE */ |
| 123 | .virt = 0x5c0000000, |
| 124 | .phys = 0x5c0000000, |
| 125 | .size = SZ_1G, |
| 126 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRE) | |
| 127 | PTE_BLOCK_INNER_SHARE | |
| 128 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 129 | }, { |
| 130 | /* I/O */ |
| 131 | .virt = 0x700000000, |
| 132 | .phys = 0x700000000, |
| 133 | .size = SZ_1G, |
| 134 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 135 | PTE_BLOCK_NON_SHARE | |
| 136 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 137 | }, { |
| 138 | /* I/O */ |
| 139 | .virt = 0xb00000000, |
| 140 | .phys = 0xb00000000, |
| 141 | .size = SZ_1G, |
| 142 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 143 | PTE_BLOCK_NON_SHARE | |
| 144 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 145 | }, { |
| 146 | /* I/O */ |
| 147 | .virt = 0xf00000000, |
| 148 | .phys = 0xf00000000, |
| 149 | .size = SZ_1G, |
| 150 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 151 | PTE_BLOCK_NON_SHARE | |
| 152 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 153 | }, { |
| 154 | /* I/O */ |
| 155 | .virt = 0x1300000000, |
| 156 | .phys = 0x1300000000, |
| 157 | .size = SZ_1G, |
| 158 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 159 | PTE_BLOCK_NON_SHARE | |
| 160 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 161 | }, { |
| 162 | /* RAM */ |
| 163 | .virt = 0x10000000000, |
| 164 | .phys = 0x10000000000, |
| 165 | .size = 16UL * SZ_1G, |
| 166 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 167 | PTE_BLOCK_INNER_SHARE |
| 168 | }, { |
| 169 | /* Framebuffer */ |
| 170 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL_NC) | |
| 171 | PTE_BLOCK_INNER_SHARE | |
| 172 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 173 | }, { |
| 174 | /* List terminator */ |
| 175 | 0, |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | struct mm_region *mem_map; |
Mark Kettenis | 003b657 | 2021-10-23 16:58:03 +0200 | [diff] [blame] | 180 | |
| 181 | int board_init(void) |
| 182 | { |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int dram_init(void) |
| 187 | { |
Mark Kettenis | a89fea7 | 2022-02-08 22:00:09 +0100 | [diff] [blame] | 188 | return fdtdec_setup_mem_size_base(); |
Mark Kettenis | 003b657 | 2021-10-23 16:58:03 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | int dram_init_banksize(void) |
| 192 | { |
| 193 | return fdtdec_setup_memory_banksize(); |
| 194 | } |
| 195 | |
Mark Kettenis | 003b657 | 2021-10-23 16:58:03 +0200 | [diff] [blame] | 196 | extern long fw_dtb_pointer; |
| 197 | |
| 198 | void *board_fdt_blob_setup(int *err) |
| 199 | { |
| 200 | /* Return DTB pointer passed by m1n1 */ |
| 201 | *err = 0; |
| 202 | return (void *)fw_dtb_pointer; |
| 203 | } |
| 204 | |
Mark Kettenis | a89fea7 | 2022-02-08 22:00:09 +0100 | [diff] [blame] | 205 | void build_mem_map(void) |
Mark Kettenis | 003b657 | 2021-10-23 16:58:03 +0200 | [diff] [blame] | 206 | { |
Mark Kettenis | a89fea7 | 2022-02-08 22:00:09 +0100 | [diff] [blame] | 207 | ofnode node; |
| 208 | fdt_addr_t base; |
| 209 | fdt_size_t size; |
| 210 | int i; |
| 211 | |
| 212 | if (of_machine_is_compatible("apple,t8103")) |
| 213 | mem_map = t8103_mem_map; |
| 214 | else if (of_machine_is_compatible("apple,t6000")) |
| 215 | mem_map = t6000_mem_map; |
| 216 | else if (of_machine_is_compatible("apple,t6001")) |
| 217 | mem_map = t6000_mem_map; |
| 218 | else |
| 219 | panic("Unsupported SoC\n"); |
| 220 | |
| 221 | /* Find list terminator. */ |
| 222 | for (i = 0; mem_map[i].size || mem_map[i].attrs; i++) |
| 223 | ; |
| 224 | |
| 225 | /* Align RAM mapping to page boundaries */ |
| 226 | base = gd->bd->bi_dram[0].start; |
| 227 | size = gd->bd->bi_dram[0].size; |
| 228 | size += (base - ALIGN_DOWN(base, SZ_4K)); |
| 229 | base = ALIGN_DOWN(base, SZ_4K); |
| 230 | size = ALIGN(size, SZ_4K); |
| 231 | |
| 232 | /* Update RAM mapping */ |
| 233 | mem_map[i - 2].virt = base; |
| 234 | mem_map[i - 2].phys = base; |
| 235 | mem_map[i - 2].size = size; |
| 236 | |
| 237 | node = ofnode_path("/chosen/framebuffer"); |
| 238 | if (!ofnode_valid(node)) |
| 239 | return; |
| 240 | |
| 241 | base = ofnode_get_addr_size(node, "reg", &size); |
| 242 | if (base == FDT_ADDR_T_NONE) |
| 243 | return; |
| 244 | |
| 245 | /* Align framebuffer mapping to page boundaries */ |
| 246 | size += (base - ALIGN_DOWN(base, SZ_4K)); |
| 247 | base = ALIGN_DOWN(base, SZ_4K); |
| 248 | size = ALIGN(size, SZ_4K); |
| 249 | |
| 250 | /* Add framebuffer mapping */ |
| 251 | mem_map[i - 1].virt = base; |
| 252 | mem_map[i - 1].phys = base; |
| 253 | mem_map[i - 1].size = size; |
| 254 | } |
| 255 | |
| 256 | void enable_caches(void) |
| 257 | { |
| 258 | build_mem_map(); |
| 259 | |
| 260 | icache_enable(); |
| 261 | dcache_enable(); |
| 262 | } |
| 263 | |
| 264 | u64 get_page_table_size(void) |
| 265 | { |
| 266 | return SZ_256K; |
Mark Kettenis | 003b657 | 2021-10-23 16:58:03 +0200 | [diff] [blame] | 267 | } |
Janne Grunau | 6fb4f73 | 2022-02-19 14:05:19 +0100 | [diff] [blame] | 268 | |
| 269 | int board_late_init(void) |
| 270 | { |
| 271 | unsigned long base; |
| 272 | unsigned long top; |
| 273 | u32 status = 0; |
| 274 | |
| 275 | /* Reserve 4M each for scriptaddr and pxefile_addr_r at the top of RAM |
| 276 | * at least 1M below the stack. |
| 277 | */ |
| 278 | top = gd->start_addr_sp - CONFIG_STACK_SIZE - SZ_8M - SZ_1M; |
| 279 | top = ALIGN_DOWN(top, SZ_8M); |
| 280 | |
| 281 | status |= env_set_hex("scriptaddr", top + SZ_4M); |
| 282 | status |= env_set_hex("pxefile_addr_r", top); |
| 283 | |
| 284 | /* somewhat based on the Linux Kernel boot requirements: |
| 285 | * align by 2M and maximal FDT size 2M |
| 286 | */ |
| 287 | base = ALIGN(gd->ram_base, SZ_2M); |
| 288 | |
| 289 | status |= env_set_hex("fdt_addr_r", base); |
| 290 | status |= env_set_hex("kernel_addr_r", base + SZ_2M); |
| 291 | status |= env_set_hex("ramdisk_addr_r", base + SZ_128M); |
| 292 | status |= env_set_hex("loadaddr", base + SZ_2G); |
| 293 | status |= env_set_hex("kernel_comp_addr_r", base + SZ_2G - SZ_128M); |
| 294 | status |= env_set_hex("kernel_comp_size", SZ_128M); |
| 295 | |
| 296 | if (status) |
| 297 | log_warning("late_init: Failed to set run time variables\n"); |
| 298 | |
| 299 | return 0; |
| 300 | } |