prom_init.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * Note that prom_init() and anything called from prom_init()
  3. * may be running at an address that is different from the address
  4. * that it was linked at. References to static data items are
  5. * handled by compiling this file with -mrelocatable-lib.
  6. */
  7. #include <linux/config.h>
  8. #include <linux/kernel.h>
  9. #include <linux/string.h>
  10. #include <linux/init.h>
  11. #include <linux/version.h>
  12. #include <linux/threads.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/ioport.h>
  15. #include <linux/pci.h>
  16. #include <linux/slab.h>
  17. #include <linux/bitops.h>
  18. #include <asm/sections.h>
  19. #include <asm/prom.h>
  20. #include <asm/page.h>
  21. #include <asm/irq.h>
  22. #include <asm/io.h>
  23. #include <asm/smp.h>
  24. #include <asm/bootx.h>
  25. #include <asm/system.h>
  26. #include <asm/mmu.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/bootinfo.h>
  29. #include <asm/btext.h>
  30. #include <asm/pci-bridge.h>
  31. #include <asm/open_pic.h>
  32. #include <asm/cacheflush.h>
  33. #ifdef CONFIG_LOGO_LINUX_CLUT224
  34. #include <linux/linux_logo.h>
  35. extern const struct linux_logo logo_linux_clut224;
  36. #endif
  37. /*
  38. * Properties whose value is longer than this get excluded from our
  39. * copy of the device tree. This way we don't waste space storing
  40. * things like "driver,AAPL,MacOS,PowerPC" properties. But this value
  41. * does need to be big enough to ensure that we don't lose things
  42. * like the interrupt-map property on a PCI-PCI bridge.
  43. */
  44. #define MAX_PROPERTY_LENGTH 4096
  45. #ifndef FB_MAX /* avoid pulling in all of the fb stuff */
  46. #define FB_MAX 8
  47. #endif
  48. #define ALIGNUL(x) (((x) + sizeof(unsigned long)-1) & -sizeof(unsigned long))
  49. typedef u32 prom_arg_t;
  50. struct prom_args {
  51. const char *service;
  52. int nargs;
  53. int nret;
  54. prom_arg_t args[10];
  55. };
  56. struct pci_address {
  57. unsigned a_hi;
  58. unsigned a_mid;
  59. unsigned a_lo;
  60. };
  61. struct pci_reg_property {
  62. struct pci_address addr;
  63. unsigned size_hi;
  64. unsigned size_lo;
  65. };
  66. struct pci_range {
  67. struct pci_address addr;
  68. unsigned phys;
  69. unsigned size_hi;
  70. unsigned size_lo;
  71. };
  72. struct isa_reg_property {
  73. unsigned space;
  74. unsigned address;
  75. unsigned size;
  76. };
  77. struct pci_intr_map {
  78. struct pci_address addr;
  79. unsigned dunno;
  80. phandle int_ctrler;
  81. unsigned intr;
  82. };
  83. static void prom_exit(void);
  84. static int call_prom(const char *service, int nargs, int nret, ...);
  85. static int call_prom_ret(const char *service, int nargs, int nret,
  86. prom_arg_t *rets, ...);
  87. static void prom_print_hex(unsigned int v);
  88. static int prom_set_color(ihandle ih, int i, int r, int g, int b);
  89. static int prom_next_node(phandle *nodep);
  90. static unsigned long check_display(unsigned long mem);
  91. static void setup_disp_fake_bi(ihandle dp);
  92. static unsigned long copy_device_tree(unsigned long mem_start,
  93. unsigned long mem_end);
  94. static unsigned long inspect_node(phandle node, struct device_node *dad,
  95. unsigned long mem_start, unsigned long mem_end,
  96. struct device_node ***allnextpp);
  97. static void prom_hold_cpus(unsigned long mem);
  98. static void prom_instantiate_rtas(void);
  99. static void * early_get_property(unsigned long base, unsigned long node,
  100. char *prop);
  101. prom_entry prom __initdata;
  102. ihandle prom_chosen __initdata;
  103. ihandle prom_stdout __initdata;
  104. static char *prom_display_paths[FB_MAX] __initdata;
  105. static phandle prom_display_nodes[FB_MAX] __initdata;
  106. static unsigned int prom_num_displays __initdata;
  107. static ihandle prom_disp_node __initdata;
  108. char *of_stdout_device __initdata;
  109. unsigned int rtas_data; /* physical pointer */
  110. unsigned int rtas_entry; /* physical pointer */
  111. unsigned int rtas_size;
  112. unsigned int old_rtas;
  113. boot_infos_t *boot_infos;
  114. char *bootpath;
  115. char *bootdevice;
  116. struct device_node *allnodes;
  117. extern char *klimit;
  118. static void __init
  119. prom_exit(void)
  120. {
  121. struct prom_args args;
  122. args.service = "exit";
  123. args.nargs = 0;
  124. args.nret = 0;
  125. prom(&args);
  126. for (;;) /* should never get here */
  127. ;
  128. }
  129. static int __init
  130. call_prom(const char *service, int nargs, int nret, ...)
  131. {
  132. va_list list;
  133. int i;
  134. struct prom_args prom_args;
  135. prom_args.service = service;
  136. prom_args.nargs = nargs;
  137. prom_args.nret = nret;
  138. va_start(list, nret);
  139. for (i = 0; i < nargs; ++i)
  140. prom_args.args[i] = va_arg(list, prom_arg_t);
  141. va_end(list);
  142. for (i = 0; i < nret; ++i)
  143. prom_args.args[i + nargs] = 0;
  144. prom(&prom_args);
  145. return prom_args.args[nargs];
  146. }
  147. static int __init
  148. call_prom_ret(const char *service, int nargs, int nret, prom_arg_t *rets, ...)
  149. {
  150. va_list list;
  151. int i;
  152. struct prom_args prom_args;
  153. prom_args.service = service;
  154. prom_args.nargs = nargs;
  155. prom_args.nret = nret;
  156. va_start(list, rets);
  157. for (i = 0; i < nargs; ++i)
  158. prom_args.args[i] = va_arg(list, int);
  159. va_end(list);
  160. for (i = 0; i < nret; ++i)
  161. prom_args.args[i + nargs] = 0;
  162. prom(&prom_args);
  163. for (i = 1; i < nret; ++i)
  164. rets[i-1] = prom_args.args[nargs + i];
  165. return prom_args.args[nargs];
  166. }
  167. void __init
  168. prom_print(const char *msg)
  169. {
  170. const char *p, *q;
  171. if (prom_stdout == 0)
  172. return;
  173. for (p = msg; *p != 0; p = q) {
  174. for (q = p; *q != 0 && *q != '\n'; ++q)
  175. ;
  176. if (q > p)
  177. call_prom("write", 3, 1, prom_stdout, p, q - p);
  178. if (*q != 0) {
  179. ++q;
  180. call_prom("write", 3, 1, prom_stdout, "\r\n", 2);
  181. }
  182. }
  183. }
  184. static void __init
  185. prom_print_hex(unsigned int v)
  186. {
  187. char buf[16];
  188. int i, c;
  189. for (i = 0; i < 8; ++i) {
  190. c = (v >> ((7-i)*4)) & 0xf;
  191. c += (c >= 10)? ('a' - 10): '0';
  192. buf[i] = c;
  193. }
  194. buf[i] = ' ';
  195. buf[i+1] = 0;
  196. prom_print(buf);
  197. }
  198. static int __init
  199. prom_set_color(ihandle ih, int i, int r, int g, int b)
  200. {
  201. return call_prom("call-method", 6, 1, "color!", ih, i, b, g, r);
  202. }
  203. static int __init
  204. prom_next_node(phandle *nodep)
  205. {
  206. phandle node;
  207. if ((node = *nodep) != 0
  208. && (*nodep = call_prom("child", 1, 1, node)) != 0)
  209. return 1;
  210. if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
  211. return 1;
  212. for (;;) {
  213. if ((node = call_prom("parent", 1, 1, node)) == 0)
  214. return 0;
  215. if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
  216. return 1;
  217. }
  218. }
  219. #ifdef CONFIG_POWER4
  220. /*
  221. * Set up a hash table with a set of entries in it to map the
  222. * first 64MB of RAM. This is used on 64-bit machines since
  223. * some of them don't have BATs.
  224. */
  225. static inline void make_pte(unsigned long htab, unsigned int hsize,
  226. unsigned int va, unsigned int pa, int mode)
  227. {
  228. unsigned int *pteg;
  229. unsigned int hash, i, vsid;
  230. vsid = ((va >> 28) * 0x111) << 12;
  231. hash = ((va ^ vsid) >> 5) & 0x7fff80;
  232. pteg = (unsigned int *)(htab + (hash & (hsize - 1)));
  233. for (i = 0; i < 8; ++i, pteg += 4) {
  234. if ((pteg[1] & 1) == 0) {
  235. pteg[1] = vsid | ((va >> 16) & 0xf80) | 1;
  236. pteg[3] = pa | mode;
  237. break;
  238. }
  239. }
  240. }
  241. extern unsigned long _SDR1;
  242. extern PTE *Hash;
  243. extern unsigned long Hash_size;
  244. static void __init
  245. prom_alloc_htab(void)
  246. {
  247. unsigned int hsize;
  248. unsigned long htab;
  249. unsigned int addr;
  250. /*
  251. * Because of OF bugs we can't use the "claim" client
  252. * interface to allocate memory for the hash table.
  253. * This code is only used on 64-bit PPCs, and the only
  254. * 64-bit PPCs at the moment are RS/6000s, and their
  255. * OF is based at 0xc00000 (the 12M point), so we just
  256. * arbitrarily use the 0x800000 - 0xc00000 region for the
  257. * hash table.
  258. * -- paulus.
  259. */
  260. hsize = 4 << 20; /* POWER4 has no BATs */
  261. htab = (8 << 20);
  262. call_prom("claim", 3, 1, htab, hsize, 0);
  263. Hash = (void *)(htab + KERNELBASE);
  264. Hash_size = hsize;
  265. _SDR1 = htab + __ilog2(hsize) - 18;
  266. /*
  267. * Put in PTEs for the first 64MB of RAM
  268. */
  269. memset((void *)htab, 0, hsize);
  270. for (addr = 0; addr < 0x4000000; addr += 0x1000)
  271. make_pte(htab, hsize, addr + KERNELBASE, addr,
  272. _PAGE_ACCESSED | _PAGE_COHERENT | PP_RWXX);
  273. #if 0 /* DEBUG stuff mapping the SCC */
  274. make_pte(htab, hsize, 0x80013000, 0x80013000,
  275. _PAGE_ACCESSED | _PAGE_NO_CACHE | _PAGE_GUARDED | PP_RWXX);
  276. #endif
  277. }
  278. #endif /* CONFIG_POWER4 */
  279. /*
  280. * If we have a display that we don't know how to drive,
  281. * we will want to try to execute OF's open method for it
  282. * later. However, OF will probably fall over if we do that
  283. * we've taken over the MMU.
  284. * So we check whether we will need to open the display,
  285. * and if so, open it now.
  286. */
  287. static unsigned long __init
  288. check_display(unsigned long mem)
  289. {
  290. phandle node;
  291. ihandle ih;
  292. int i, j;
  293. char type[16], *path;
  294. static unsigned char default_colors[] = {
  295. 0x00, 0x00, 0x00,
  296. 0x00, 0x00, 0xaa,
  297. 0x00, 0xaa, 0x00,
  298. 0x00, 0xaa, 0xaa,
  299. 0xaa, 0x00, 0x00,
  300. 0xaa, 0x00, 0xaa,
  301. 0xaa, 0xaa, 0x00,
  302. 0xaa, 0xaa, 0xaa,
  303. 0x55, 0x55, 0x55,
  304. 0x55, 0x55, 0xff,
  305. 0x55, 0xff, 0x55,
  306. 0x55, 0xff, 0xff,
  307. 0xff, 0x55, 0x55,
  308. 0xff, 0x55, 0xff,
  309. 0xff, 0xff, 0x55,
  310. 0xff, 0xff, 0xff
  311. };
  312. const unsigned char *clut;
  313. prom_disp_node = 0;
  314. for (node = 0; prom_next_node(&node); ) {
  315. type[0] = 0;
  316. call_prom("getprop", 4, 1, node, "device_type",
  317. type, sizeof(type));
  318. if (strcmp(type, "display") != 0)
  319. continue;
  320. /* It seems OF doesn't null-terminate the path :-( */
  321. path = (char *) mem;
  322. memset(path, 0, 256);
  323. if (call_prom("package-to-path", 3, 1, node, path, 255) < 0)
  324. continue;
  325. /*
  326. * If this display is the device that OF is using for stdout,
  327. * move it to the front of the list.
  328. */
  329. mem += strlen(path) + 1;
  330. i = prom_num_displays++;
  331. if (of_stdout_device != 0 && i > 0
  332. && strcmp(of_stdout_device, path) == 0) {
  333. for (; i > 0; --i) {
  334. prom_display_paths[i]
  335. = prom_display_paths[i-1];
  336. prom_display_nodes[i]
  337. = prom_display_nodes[i-1];
  338. }
  339. }
  340. prom_display_paths[i] = path;
  341. prom_display_nodes[i] = node;
  342. if (i == 0)
  343. prom_disp_node = node;
  344. if (prom_num_displays >= FB_MAX)
  345. break;
  346. }
  347. for (j=0; j<prom_num_displays; j++) {
  348. path = prom_display_paths[j];
  349. node = prom_display_nodes[j];
  350. prom_print("opening display ");
  351. prom_print(path);
  352. ih = call_prom("open", 1, 1, path);
  353. if (ih == 0 || ih == (ihandle) -1) {
  354. prom_print("... failed\n");
  355. for (i=j+1; i<prom_num_displays; i++) {
  356. prom_display_paths[i-1] = prom_display_paths[i];
  357. prom_display_nodes[i-1] = prom_display_nodes[i];
  358. }
  359. if (--prom_num_displays > 0) {
  360. prom_disp_node = prom_display_nodes[j];
  361. j--;
  362. } else
  363. prom_disp_node = 0;
  364. continue;
  365. } else {
  366. prom_print("... ok\n");
  367. call_prom("setprop", 4, 1, node, "linux,opened", 0, 0);
  368. /*
  369. * Setup a usable color table when the appropriate
  370. * method is available.
  371. * Should update this to use set-colors.
  372. */
  373. clut = default_colors;
  374. for (i = 0; i < 32; i++, clut += 3)
  375. if (prom_set_color(ih, i, clut[0], clut[1],
  376. clut[2]) != 0)
  377. break;
  378. #ifdef CONFIG_LOGO_LINUX_CLUT224
  379. clut = PTRRELOC(logo_linux_clut224.clut);
  380. for (i = 0; i < logo_linux_clut224.clutsize;
  381. i++, clut += 3)
  382. if (prom_set_color(ih, i + 32, clut[0],
  383. clut[1], clut[2]) != 0)
  384. break;
  385. #endif /* CONFIG_LOGO_LINUX_CLUT224 */
  386. }
  387. }
  388. if (prom_stdout) {
  389. phandle p;
  390. p = call_prom("instance-to-package", 1, 1, prom_stdout);
  391. if (p && p != -1) {
  392. type[0] = 0;
  393. call_prom("getprop", 4, 1, p, "device_type",
  394. type, sizeof(type));
  395. if (strcmp(type, "display") == 0)
  396. call_prom("setprop", 4, 1, p, "linux,boot-display",
  397. 0, 0);
  398. }
  399. }
  400. return ALIGNUL(mem);
  401. }
  402. /* This function will enable the early boot text when doing OF booting. This
  403. * way, xmon output should work too
  404. */
  405. static void __init
  406. setup_disp_fake_bi(ihandle dp)
  407. {
  408. #ifdef CONFIG_BOOTX_TEXT
  409. int width = 640, height = 480, depth = 8, pitch;
  410. unsigned address;
  411. struct pci_reg_property addrs[8];
  412. int i, naddrs;
  413. char name[32];
  414. char *getprop = "getprop";
  415. prom_print("Initializing fake screen: ");
  416. memset(name, 0, sizeof(name));
  417. call_prom(getprop, 4, 1, dp, "name", name, sizeof(name));
  418. name[sizeof(name)-1] = 0;
  419. prom_print(name);
  420. prom_print("\n");
  421. call_prom(getprop, 4, 1, dp, "width", &width, sizeof(width));
  422. call_prom(getprop, 4, 1, dp, "height", &height, sizeof(height));
  423. call_prom(getprop, 4, 1, dp, "depth", &depth, sizeof(depth));
  424. pitch = width * ((depth + 7) / 8);
  425. call_prom(getprop, 4, 1, dp, "linebytes",
  426. &pitch, sizeof(pitch));
  427. if (pitch == 1)
  428. pitch = 0x1000; /* for strange IBM display */
  429. address = 0;
  430. call_prom(getprop, 4, 1, dp, "address",
  431. &address, sizeof(address));
  432. if (address == 0) {
  433. /* look for an assigned address with a size of >= 1MB */
  434. naddrs = call_prom(getprop, 4, 1, dp, "assigned-addresses",
  435. addrs, sizeof(addrs));
  436. naddrs /= sizeof(struct pci_reg_property);
  437. for (i = 0; i < naddrs; ++i) {
  438. if (addrs[i].size_lo >= (1 << 20)) {
  439. address = addrs[i].addr.a_lo;
  440. /* use the BE aperture if possible */
  441. if (addrs[i].size_lo >= (16 << 20))
  442. address += (8 << 20);
  443. break;
  444. }
  445. }
  446. if (address == 0) {
  447. prom_print("Failed to get address\n");
  448. return;
  449. }
  450. }
  451. /* kludge for valkyrie */
  452. if (strcmp(name, "valkyrie") == 0)
  453. address += 0x1000;
  454. #ifdef CONFIG_POWER4
  455. #if CONFIG_TASK_SIZE > 0x80000000
  456. #error CONFIG_TASK_SIZE cannot be above 0x80000000 with BOOTX_TEXT on G5
  457. #endif
  458. {
  459. extern boot_infos_t disp_bi;
  460. unsigned long va, pa, i, offset;
  461. va = 0x90000000;
  462. pa = address & 0xfffff000ul;
  463. offset = address & 0x00000fff;
  464. for (i=0; i<0x4000; i++) {
  465. make_pte((unsigned long)Hash - KERNELBASE, Hash_size, va, pa,
  466. _PAGE_ACCESSED | _PAGE_NO_CACHE |
  467. _PAGE_GUARDED | PP_RWXX);
  468. va += 0x1000;
  469. pa += 0x1000;
  470. }
  471. btext_setup_display(width, height, depth, pitch, 0x90000000 | offset);
  472. disp_bi.dispDeviceBase = (u8 *)address;
  473. }
  474. #else /* CONFIG_POWER4 */
  475. btext_setup_display(width, height, depth, pitch, address);
  476. btext_prepare_BAT();
  477. #endif /* CONFIG_POWER4 */
  478. #endif /* CONFIG_BOOTX_TEXT */
  479. }
  480. /*
  481. * Make a copy of the device tree from the PROM.
  482. */
  483. static unsigned long __init
  484. copy_device_tree(unsigned long mem_start, unsigned long mem_end)
  485. {
  486. phandle root;
  487. unsigned long new_start;
  488. struct device_node **allnextp;
  489. root = call_prom("peer", 1, 1, (phandle)0);
  490. if (root == (phandle)0) {
  491. prom_print("couldn't get device tree root\n");
  492. prom_exit();
  493. }
  494. allnextp = &allnodes;
  495. mem_start = ALIGNUL(mem_start);
  496. new_start = inspect_node(root, NULL, mem_start, mem_end, &allnextp);
  497. *allnextp = NULL;
  498. return new_start;
  499. }
  500. static unsigned long __init
  501. inspect_node(phandle node, struct device_node *dad,
  502. unsigned long mem_start, unsigned long mem_end,
  503. struct device_node ***allnextpp)
  504. {
  505. int l;
  506. phandle child;
  507. struct device_node *np;
  508. struct property *pp, **prev_propp;
  509. char *prev_name, *namep;
  510. unsigned char *valp;
  511. np = (struct device_node *) mem_start;
  512. mem_start += sizeof(struct device_node);
  513. memset(np, 0, sizeof(*np));
  514. np->node = node;
  515. **allnextpp = PTRUNRELOC(np);
  516. *allnextpp = &np->allnext;
  517. if (dad != 0) {
  518. np->parent = PTRUNRELOC(dad);
  519. /* we temporarily use the `next' field as `last_child'. */
  520. if (dad->next == 0)
  521. dad->child = PTRUNRELOC(np);
  522. else
  523. dad->next->sibling = PTRUNRELOC(np);
  524. dad->next = np;
  525. }
  526. /* get and store all properties */
  527. prev_propp = &np->properties;
  528. prev_name = "";
  529. for (;;) {
  530. pp = (struct property *) mem_start;
  531. namep = (char *) (pp + 1);
  532. pp->name = PTRUNRELOC(namep);
  533. if (call_prom("nextprop", 3, 1, node, prev_name, namep) <= 0)
  534. break;
  535. mem_start = ALIGNUL((unsigned long)namep + strlen(namep) + 1);
  536. prev_name = namep;
  537. valp = (unsigned char *) mem_start;
  538. pp->value = PTRUNRELOC(valp);
  539. pp->length = call_prom("getprop", 4, 1, node, namep,
  540. valp, mem_end - mem_start);
  541. if (pp->length < 0)
  542. continue;
  543. #ifdef MAX_PROPERTY_LENGTH
  544. if (pp->length > MAX_PROPERTY_LENGTH)
  545. continue; /* ignore this property */
  546. #endif
  547. mem_start = ALIGNUL(mem_start + pp->length);
  548. *prev_propp = PTRUNRELOC(pp);
  549. prev_propp = &pp->next;
  550. }
  551. if (np->node != 0) {
  552. /* Add a "linux,phandle" property" */
  553. pp = (struct property *) mem_start;
  554. *prev_propp = PTRUNRELOC(pp);
  555. prev_propp = &pp->next;
  556. namep = (char *) (pp + 1);
  557. pp->name = PTRUNRELOC(namep);
  558. strcpy(namep, "linux,phandle");
  559. mem_start = ALIGNUL((unsigned long)namep + strlen(namep) + 1);
  560. pp->value = (unsigned char *) PTRUNRELOC(&np->node);
  561. pp->length = sizeof(np->node);
  562. }
  563. *prev_propp = NULL;
  564. /* get the node's full name */
  565. l = call_prom("package-to-path", 3, 1, node,
  566. mem_start, mem_end - mem_start);
  567. if (l >= 0) {
  568. np->full_name = PTRUNRELOC((char *) mem_start);
  569. *(char *)(mem_start + l) = 0;
  570. mem_start = ALIGNUL(mem_start + l + 1);
  571. }
  572. /* do all our children */
  573. child = call_prom("child", 1, 1, node);
  574. while (child != 0) {
  575. mem_start = inspect_node(child, np, mem_start, mem_end,
  576. allnextpp);
  577. child = call_prom("peer", 1, 1, child);
  578. }
  579. return mem_start;
  580. }
  581. unsigned long smp_chrp_cpu_nr __initdata = 0;
  582. /*
  583. * With CHRP SMP we need to use the OF to start the other
  584. * processors so we can't wait until smp_boot_cpus (the OF is
  585. * trashed by then) so we have to put the processors into
  586. * a holding pattern controlled by the kernel (not OF) before
  587. * we destroy the OF.
  588. *
  589. * This uses a chunk of high memory, puts some holding pattern
  590. * code there and sends the other processors off to there until
  591. * smp_boot_cpus tells them to do something. We do that by using
  592. * physical address 0x0. The holding pattern checks that address
  593. * until its cpu # is there, when it is that cpu jumps to
  594. * __secondary_start(). smp_boot_cpus() takes care of setting those
  595. * values.
  596. *
  597. * We also use physical address 0x4 here to tell when a cpu
  598. * is in its holding pattern code.
  599. *
  600. * -- Cort
  601. *
  602. * Note that we have to do this if we have more than one CPU,
  603. * even if this is a UP kernel. Otherwise when we trash OF
  604. * the other CPUs will start executing some random instructions
  605. * and crash the system. -- paulus
  606. */
  607. static void __init
  608. prom_hold_cpus(unsigned long mem)
  609. {
  610. extern void __secondary_hold(void);
  611. unsigned long i;
  612. int cpu;
  613. phandle node;
  614. char type[16], *path;
  615. unsigned int reg;
  616. /*
  617. * XXX: hack to make sure we're chrp, assume that if we're
  618. * chrp we have a device_type property -- Cort
  619. */
  620. node = call_prom("finddevice", 1, 1, "/");
  621. if (call_prom("getprop", 4, 1, node,
  622. "device_type", type, sizeof(type)) <= 0)
  623. return;
  624. /* copy the holding pattern code to someplace safe (0) */
  625. /* the holding pattern is now within the first 0x100
  626. bytes of the kernel image -- paulus */
  627. memcpy((void *)0, _stext, 0x100);
  628. flush_icache_range(0, 0x100);
  629. /* look for cpus */
  630. *(unsigned long *)(0x0) = 0;
  631. asm volatile("dcbf 0,%0": : "r" (0) : "memory");
  632. for (node = 0; prom_next_node(&node); ) {
  633. type[0] = 0;
  634. call_prom("getprop", 4, 1, node, "device_type",
  635. type, sizeof(type));
  636. if (strcmp(type, "cpu") != 0)
  637. continue;
  638. path = (char *) mem;
  639. memset(path, 0, 256);
  640. if (call_prom("package-to-path", 3, 1, node, path, 255) < 0)
  641. continue;
  642. reg = -1;
  643. call_prom("getprop", 4, 1, node, "reg", &reg, sizeof(reg));
  644. cpu = smp_chrp_cpu_nr++;
  645. #ifdef CONFIG_SMP
  646. smp_hw_index[cpu] = reg;
  647. #endif /* CONFIG_SMP */
  648. /* XXX: hack - don't start cpu 0, this cpu -- Cort */
  649. if (cpu == 0)
  650. continue;
  651. prom_print("starting cpu ");
  652. prom_print(path);
  653. *(ulong *)(0x4) = 0;
  654. call_prom("start-cpu", 3, 0, node,
  655. (char *)__secondary_hold - _stext, cpu);
  656. prom_print("...");
  657. for ( i = 0 ; (i < 10000) && (*(ulong *)(0x4) == 0); i++ )
  658. ;
  659. if (*(ulong *)(0x4) == cpu)
  660. prom_print("ok\n");
  661. else {
  662. prom_print("failed: ");
  663. prom_print_hex(*(ulong *)0x4);
  664. prom_print("\n");
  665. }
  666. }
  667. }
  668. static void __init
  669. prom_instantiate_rtas(void)
  670. {
  671. ihandle prom_rtas;
  672. prom_arg_t result;
  673. prom_rtas = call_prom("finddevice", 1, 1, "/rtas");
  674. if (prom_rtas == -1)
  675. return;
  676. rtas_size = 0;
  677. call_prom("getprop", 4, 1, prom_rtas,
  678. "rtas-size", &rtas_size, sizeof(rtas_size));
  679. prom_print("instantiating rtas");
  680. if (rtas_size == 0) {
  681. rtas_data = 0;
  682. } else {
  683. /*
  684. * Ask OF for some space for RTAS.
  685. * Actually OF has bugs so we just arbitrarily
  686. * use memory at the 6MB point.
  687. */
  688. rtas_data = 6 << 20;
  689. prom_print(" at ");
  690. prom_print_hex(rtas_data);
  691. }
  692. prom_rtas = call_prom("open", 1, 1, "/rtas");
  693. prom_print("...");
  694. rtas_entry = 0;
  695. if (call_prom_ret("call-method", 3, 2, &result,
  696. "instantiate-rtas", prom_rtas, rtas_data) == 0)
  697. rtas_entry = result;
  698. if ((rtas_entry == -1) || (rtas_entry == 0))
  699. prom_print(" failed\n");
  700. else
  701. prom_print(" done\n");
  702. }
  703. /*
  704. * We enter here early on, when the Open Firmware prom is still
  705. * handling exceptions and the MMU hash table for us.
  706. */
  707. unsigned long __init
  708. prom_init(int r3, int r4, prom_entry pp)
  709. {
  710. unsigned long mem;
  711. ihandle prom_mmu;
  712. unsigned long offset = reloc_offset();
  713. int i, l;
  714. char *p, *d;
  715. unsigned long phys;
  716. prom_arg_t result[3];
  717. char model[32];
  718. phandle node;
  719. int rc;
  720. /* Default */
  721. phys = (unsigned long) &_stext;
  722. /* First get a handle for the stdout device */
  723. prom = pp;
  724. prom_chosen = call_prom("finddevice", 1, 1, "/chosen");
  725. if (prom_chosen == -1)
  726. prom_exit();
  727. if (call_prom("getprop", 4, 1, prom_chosen, "stdout",
  728. &prom_stdout, sizeof(prom_stdout)) <= 0)
  729. prom_exit();
  730. /* Get the full OF pathname of the stdout device */
  731. mem = (unsigned long) klimit + offset;
  732. p = (char *) mem;
  733. memset(p, 0, 256);
  734. call_prom("instance-to-path", 3, 1, prom_stdout, p, 255);
  735. of_stdout_device = p;
  736. mem += strlen(p) + 1;
  737. /* Get the boot device and translate it to a full OF pathname. */
  738. p = (char *) mem;
  739. l = call_prom("getprop", 4, 1, prom_chosen, "bootpath", p, 1<<20);
  740. if (l > 0) {
  741. p[l] = 0; /* should already be null-terminated */
  742. bootpath = PTRUNRELOC(p);
  743. mem += l + 1;
  744. d = (char *) mem;
  745. *d = 0;
  746. call_prom("canon", 3, 1, p, d, 1<<20);
  747. bootdevice = PTRUNRELOC(d);
  748. mem = ALIGNUL(mem + strlen(d) + 1);
  749. }
  750. prom_instantiate_rtas();
  751. #ifdef CONFIG_POWER4
  752. /*
  753. * Find out how much memory we have and allocate a
  754. * suitably-sized hash table.
  755. */
  756. prom_alloc_htab();
  757. #endif
  758. mem = check_display(mem);
  759. prom_print("copying OF device tree...");
  760. mem = copy_device_tree(mem, mem + (1<<20));
  761. prom_print("done\n");
  762. prom_hold_cpus(mem);
  763. klimit = (char *) (mem - offset);
  764. node = call_prom("finddevice", 1, 1, "/");
  765. rc = call_prom("getprop", 4, 1, node, "model", model, sizeof(model));
  766. if (rc > 0 && !strncmp (model, "Pegasos", 7)
  767. && strncmp (model, "Pegasos2", 8)) {
  768. /* Pegasos 1 has a broken translate method in the OF,
  769. * and furthermore the BATs are mapped 1:1 so the phys
  770. * address calculated above is correct, so let's use
  771. * it directly.
  772. */
  773. } else if (offset == 0) {
  774. /* If we are already running at 0xc0000000, we assume we were
  775. * loaded by an OF bootloader which did set a BAT for us.
  776. * This breaks OF translate so we force phys to be 0.
  777. */
  778. prom_print("(already at 0xc0000000) phys=0\n");
  779. phys = 0;
  780. } else if (call_prom("getprop", 4, 1, prom_chosen, "mmu",
  781. &prom_mmu, sizeof(prom_mmu)) <= 0) {
  782. prom_print(" no MMU found\n");
  783. } else if (call_prom_ret("call-method", 4, 4, result, "translate",
  784. prom_mmu, &_stext, 1) != 0) {
  785. prom_print(" (translate failed)\n");
  786. } else {
  787. /* We assume the phys. address size is 3 cells */
  788. phys = result[2];
  789. }
  790. if (prom_disp_node != 0)
  791. setup_disp_fake_bi(prom_disp_node);
  792. /* Use quiesce call to get OF to shut down any devices it's using */
  793. prom_print("Calling quiesce ...\n");
  794. call_prom("quiesce", 0, 0);
  795. /* Relocate various pointers which will be used once the
  796. kernel is running at the address it was linked at. */
  797. for (i = 0; i < prom_num_displays; ++i)
  798. prom_display_paths[i] = PTRUNRELOC(prom_display_paths[i]);
  799. #ifdef CONFIG_SERIAL_CORE_CONSOLE
  800. /* Relocate the of stdout for console autodetection */
  801. of_stdout_device = PTRUNRELOC(of_stdout_device);
  802. #endif
  803. prom_print("returning 0x");
  804. prom_print_hex(phys);
  805. prom_print("from prom_init\n");
  806. prom_stdout = 0;
  807. return phys;
  808. }
  809. /*
  810. * early_get_property is used to access the device tree image prepared
  811. * by BootX very early on, before the pointers in it have been relocated.
  812. */
  813. static void * __init
  814. early_get_property(unsigned long base, unsigned long node, char *prop)
  815. {
  816. struct device_node *np = (struct device_node *)(base + node);
  817. struct property *pp;
  818. for (pp = np->properties; pp != 0; pp = pp->next) {
  819. pp = (struct property *) (base + (unsigned long)pp);
  820. if (strcmp((char *)((unsigned long)pp->name + base),
  821. prop) == 0) {
  822. return (void *)((unsigned long)pp->value + base);
  823. }
  824. }
  825. return NULL;
  826. }
  827. /* Is boot-info compatible ? */
  828. #define BOOT_INFO_IS_COMPATIBLE(bi) ((bi)->compatible_version <= BOOT_INFO_VERSION)
  829. #define BOOT_INFO_IS_V2_COMPATIBLE(bi) ((bi)->version >= 2)
  830. #define BOOT_INFO_IS_V4_COMPATIBLE(bi) ((bi)->version >= 4)
  831. void __init
  832. bootx_init(unsigned long r4, unsigned long phys)
  833. {
  834. boot_infos_t *bi = (boot_infos_t *) r4;
  835. unsigned long space;
  836. unsigned long ptr, x;
  837. char *model;
  838. boot_infos = PTRUNRELOC(bi);
  839. if (!BOOT_INFO_IS_V2_COMPATIBLE(bi))
  840. bi->logicalDisplayBase = NULL;
  841. #ifdef CONFIG_BOOTX_TEXT
  842. btext_init(bi);
  843. /*
  844. * Test if boot-info is compatible. Done only in config
  845. * CONFIG_BOOTX_TEXT since there is nothing much we can do
  846. * with an incompatible version, except display a message
  847. * and eventually hang the processor...
  848. *
  849. * I'll try to keep enough of boot-info compatible in the
  850. * future to always allow display of this message;
  851. */
  852. if (!BOOT_INFO_IS_COMPATIBLE(bi)) {
  853. btext_drawstring(" !!! WARNING - Incompatible version of BootX !!!\n\n\n");
  854. btext_flushscreen();
  855. }
  856. #endif /* CONFIG_BOOTX_TEXT */
  857. /* New BootX enters kernel with MMU off, i/os are not allowed
  858. here. This hack will have been done by the boostrap anyway.
  859. */
  860. if (bi->version < 4) {
  861. /*
  862. * XXX If this is an iMac, turn off the USB controller.
  863. */
  864. model = (char *) early_get_property
  865. (r4 + bi->deviceTreeOffset, 4, "model");
  866. if (model
  867. && (strcmp(model, "iMac,1") == 0
  868. || strcmp(model, "PowerMac1,1") == 0)) {
  869. out_le32((unsigned *)0x80880008, 1); /* XXX */
  870. }
  871. }
  872. /* Move klimit to enclose device tree, args, ramdisk, etc... */
  873. if (bi->version < 5) {
  874. space = bi->deviceTreeOffset + bi->deviceTreeSize;
  875. if (bi->ramDisk)
  876. space = bi->ramDisk + bi->ramDiskSize;
  877. } else
  878. space = bi->totalParamsSize;
  879. klimit = PTRUNRELOC((char *) bi + space);
  880. /* New BootX will have flushed all TLBs and enters kernel with
  881. MMU switched OFF, so this should not be useful anymore.
  882. */
  883. if (bi->version < 4) {
  884. /*
  885. * Touch each page to make sure the PTEs for them
  886. * are in the hash table - the aim is to try to avoid
  887. * getting DSI exceptions while copying the kernel image.
  888. */
  889. for (ptr = ((unsigned long) &_stext) & PAGE_MASK;
  890. ptr < (unsigned long)bi + space; ptr += PAGE_SIZE)
  891. x = *(volatile unsigned long *)ptr;
  892. }
  893. #ifdef CONFIG_BOOTX_TEXT
  894. /*
  895. * Note that after we call btext_prepare_BAT, we can't do
  896. * prom_draw*, flushscreen or clearscreen until we turn the MMU
  897. * on, since btext_prepare_BAT sets disp_bi.logicalDisplayBase
  898. * to a virtual address.
  899. */
  900. btext_prepare_BAT();
  901. #endif
  902. }