nvtboot_mem.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2016-2018, NVIDIA CORPORATION.
  4. */
  5. #include <common.h>
  6. #include <fdt_support.h>
  7. #include <fdtdec.h>
  8. #include <asm/arch/tegra.h>
  9. #include <asm/armv8/mmu.h>
  10. #define SZ_4G 0x100000000ULL
  11. /*
  12. * Size of a region that's large enough to hold the relocated U-Boot and all
  13. * other allocations made around it (stack, heap, page tables, etc.)
  14. * In practice, running "bdinfo" at the shell prompt, the stack reaches about
  15. * 5MB from the address selected for ram_top as of the time of writing,
  16. * so a 16MB region should be plenty.
  17. */
  18. #define MIN_USABLE_RAM_SIZE SZ_16M
  19. /*
  20. * The amount of space we expect to require for stack usage. Used to validate
  21. * that all reservations fit into the region selected for the relocation target
  22. */
  23. #define MIN_USABLE_STACK_SIZE SZ_1M
  24. DECLARE_GLOBAL_DATA_PTR;
  25. extern unsigned long nvtboot_boot_x0;
  26. extern struct mm_region tegra_mem_map[];
  27. /*
  28. * These variables are written to before relocation, and hence cannot be
  29. * in.bss, since .bss overlaps the DTB that's appended to the U-Boot binary.
  30. * The section attribute forces this into .data and avoids this issue. This
  31. * also has the nice side-effect of the content being valid after relocation.
  32. */
  33. /* The number of valid entries in ram_banks[] */
  34. static int ram_bank_count __attribute__((section(".data")));
  35. /*
  36. * The usable top-of-RAM for U-Boot. This is both:
  37. * a) Below 4GB to avoid issues with peripherals that use 32-bit addressing.
  38. * b) At the end of a region that has enough space to hold the relocated U-Boot
  39. * and all other allocations made around it (stack, heap, page tables, etc.)
  40. */
  41. static u64 ram_top __attribute__((section(".data")));
  42. /* The base address of the region of RAM that ends at ram_top */
  43. static u64 region_base __attribute__((section(".data")));
  44. int dram_init(void)
  45. {
  46. unsigned int na, ns;
  47. const void *nvtboot_blob = (void *)nvtboot_boot_x0;
  48. int node, len, i;
  49. const u32 *prop;
  50. na = fdtdec_get_uint(nvtboot_blob, 0, "#address-cells", 2);
  51. ns = fdtdec_get_uint(nvtboot_blob, 0, "#size-cells", 2);
  52. node = fdt_path_offset(nvtboot_blob, "/memory");
  53. if (node < 0) {
  54. pr_err("Can't find /memory node in nvtboot DTB");
  55. hang();
  56. }
  57. prop = fdt_getprop(nvtboot_blob, node, "reg", &len);
  58. if (!prop) {
  59. pr_err("Can't find /memory/reg property in nvtboot DTB");
  60. hang();
  61. }
  62. /* Calculate the true # of base/size pairs to read */
  63. len /= 4; /* Convert bytes to number of cells */
  64. len /= (na + ns); /* Convert cells to number of banks */
  65. if (len > CONFIG_NR_DRAM_BANKS)
  66. len = CONFIG_NR_DRAM_BANKS;
  67. /* Parse the /memory node, and save useful entries */
  68. gd->ram_size = 0;
  69. ram_bank_count = 0;
  70. for (i = 0; i < len; i++) {
  71. u64 bank_start, bank_end, bank_size, usable_bank_size;
  72. /* Extract raw memory region data from DTB */
  73. bank_start = fdt_read_number(prop, na);
  74. prop += na;
  75. bank_size = fdt_read_number(prop, ns);
  76. prop += ns;
  77. gd->ram_size += bank_size;
  78. bank_end = bank_start + bank_size;
  79. debug("Bank %d: %llx..%llx (+%llx)\n", i,
  80. bank_start, bank_end, bank_size);
  81. /*
  82. * Align the bank to MMU section size. This is not strictly
  83. * necessary, since the translation table construction code
  84. * handles page granularity without issue. However, aligning
  85. * the MMU entries reduces the size and number of levels in the
  86. * page table, so is worth it.
  87. */
  88. bank_start = ROUND(bank_start, SZ_2M);
  89. bank_end = bank_end & ~(SZ_2M - 1);
  90. bank_size = bank_end - bank_start;
  91. debug(" aligned: %llx..%llx (+%llx)\n",
  92. bank_start, bank_end, bank_size);
  93. if (bank_end <= bank_start)
  94. continue;
  95. /* Record data used to create MMU translation tables */
  96. ram_bank_count++;
  97. /* Index below is deliberately 1-based to skip MMIO entry */
  98. tegra_mem_map[ram_bank_count].virt = bank_start;
  99. tegra_mem_map[ram_bank_count].phys = bank_start;
  100. tegra_mem_map[ram_bank_count].size = bank_size;
  101. tegra_mem_map[ram_bank_count].attrs =
  102. PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_INNER_SHARE;
  103. /* Determine best bank to relocate U-Boot into */
  104. if (bank_end > SZ_4G)
  105. bank_end = SZ_4G;
  106. debug(" end %llx (usable)\n", bank_end);
  107. usable_bank_size = bank_end - bank_start;
  108. debug(" size %llx (usable)\n", usable_bank_size);
  109. if ((usable_bank_size >= MIN_USABLE_RAM_SIZE) &&
  110. (bank_end > ram_top)) {
  111. ram_top = bank_end;
  112. region_base = bank_start;
  113. debug("ram top now %llx\n", ram_top);
  114. }
  115. }
  116. /* Ensure memory map contains the desired sentinel entry */
  117. tegra_mem_map[ram_bank_count + 1].virt = 0;
  118. tegra_mem_map[ram_bank_count + 1].phys = 0;
  119. tegra_mem_map[ram_bank_count + 1].size = 0;
  120. tegra_mem_map[ram_bank_count + 1].attrs = 0;
  121. /* Error out if a relocation target couldn't be found */
  122. if (!ram_top) {
  123. pr_err("Can't find a usable RAM top");
  124. hang();
  125. }
  126. return 0;
  127. }
  128. int dram_init_banksize(void)
  129. {
  130. int i;
  131. if ((gd->start_addr_sp - region_base) < MIN_USABLE_STACK_SIZE) {
  132. pr_err("Reservations exceed chosen region size");
  133. hang();
  134. }
  135. for (i = 0; i < ram_bank_count; i++) {
  136. gd->bd->bi_dram[i].start = tegra_mem_map[1 + i].virt;
  137. gd->bd->bi_dram[i].size = tegra_mem_map[1 + i].size;
  138. }
  139. #ifdef CONFIG_PCI
  140. gd->pci_ram_top = ram_top;
  141. #endif
  142. return 0;
  143. }
  144. ulong board_get_usable_ram_top(ulong total_size)
  145. {
  146. return ram_top;
  147. }