payload.c 4.1 KB

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