/arch/powerpc/platforms/powermac/bootx_init.c

http://github.com/mirrors/linux · C · 594 lines · 453 code · 72 blank · 69 comment · 103 complexity · f65bf85ab82db472b2452de1605395bb MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Early boot support code for BootX bootloader
  4. *
  5. * Copyright (C) 2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/string.h>
  9. #include <linux/init.h>
  10. #include <generated/utsrelease.h>
  11. #include <asm/sections.h>
  12. #include <asm/prom.h>
  13. #include <asm/page.h>
  14. #include <asm/bootx.h>
  15. #include <asm/btext.h>
  16. #include <asm/io.h>
  17. #include <asm/setup.h>
  18. #undef DEBUG
  19. #define SET_BOOT_BAT
  20. #ifdef DEBUG
  21. #define DBG(fmt...) do { bootx_printf(fmt); } while(0)
  22. #else
  23. #define DBG(fmt...) do { } while(0)
  24. #endif
  25. extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
  26. static unsigned long __initdata bootx_dt_strbase;
  27. static unsigned long __initdata bootx_dt_strend;
  28. static unsigned long __initdata bootx_node_chosen;
  29. static boot_infos_t * __initdata bootx_info;
  30. static char __initdata bootx_disp_path[256];
  31. /* Is boot-info compatible ? */
  32. #define BOOT_INFO_IS_COMPATIBLE(bi) \
  33. ((bi)->compatible_version <= BOOT_INFO_VERSION)
  34. #define BOOT_INFO_IS_V2_COMPATIBLE(bi) ((bi)->version >= 2)
  35. #define BOOT_INFO_IS_V4_COMPATIBLE(bi) ((bi)->version >= 4)
  36. #ifdef CONFIG_BOOTX_TEXT
  37. static void __init bootx_printf(const char *format, ...)
  38. {
  39. const char *p, *q, *s;
  40. va_list args;
  41. unsigned long v;
  42. va_start(args, format);
  43. for (p = format; *p != 0; p = q) {
  44. for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
  45. ;
  46. if (q > p)
  47. btext_drawtext(p, q - p);
  48. if (*q == 0)
  49. break;
  50. if (*q == '\n') {
  51. ++q;
  52. btext_flushline();
  53. btext_drawstring("\r\n");
  54. btext_flushline();
  55. continue;
  56. }
  57. ++q;
  58. if (*q == 0)
  59. break;
  60. switch (*q) {
  61. case 's':
  62. ++q;
  63. s = va_arg(args, const char *);
  64. if (s == NULL)
  65. s = "<NULL>";
  66. btext_drawstring(s);
  67. break;
  68. case 'x':
  69. ++q;
  70. v = va_arg(args, unsigned long);
  71. btext_drawhex(v);
  72. break;
  73. }
  74. }
  75. va_end(args);
  76. }
  77. #else /* CONFIG_BOOTX_TEXT */
  78. static void __init bootx_printf(const char *format, ...) {}
  79. #endif /* CONFIG_BOOTX_TEXT */
  80. static void * __init bootx_early_getprop(unsigned long base,
  81. unsigned long node,
  82. char *prop)
  83. {
  84. struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
  85. u32 *ppp = &np->properties;
  86. while(*ppp) {
  87. struct bootx_dt_prop *pp =
  88. (struct bootx_dt_prop *)(base + *ppp);
  89. if (strcmp((char *)((unsigned long)pp->name + base),
  90. prop) == 0) {
  91. return (void *)((unsigned long)pp->value + base);
  92. }
  93. ppp = &pp->next;
  94. }
  95. return NULL;
  96. }
  97. #define dt_push_token(token, mem) \
  98. do { \
  99. *(mem) = _ALIGN_UP(*(mem),4); \
  100. *((u32 *)*(mem)) = token; \
  101. *(mem) += 4; \
  102. } while(0)
  103. static unsigned long __init bootx_dt_find_string(char *str)
  104. {
  105. char *s, *os;
  106. s = os = (char *)bootx_dt_strbase;
  107. s += 4;
  108. while (s < (char *)bootx_dt_strend) {
  109. if (strcmp(s, str) == 0)
  110. return s - os;
  111. s += strlen(s) + 1;
  112. }
  113. return 0;
  114. }
  115. static void __init bootx_dt_add_prop(char *name, void *data, int size,
  116. unsigned long *mem_end)
  117. {
  118. unsigned long soff = bootx_dt_find_string(name);
  119. if (data == NULL)
  120. size = 0;
  121. if (soff == 0) {
  122. bootx_printf("WARNING: Can't find string index for <%s>\n",
  123. name);
  124. return;
  125. }
  126. if (size > 0x20000) {
  127. bootx_printf("WARNING: ignoring large property ");
  128. bootx_printf("%s length 0x%x\n", name, size);
  129. return;
  130. }
  131. dt_push_token(OF_DT_PROP, mem_end);
  132. dt_push_token(size, mem_end);
  133. dt_push_token(soff, mem_end);
  134. /* push property content */
  135. if (size && data) {
  136. memcpy((void *)*mem_end, data, size);
  137. *mem_end = _ALIGN_UP(*mem_end + size, 4);
  138. }
  139. }
  140. static void __init bootx_add_chosen_props(unsigned long base,
  141. unsigned long *mem_end)
  142. {
  143. u32 val;
  144. bootx_dt_add_prop("linux,bootx", NULL, 0, mem_end);
  145. if (bootx_info->kernelParamsOffset) {
  146. char *args = (char *)((unsigned long)bootx_info) +
  147. bootx_info->kernelParamsOffset;
  148. bootx_dt_add_prop("bootargs", args, strlen(args) + 1, mem_end);
  149. }
  150. if (bootx_info->ramDisk) {
  151. val = ((unsigned long)bootx_info) + bootx_info->ramDisk;
  152. bootx_dt_add_prop("linux,initrd-start", &val, 4, mem_end);
  153. val += bootx_info->ramDiskSize;
  154. bootx_dt_add_prop("linux,initrd-end", &val, 4, mem_end);
  155. }
  156. if (strlen(bootx_disp_path))
  157. bootx_dt_add_prop("linux,stdout-path", bootx_disp_path,
  158. strlen(bootx_disp_path) + 1, mem_end);
  159. }
  160. static void __init bootx_add_display_props(unsigned long base,
  161. unsigned long *mem_end,
  162. int has_real_node)
  163. {
  164. boot_infos_t *bi = bootx_info;
  165. u32 tmp;
  166. if (has_real_node) {
  167. bootx_dt_add_prop("linux,boot-display", NULL, 0, mem_end);
  168. bootx_dt_add_prop("linux,opened", NULL, 0, mem_end);
  169. } else
  170. bootx_dt_add_prop("linux,bootx-noscreen", NULL, 0, mem_end);
  171. tmp = bi->dispDeviceDepth;
  172. bootx_dt_add_prop("linux,bootx-depth", &tmp, 4, mem_end);
  173. tmp = bi->dispDeviceRect[2] - bi->dispDeviceRect[0];
  174. bootx_dt_add_prop("linux,bootx-width", &tmp, 4, mem_end);
  175. tmp = bi->dispDeviceRect[3] - bi->dispDeviceRect[1];
  176. bootx_dt_add_prop("linux,bootx-height", &tmp, 4, mem_end);
  177. tmp = bi->dispDeviceRowBytes;
  178. bootx_dt_add_prop("linux,bootx-linebytes", &tmp, 4, mem_end);
  179. tmp = (u32)bi->dispDeviceBase;
  180. if (tmp == 0)
  181. tmp = (u32)bi->logicalDisplayBase;
  182. tmp += bi->dispDeviceRect[1] * bi->dispDeviceRowBytes;
  183. tmp += bi->dispDeviceRect[0] * ((bi->dispDeviceDepth + 7) / 8);
  184. bootx_dt_add_prop("linux,bootx-addr", &tmp, 4, mem_end);
  185. }
  186. static void __init bootx_dt_add_string(char *s, unsigned long *mem_end)
  187. {
  188. unsigned int l = strlen(s) + 1;
  189. memcpy((void *)*mem_end, s, l);
  190. bootx_dt_strend = *mem_end = *mem_end + l;
  191. }
  192. static void __init bootx_scan_dt_build_strings(unsigned long base,
  193. unsigned long node,
  194. unsigned long *mem_end)
  195. {
  196. struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
  197. u32 *cpp, *ppp = &np->properties;
  198. unsigned long soff;
  199. char *namep;
  200. /* Keep refs to known nodes */
  201. namep = np->full_name ? (char *)(base + np->full_name) : NULL;
  202. if (namep == NULL) {
  203. bootx_printf("Node without a full name !\n");
  204. namep = "";
  205. }
  206. DBG("* strings: %s\n", namep);
  207. if (!strcmp(namep, "/chosen")) {
  208. DBG(" detected /chosen ! adding properties names !\n");
  209. bootx_dt_add_string("linux,bootx", mem_end);
  210. bootx_dt_add_string("linux,stdout-path", mem_end);
  211. bootx_dt_add_string("linux,initrd-start", mem_end);
  212. bootx_dt_add_string("linux,initrd-end", mem_end);
  213. bootx_dt_add_string("bootargs", mem_end);
  214. bootx_node_chosen = node;
  215. }
  216. if (node == bootx_info->dispDeviceRegEntryOffset) {
  217. DBG(" detected display ! adding properties names !\n");
  218. bootx_dt_add_string("linux,boot-display", mem_end);
  219. bootx_dt_add_string("linux,opened", mem_end);
  220. strlcpy(bootx_disp_path, namep, sizeof(bootx_disp_path));
  221. }
  222. /* get and store all property names */
  223. while (*ppp) {
  224. struct bootx_dt_prop *pp =
  225. (struct bootx_dt_prop *)(base + *ppp);
  226. namep = pp->name ? (char *)(base + pp->name) : NULL;
  227. if (namep == NULL || strcmp(namep, "name") == 0)
  228. goto next;
  229. /* get/create string entry */
  230. soff = bootx_dt_find_string(namep);
  231. if (soff == 0)
  232. bootx_dt_add_string(namep, mem_end);
  233. next:
  234. ppp = &pp->next;
  235. }
  236. /* do all our children */
  237. cpp = &np->child;
  238. while(*cpp) {
  239. np = (struct bootx_dt_node *)(base + *cpp);
  240. bootx_scan_dt_build_strings(base, *cpp, mem_end);
  241. cpp = &np->sibling;
  242. }
  243. }
  244. static void __init bootx_scan_dt_build_struct(unsigned long base,
  245. unsigned long node,
  246. unsigned long *mem_end)
  247. {
  248. struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
  249. u32 *cpp, *ppp = &np->properties;
  250. char *namep, *p, *ep, *lp;
  251. int l;
  252. dt_push_token(OF_DT_BEGIN_NODE, mem_end);
  253. /* get the node's full name */
  254. namep = np->full_name ? (char *)(base + np->full_name) : NULL;
  255. if (namep == NULL)
  256. namep = "";
  257. l = strlen(namep);
  258. DBG("* struct: %s\n", namep);
  259. /* Fixup an Apple bug where they have bogus \0 chars in the
  260. * middle of the path in some properties, and extract
  261. * the unit name (everything after the last '/').
  262. */
  263. memcpy((void *)*mem_end, namep, l + 1);
  264. namep = (char *)*mem_end;
  265. for (lp = p = namep, ep = namep + l; p < ep; p++) {
  266. if (*p == '/')
  267. lp = namep;
  268. else if (*p != 0)
  269. *lp++ = *p;
  270. }
  271. *lp = 0;
  272. *mem_end = _ALIGN_UP((unsigned long)lp + 1, 4);
  273. /* get and store all properties */
  274. while (*ppp) {
  275. struct bootx_dt_prop *pp =
  276. (struct bootx_dt_prop *)(base + *ppp);
  277. namep = pp->name ? (char *)(base + pp->name) : NULL;
  278. /* Skip "name" */
  279. if (namep == NULL || !strcmp(namep, "name"))
  280. goto next;
  281. /* Skip "bootargs" in /chosen too as we replace it */
  282. if (node == bootx_node_chosen && !strcmp(namep, "bootargs"))
  283. goto next;
  284. /* push property head */
  285. bootx_dt_add_prop(namep,
  286. pp->value ? (void *)(base + pp->value): NULL,
  287. pp->length, mem_end);
  288. next:
  289. ppp = &pp->next;
  290. }
  291. if (node == bootx_node_chosen) {
  292. bootx_add_chosen_props(base, mem_end);
  293. if (bootx_info->dispDeviceRegEntryOffset == 0)
  294. bootx_add_display_props(base, mem_end, 0);
  295. }
  296. else if (node == bootx_info->dispDeviceRegEntryOffset)
  297. bootx_add_display_props(base, mem_end, 1);
  298. /* do all our children */
  299. cpp = &np->child;
  300. while(*cpp) {
  301. np = (struct bootx_dt_node *)(base + *cpp);
  302. bootx_scan_dt_build_struct(base, *cpp, mem_end);
  303. cpp = &np->sibling;
  304. }
  305. dt_push_token(OF_DT_END_NODE, mem_end);
  306. }
  307. static unsigned long __init bootx_flatten_dt(unsigned long start)
  308. {
  309. boot_infos_t *bi = bootx_info;
  310. unsigned long mem_start, mem_end;
  311. struct boot_param_header *hdr;
  312. unsigned long base;
  313. u64 *rsvmap;
  314. /* Start using memory after the big blob passed by BootX, get
  315. * some space for the header
  316. */
  317. mem_start = mem_end = _ALIGN_UP(((unsigned long)bi) + start, 4);
  318. DBG("Boot params header at: %x\n", mem_start);
  319. hdr = (struct boot_param_header *)mem_start;
  320. mem_end += sizeof(struct boot_param_header);
  321. rsvmap = (u64 *)(_ALIGN_UP(mem_end, 8));
  322. hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - mem_start;
  323. mem_end = ((unsigned long)rsvmap) + 8 * sizeof(u64);
  324. /* Get base of tree */
  325. base = ((unsigned long)bi) + bi->deviceTreeOffset;
  326. /* Build string array */
  327. DBG("Building string array at: %x\n", mem_end);
  328. DBG("Device Tree Base=%x\n", base);
  329. bootx_dt_strbase = mem_end;
  330. mem_end += 4;
  331. bootx_dt_strend = mem_end;
  332. bootx_scan_dt_build_strings(base, 4, &mem_end);
  333. /* Add some strings */
  334. bootx_dt_add_string("linux,bootx-noscreen", &mem_end);
  335. bootx_dt_add_string("linux,bootx-depth", &mem_end);
  336. bootx_dt_add_string("linux,bootx-width", &mem_end);
  337. bootx_dt_add_string("linux,bootx-height", &mem_end);
  338. bootx_dt_add_string("linux,bootx-linebytes", &mem_end);
  339. bootx_dt_add_string("linux,bootx-addr", &mem_end);
  340. /* Wrap up strings */
  341. hdr->off_dt_strings = bootx_dt_strbase - mem_start;
  342. hdr->dt_strings_size = bootx_dt_strend - bootx_dt_strbase;
  343. /* Build structure */
  344. mem_end = _ALIGN(mem_end, 16);
  345. DBG("Building device tree structure at: %x\n", mem_end);
  346. hdr->off_dt_struct = mem_end - mem_start;
  347. bootx_scan_dt_build_struct(base, 4, &mem_end);
  348. dt_push_token(OF_DT_END, &mem_end);
  349. /* Finish header */
  350. hdr->boot_cpuid_phys = 0;
  351. hdr->magic = OF_DT_HEADER;
  352. hdr->totalsize = mem_end - mem_start;
  353. hdr->version = OF_DT_VERSION;
  354. /* Version 16 is not backward compatible */
  355. hdr->last_comp_version = 0x10;
  356. /* Reserve the whole thing and copy the reserve map in, we
  357. * also bump mem_reserve_cnt to cause further reservations to
  358. * fail since it's too late.
  359. */
  360. mem_end = _ALIGN(mem_end, PAGE_SIZE);
  361. DBG("End of boot params: %x\n", mem_end);
  362. rsvmap[0] = mem_start;
  363. rsvmap[1] = mem_end;
  364. if (bootx_info->ramDisk) {
  365. rsvmap[2] = ((unsigned long)bootx_info) + bootx_info->ramDisk;
  366. rsvmap[3] = rsvmap[2] + bootx_info->ramDiskSize;
  367. rsvmap[4] = 0;
  368. rsvmap[5] = 0;
  369. } else {
  370. rsvmap[2] = 0;
  371. rsvmap[3] = 0;
  372. }
  373. return (unsigned long)hdr;
  374. }
  375. #ifdef CONFIG_BOOTX_TEXT
  376. static void __init btext_welcome(boot_infos_t *bi)
  377. {
  378. unsigned long flags;
  379. unsigned long pvr;
  380. bootx_printf("Welcome to Linux, kernel " UTS_RELEASE "\n");
  381. bootx_printf("\nlinked at : 0x%x", KERNELBASE);
  382. bootx_printf("\nframe buffer at : 0x%x", bi->dispDeviceBase);
  383. bootx_printf(" (phys), 0x%x", bi->logicalDisplayBase);
  384. bootx_printf(" (log)");
  385. bootx_printf("\nklimit : 0x%x",(unsigned long)klimit);
  386. bootx_printf("\nboot_info at : 0x%x", bi);
  387. __asm__ __volatile__ ("mfmsr %0" : "=r" (flags));
  388. bootx_printf("\nMSR : 0x%x", flags);
  389. __asm__ __volatile__ ("mfspr %0, 287" : "=r" (pvr));
  390. bootx_printf("\nPVR : 0x%x", pvr);
  391. pvr >>= 16;
  392. if (pvr > 1) {
  393. __asm__ __volatile__ ("mfspr %0, 1008" : "=r" (flags));
  394. bootx_printf("\nHID0 : 0x%x", flags);
  395. }
  396. if (pvr == 8 || pvr == 12 || pvr == 0x800c) {
  397. __asm__ __volatile__ ("mfspr %0, 1019" : "=r" (flags));
  398. bootx_printf("\nICTC : 0x%x", flags);
  399. }
  400. #ifdef DEBUG
  401. bootx_printf("\n\n");
  402. bootx_printf("bi->deviceTreeOffset : 0x%x\n",
  403. bi->deviceTreeOffset);
  404. bootx_printf("bi->deviceTreeSize : 0x%x\n",
  405. bi->deviceTreeSize);
  406. #endif
  407. bootx_printf("\n\n");
  408. }
  409. #endif /* CONFIG_BOOTX_TEXT */
  410. void __init bootx_init(unsigned long r3, unsigned long r4)
  411. {
  412. boot_infos_t *bi = (boot_infos_t *) r4;
  413. unsigned long hdr;
  414. unsigned long space;
  415. unsigned long ptr;
  416. char *model;
  417. unsigned long offset = reloc_offset();
  418. reloc_got2(offset);
  419. bootx_info = bi;
  420. /* We haven't cleared any bss at this point, make sure
  421. * what we need is initialized
  422. */
  423. bootx_dt_strbase = bootx_dt_strend = 0;
  424. bootx_node_chosen = 0;
  425. bootx_disp_path[0] = 0;
  426. if (!BOOT_INFO_IS_V2_COMPATIBLE(bi))
  427. bi->logicalDisplayBase = bi->dispDeviceBase;
  428. /* Fixup depth 16 -> 15 as that's what MacOS calls 16bpp */
  429. if (bi->dispDeviceDepth == 16)
  430. bi->dispDeviceDepth = 15;
  431. #ifdef CONFIG_BOOTX_TEXT
  432. ptr = (unsigned long)bi->logicalDisplayBase;
  433. ptr += bi->dispDeviceRect[1] * bi->dispDeviceRowBytes;
  434. ptr += bi->dispDeviceRect[0] * ((bi->dispDeviceDepth + 7) / 8);
  435. btext_setup_display(bi->dispDeviceRect[2] - bi->dispDeviceRect[0],
  436. bi->dispDeviceRect[3] - bi->dispDeviceRect[1],
  437. bi->dispDeviceDepth, bi->dispDeviceRowBytes,
  438. (unsigned long)bi->logicalDisplayBase);
  439. btext_clearscreen();
  440. btext_flushscreen();
  441. #endif /* CONFIG_BOOTX_TEXT */
  442. /*
  443. * Test if boot-info is compatible. Done only in config
  444. * CONFIG_BOOTX_TEXT since there is nothing much we can do
  445. * with an incompatible version, except display a message
  446. * and eventually hang the processor...
  447. *
  448. * I'll try to keep enough of boot-info compatible in the
  449. * future to always allow display of this message;
  450. */
  451. if (!BOOT_INFO_IS_COMPATIBLE(bi)) {
  452. bootx_printf(" !!! WARNING - Incompatible version"
  453. " of BootX !!!\n\n\n");
  454. for (;;)
  455. ;
  456. }
  457. if (bi->architecture != BOOT_ARCH_PCI) {
  458. bootx_printf(" !!! WARNING - Unsupported machine"
  459. " architecture !\n");
  460. for (;;)
  461. ;
  462. }
  463. #ifdef CONFIG_BOOTX_TEXT
  464. btext_welcome(bi);
  465. #endif
  466. /* New BootX enters kernel with MMU off, i/os are not allowed
  467. * here. This hack will have been done by the boostrap anyway.
  468. */
  469. if (bi->version < 4) {
  470. /*
  471. * XXX If this is an iMac, turn off the USB controller.
  472. */
  473. model = (char *) bootx_early_getprop(r4 + bi->deviceTreeOffset,
  474. 4, "model");
  475. if (model
  476. && (strcmp(model, "iMac,1") == 0
  477. || strcmp(model, "PowerMac1,1") == 0)) {
  478. bootx_printf("iMac,1 detected, shutting down USB\n");
  479. out_le32((unsigned __iomem *)0x80880008, 1); /* XXX */
  480. }
  481. }
  482. /* Get a pointer that points above the device tree, args, ramdisk,
  483. * etc... to use for generating the flattened tree
  484. */
  485. if (bi->version < 5) {
  486. space = bi->deviceTreeOffset + bi->deviceTreeSize;
  487. if (bi->ramDisk >= space)
  488. space = bi->ramDisk + bi->ramDiskSize;
  489. } else
  490. space = bi->totalParamsSize;
  491. bootx_printf("Total space used by parameters & ramdisk: 0x%x\n", space);
  492. /* New BootX will have flushed all TLBs and enters kernel with
  493. * MMU switched OFF, so this should not be useful anymore.
  494. */
  495. if (bi->version < 4) {
  496. unsigned long x __maybe_unused;
  497. bootx_printf("Touching pages...\n");
  498. /*
  499. * Touch each page to make sure the PTEs for them
  500. * are in the hash table - the aim is to try to avoid
  501. * getting DSI exceptions while copying the kernel image.
  502. */
  503. for (ptr = ((unsigned long) &_stext) & PAGE_MASK;
  504. ptr < (unsigned long)bi + space; ptr += PAGE_SIZE)
  505. x = *(volatile unsigned long *)ptr;
  506. }
  507. /* Ok, now we need to generate a flattened device-tree to pass
  508. * to the kernel
  509. */
  510. bootx_printf("Preparing boot params...\n");
  511. hdr = bootx_flatten_dt(space);
  512. #ifdef CONFIG_BOOTX_TEXT
  513. #ifdef SET_BOOT_BAT
  514. bootx_printf("Preparing BAT...\n");
  515. btext_prepare_BAT();
  516. #else
  517. btext_unmap();
  518. #endif
  519. #endif
  520. reloc_got2(-offset);
  521. __start(hdr, KERNELBASE + offset, 0);
  522. }