prom_init.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  1. /*
  2. *
  3. *
  4. * Procedures for interfacing to Open Firmware.
  5. *
  6. * Paul Mackerras August 1996.
  7. * Copyright (C) 1996 Paul Mackerras.
  8. *
  9. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  10. * {engebret|bergner}@us.ibm.com
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #undef DEBUG_PROM
  18. #include <stdarg.h>
  19. #include <linux/config.h>
  20. #include <linux/kernel.h>
  21. #include <linux/string.h>
  22. #include <linux/init.h>
  23. #include <linux/version.h>
  24. #include <linux/threads.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/types.h>
  27. #include <linux/pci.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/stringify.h>
  30. #include <linux/delay.h>
  31. #include <linux/initrd.h>
  32. #include <linux/bitops.h>
  33. #include <asm/prom.h>
  34. #include <asm/rtas.h>
  35. #include <asm/abs_addr.h>
  36. #include <asm/page.h>
  37. #include <asm/processor.h>
  38. #include <asm/irq.h>
  39. #include <asm/io.h>
  40. #include <asm/smp.h>
  41. #include <asm/system.h>
  42. #include <asm/mmu.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/pci.h>
  45. #include <asm/iommu.h>
  46. #include <asm/bootinfo.h>
  47. #include <asm/ppcdebug.h>
  48. #include <asm/btext.h>
  49. #include <asm/sections.h>
  50. #include <asm/machdep.h>
  51. #ifdef CONFIG_LOGO_LINUX_CLUT224
  52. #include <linux/linux_logo.h>
  53. extern const struct linux_logo logo_linux_clut224;
  54. #endif
  55. /*
  56. * Properties whose value is longer than this get excluded from our
  57. * copy of the device tree. This value does need to be big enough to
  58. * ensure that we don't lose things like the interrupt-map property
  59. * on a PCI-PCI bridge.
  60. */
  61. #define MAX_PROPERTY_LENGTH (1UL * 1024 * 1024)
  62. /*
  63. * Eventually bump that one up
  64. */
  65. #define DEVTREE_CHUNK_SIZE 0x100000
  66. /*
  67. * This is the size of the local memory reserve map that gets copied
  68. * into the boot params passed to the kernel. That size is totally
  69. * flexible as the kernel just reads the list until it encounters an
  70. * entry with size 0, so it can be changed without breaking binary
  71. * compatibility
  72. */
  73. #define MEM_RESERVE_MAP_SIZE 8
  74. /*
  75. * prom_init() is called very early on, before the kernel text
  76. * and data have been mapped to KERNELBASE. At this point the code
  77. * is running at whatever address it has been loaded at, so
  78. * references to extern and static variables must be relocated
  79. * explicitly. The procedure reloc_offset() returns the address
  80. * we're currently running at minus the address we were linked at.
  81. * (Note that strings count as static variables.)
  82. *
  83. * Because OF may have mapped I/O devices into the area starting at
  84. * KERNELBASE, particularly on CHRP machines, we can't safely call
  85. * OF once the kernel has been mapped to KERNELBASE. Therefore all
  86. * OF calls should be done within prom_init(), and prom_init()
  87. * and all routines called within it must be careful to relocate
  88. * references as necessary.
  89. *
  90. * Note that the bss is cleared *after* prom_init runs, so we have
  91. * to make sure that any static or extern variables it accesses
  92. * are put in the data segment.
  93. */
  94. #define PROM_BUG() do { \
  95. prom_printf("kernel BUG at %s line 0x%x!\n", \
  96. RELOC(__FILE__), __LINE__); \
  97. __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \
  98. } while (0)
  99. #ifdef DEBUG_PROM
  100. #define prom_debug(x...) prom_printf(x)
  101. #else
  102. #define prom_debug(x...)
  103. #endif
  104. typedef u32 prom_arg_t;
  105. struct prom_args {
  106. u32 service;
  107. u32 nargs;
  108. u32 nret;
  109. prom_arg_t args[10];
  110. prom_arg_t *rets; /* Pointer to return values in args[16]. */
  111. };
  112. struct prom_t {
  113. unsigned long entry;
  114. ihandle root;
  115. ihandle chosen;
  116. int cpu;
  117. ihandle stdout;
  118. ihandle disp_node;
  119. struct prom_args args;
  120. unsigned long version;
  121. unsigned long root_size_cells;
  122. unsigned long root_addr_cells;
  123. };
  124. struct pci_reg_property {
  125. struct pci_address addr;
  126. u32 size_hi;
  127. u32 size_lo;
  128. };
  129. struct mem_map_entry {
  130. u64 base;
  131. u64 size;
  132. };
  133. typedef u32 cell_t;
  134. extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
  135. extern void enter_prom(struct prom_args *args, unsigned long entry);
  136. extern void copy_and_flush(unsigned long dest, unsigned long src,
  137. unsigned long size, unsigned long offset);
  138. extern unsigned long klimit;
  139. /* prom structure */
  140. static struct prom_t __initdata prom;
  141. #define PROM_SCRATCH_SIZE 256
  142. static char __initdata of_stdout_device[256];
  143. static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
  144. static unsigned long __initdata dt_header_start;
  145. static unsigned long __initdata dt_struct_start, dt_struct_end;
  146. static unsigned long __initdata dt_string_start, dt_string_end;
  147. static unsigned long __initdata prom_initrd_start, prom_initrd_end;
  148. static int __initdata iommu_force_on;
  149. static int __initdata ppc64_iommu_off;
  150. static int __initdata of_platform;
  151. static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
  152. static unsigned long __initdata prom_memory_limit;
  153. static unsigned long __initdata prom_tce_alloc_start;
  154. static unsigned long __initdata prom_tce_alloc_end;
  155. static unsigned long __initdata alloc_top;
  156. static unsigned long __initdata alloc_top_high;
  157. static unsigned long __initdata alloc_bottom;
  158. static unsigned long __initdata rmo_top;
  159. static unsigned long __initdata ram_top;
  160. static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
  161. static int __initdata mem_reserve_cnt;
  162. static cell_t __initdata regbuf[1024];
  163. #define MAX_CPU_THREADS 2
  164. /* TO GO */
  165. #ifdef CONFIG_HMT
  166. struct {
  167. unsigned int pir;
  168. unsigned int threadid;
  169. } hmt_thread_data[NR_CPUS];
  170. #endif /* CONFIG_HMT */
  171. /*
  172. * This are used in calls to call_prom. The 4th and following
  173. * arguments to call_prom should be 32-bit values. 64 bit values
  174. * are truncated to 32 bits (and fortunately don't get interpreted
  175. * as two arguments).
  176. */
  177. #define ADDR(x) (u32) ((unsigned long)(x) - offset)
  178. /*
  179. * Error results ... some OF calls will return "-1" on error, some
  180. * will return 0, some will return either. To simplify, here are
  181. * macros to use with any ihandle or phandle return value to check if
  182. * it is valid
  183. */
  184. #define PROM_ERROR (-1u)
  185. #define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
  186. #define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
  187. /* This is the one and *ONLY* place where we actually call open
  188. * firmware from, since we need to make sure we're running in 32b
  189. * mode when we do. We switch back to 64b mode upon return.
  190. */
  191. static int __init call_prom(const char *service, int nargs, int nret, ...)
  192. {
  193. int i;
  194. unsigned long offset = reloc_offset();
  195. struct prom_t *_prom = PTRRELOC(&prom);
  196. va_list list;
  197. _prom->args.service = ADDR(service);
  198. _prom->args.nargs = nargs;
  199. _prom->args.nret = nret;
  200. _prom->args.rets = (prom_arg_t *)&(_prom->args.args[nargs]);
  201. va_start(list, nret);
  202. for (i=0; i < nargs; i++)
  203. _prom->args.args[i] = va_arg(list, prom_arg_t);
  204. va_end(list);
  205. for (i=0; i < nret ;i++)
  206. _prom->args.rets[i] = 0;
  207. enter_prom(&_prom->args, _prom->entry);
  208. return (nret > 0) ? _prom->args.rets[0] : 0;
  209. }
  210. static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
  211. unsigned long align)
  212. {
  213. return (unsigned int)call_prom("claim", 3, 1,
  214. (prom_arg_t)virt, (prom_arg_t)size,
  215. (prom_arg_t)align);
  216. }
  217. static void __init prom_print(const char *msg)
  218. {
  219. const char *p, *q;
  220. unsigned long offset = reloc_offset();
  221. struct prom_t *_prom = PTRRELOC(&prom);
  222. if (_prom->stdout == 0)
  223. return;
  224. for (p = msg; *p != 0; p = q) {
  225. for (q = p; *q != 0 && *q != '\n'; ++q)
  226. ;
  227. if (q > p)
  228. call_prom("write", 3, 1, _prom->stdout, p, q - p);
  229. if (*q == 0)
  230. break;
  231. ++q;
  232. call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
  233. }
  234. }
  235. static void __init prom_print_hex(unsigned long val)
  236. {
  237. unsigned long offset = reloc_offset();
  238. int i, nibbles = sizeof(val)*2;
  239. char buf[sizeof(val)*2+1];
  240. struct prom_t *_prom = PTRRELOC(&prom);
  241. for (i = nibbles-1; i >= 0; i--) {
  242. buf[i] = (val & 0xf) + '0';
  243. if (buf[i] > '9')
  244. buf[i] += ('a'-'0'-10);
  245. val >>= 4;
  246. }
  247. buf[nibbles] = '\0';
  248. call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
  249. }
  250. static void __init prom_printf(const char *format, ...)
  251. {
  252. unsigned long offset = reloc_offset();
  253. const char *p, *q, *s;
  254. va_list args;
  255. unsigned long v;
  256. struct prom_t *_prom = PTRRELOC(&prom);
  257. va_start(args, format);
  258. for (p = PTRRELOC(format); *p != 0; p = q) {
  259. for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
  260. ;
  261. if (q > p)
  262. call_prom("write", 3, 1, _prom->stdout, p, q - p);
  263. if (*q == 0)
  264. break;
  265. if (*q == '\n') {
  266. ++q;
  267. call_prom("write", 3, 1, _prom->stdout,
  268. ADDR("\r\n"), 2);
  269. continue;
  270. }
  271. ++q;
  272. if (*q == 0)
  273. break;
  274. switch (*q) {
  275. case 's':
  276. ++q;
  277. s = va_arg(args, const char *);
  278. prom_print(s);
  279. break;
  280. case 'x':
  281. ++q;
  282. v = va_arg(args, unsigned long);
  283. prom_print_hex(v);
  284. break;
  285. }
  286. }
  287. }
  288. static void __init __attribute__((noreturn)) prom_panic(const char *reason)
  289. {
  290. unsigned long offset = reloc_offset();
  291. prom_print(PTRRELOC(reason));
  292. /* ToDo: should put up an SRC here */
  293. call_prom("exit", 0, 0);
  294. for (;;) /* should never get here */
  295. ;
  296. }
  297. static int __init prom_next_node(phandle *nodep)
  298. {
  299. phandle node;
  300. if ((node = *nodep) != 0
  301. && (*nodep = call_prom("child", 1, 1, node)) != 0)
  302. return 1;
  303. if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
  304. return 1;
  305. for (;;) {
  306. if ((node = call_prom("parent", 1, 1, node)) == 0)
  307. return 0;
  308. if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
  309. return 1;
  310. }
  311. }
  312. static int __init prom_getprop(phandle node, const char *pname,
  313. void *value, size_t valuelen)
  314. {
  315. unsigned long offset = reloc_offset();
  316. return call_prom("getprop", 4, 1, node, ADDR(pname),
  317. (u32)(unsigned long) value, (u32) valuelen);
  318. }
  319. static int __init prom_getproplen(phandle node, const char *pname)
  320. {
  321. unsigned long offset = reloc_offset();
  322. return call_prom("getproplen", 2, 1, node, ADDR(pname));
  323. }
  324. static int __init prom_setprop(phandle node, const char *pname,
  325. void *value, size_t valuelen)
  326. {
  327. unsigned long offset = reloc_offset();
  328. return call_prom("setprop", 4, 1, node, ADDR(pname),
  329. (u32)(unsigned long) value, (u32) valuelen);
  330. }
  331. /* We can't use the standard versions because of RELOC headaches. */
  332. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  333. || ('a' <= (c) && (c) <= 'f') \
  334. || ('A' <= (c) && (c) <= 'F'))
  335. #define isdigit(c) ('0' <= (c) && (c) <= '9')
  336. #define islower(c) ('a' <= (c) && (c) <= 'z')
  337. #define toupper(c) (islower(c) ? ((c) - 'a' + 'A') : (c))
  338. unsigned long prom_strtoul(const char *cp, const char **endp)
  339. {
  340. unsigned long result = 0, base = 10, value;
  341. if (*cp == '0') {
  342. base = 8;
  343. cp++;
  344. if (toupper(*cp) == 'X') {
  345. cp++;
  346. base = 16;
  347. }
  348. }
  349. while (isxdigit(*cp) &&
  350. (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
  351. result = result * base + value;
  352. cp++;
  353. }
  354. if (endp)
  355. *endp = cp;
  356. return result;
  357. }
  358. unsigned long prom_memparse(const char *ptr, const char **retptr)
  359. {
  360. unsigned long ret = prom_strtoul(ptr, retptr);
  361. int shift = 0;
  362. /*
  363. * We can't use a switch here because GCC *may* generate a
  364. * jump table which won't work, because we're not running at
  365. * the address we're linked at.
  366. */
  367. if ('G' == **retptr || 'g' == **retptr)
  368. shift = 30;
  369. if ('M' == **retptr || 'm' == **retptr)
  370. shift = 20;
  371. if ('K' == **retptr || 'k' == **retptr)
  372. shift = 10;
  373. if (shift) {
  374. ret <<= shift;
  375. (*retptr)++;
  376. }
  377. return ret;
  378. }
  379. /*
  380. * Early parsing of the command line passed to the kernel, used for
  381. * "mem=x" and the options that affect the iommu
  382. */
  383. static void __init early_cmdline_parse(void)
  384. {
  385. unsigned long offset = reloc_offset();
  386. struct prom_t *_prom = PTRRELOC(&prom);
  387. char *opt, *p;
  388. int l = 0;
  389. RELOC(prom_cmd_line[0]) = 0;
  390. p = RELOC(prom_cmd_line);
  391. if ((long)_prom->chosen > 0)
  392. l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
  393. #ifdef CONFIG_CMDLINE
  394. if (l == 0) /* dbl check */
  395. strlcpy(RELOC(prom_cmd_line),
  396. RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
  397. #endif /* CONFIG_CMDLINE */
  398. prom_printf("command line: %s\n", RELOC(prom_cmd_line));
  399. opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
  400. if (opt) {
  401. prom_printf("iommu opt is: %s\n", opt);
  402. opt += 6;
  403. while (*opt && *opt == ' ')
  404. opt++;
  405. if (!strncmp(opt, RELOC("off"), 3))
  406. RELOC(ppc64_iommu_off) = 1;
  407. else if (!strncmp(opt, RELOC("force"), 5))
  408. RELOC(iommu_force_on) = 1;
  409. }
  410. opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
  411. if (opt) {
  412. opt += 4;
  413. RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
  414. /* Align to 16 MB == size of large page */
  415. RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
  416. }
  417. }
  418. /*
  419. * To tell the firmware what our capabilities are, we have to pass
  420. * it a fake 32-bit ELF header containing a couple of PT_NOTE sections
  421. * that contain structures that contain the actual values.
  422. */
  423. static struct fake_elf {
  424. Elf32_Ehdr elfhdr;
  425. Elf32_Phdr phdr[2];
  426. struct chrpnote {
  427. u32 namesz;
  428. u32 descsz;
  429. u32 type;
  430. char name[8]; /* "PowerPC" */
  431. struct chrpdesc {
  432. u32 real_mode;
  433. u32 real_base;
  434. u32 real_size;
  435. u32 virt_base;
  436. u32 virt_size;
  437. u32 load_base;
  438. } chrpdesc;
  439. } chrpnote;
  440. struct rpanote {
  441. u32 namesz;
  442. u32 descsz;
  443. u32 type;
  444. char name[24]; /* "IBM,RPA-Client-Config" */
  445. struct rpadesc {
  446. u32 lpar_affinity;
  447. u32 min_rmo_size;
  448. u32 min_rmo_percent;
  449. u32 max_pft_size;
  450. u32 splpar;
  451. u32 min_load;
  452. u32 new_mem_def;
  453. u32 ignore_me;
  454. } rpadesc;
  455. } rpanote;
  456. } fake_elf = {
  457. .elfhdr = {
  458. .e_ident = { 0x7f, 'E', 'L', 'F',
  459. ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
  460. .e_type = ET_EXEC, /* yeah right */
  461. .e_machine = EM_PPC,
  462. .e_version = EV_CURRENT,
  463. .e_phoff = offsetof(struct fake_elf, phdr),
  464. .e_phentsize = sizeof(Elf32_Phdr),
  465. .e_phnum = 2
  466. },
  467. .phdr = {
  468. [0] = {
  469. .p_type = PT_NOTE,
  470. .p_offset = offsetof(struct fake_elf, chrpnote),
  471. .p_filesz = sizeof(struct chrpnote)
  472. }, [1] = {
  473. .p_type = PT_NOTE,
  474. .p_offset = offsetof(struct fake_elf, rpanote),
  475. .p_filesz = sizeof(struct rpanote)
  476. }
  477. },
  478. .chrpnote = {
  479. .namesz = sizeof("PowerPC"),
  480. .descsz = sizeof(struct chrpdesc),
  481. .type = 0x1275,
  482. .name = "PowerPC",
  483. .chrpdesc = {
  484. .real_mode = ~0U, /* ~0 means "don't care" */
  485. .real_base = ~0U,
  486. .real_size = ~0U,
  487. .virt_base = ~0U,
  488. .virt_size = ~0U,
  489. .load_base = ~0U
  490. },
  491. },
  492. .rpanote = {
  493. .namesz = sizeof("IBM,RPA-Client-Config"),
  494. .descsz = sizeof(struct rpadesc),
  495. .type = 0x12759999,
  496. .name = "IBM,RPA-Client-Config",
  497. .rpadesc = {
  498. .lpar_affinity = 0,
  499. .min_rmo_size = 64, /* in megabytes */
  500. .min_rmo_percent = 0,
  501. .max_pft_size = 48, /* 2^48 bytes max PFT size */
  502. .splpar = 1,
  503. .min_load = ~0U,
  504. .new_mem_def = 0
  505. }
  506. }
  507. };
  508. static void __init prom_send_capabilities(void)
  509. {
  510. unsigned long offset = reloc_offset();
  511. ihandle elfloader;
  512. elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
  513. if (elfloader == 0) {
  514. prom_printf("couldn't open /packages/elf-loader\n");
  515. return;
  516. }
  517. call_prom("call-method", 3, 1, ADDR("process-elf-header"),
  518. elfloader, ADDR(&fake_elf));
  519. call_prom("close", 1, 0, elfloader);
  520. }
  521. /*
  522. * Memory allocation strategy... our layout is normally:
  523. *
  524. * at 14Mb or more we vmlinux, then a gap and initrd. In some rare cases, initrd
  525. * might end up beeing before the kernel though. We assume this won't override
  526. * the final kernel at 0, we have no provision to handle that in this version,
  527. * but it should hopefully never happen.
  528. *
  529. * alloc_top is set to the top of RMO, eventually shrink down if the TCEs overlap
  530. * alloc_bottom is set to the top of kernel/initrd
  531. *
  532. * from there, allocations are done that way : rtas is allocated topmost, and
  533. * the device-tree is allocated from the bottom. We try to grow the device-tree
  534. * allocation as we progress. If we can't, then we fail, we don't currently have
  535. * a facility to restart elsewhere, but that shouldn't be necessary neither
  536. *
  537. * Note that calls to reserve_mem have to be done explicitely, memory allocated
  538. * with either alloc_up or alloc_down isn't automatically reserved.
  539. */
  540. /*
  541. * Allocates memory in the RMO upward from the kernel/initrd
  542. *
  543. * When align is 0, this is a special case, it means to allocate in place
  544. * at the current location of alloc_bottom or fail (that is basically
  545. * extending the previous allocation). Used for the device-tree flattening
  546. */
  547. static unsigned long __init alloc_up(unsigned long size, unsigned long align)
  548. {
  549. unsigned long offset = reloc_offset();
  550. unsigned long base = _ALIGN_UP(RELOC(alloc_bottom), align);
  551. unsigned long addr = 0;
  552. prom_debug("alloc_up(%x, %x)\n", size, align);
  553. if (RELOC(ram_top) == 0)
  554. prom_panic("alloc_up() called with mem not initialized\n");
  555. if (align)
  556. base = _ALIGN_UP(RELOC(alloc_bottom), align);
  557. else
  558. base = RELOC(alloc_bottom);
  559. for(; (base + size) <= RELOC(alloc_top);
  560. base = _ALIGN_UP(base + 0x100000, align)) {
  561. prom_debug(" trying: 0x%x\n\r", base);
  562. addr = (unsigned long)prom_claim(base, size, 0);
  563. if (addr != PROM_ERROR)
  564. break;
  565. addr = 0;
  566. if (align == 0)
  567. break;
  568. }
  569. if (addr == 0)
  570. return 0;
  571. RELOC(alloc_bottom) = addr;
  572. prom_debug(" -> %x\n", addr);
  573. prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  574. prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
  575. prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  576. prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
  577. prom_debug(" ram_top : %x\n", RELOC(ram_top));
  578. return addr;
  579. }
  580. /*
  581. * Allocates memory downard, either from top of RMO, or if highmem
  582. * is set, from the top of RAM. Note that this one doesn't handle
  583. * failures. In does claim memory if highmem is not set.
  584. */
  585. static unsigned long __init alloc_down(unsigned long size, unsigned long align,
  586. int highmem)
  587. {
  588. unsigned long offset = reloc_offset();
  589. unsigned long base, addr = 0;
  590. prom_debug("alloc_down(%x, %x, %s)\n", size, align,
  591. highmem ? RELOC("(high)") : RELOC("(low)"));
  592. if (RELOC(ram_top) == 0)
  593. prom_panic("alloc_down() called with mem not initialized\n");
  594. if (highmem) {
  595. /* Carve out storage for the TCE table. */
  596. addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
  597. if (addr <= RELOC(alloc_bottom))
  598. return 0;
  599. else {
  600. /* Will we bump into the RMO ? If yes, check out that we
  601. * didn't overlap existing allocations there, if we did,
  602. * we are dead, we must be the first in town !
  603. */
  604. if (addr < RELOC(rmo_top)) {
  605. /* Good, we are first */
  606. if (RELOC(alloc_top) == RELOC(rmo_top))
  607. RELOC(alloc_top) = RELOC(rmo_top) = addr;
  608. else
  609. return 0;
  610. }
  611. RELOC(alloc_top_high) = addr;
  612. }
  613. goto bail;
  614. }
  615. base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
  616. for(; base > RELOC(alloc_bottom); base = _ALIGN_DOWN(base - 0x100000, align)) {
  617. prom_debug(" trying: 0x%x\n\r", base);
  618. addr = (unsigned long)prom_claim(base, size, 0);
  619. if (addr != PROM_ERROR)
  620. break;
  621. addr = 0;
  622. }
  623. if (addr == 0)
  624. return 0;
  625. RELOC(alloc_top) = addr;
  626. bail:
  627. prom_debug(" -> %x\n", addr);
  628. prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  629. prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
  630. prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  631. prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
  632. prom_debug(" ram_top : %x\n", RELOC(ram_top));
  633. return addr;
  634. }
  635. /*
  636. * Parse a "reg" cell
  637. */
  638. static unsigned long __init prom_next_cell(int s, cell_t **cellp)
  639. {
  640. cell_t *p = *cellp;
  641. unsigned long r = 0;
  642. /* Ignore more than 2 cells */
  643. while (s > 2) {
  644. p++;
  645. s--;
  646. }
  647. while (s) {
  648. r <<= 32;
  649. r |= *(p++);
  650. s--;
  651. }
  652. *cellp = p;
  653. return r;
  654. }
  655. /*
  656. * Very dumb function for adding to the memory reserve list, but
  657. * we don't need anything smarter at this point
  658. *
  659. * XXX Eventually check for collisions. They should NEVER happen
  660. * if problems seem to show up, it would be a good start to track
  661. * them down.
  662. */
  663. static void reserve_mem(unsigned long base, unsigned long size)
  664. {
  665. unsigned long offset = reloc_offset();
  666. unsigned long top = base + size;
  667. unsigned long cnt = RELOC(mem_reserve_cnt);
  668. if (size == 0)
  669. return;
  670. /* We need to always keep one empty entry so that we
  671. * have our terminator with "size" set to 0 since we are
  672. * dumb and just copy this entire array to the boot params
  673. */
  674. base = _ALIGN_DOWN(base, PAGE_SIZE);
  675. top = _ALIGN_UP(top, PAGE_SIZE);
  676. size = top - base;
  677. if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
  678. prom_panic("Memory reserve map exhausted !\n");
  679. RELOC(mem_reserve_map)[cnt].base = base;
  680. RELOC(mem_reserve_map)[cnt].size = size;
  681. RELOC(mem_reserve_cnt) = cnt + 1;
  682. }
  683. /*
  684. * Initialize memory allocation mecanism, parse "memory" nodes and
  685. * obtain that way the top of memory and RMO to setup out local allocator
  686. */
  687. static void __init prom_init_mem(void)
  688. {
  689. phandle node;
  690. char *path, type[64];
  691. unsigned int plen;
  692. cell_t *p, *endp;
  693. unsigned long offset = reloc_offset();
  694. struct prom_t *_prom = PTRRELOC(&prom);
  695. /*
  696. * We iterate the memory nodes to find
  697. * 1) top of RMO (first node)
  698. * 2) top of memory
  699. */
  700. prom_debug("root_addr_cells: %x\n", (long)_prom->root_addr_cells);
  701. prom_debug("root_size_cells: %x\n", (long)_prom->root_size_cells);
  702. prom_debug("scanning memory:\n");
  703. path = RELOC(prom_scratch);
  704. for (node = 0; prom_next_node(&node); ) {
  705. type[0] = 0;
  706. prom_getprop(node, "device_type", type, sizeof(type));
  707. if (strcmp(type, RELOC("memory")))
  708. continue;
  709. plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
  710. if (plen > sizeof(regbuf)) {
  711. prom_printf("memory node too large for buffer !\n");
  712. plen = sizeof(regbuf);
  713. }
  714. p = RELOC(regbuf);
  715. endp = p + (plen / sizeof(cell_t));
  716. #ifdef DEBUG_PROM
  717. memset(path, 0, PROM_SCRATCH_SIZE);
  718. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  719. prom_debug(" node %s :\n", path);
  720. #endif /* DEBUG_PROM */
  721. while ((endp - p) >= (_prom->root_addr_cells + _prom->root_size_cells)) {
  722. unsigned long base, size;
  723. base = prom_next_cell(_prom->root_addr_cells, &p);
  724. size = prom_next_cell(_prom->root_size_cells, &p);
  725. if (size == 0)
  726. continue;
  727. prom_debug(" %x %x\n", base, size);
  728. if (base == 0)
  729. RELOC(rmo_top) = size;
  730. if ((base + size) > RELOC(ram_top))
  731. RELOC(ram_top) = base + size;
  732. }
  733. }
  734. RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(klimit) - offset + 0x4000);
  735. /* Check if we have an initrd after the kernel, if we do move our bottom
  736. * point to after it
  737. */
  738. if (RELOC(prom_initrd_start)) {
  739. if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
  740. RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
  741. }
  742. /*
  743. * If prom_memory_limit is set we reduce the upper limits *except* for
  744. * alloc_top_high. This must be the real top of RAM so we can put
  745. * TCE's up there.
  746. */
  747. RELOC(alloc_top_high) = RELOC(ram_top);
  748. if (RELOC(prom_memory_limit)) {
  749. if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
  750. prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
  751. RELOC(prom_memory_limit));
  752. RELOC(prom_memory_limit) = 0;
  753. } else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
  754. prom_printf("Ignoring mem=%x >= ram_top.\n",
  755. RELOC(prom_memory_limit));
  756. RELOC(prom_memory_limit) = 0;
  757. } else {
  758. RELOC(ram_top) = RELOC(prom_memory_limit);
  759. RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
  760. }
  761. }
  762. /*
  763. * Setup our top alloc point, that is top of RMO or top of
  764. * segment 0 when running non-LPAR.
  765. */
  766. if ( RELOC(of_platform) == PLATFORM_PSERIES_LPAR )
  767. RELOC(alloc_top) = RELOC(rmo_top);
  768. else
  769. /* Some RS64 machines have buggy firmware where claims up at 1GB
  770. * fails. Cap at 768MB as a workaround. Still plenty of room.
  771. */
  772. RELOC(alloc_top) = RELOC(rmo_top) = min(0x30000000ul, RELOC(ram_top));
  773. prom_printf("memory layout at init:\n");
  774. prom_printf(" memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
  775. prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  776. prom_printf(" alloc_top : %x\n", RELOC(alloc_top));
  777. prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  778. prom_printf(" rmo_top : %x\n", RELOC(rmo_top));
  779. prom_printf(" ram_top : %x\n", RELOC(ram_top));
  780. }
  781. /*
  782. * Allocate room for and instanciate RTAS
  783. */
  784. static void __init prom_instantiate_rtas(void)
  785. {
  786. unsigned long offset = reloc_offset();
  787. struct prom_t *_prom = PTRRELOC(&prom);
  788. phandle rtas_node;
  789. ihandle rtas_inst;
  790. u32 base, entry = 0;
  791. u32 size = 0;
  792. prom_debug("prom_instantiate_rtas: start...\n");
  793. rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  794. prom_debug("rtas_node: %x\n", rtas_node);
  795. if (!PHANDLE_VALID(rtas_node))
  796. return;
  797. prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
  798. if (size == 0)
  799. return;
  800. base = alloc_down(size, PAGE_SIZE, 0);
  801. if (base == 0) {
  802. prom_printf("RTAS allocation failed !\n");
  803. return;
  804. }
  805. rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
  806. if (!IHANDLE_VALID(rtas_inst)) {
  807. prom_printf("opening rtas package failed");
  808. return;
  809. }
  810. prom_printf("instantiating rtas at 0x%x ...", base);
  811. if (call_prom("call-method", 3, 2,
  812. ADDR("instantiate-rtas"),
  813. rtas_inst, base) != PROM_ERROR) {
  814. entry = (long)_prom->args.rets[1];
  815. }
  816. if (entry == 0) {
  817. prom_printf(" failed\n");
  818. return;
  819. }
  820. prom_printf(" done\n");
  821. reserve_mem(base, size);
  822. prom_setprop(rtas_node, "linux,rtas-base", &base, sizeof(base));
  823. prom_setprop(rtas_node, "linux,rtas-entry", &entry, sizeof(entry));
  824. prom_debug("rtas base = 0x%x\n", base);
  825. prom_debug("rtas entry = 0x%x\n", entry);
  826. prom_debug("rtas size = 0x%x\n", (long)size);
  827. prom_debug("prom_instantiate_rtas: end...\n");
  828. }
  829. /*
  830. * Allocate room for and initialize TCE tables
  831. */
  832. static void __init prom_initialize_tce_table(void)
  833. {
  834. phandle node;
  835. ihandle phb_node;
  836. unsigned long offset = reloc_offset();
  837. char compatible[64], type[64], model[64];
  838. char *path = RELOC(prom_scratch);
  839. u64 base, align;
  840. u32 minalign, minsize;
  841. u64 tce_entry, *tce_entryp;
  842. u64 local_alloc_top, local_alloc_bottom;
  843. u64 i;
  844. if (RELOC(ppc64_iommu_off))
  845. return;
  846. prom_debug("starting prom_initialize_tce_table\n");
  847. /* Cache current top of allocs so we reserve a single block */
  848. local_alloc_top = RELOC(alloc_top_high);
  849. local_alloc_bottom = local_alloc_top;
  850. /* Search all nodes looking for PHBs. */
  851. for (node = 0; prom_next_node(&node); ) {
  852. compatible[0] = 0;
  853. type[0] = 0;
  854. model[0] = 0;
  855. prom_getprop(node, "compatible",
  856. compatible, sizeof(compatible));
  857. prom_getprop(node, "device_type", type, sizeof(type));
  858. prom_getprop(node, "model", model, sizeof(model));
  859. if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
  860. continue;
  861. /* Keep the old logic in tack to avoid regression. */
  862. if (compatible[0] != 0) {
  863. if ((strstr(compatible, RELOC("python")) == NULL) &&
  864. (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
  865. (strstr(compatible, RELOC("Winnipeg")) == NULL))
  866. continue;
  867. } else if (model[0] != 0) {
  868. if ((strstr(model, RELOC("ython")) == NULL) &&
  869. (strstr(model, RELOC("peedwagon")) == NULL) &&
  870. (strstr(model, RELOC("innipeg")) == NULL))
  871. continue;
  872. }
  873. if (prom_getprop(node, "tce-table-minalign", &minalign,
  874. sizeof(minalign)) == PROM_ERROR)
  875. minalign = 0;
  876. if (prom_getprop(node, "tce-table-minsize", &minsize,
  877. sizeof(minsize)) == PROM_ERROR)
  878. minsize = 4UL << 20;
  879. /*
  880. * Even though we read what OF wants, we just set the table
  881. * size to 4 MB. This is enough to map 2GB of PCI DMA space.
  882. * By doing this, we avoid the pitfalls of trying to DMA to
  883. * MMIO space and the DMA alias hole.
  884. *
  885. * On POWER4, firmware sets the TCE region by assuming
  886. * each TCE table is 8MB. Using this memory for anything
  887. * else will impact performance, so we always allocate 8MB.
  888. * Anton
  889. */
  890. if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
  891. minsize = 8UL << 20;
  892. else
  893. minsize = 4UL << 20;
  894. /* Align to the greater of the align or size */
  895. align = max(minalign, minsize);
  896. base = alloc_down(minsize, align, 1);
  897. if (base == 0)
  898. prom_panic("ERROR, cannot find space for TCE table.\n");
  899. if (base < local_alloc_bottom)
  900. local_alloc_bottom = base;
  901. /* Save away the TCE table attributes for later use. */
  902. prom_setprop(node, "linux,tce-base", &base, sizeof(base));
  903. prom_setprop(node, "linux,tce-size", &minsize, sizeof(minsize));
  904. /* It seems OF doesn't null-terminate the path :-( */
  905. memset(path, 0, sizeof(path));
  906. /* Call OF to setup the TCE hardware */
  907. if (call_prom("package-to-path", 3, 1, node,
  908. path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
  909. prom_printf("package-to-path failed\n");
  910. }
  911. prom_debug("TCE table: %s\n", path);
  912. prom_debug("\tnode = 0x%x\n", node);
  913. prom_debug("\tbase = 0x%x\n", base);
  914. prom_debug("\tsize = 0x%x\n", minsize);
  915. /* Initialize the table to have a one-to-one mapping
  916. * over the allocated size.
  917. */
  918. tce_entryp = (unsigned long *)base;
  919. for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
  920. tce_entry = (i << PAGE_SHIFT);
  921. tce_entry |= 0x3;
  922. *tce_entryp = tce_entry;
  923. }
  924. prom_printf("opening PHB %s", path);
  925. phb_node = call_prom("open", 1, 1, path);
  926. if (phb_node == 0)
  927. prom_printf("... failed\n");
  928. else
  929. prom_printf("... done\n");
  930. call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
  931. phb_node, -1, minsize,
  932. (u32) base, (u32) (base >> 32));
  933. call_prom("close", 1, 0, phb_node);
  934. }
  935. reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
  936. if (RELOC(prom_memory_limit)) {
  937. /*
  938. * We align the start to a 16MB boundary so we can map the TCE area
  939. * using large pages if possible. The end should be the top of RAM
  940. * so no need to align it.
  941. */
  942. RELOC(prom_tce_alloc_start) = _ALIGN_DOWN(local_alloc_bottom, 0x1000000);
  943. RELOC(prom_tce_alloc_end) = local_alloc_top;
  944. }
  945. /* Flag the first invalid entry */
  946. prom_debug("ending prom_initialize_tce_table\n");
  947. }
  948. /*
  949. * With CHRP SMP we need to use the OF to start the other
  950. * processors so we can't wait until smp_boot_cpus (the OF is
  951. * trashed by then) so we have to put the processors into
  952. * a holding pattern controlled by the kernel (not OF) before
  953. * we destroy the OF.
  954. *
  955. * This uses a chunk of low memory, puts some holding pattern
  956. * code there and sends the other processors off to there until
  957. * smp_boot_cpus tells them to do something. The holding pattern
  958. * checks that address until its cpu # is there, when it is that
  959. * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
  960. * of setting those values.
  961. *
  962. * We also use physical address 0x4 here to tell when a cpu
  963. * is in its holding pattern code.
  964. *
  965. * Fixup comment... DRENG / PPPBBB - Peter
  966. *
  967. * -- Cort
  968. */
  969. static void __init prom_hold_cpus(void)
  970. {
  971. unsigned long i;
  972. unsigned int reg;
  973. phandle node;
  974. unsigned long offset = reloc_offset();
  975. char type[64];
  976. int cpuid = 0;
  977. unsigned int interrupt_server[MAX_CPU_THREADS];
  978. unsigned int cpu_threads, hw_cpu_num;
  979. int propsize;
  980. extern void __secondary_hold(void);
  981. extern unsigned long __secondary_hold_spinloop;
  982. extern unsigned long __secondary_hold_acknowledge;
  983. unsigned long *spinloop
  984. = (void *)virt_to_abs(&__secondary_hold_spinloop);
  985. unsigned long *acknowledge
  986. = (void *)virt_to_abs(&__secondary_hold_acknowledge);
  987. unsigned long secondary_hold
  988. = virt_to_abs(*PTRRELOC((unsigned long *)__secondary_hold));
  989. struct prom_t *_prom = PTRRELOC(&prom);
  990. prom_debug("prom_hold_cpus: start...\n");
  991. prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
  992. prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
  993. prom_debug(" 1) acknowledge = 0x%x\n",
  994. (unsigned long)acknowledge);
  995. prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
  996. prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
  997. /* Set the common spinloop variable, so all of the secondary cpus
  998. * will block when they are awakened from their OF spinloop.
  999. * This must occur for both SMP and non SMP kernels, since OF will
  1000. * be trashed when we move the kernel.
  1001. */
  1002. *spinloop = 0;
  1003. #ifdef CONFIG_HMT
  1004. for (i=0; i < NR_CPUS; i++) {
  1005. RELOC(hmt_thread_data)[i].pir = 0xdeadbeef;
  1006. }
  1007. #endif
  1008. /* look for cpus */
  1009. for (node = 0; prom_next_node(&node); ) {
  1010. type[0] = 0;
  1011. prom_getprop(node, "device_type", type, sizeof(type));
  1012. if (strcmp(type, RELOC("cpu")) != 0)
  1013. continue;
  1014. /* Skip non-configured cpus. */
  1015. if (prom_getprop(node, "status", type, sizeof(type)) > 0)
  1016. if (strcmp(type, RELOC("okay")) != 0)
  1017. continue;
  1018. reg = -1;
  1019. prom_getprop(node, "reg", &reg, sizeof(reg));
  1020. prom_debug("\ncpuid = 0x%x\n", cpuid);
  1021. prom_debug("cpu hw idx = 0x%x\n", reg);
  1022. /* Init the acknowledge var which will be reset by
  1023. * the secondary cpu when it awakens from its OF
  1024. * spinloop.
  1025. */
  1026. *acknowledge = (unsigned long)-1;
  1027. propsize = prom_getprop(node, "ibm,ppc-interrupt-server#s",
  1028. &interrupt_server,
  1029. sizeof(interrupt_server));
  1030. if (propsize < 0) {
  1031. /* no property. old hardware has no SMT */
  1032. cpu_threads = 1;
  1033. interrupt_server[0] = reg; /* fake it with phys id */
  1034. } else {
  1035. /* We have a threaded processor */
  1036. cpu_threads = propsize / sizeof(u32);
  1037. if (cpu_threads > MAX_CPU_THREADS) {
  1038. prom_printf("SMT: too many threads!\n"
  1039. "SMT: found %x, max is %x\n",
  1040. cpu_threads, MAX_CPU_THREADS);
  1041. cpu_threads = 1; /* ToDo: panic? */
  1042. }
  1043. }
  1044. hw_cpu_num = interrupt_server[0];
  1045. if (hw_cpu_num != _prom->cpu) {
  1046. /* Primary Thread of non-boot cpu */
  1047. prom_printf("%x : starting cpu hw idx %x... ", cpuid, reg);
  1048. call_prom("start-cpu", 3, 0, node,
  1049. secondary_hold, reg);
  1050. for ( i = 0 ; (i < 100000000) &&
  1051. (*acknowledge == ((unsigned long)-1)); i++ )
  1052. mb();
  1053. if (*acknowledge == reg) {
  1054. prom_printf("done\n");
  1055. /* We have to get every CPU out of OF,
  1056. * even if we never start it. */
  1057. if (cpuid >= NR_CPUS)
  1058. goto next;
  1059. } else {
  1060. prom_printf("failed: %x\n", *acknowledge);
  1061. }
  1062. }
  1063. #ifdef CONFIG_SMP
  1064. else
  1065. prom_printf("%x : boot cpu %x\n", cpuid, reg);
  1066. #endif
  1067. next:
  1068. #ifdef CONFIG_SMP
  1069. /* Init paca for secondary threads. They start later. */
  1070. for (i=1; i < cpu_threads; i++) {
  1071. cpuid++;
  1072. if (cpuid >= NR_CPUS)
  1073. continue;
  1074. }
  1075. #endif /* CONFIG_SMP */
  1076. cpuid++;
  1077. }
  1078. #ifdef CONFIG_HMT
  1079. /* Only enable HMT on processors that provide support. */
  1080. if (__is_processor(PV_PULSAR) ||
  1081. __is_processor(PV_ICESTAR) ||
  1082. __is_processor(PV_SSTAR)) {
  1083. prom_printf(" starting secondary threads\n");
  1084. for (i = 0; i < NR_CPUS; i += 2) {
  1085. if (!cpu_online(i))
  1086. continue;
  1087. if (i == 0) {
  1088. unsigned long pir = mfspr(SPRN_PIR);
  1089. if (__is_processor(PV_PULSAR)) {
  1090. RELOC(hmt_thread_data)[i].pir =
  1091. pir & 0x1f;
  1092. } else {
  1093. RELOC(hmt_thread_data)[i].pir =
  1094. pir & 0x3ff;
  1095. }
  1096. }
  1097. }
  1098. } else {
  1099. prom_printf("Processor is not HMT capable\n");
  1100. }
  1101. #endif
  1102. if (cpuid > NR_CPUS)
  1103. prom_printf("WARNING: maximum CPUs (" __stringify(NR_CPUS)
  1104. ") exceeded: ignoring extras\n");
  1105. prom_debug("prom_hold_cpus: end...\n");
  1106. }
  1107. static void __init prom_init_client_services(unsigned long pp)
  1108. {
  1109. unsigned long offset = reloc_offset();
  1110. struct prom_t *_prom = PTRRELOC(&prom);
  1111. /* Get a handle to the prom entry point before anything else */
  1112. _prom->entry = pp;
  1113. /* Init default value for phys size */
  1114. _prom->root_size_cells = 1;
  1115. _prom->root_addr_cells = 2;
  1116. /* get a handle for the stdout device */
  1117. _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
  1118. if (!PHANDLE_VALID(_prom->chosen))
  1119. prom_panic("cannot find chosen"); /* msg won't be printed :( */
  1120. /* get device tree root */
  1121. _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
  1122. if (!PHANDLE_VALID(_prom->root))
  1123. prom_panic("cannot find device tree root"); /* msg won't be printed :( */
  1124. }
  1125. static void __init prom_init_stdout(void)
  1126. {
  1127. unsigned long offset = reloc_offset();
  1128. struct prom_t *_prom = PTRRELOC(&prom);
  1129. char *path = RELOC(of_stdout_device);
  1130. char type[16];
  1131. u32 val;
  1132. if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
  1133. prom_panic("cannot find stdout");
  1134. _prom->stdout = val;
  1135. /* Get the full OF pathname of the stdout device */
  1136. memset(path, 0, 256);
  1137. call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
  1138. val = call_prom("instance-to-package", 1, 1, _prom->stdout);
  1139. prom_setprop(_prom->chosen, "linux,stdout-package", &val, sizeof(val));
  1140. prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
  1141. prom_setprop(_prom->chosen, "linux,stdout-path",
  1142. RELOC(of_stdout_device), strlen(RELOC(of_stdout_device))+1);
  1143. /* If it's a display, note it */
  1144. memset(type, 0, sizeof(type));
  1145. prom_getprop(val, "device_type", type, sizeof(type));
  1146. if (strcmp(type, RELOC("display")) == 0) {
  1147. _prom->disp_node = val;
  1148. prom_setprop(val, "linux,boot-display", NULL, 0);
  1149. }
  1150. }
  1151. static void __init prom_close_stdin(void)
  1152. {
  1153. unsigned long offset = reloc_offset();
  1154. struct prom_t *_prom = PTRRELOC(&prom);
  1155. ihandle val;
  1156. if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
  1157. call_prom("close", 1, 0, val);
  1158. }
  1159. static int __init prom_find_machine_type(void)
  1160. {
  1161. unsigned long offset = reloc_offset();
  1162. struct prom_t *_prom = PTRRELOC(&prom);
  1163. char compat[256];
  1164. int len, i = 0;
  1165. phandle rtas;
  1166. len = prom_getprop(_prom->root, "compatible",
  1167. compat, sizeof(compat)-1);
  1168. if (len > 0) {
  1169. compat[len] = 0;
  1170. while (i < len) {
  1171. char *p = &compat[i];
  1172. int sl = strlen(p);
  1173. if (sl == 0)
  1174. break;
  1175. if (strstr(p, RELOC("Power Macintosh")) ||
  1176. strstr(p, RELOC("MacRISC4")))
  1177. return PLATFORM_POWERMAC;
  1178. if (strstr(p, RELOC("Momentum,Maple")))
  1179. return PLATFORM_MAPLE;
  1180. i += sl + 1;
  1181. }
  1182. }
  1183. /* Default to pSeries. We need to know if we are running LPAR */
  1184. rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  1185. if (PHANDLE_VALID(rtas)) {
  1186. int x = prom_getproplen(rtas, "ibm,hypertas-functions");
  1187. if (x != PROM_ERROR) {
  1188. prom_printf("Hypertas detected, assuming LPAR !\n");
  1189. return PLATFORM_PSERIES_LPAR;
  1190. }
  1191. }
  1192. return PLATFORM_PSERIES;
  1193. }
  1194. static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
  1195. {
  1196. unsigned long offset = reloc_offset();
  1197. return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
  1198. }
  1199. /*
  1200. * If we have a display that we don't know how to drive,
  1201. * we will want to try to execute OF's open method for it
  1202. * later. However, OF will probably fall over if we do that
  1203. * we've taken over the MMU.
  1204. * So we check whether we will need to open the display,
  1205. * and if so, open it now.
  1206. */
  1207. static void __init prom_check_displays(void)
  1208. {
  1209. unsigned long offset = reloc_offset();
  1210. struct prom_t *_prom = PTRRELOC(&prom);
  1211. char type[16], *path;
  1212. phandle node;
  1213. ihandle ih;
  1214. int i;
  1215. static unsigned char default_colors[] = {
  1216. 0x00, 0x00, 0x00,
  1217. 0x00, 0x00, 0xaa,
  1218. 0x00, 0xaa, 0x00,
  1219. 0x00, 0xaa, 0xaa,
  1220. 0xaa, 0x00, 0x00,
  1221. 0xaa, 0x00, 0xaa,
  1222. 0xaa, 0xaa, 0x00,
  1223. 0xaa, 0xaa, 0xaa,
  1224. 0x55, 0x55, 0x55,
  1225. 0x55, 0x55, 0xff,
  1226. 0x55, 0xff, 0x55,
  1227. 0x55, 0xff, 0xff,
  1228. 0xff, 0x55, 0x55,
  1229. 0xff, 0x55, 0xff,
  1230. 0xff, 0xff, 0x55,
  1231. 0xff, 0xff, 0xff
  1232. };
  1233. const unsigned char *clut;
  1234. prom_printf("Looking for displays\n");
  1235. for (node = 0; prom_next_node(&node); ) {
  1236. memset(type, 0, sizeof(type));
  1237. prom_getprop(node, "device_type", type, sizeof(type));
  1238. if (strcmp(type, RELOC("display")) != 0)
  1239. continue;
  1240. /* It seems OF doesn't null-terminate the path :-( */
  1241. path = RELOC(prom_scratch);
  1242. memset(path, 0, PROM_SCRATCH_SIZE);
  1243. /*
  1244. * leave some room at the end of the path for appending extra
  1245. * arguments
  1246. */
  1247. if (call_prom("package-to-path", 3, 1, node, path,
  1248. PROM_SCRATCH_SIZE-10) == PROM_ERROR)
  1249. continue;
  1250. prom_printf("found display : %s, opening ... ", path);
  1251. ih = call_prom("open", 1, 1, path);
  1252. if (ih == 0) {
  1253. prom_printf("failed\n");
  1254. continue;
  1255. }
  1256. /* Success */
  1257. prom_printf("done\n");
  1258. prom_setprop(node, "linux,opened", NULL, 0);
  1259. /*
  1260. * stdout wasn't a display node, pick the first we can find
  1261. * for btext
  1262. */
  1263. if (_prom->disp_node == 0)
  1264. _prom->disp_node = node;
  1265. /* Setup a useable color table when the appropriate
  1266. * method is available. Should update this to set-colors */
  1267. clut = RELOC(default_colors);
  1268. for (i = 0; i < 32; i++, clut += 3)
  1269. if (prom_set_color(ih, i, clut[0], clut[1],
  1270. clut[2]) != 0)
  1271. break;
  1272. #ifdef CONFIG_LOGO_LINUX_CLUT224
  1273. clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
  1274. for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
  1275. if (prom_set_color(ih, i + 32, clut[0], clut[1],
  1276. clut[2]) != 0)
  1277. break;
  1278. #endif /* CONFIG_LOGO_LINUX_CLUT224 */
  1279. }
  1280. }
  1281. /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
  1282. static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
  1283. unsigned long needed, unsigned long align)
  1284. {
  1285. unsigned long offset = reloc_offset();
  1286. void *ret;
  1287. *mem_start = _ALIGN(*mem_start, align);
  1288. while ((*mem_start + needed) > *mem_end) {
  1289. unsigned long room, chunk;
  1290. prom_debug("Chunk exhausted, claiming more at %x...\n",
  1291. RELOC(alloc_bottom));
  1292. room = RELOC(alloc_top) - RELOC(alloc_bottom);
  1293. if (room > DEVTREE_CHUNK_SIZE)
  1294. room = DEVTREE_CHUNK_SIZE;
  1295. if (room < PAGE_SIZE)
  1296. prom_panic("No memory for flatten_device_tree (no room)");
  1297. chunk = alloc_up(room, 0);
  1298. if (chunk == 0)
  1299. prom_panic("No memory for flatten_device_tree (claim failed)");
  1300. *mem_end = RELOC(alloc_top);
  1301. }
  1302. ret = (void *)*mem_start;
  1303. *mem_start += needed;
  1304. return ret;
  1305. }
  1306. #define dt_push_token(token, mem_start, mem_end) \
  1307. do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
  1308. static unsigned long __init dt_find_string(char *str)
  1309. {
  1310. unsigned long offset = reloc_offset();
  1311. char *s, *os;
  1312. s = os = (char *)RELOC(dt_string_start);
  1313. s += 4;
  1314. while (s < (char *)RELOC(dt_string_end)) {
  1315. if (strcmp(s, str) == 0)
  1316. return s - os;
  1317. s += strlen(s) + 1;
  1318. }
  1319. return 0;
  1320. }
  1321. /*
  1322. * The Open Firmware 1275 specification states properties must be 31 bytes or
  1323. * less, however not all firmwares obey this. Make it 64 bytes to be safe.
  1324. */
  1325. #define MAX_PROPERTY_NAME 64
  1326. static void __init scan_dt_build_strings(phandle node,
  1327. unsigned long *mem_start,
  1328. unsigned long *mem_end)
  1329. {
  1330. unsigned long offset = reloc_offset();
  1331. char *prev_name, *namep, *sstart;
  1332. unsigned long soff;
  1333. phandle child;
  1334. sstart = (char *)RELOC(dt_string_start);
  1335. /* get and store all property names */
  1336. prev_name = RELOC("");
  1337. for (;;) {
  1338. /* 64 is max len of name including nul. */
  1339. namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
  1340. if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
  1341. /* No more nodes: unwind alloc */
  1342. *mem_start = (unsigned long)namep;
  1343. break;
  1344. }
  1345. /* skip "name" */
  1346. if (strcmp(namep, RELOC("name")) == 0) {
  1347. *mem_start = (unsigned long)namep;
  1348. prev_name = RELOC("name");
  1349. continue;
  1350. }
  1351. /* get/create string entry */
  1352. soff = dt_find_string(namep);
  1353. if (soff != 0) {
  1354. *mem_start = (unsigned long)namep;
  1355. namep = sstart + soff;
  1356. } else {
  1357. /* Trim off some if we can */
  1358. *mem_start = (unsigned long)namep + strlen(namep) + 1;
  1359. RELOC(dt_string_end) = *mem_start;
  1360. }
  1361. prev_name = namep;
  1362. }
  1363. /* do all our children */
  1364. child = call_prom("child", 1, 1, node);
  1365. while (child != 0) {
  1366. scan_dt_build_strings(child, mem_start, mem_end);
  1367. child = call_prom("peer", 1, 1, child);
  1368. }
  1369. }
  1370. static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
  1371. unsigned long *mem_end)
  1372. {
  1373. phandle child;
  1374. char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
  1375. unsigned long soff;
  1376. unsigned char *valp;
  1377. unsigned long offset = reloc_offset();
  1378. static char pname[MAX_PROPERTY_NAME];
  1379. int l;
  1380. dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
  1381. /* get the node's full name */
  1382. namep = (char *)*mem_start;
  1383. l = call_prom("package-to-path", 3, 1, node,
  1384. namep, *mem_end - *mem_start);
  1385. if (l >= 0) {
  1386. /* Didn't fit? Get more room. */
  1387. if ((l+1) > (*mem_end - *mem_start)) {
  1388. namep = make_room(mem_start, mem_end, l+1, 1);
  1389. call_prom("package-to-path", 3, 1, node, namep, l);
  1390. }
  1391. namep[l] = '\0';
  1392. /* Fixup an Apple bug where they have bogus \0 chars in the
  1393. * middle of the path in some properties
  1394. */
  1395. for (p = namep, ep = namep + l; p < ep; p++)
  1396. if (*p == '\0') {
  1397. memmove(p, p+1, ep - p);
  1398. ep--; l--; p--;
  1399. }
  1400. /* now try to extract the unit name in that mess */
  1401. for (p = namep, lp = NULL; *p; p++)
  1402. if (*p == '/')
  1403. lp = p + 1;
  1404. if (lp != NULL)
  1405. memmove(namep, lp, strlen(lp) + 1);
  1406. *mem_start = _ALIGN(((unsigned long) namep) +
  1407. strlen(namep) + 1, 4);
  1408. }
  1409. /* get it again for debugging */
  1410. path = RELOC(prom_scratch);
  1411. memset(path, 0, PROM_SCRATCH_SIZE);
  1412. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  1413. /* get and store all properties */
  1414. prev_name = RELOC("");
  1415. sstart = (char *)RELOC(dt_string_start);
  1416. for (;;) {
  1417. if (call_prom("nextprop", 3, 1, node, prev_name,
  1418. RELOC(pname)) != 1)
  1419. break;
  1420. /* skip "name" */
  1421. if (strcmp(RELOC(pname), RELOC("name")) == 0) {
  1422. prev_name = RELOC("name");
  1423. continue;
  1424. }
  1425. /* find string offset */
  1426. soff = dt_find_string(RELOC(pname));
  1427. if (soff == 0) {
  1428. prom_printf("WARNING: Can't find string index for"
  1429. " <%s>, node %s\n", RELOC(pname), path);
  1430. break;
  1431. }
  1432. prev_name = sstart + soff;
  1433. /* get length */
  1434. l = call_prom("getproplen", 2, 1, node, RELOC(pname));
  1435. /* sanity checks */
  1436. if (l == PROM_ERROR)
  1437. continue;
  1438. if (l > MAX_PROPERTY_LENGTH) {
  1439. prom_printf("WARNING: ignoring large property ");
  1440. /* It seems OF doesn't null-terminate the path :-( */
  1441. prom_printf("[%s] ", path);
  1442. prom_printf("%s length 0x%x\n", RELOC(pname), l);
  1443. continue;
  1444. }
  1445. /* push property head */
  1446. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1447. dt_push_token(l, mem_start, mem_end);
  1448. dt_push_token(soff, mem_start, mem_end);
  1449. /* push property content */
  1450. valp = make_room(mem_start, mem_end, l, 4);
  1451. call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
  1452. *mem_start = _ALIGN(*mem_start, 4);
  1453. }
  1454. /* Add a "linux,phandle" property. */
  1455. soff = dt_find_string(RELOC("linux,phandle"));
  1456. if (soff == 0)
  1457. prom_printf("WARNING: Can't find string index for"
  1458. " <linux-phandle> node %s\n", path);
  1459. else {
  1460. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1461. dt_push_token(4, mem_start, mem_end);
  1462. dt_push_token(soff, mem_start, mem_end);
  1463. valp = make_room(mem_start, mem_end, 4, 4);
  1464. *(u32 *)valp = node;
  1465. }
  1466. /* do all our children */
  1467. child = call_prom("child", 1, 1, node);
  1468. while (child != 0) {
  1469. scan_dt_build_struct(child, mem_start, mem_end);
  1470. child = call_prom("peer", 1, 1, child);
  1471. }
  1472. dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
  1473. }
  1474. static void __init flatten_device_tree(void)
  1475. {
  1476. phandle root;
  1477. unsigned long offset = reloc_offset();
  1478. unsigned long mem_start, mem_end, room;
  1479. struct boot_param_header *hdr;
  1480. char *namep;
  1481. u64 *rsvmap;
  1482. /*
  1483. * Check how much room we have between alloc top & bottom (+/- a
  1484. * few pages), crop to 4Mb, as this is our "chuck" size
  1485. */
  1486. room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
  1487. if (room > DEVTREE_CHUNK_SIZE)
  1488. room = DEVTREE_CHUNK_SIZE;
  1489. prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
  1490. /* Now try to claim that */
  1491. mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
  1492. if (mem_start == 0)
  1493. prom_panic("Can't allocate initial device-tree chunk\n");
  1494. mem_end = RELOC(alloc_top);
  1495. /* Get root of tree */
  1496. root = call_prom("peer", 1, 1, (phandle)0);
  1497. if (root == (phandle)0)
  1498. prom_panic ("couldn't get device tree root\n");
  1499. /* Build header and make room for mem rsv map */
  1500. mem_start = _ALIGN(mem_start, 4);
  1501. hdr = make_room(&mem_start, &mem_end,
  1502. sizeof(struct boot_param_header), 4);
  1503. RELOC(dt_header_start) = (unsigned long)hdr;
  1504. rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
  1505. /* Start of strings */
  1506. mem_start = PAGE_ALIGN(mem_start);
  1507. RELOC(dt_string_start) = mem_start;
  1508. mem_start += 4; /* hole */
  1509. /* Add "linux,phandle" in there, we'll need it */
  1510. namep = make_room(&mem_start, &mem_end, 16, 1);
  1511. strcpy(namep, RELOC("linux,phandle"));
  1512. mem_start = (unsigned long)namep + strlen(namep) + 1;
  1513. /* Build string array */
  1514. prom_printf("Building dt strings...\n");
  1515. scan_dt_build_strings(root, &mem_start, &mem_end);
  1516. RELOC(dt_string_end) = mem_start;
  1517. /* Build structure */
  1518. mem_start = PAGE_ALIGN(mem_start);
  1519. RELOC(dt_struct_start) = mem_start;
  1520. prom_printf("Building dt structure...\n");
  1521. scan_dt_build_struct(root, &mem_start, &mem_end);
  1522. dt_push_token(OF_DT_END, &mem_start, &mem_end);
  1523. RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
  1524. /* Finish header */
  1525. hdr->magic = OF_DT_HEADER;
  1526. hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
  1527. hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
  1528. hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
  1529. hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
  1530. hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
  1531. hdr->version = OF_DT_VERSION;
  1532. /* Version 16 is not backward compatible */
  1533. hdr->last_comp_version = 0x10;
  1534. /* Reserve the whole thing and copy the reserve map in, we
  1535. * also bump mem_reserve_cnt to cause further reservations to
  1536. * fail since it's too late.
  1537. */
  1538. reserve_mem(RELOC(dt_header_start), hdr->totalsize);
  1539. memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
  1540. #ifdef DEBUG_PROM
  1541. {
  1542. int i;
  1543. prom_printf("reserved memory map:\n");
  1544. for (i = 0; i < RELOC(mem_reserve_cnt); i++)
  1545. prom_printf(" %x - %x\n", RELOC(mem_reserve_map)[i].base,
  1546. RELOC(mem_reserve_map)[i].size);
  1547. }
  1548. #endif
  1549. RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
  1550. prom_printf("Device tree strings 0x%x -> 0x%x\n",
  1551. RELOC(dt_string_start), RELOC(dt_string_end));
  1552. prom_printf("Device tree struct 0x%x -> 0x%x\n",
  1553. RELOC(dt_struct_start), RELOC(dt_struct_end));
  1554. }
  1555. static void __init fixup_device_tree(void)
  1556. {
  1557. unsigned long offset = reloc_offset();
  1558. phandle u3, i2c, mpic;
  1559. u32 u3_rev;
  1560. u32 interrupts[2];
  1561. u32 parent;
  1562. /* Some G5s have a missing interrupt definition, fix it up here */
  1563. u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
  1564. if (!PHANDLE_VALID(u3))
  1565. return;
  1566. i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
  1567. if (!PHANDLE_VALID(i2c))
  1568. return;
  1569. mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
  1570. if (!PHANDLE_VALID(mpic))
  1571. return;
  1572. /* check if proper rev of u3 */
  1573. if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
  1574. == PROM_ERROR)
  1575. return;
  1576. if (u3_rev != 0x35 && u3_rev != 0x37)
  1577. return;
  1578. /* does it need fixup ? */
  1579. if (prom_getproplen(i2c, "interrupts") > 0)
  1580. return;
  1581. prom_printf("fixing up bogus interrupts for u3 i2c...\n");
  1582. /* interrupt on this revision of u3 is number 0 and level */
  1583. interrupts[0] = 0;
  1584. interrupts[1] = 1;
  1585. prom_setprop(i2c, "interrupts", &interrupts, sizeof(interrupts));
  1586. parent = (u32)mpic;
  1587. prom_setprop(i2c, "interrupt-parent", &parent, sizeof(parent));
  1588. }
  1589. static void __init prom_find_boot_cpu(void)
  1590. {
  1591. unsigned long offset = reloc_offset();
  1592. struct prom_t *_prom = PTRRELOC(&prom);
  1593. u32 getprop_rval;
  1594. ihandle prom_cpu;
  1595. phandle cpu_pkg;
  1596. if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
  1597. prom_panic("cannot find boot cpu");
  1598. cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
  1599. prom_setprop(cpu_pkg, "linux,boot-cpu", NULL, 0);
  1600. prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
  1601. _prom->cpu = getprop_rval;
  1602. prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
  1603. }
  1604. static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
  1605. {
  1606. #ifdef CONFIG_BLK_DEV_INITRD
  1607. unsigned long offset = reloc_offset();
  1608. struct prom_t *_prom = PTRRELOC(&prom);
  1609. if ( r3 && r4 && r4 != 0xdeadbeef) {
  1610. u64 val;
  1611. RELOC(prom_initrd_start) = (r3 >= KERNELBASE) ? __pa(r3) : r3;
  1612. RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
  1613. val = (u64)RELOC(prom_initrd_start);
  1614. prom_setprop(_prom->chosen, "linux,initrd-start", &val, sizeof(val));
  1615. val = (u64)RELOC(prom_initrd_end);
  1616. prom_setprop(_prom->chosen, "linux,initrd-end", &val, sizeof(val));
  1617. reserve_mem(RELOC(prom_initrd_start),
  1618. RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
  1619. prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
  1620. prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
  1621. }
  1622. #endif /* CONFIG_BLK_DEV_INITRD */
  1623. }
  1624. /*
  1625. * We enter here early on, when the Open Firmware prom is still
  1626. * handling exceptions and the MMU hash table for us.
  1627. */
  1628. unsigned long __init prom_init(unsigned long r3, unsigned long r4, unsigned long pp,
  1629. unsigned long r6, unsigned long r7)
  1630. {
  1631. unsigned long offset = reloc_offset();
  1632. struct prom_t *_prom = PTRRELOC(&prom);
  1633. unsigned long phys = KERNELBASE - offset;
  1634. u32 getprop_rval;
  1635. /*
  1636. * First zero the BSS
  1637. */
  1638. memset(PTRRELOC(&__bss_start), 0, __bss_stop - __bss_start);
  1639. /*
  1640. * Init interface to Open Firmware, get some node references,
  1641. * like /chosen
  1642. */
  1643. prom_init_client_services(pp);
  1644. /*
  1645. * Init prom stdout device
  1646. */
  1647. prom_init_stdout();
  1648. prom_debug("klimit=0x%x\n", RELOC(klimit));
  1649. prom_debug("offset=0x%x\n", offset);
  1650. /*
  1651. * Check for an initrd
  1652. */
  1653. prom_check_initrd(r3, r4);
  1654. /*
  1655. * Get default machine type. At this point, we do not differenciate
  1656. * between pSeries SMP and pSeries LPAR
  1657. */
  1658. RELOC(of_platform) = prom_find_machine_type();
  1659. getprop_rval = RELOC(of_platform);
  1660. prom_setprop(_prom->chosen, "linux,platform",
  1661. &getprop_rval, sizeof(getprop_rval));
  1662. /*
  1663. * On pSeries, inform the firmware about our capabilities
  1664. */
  1665. if (RELOC(of_platform) & PLATFORM_PSERIES)
  1666. prom_send_capabilities();
  1667. /*
  1668. * On pSeries and BPA, copy the CPU hold code
  1669. */
  1670. if (RELOC(of_platform) & (PLATFORM_PSERIES | PLATFORM_BPA))
  1671. copy_and_flush(0, KERNELBASE - offset, 0x100, 0);
  1672. /*
  1673. * Get memory cells format
  1674. */
  1675. getprop_rval = 1;
  1676. prom_getprop(_prom->root, "#size-cells",
  1677. &getprop_rval, sizeof(getprop_rval));
  1678. _prom->root_size_cells = getprop_rval;
  1679. getprop_rval = 2;
  1680. prom_getprop(_prom->root, "#address-cells",
  1681. &getprop_rval, sizeof(getprop_rval));
  1682. _prom->root_addr_cells = getprop_rval;
  1683. /*
  1684. * Do early parsing of command line
  1685. */
  1686. early_cmdline_parse();
  1687. /*
  1688. * Initialize memory management within prom_init
  1689. */
  1690. prom_init_mem();
  1691. /*
  1692. * Determine which cpu is actually running right _now_
  1693. */
  1694. prom_find_boot_cpu();
  1695. /*
  1696. * Initialize display devices
  1697. */
  1698. prom_check_displays();
  1699. /*
  1700. * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
  1701. * that uses the allocator, we need to make sure we get the top of memory
  1702. * available for us here...
  1703. */
  1704. if (RELOC(of_platform) == PLATFORM_PSERIES)
  1705. prom_initialize_tce_table();
  1706. /*
  1707. * On non-powermacs, try to instantiate RTAS and puts all CPUs
  1708. * in spin-loops. PowerMacs don't have a working RTAS and use
  1709. * a different way to spin CPUs
  1710. */
  1711. if (RELOC(of_platform) != PLATFORM_POWERMAC) {
  1712. prom_instantiate_rtas();
  1713. prom_hold_cpus();
  1714. }
  1715. /*
  1716. * Fill in some infos for use by the kernel later on
  1717. */
  1718. if (RELOC(ppc64_iommu_off))
  1719. prom_setprop(_prom->chosen, "linux,iommu-off", NULL, 0);
  1720. if (RELOC(iommu_force_on))
  1721. prom_setprop(_prom->chosen, "linux,iommu-force-on", NULL, 0);
  1722. if (RELOC(prom_memory_limit))
  1723. prom_setprop(_prom->chosen, "linux,memory-limit",
  1724. PTRRELOC(&prom_memory_limit), sizeof(RELOC(prom_memory_limit)));
  1725. if (RELOC(prom_tce_alloc_start)) {
  1726. prom_setprop(_prom->chosen, "linux,tce-alloc-start",
  1727. PTRRELOC(&prom_tce_alloc_start), sizeof(RELOC(prom_tce_alloc_start)));
  1728. prom_setprop(_prom->chosen, "linux,tce-alloc-end",
  1729. PTRRELOC(&prom_tce_alloc_end), sizeof(RELOC(prom_tce_alloc_end)));
  1730. }
  1731. /*
  1732. * Fixup any known bugs in the device-tree
  1733. */
  1734. fixup_device_tree();
  1735. /*
  1736. * Now finally create the flattened device-tree
  1737. */
  1738. prom_printf("copying OF device tree ...\n");
  1739. flatten_device_tree();
  1740. /* in case stdin is USB and still active on IBM machines... */
  1741. prom_close_stdin();
  1742. /*
  1743. * Call OF "quiesce" method to shut down pending DMA's from
  1744. * devices etc...
  1745. */
  1746. prom_printf("Calling quiesce ...\n");
  1747. call_prom("quiesce", 0, 0);
  1748. /*
  1749. * And finally, call the kernel passing it the flattened device
  1750. * tree and NULL as r5, thus triggering the new entry point which
  1751. * is common to us and kexec
  1752. */
  1753. prom_printf("returning from prom_init\n");
  1754. prom_debug("->dt_header_start=0x%x\n", RELOC(dt_header_start));
  1755. prom_debug("->phys=0x%x\n", phys);
  1756. __start(RELOC(dt_header_start), phys, 0);
  1757. return 0;
  1758. }