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