bootm.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2008 Semihalf
  4. *
  5. * (C) Copyright 2000-2006
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. */
  8. #include <common.h>
  9. #include <bootstage.h>
  10. #include <cpu_func.h>
  11. #include <env.h>
  12. #include <init.h>
  13. #include <lmb.h>
  14. #include <log.h>
  15. #include <watchdog.h>
  16. #include <command.h>
  17. #include <image.h>
  18. #include <malloc.h>
  19. #include <asm/global_data.h>
  20. #include <u-boot/zlib.h>
  21. #include <bzlib.h>
  22. #include <asm/byteorder.h>
  23. #include <asm/mp.h>
  24. #include <bootm.h>
  25. #include <vxworks.h>
  26. #if defined(CONFIG_OF_LIBFDT)
  27. #include <linux/libfdt.h>
  28. #include <fdt_support.h>
  29. #endif
  30. #ifdef CONFIG_SYS_INIT_RAM_LOCK
  31. #include <asm/cache.h>
  32. #endif
  33. DECLARE_GLOBAL_DATA_PTR;
  34. static ulong get_sp (void);
  35. extern void ft_fixup_num_cores(void *blob);
  36. static void set_clocks_in_mhz (struct bd_info *kbd);
  37. #ifndef CFG_SYS_LINUX_LOWMEM_MAX_SIZE
  38. #define CFG_SYS_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
  39. #endif
  40. static void boot_jump_linux(struct bootm_headers *images)
  41. {
  42. void (*kernel)(struct bd_info *, ulong r4, ulong r5, ulong r6,
  43. ulong r7, ulong r8, ulong r9);
  44. #ifdef CONFIG_OF_LIBFDT
  45. char *of_flat_tree = images->ft_addr;
  46. #endif
  47. kernel = (void (*)(struct bd_info *, ulong, ulong, ulong,
  48. ulong, ulong, ulong))images->ep;
  49. debug("## Transferring control to Linux (at address %08lx) ...\n",
  50. (ulong)kernel);
  51. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  52. #ifdef CONFIG_BOOTSTAGE_FDT
  53. bootstage_fdt_add_report();
  54. #endif
  55. #ifdef CONFIG_BOOTSTAGE_REPORT
  56. bootstage_report();
  57. #endif
  58. #if defined(CONFIG_SYS_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  59. unlock_ram_in_cache();
  60. #endif
  61. #if defined(CONFIG_OF_LIBFDT)
  62. if (of_flat_tree) { /* device tree; boot new style */
  63. /*
  64. * Linux Kernel Parameters (passing device tree):
  65. * r3: pointer to the fdt
  66. * r4: 0
  67. * r5: 0
  68. * r6: epapr magic
  69. * r7: size of IMA in bytes
  70. * r8: 0
  71. * r9: 0
  72. */
  73. debug(" Booting using OF flat tree...\n");
  74. schedule();
  75. (*kernel) ((struct bd_info *)of_flat_tree, 0, 0, EPAPR_MAGIC,
  76. env_get_bootm_mapsize(), 0, 0);
  77. /* does not return */
  78. } else
  79. #endif
  80. {
  81. /*
  82. * Linux Kernel Parameters (passing board info data):
  83. * r3: ptr to board info data
  84. * r4: initrd_start or 0 if no initrd
  85. * r5: initrd_end - unused if r4 is 0
  86. * r6: Start of command line string
  87. * r7: End of command line string
  88. * r8: 0
  89. * r9: 0
  90. */
  91. ulong cmd_start = images->cmdline_start;
  92. ulong cmd_end = images->cmdline_end;
  93. ulong initrd_start = images->initrd_start;
  94. ulong initrd_end = images->initrd_end;
  95. struct bd_info *kbd = images->kbd;
  96. debug(" Booting using board info...\n");
  97. schedule();
  98. (*kernel) (kbd, initrd_start, initrd_end,
  99. cmd_start, cmd_end, 0, 0);
  100. /* does not return */
  101. }
  102. return;
  103. }
  104. void arch_lmb_reserve(struct lmb *lmb)
  105. {
  106. phys_size_t bootm_size;
  107. ulong size, bootmap_base;
  108. bootmap_base = env_get_bootm_low();
  109. bootm_size = env_get_bootm_size();
  110. #ifdef DEBUG
  111. if (((u64)bootmap_base + bootm_size) >
  112. (CFG_SYS_SDRAM_BASE + (u64)gd->ram_size))
  113. puts("WARNING: bootm_low + bootm_size exceed total memory\n");
  114. if ((bootmap_base + bootm_size) > get_effective_memsize())
  115. puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
  116. #endif
  117. size = min(bootm_size, get_effective_memsize());
  118. size = min(size, (ulong)CFG_SYS_LINUX_LOWMEM_MAX_SIZE);
  119. if (size < bootm_size) {
  120. ulong base = bootmap_base + size;
  121. printf("WARNING: adjusting available memory from 0x%lx to 0x%llx\n",
  122. size, (unsigned long long)bootm_size);
  123. lmb_reserve(lmb, base, bootm_size - size);
  124. }
  125. arch_lmb_reserve_generic(lmb, get_sp(), gd->ram_top, 4096);
  126. #ifdef CONFIG_MP
  127. cpu_mp_lmb_reserve(lmb);
  128. #endif
  129. return;
  130. }
  131. static void boot_prep_linux(struct bootm_headers *images)
  132. {
  133. #ifdef CONFIG_MP
  134. /*
  135. * if we are MP make sure to flush the device tree so any changes are
  136. * made visibile to all other cores. In AMP boot scenarios the cores
  137. * might not be HW cache coherent with each other.
  138. */
  139. flush_cache((unsigned long)images->ft_addr, images->ft_len);
  140. #endif
  141. }
  142. static int boot_cmdline_linux(struct bootm_headers *images)
  143. {
  144. ulong of_size = images->ft_len;
  145. struct lmb *lmb = &images->lmb;
  146. ulong *cmd_start = &images->cmdline_start;
  147. ulong *cmd_end = &images->cmdline_end;
  148. int ret = 0;
  149. if (!of_size) {
  150. /* allocate space and init command line */
  151. ret = boot_get_cmdline (lmb, cmd_start, cmd_end);
  152. if (ret) {
  153. puts("ERROR with allocation of cmdline\n");
  154. return ret;
  155. }
  156. }
  157. return ret;
  158. }
  159. static int boot_bd_t_linux(struct bootm_headers *images)
  160. {
  161. ulong of_size = images->ft_len;
  162. struct lmb *lmb = &images->lmb;
  163. struct bd_info **kbd = &images->kbd;
  164. int ret = 0;
  165. if (!of_size) {
  166. /* allocate space for kernel copy of board info */
  167. ret = boot_get_kbd (lmb, kbd);
  168. if (ret) {
  169. puts("ERROR with allocation of kernel bd\n");
  170. return ret;
  171. }
  172. set_clocks_in_mhz(*kbd);
  173. }
  174. return ret;
  175. }
  176. static int boot_body_linux(struct bootm_headers *images)
  177. {
  178. int ret;
  179. /* allocate space for kernel copy of board info */
  180. ret = boot_bd_t_linux(images);
  181. if (ret)
  182. return ret;
  183. if (IS_ENABLED(CONFIG_LMB)) {
  184. ret = image_setup_linux(images);
  185. if (ret)
  186. return ret;
  187. }
  188. return 0;
  189. }
  190. noinline int do_bootm_linux(int flag, int argc, char *const argv[],
  191. struct bootm_headers *images)
  192. {
  193. int ret;
  194. if (flag & BOOTM_STATE_OS_CMDLINE) {
  195. boot_cmdline_linux(images);
  196. return 0;
  197. }
  198. if (flag & BOOTM_STATE_OS_BD_T) {
  199. boot_bd_t_linux(images);
  200. return 0;
  201. }
  202. if (flag & BOOTM_STATE_OS_PREP) {
  203. boot_prep_linux(images);
  204. return 0;
  205. }
  206. boot_prep_linux(images);
  207. ret = boot_body_linux(images);
  208. if (ret)
  209. return ret;
  210. boot_jump_linux(images);
  211. return 0;
  212. }
  213. static ulong get_sp (void)
  214. {
  215. ulong sp;
  216. asm( "mr %0,1": "=r"(sp) : );
  217. return sp;
  218. }
  219. static void set_clocks_in_mhz (struct bd_info *kbd)
  220. {
  221. char *s;
  222. s = env_get("clocks_in_mhz");
  223. if (s) {
  224. /* convert all clock information to MHz */
  225. kbd->bi_intfreq /= 1000000L;
  226. kbd->bi_busfreq /= 1000000L;
  227. }
  228. }
  229. #if defined(CONFIG_BOOTM_VXWORKS)
  230. void boot_prep_vxworks(struct bootm_headers *images)
  231. {
  232. #if defined(CONFIG_OF_LIBFDT)
  233. int off;
  234. u64 base, size;
  235. if (!images->ft_addr)
  236. return;
  237. base = (u64)gd->ram_base;
  238. size = (u64)gd->ram_size;
  239. off = fdt_path_offset(images->ft_addr, "/memory");
  240. if (off < 0)
  241. fdt_fixup_memory(images->ft_addr, base, size);
  242. #if defined(CONFIG_MP)
  243. #if defined(CONFIG_MPC85xx)
  244. ft_fixup_cpu(images->ft_addr, base + size);
  245. ft_fixup_num_cores(images->ft_addr);
  246. #elif defined(CONFIG_MPC86xx)
  247. off = fdt_add_mem_rsv(images->ft_addr,
  248. determine_mp_bootpg(NULL), (u64)4096);
  249. if (off < 0)
  250. printf("## WARNING %s: %s\n", __func__, fdt_strerror(off));
  251. ft_fixup_num_cores(images->ft_addr);
  252. #endif
  253. flush_cache((unsigned long)images->ft_addr, images->ft_len);
  254. #endif
  255. #endif
  256. }
  257. void boot_jump_vxworks(struct bootm_headers *images)
  258. {
  259. /* PowerPC VxWorks boot interface conforms to the ePAPR standard
  260. * general purpuse registers:
  261. *
  262. * r3: Effective address of the device tree image
  263. * r4: 0
  264. * r5: 0
  265. * r6: ePAPR magic value
  266. * r7: shall be the size of the boot IMA in bytes
  267. * r8: 0
  268. * r9: 0
  269. * TCR: WRC = 0, no watchdog timer reset will occur
  270. */
  271. schedule();
  272. ((void (*)(void *, ulong, ulong, ulong,
  273. ulong, ulong, ulong))images->ep)(images->ft_addr,
  274. 0, 0, EPAPR_MAGIC, env_get_bootm_mapsize(), 0, 0);
  275. }
  276. #endif