efi.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <debug_uart.h>
  9. #include <efi.h>
  10. #include <errno.h>
  11. #include <linux/err.h>
  12. #include <linux/types.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /*
  15. * This function looks for the highest region of memory lower than 4GB which
  16. * has enough space for U-Boot where U-Boot is aligned on a page boundary.
  17. * It overrides the default implementation found elsewhere which simply
  18. * picks the end of ram, wherever that may be. The location of the stack,
  19. * the relocation address, and how far U-Boot is moved by relocation are
  20. * set in the global data structure.
  21. */
  22. ulong board_get_usable_ram_top(ulong total_size)
  23. {
  24. struct efi_mem_desc *desc, *end;
  25. struct efi_entry_memmap *map;
  26. int ret, size;
  27. uintptr_t dest_addr = 0;
  28. struct efi_mem_desc *largest = NULL;
  29. /*
  30. * Find largest area of memory below 4GB. We could
  31. * call efi_build_mem_table() for a more accurate picture since it
  32. * merges areas together where possible. But that function uses more
  33. * pre-relocation memory, and it's not critical that we find the
  34. * absolute largest region.
  35. */
  36. ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
  37. if (ret) {
  38. /* We should have stopped in dram_init(), something is wrong */
  39. debug("%s: Missing memory map\n", __func__);
  40. goto err;
  41. }
  42. end = (struct efi_mem_desc *)((ulong)map + size);
  43. desc = map->desc;
  44. for (; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
  45. if (desc->type != EFI_CONVENTIONAL_MEMORY ||
  46. desc->physical_start >= 1ULL << 32)
  47. continue;
  48. if (!largest || desc->num_pages > largest->num_pages)
  49. largest = desc;
  50. }
  51. /* If no suitable area was found, return an error. */
  52. assert(largest);
  53. if (!largest || (largest->num_pages << EFI_PAGE_SHIFT) < (2 << 20))
  54. goto err;
  55. dest_addr = largest->physical_start + (largest->num_pages <<
  56. EFI_PAGE_SHIFT);
  57. return (ulong)dest_addr;
  58. err:
  59. panic("No available memory found for relocation");
  60. return 0;
  61. }
  62. int dram_init(void)
  63. {
  64. struct efi_mem_desc *desc, *end;
  65. struct efi_entry_memmap *map;
  66. int size, ret;
  67. ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
  68. if (ret) {
  69. printf("Cannot find EFI memory map tables, ret=%d\n", ret);
  70. return -ENODEV;
  71. }
  72. end = (struct efi_mem_desc *)((ulong)map + size);
  73. gd->ram_size = 0;
  74. desc = map->desc;
  75. for (; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
  76. if (desc->type < EFI_MMAP_IO)
  77. gd->ram_size += desc->num_pages << EFI_PAGE_SHIFT;
  78. }
  79. return 0;
  80. }
  81. void dram_init_banksize(void)
  82. {
  83. struct efi_mem_desc *desc, *end;
  84. struct efi_entry_memmap *map;
  85. int ret, size;
  86. int num_banks;
  87. ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
  88. if (ret) {
  89. /* We should have stopped in dram_init(), something is wrong */
  90. debug("%s: Missing memory map\n", __func__);
  91. return;
  92. }
  93. end = (struct efi_mem_desc *)((ulong)map + size);
  94. desc = map->desc;
  95. for (num_banks = 0;
  96. desc < end && num_banks < CONFIG_NR_DRAM_BANKS;
  97. desc = efi_get_next_mem_desc(map, desc)) {
  98. /*
  99. * We only use conventional memory below 4GB, and ignore
  100. * anything less than 1MB.
  101. */
  102. if (desc->type != EFI_CONVENTIONAL_MEMORY ||
  103. desc->physical_start >= 1ULL << 32 ||
  104. (desc->num_pages << EFI_PAGE_SHIFT) < 1 << 20)
  105. continue;
  106. gd->bd->bi_dram[num_banks].start = desc->physical_start;
  107. gd->bd->bi_dram[num_banks].size = desc->num_pages <<
  108. EFI_PAGE_SHIFT;
  109. num_banks++;
  110. }
  111. }
  112. int print_cpuinfo(void)
  113. {
  114. return default_print_cpuinfo();
  115. }
  116. /* Find any available tables and copy them to a safe place */
  117. int reserve_arch(void)
  118. {
  119. struct efi_info_hdr *hdr;
  120. debug("table=%lx\n", gd->arch.table);
  121. if (!gd->arch.table)
  122. return 0;
  123. hdr = (struct efi_info_hdr *)gd->arch.table;
  124. gd->start_addr_sp -= hdr->total_size;
  125. memcpy((void *)gd->start_addr_sp, hdr, hdr->total_size);
  126. debug("Stashing EFI table at %lx to %lx, size %x\n",
  127. gd->arch.table, gd->start_addr_sp, hdr->total_size);
  128. gd->arch.table = gd->start_addr_sp;
  129. return 0;
  130. }