bootm.c 7.6 KB

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