prom_init.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049
  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. RELOC(alloc_top) = RELOC(rmo_top) = min(0x40000000ul, RELOC(ram_top));
  770. prom_printf("memory layout at init:\n");
  771. prom_printf(" memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
  772. prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  773. prom_printf(" alloc_top : %x\n", RELOC(alloc_top));
  774. prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  775. prom_printf(" rmo_top : %x\n", RELOC(rmo_top));
  776. prom_printf(" ram_top : %x\n", RELOC(ram_top));
  777. }
  778. /*
  779. * Allocate room for and instanciate RTAS
  780. */
  781. static void __init prom_instantiate_rtas(void)
  782. {
  783. unsigned long offset = reloc_offset();
  784. struct prom_t *_prom = PTRRELOC(&prom);
  785. phandle rtas_node;
  786. ihandle rtas_inst;
  787. u32 base, entry = 0;
  788. u32 size = 0;
  789. prom_debug("prom_instantiate_rtas: start...\n");
  790. rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  791. prom_debug("rtas_node: %x\n", rtas_node);
  792. if (!PHANDLE_VALID(rtas_node))
  793. return;
  794. prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
  795. if (size == 0)
  796. return;
  797. base = alloc_down(size, PAGE_SIZE, 0);
  798. if (base == 0) {
  799. prom_printf("RTAS allocation failed !\n");
  800. return;
  801. }
  802. rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
  803. if (!IHANDLE_VALID(rtas_inst)) {
  804. prom_printf("opening rtas package failed");
  805. return;
  806. }
  807. prom_printf("instantiating rtas at 0x%x ...", base);
  808. if (call_prom("call-method", 3, 2,
  809. ADDR("instantiate-rtas"),
  810. rtas_inst, base) != PROM_ERROR) {
  811. entry = (long)_prom->args.rets[1];
  812. }
  813. if (entry == 0) {
  814. prom_printf(" failed\n");
  815. return;
  816. }
  817. prom_printf(" done\n");
  818. reserve_mem(base, size);
  819. prom_setprop(rtas_node, "linux,rtas-base", &base, sizeof(base));
  820. prom_setprop(rtas_node, "linux,rtas-entry", &entry, sizeof(entry));
  821. prom_debug("rtas base = 0x%x\n", base);
  822. prom_debug("rtas entry = 0x%x\n", entry);
  823. prom_debug("rtas size = 0x%x\n", (long)size);
  824. prom_debug("prom_instantiate_rtas: end...\n");
  825. }
  826. /*
  827. * Allocate room for and initialize TCE tables
  828. */
  829. static void __init prom_initialize_tce_table(void)
  830. {
  831. phandle node;
  832. ihandle phb_node;
  833. unsigned long offset = reloc_offset();
  834. char compatible[64], type[64], model[64];
  835. char *path = RELOC(prom_scratch);
  836. u64 base, align;
  837. u32 minalign, minsize;
  838. u64 tce_entry, *tce_entryp;
  839. u64 local_alloc_top, local_alloc_bottom;
  840. u64 i;
  841. if (RELOC(ppc64_iommu_off))
  842. return;
  843. prom_debug("starting prom_initialize_tce_table\n");
  844. /* Cache current top of allocs so we reserve a single block */
  845. local_alloc_top = RELOC(alloc_top_high);
  846. local_alloc_bottom = local_alloc_top;
  847. /* Search all nodes looking for PHBs. */
  848. for (node = 0; prom_next_node(&node); ) {
  849. compatible[0] = 0;
  850. type[0] = 0;
  851. model[0] = 0;
  852. prom_getprop(node, "compatible",
  853. compatible, sizeof(compatible));
  854. prom_getprop(node, "device_type", type, sizeof(type));
  855. prom_getprop(node, "model", model, sizeof(model));
  856. if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
  857. continue;
  858. /* Keep the old logic in tack to avoid regression. */
  859. if (compatible[0] != 0) {
  860. if ((strstr(compatible, RELOC("python")) == NULL) &&
  861. (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
  862. (strstr(compatible, RELOC("Winnipeg")) == NULL))
  863. continue;
  864. } else if (model[0] != 0) {
  865. if ((strstr(model, RELOC("ython")) == NULL) &&
  866. (strstr(model, RELOC("peedwagon")) == NULL) &&
  867. (strstr(model, RELOC("innipeg")) == NULL))
  868. continue;
  869. }
  870. if (prom_getprop(node, "tce-table-minalign", &minalign,
  871. sizeof(minalign)) == PROM_ERROR)
  872. minalign = 0;
  873. if (prom_getprop(node, "tce-table-minsize", &minsize,
  874. sizeof(minsize)) == PROM_ERROR)
  875. minsize = 4UL << 20;
  876. /*
  877. * Even though we read what OF wants, we just set the table
  878. * size to 4 MB. This is enough to map 2GB of PCI DMA space.
  879. * By doing this, we avoid the pitfalls of trying to DMA to
  880. * MMIO space and the DMA alias hole.
  881. *
  882. * On POWER4, firmware sets the TCE region by assuming
  883. * each TCE table is 8MB. Using this memory for anything
  884. * else will impact performance, so we always allocate 8MB.
  885. * Anton
  886. */
  887. if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
  888. minsize = 8UL << 20;
  889. else
  890. minsize = 4UL << 20;
  891. /* Align to the greater of the align or size */
  892. align = max(minalign, minsize);
  893. base = alloc_down(minsize, align, 1);
  894. if (base == 0)
  895. prom_panic("ERROR, cannot find space for TCE table.\n");
  896. if (base < local_alloc_bottom)
  897. local_alloc_bottom = base;
  898. /* Save away the TCE table attributes for later use. */
  899. prom_setprop(node, "linux,tce-base", &base, sizeof(base));
  900. prom_setprop(node, "linux,tce-size", &minsize, sizeof(minsize));
  901. /* It seems OF doesn't null-terminate the path :-( */
  902. memset(path, 0, sizeof(path));
  903. /* Call OF to setup the TCE hardware */
  904. if (call_prom("package-to-path", 3, 1, node,
  905. path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
  906. prom_printf("package-to-path failed\n");
  907. }
  908. prom_debug("TCE table: %s\n", path);
  909. prom_debug("\tnode = 0x%x\n", node);
  910. prom_debug("\tbase = 0x%x\n", base);
  911. prom_debug("\tsize = 0x%x\n", minsize);
  912. /* Initialize the table to have a one-to-one mapping
  913. * over the allocated size.
  914. */
  915. tce_entryp = (unsigned long *)base;
  916. for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
  917. tce_entry = (i << PAGE_SHIFT);
  918. tce_entry |= 0x3;
  919. *tce_entryp = tce_entry;
  920. }
  921. prom_printf("opening PHB %s", path);
  922. phb_node = call_prom("open", 1, 1, path);
  923. if (phb_node == 0)
  924. prom_printf("... failed\n");
  925. else
  926. prom_printf("... done\n");
  927. call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
  928. phb_node, -1, minsize,
  929. (u32) base, (u32) (base >> 32));
  930. call_prom("close", 1, 0, phb_node);
  931. }
  932. reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
  933. if (RELOC(prom_memory_limit)) {
  934. /*
  935. * We align the start to a 16MB boundary so we can map the TCE area
  936. * using large pages if possible. The end should be the top of RAM
  937. * so no need to align it.
  938. */
  939. RELOC(prom_tce_alloc_start) = _ALIGN_DOWN(local_alloc_bottom, 0x1000000);
  940. RELOC(prom_tce_alloc_end) = local_alloc_top;
  941. }
  942. /* Flag the first invalid entry */
  943. prom_debug("ending prom_initialize_tce_table\n");
  944. }
  945. /*
  946. * With CHRP SMP we need to use the OF to start the other
  947. * processors so we can't wait until smp_boot_cpus (the OF is
  948. * trashed by then) so we have to put the processors into
  949. * a holding pattern controlled by the kernel (not OF) before
  950. * we destroy the OF.
  951. *
  952. * This uses a chunk of low memory, puts some holding pattern
  953. * code there and sends the other processors off to there until
  954. * smp_boot_cpus tells them to do something. The holding pattern
  955. * checks that address until its cpu # is there, when it is that
  956. * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
  957. * of setting those values.
  958. *
  959. * We also use physical address 0x4 here to tell when a cpu
  960. * is in its holding pattern code.
  961. *
  962. * Fixup comment... DRENG / PPPBBB - Peter
  963. *
  964. * -- Cort
  965. */
  966. static void __init prom_hold_cpus(void)
  967. {
  968. unsigned long i;
  969. unsigned int reg;
  970. phandle node;
  971. unsigned long offset = reloc_offset();
  972. char type[64];
  973. int cpuid = 0;
  974. unsigned int interrupt_server[MAX_CPU_THREADS];
  975. unsigned int cpu_threads, hw_cpu_num;
  976. int propsize;
  977. extern void __secondary_hold(void);
  978. extern unsigned long __secondary_hold_spinloop;
  979. extern unsigned long __secondary_hold_acknowledge;
  980. unsigned long *spinloop
  981. = (void *)virt_to_abs(&__secondary_hold_spinloop);
  982. unsigned long *acknowledge
  983. = (void *)virt_to_abs(&__secondary_hold_acknowledge);
  984. unsigned long secondary_hold
  985. = virt_to_abs(*PTRRELOC((unsigned long *)__secondary_hold));
  986. struct prom_t *_prom = PTRRELOC(&prom);
  987. prom_debug("prom_hold_cpus: start...\n");
  988. prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
  989. prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
  990. prom_debug(" 1) acknowledge = 0x%x\n",
  991. (unsigned long)acknowledge);
  992. prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
  993. prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
  994. /* Set the common spinloop variable, so all of the secondary cpus
  995. * will block when they are awakened from their OF spinloop.
  996. * This must occur for both SMP and non SMP kernels, since OF will
  997. * be trashed when we move the kernel.
  998. */
  999. *spinloop = 0;
  1000. #ifdef CONFIG_HMT
  1001. for (i=0; i < NR_CPUS; i++) {
  1002. RELOC(hmt_thread_data)[i].pir = 0xdeadbeef;
  1003. }
  1004. #endif
  1005. /* look for cpus */
  1006. for (node = 0; prom_next_node(&node); ) {
  1007. type[0] = 0;
  1008. prom_getprop(node, "device_type", type, sizeof(type));
  1009. if (strcmp(type, RELOC("cpu")) != 0)
  1010. continue;
  1011. /* Skip non-configured cpus. */
  1012. if (prom_getprop(node, "status", type, sizeof(type)) > 0)
  1013. if (strcmp(type, RELOC("okay")) != 0)
  1014. continue;
  1015. reg = -1;
  1016. prom_getprop(node, "reg", &reg, sizeof(reg));
  1017. prom_debug("\ncpuid = 0x%x\n", cpuid);
  1018. prom_debug("cpu hw idx = 0x%x\n", reg);
  1019. /* Init the acknowledge var which will be reset by
  1020. * the secondary cpu when it awakens from its OF
  1021. * spinloop.
  1022. */
  1023. *acknowledge = (unsigned long)-1;
  1024. propsize = prom_getprop(node, "ibm,ppc-interrupt-server#s",
  1025. &interrupt_server,
  1026. sizeof(interrupt_server));
  1027. if (propsize < 0) {
  1028. /* no property. old hardware has no SMT */
  1029. cpu_threads = 1;
  1030. interrupt_server[0] = reg; /* fake it with phys id */
  1031. } else {
  1032. /* We have a threaded processor */
  1033. cpu_threads = propsize / sizeof(u32);
  1034. if (cpu_threads > MAX_CPU_THREADS) {
  1035. prom_printf("SMT: too many threads!\n"
  1036. "SMT: found %x, max is %x\n",
  1037. cpu_threads, MAX_CPU_THREADS);
  1038. cpu_threads = 1; /* ToDo: panic? */
  1039. }
  1040. }
  1041. hw_cpu_num = interrupt_server[0];
  1042. if (hw_cpu_num != _prom->cpu) {
  1043. /* Primary Thread of non-boot cpu */
  1044. prom_printf("%x : starting cpu hw idx %x... ", cpuid, reg);
  1045. call_prom("start-cpu", 3, 0, node,
  1046. secondary_hold, reg);
  1047. for ( i = 0 ; (i < 100000000) &&
  1048. (*acknowledge == ((unsigned long)-1)); i++ )
  1049. mb();
  1050. if (*acknowledge == reg) {
  1051. prom_printf("done\n");
  1052. /* We have to get every CPU out of OF,
  1053. * even if we never start it. */
  1054. if (cpuid >= NR_CPUS)
  1055. goto next;
  1056. } else {
  1057. prom_printf("failed: %x\n", *acknowledge);
  1058. }
  1059. }
  1060. #ifdef CONFIG_SMP
  1061. else
  1062. prom_printf("%x : boot cpu %x\n", cpuid, reg);
  1063. #endif
  1064. next:
  1065. #ifdef CONFIG_SMP
  1066. /* Init paca for secondary threads. They start later. */
  1067. for (i=1; i < cpu_threads; i++) {
  1068. cpuid++;
  1069. if (cpuid >= NR_CPUS)
  1070. continue;
  1071. }
  1072. #endif /* CONFIG_SMP */
  1073. cpuid++;
  1074. }
  1075. #ifdef CONFIG_HMT
  1076. /* Only enable HMT on processors that provide support. */
  1077. if (__is_processor(PV_PULSAR) ||
  1078. __is_processor(PV_ICESTAR) ||
  1079. __is_processor(PV_SSTAR)) {
  1080. prom_printf(" starting secondary threads\n");
  1081. for (i = 0; i < NR_CPUS; i += 2) {
  1082. if (!cpu_online(i))
  1083. continue;
  1084. if (i == 0) {
  1085. unsigned long pir = mfspr(SPRN_PIR);
  1086. if (__is_processor(PV_PULSAR)) {
  1087. RELOC(hmt_thread_data)[i].pir =
  1088. pir & 0x1f;
  1089. } else {
  1090. RELOC(hmt_thread_data)[i].pir =
  1091. pir & 0x3ff;
  1092. }
  1093. }
  1094. }
  1095. } else {
  1096. prom_printf("Processor is not HMT capable\n");
  1097. }
  1098. #endif
  1099. if (cpuid > NR_CPUS)
  1100. prom_printf("WARNING: maximum CPUs (" __stringify(NR_CPUS)
  1101. ") exceeded: ignoring extras\n");
  1102. prom_debug("prom_hold_cpus: end...\n");
  1103. }
  1104. static void __init prom_init_client_services(unsigned long pp)
  1105. {
  1106. unsigned long offset = reloc_offset();
  1107. struct prom_t *_prom = PTRRELOC(&prom);
  1108. /* Get a handle to the prom entry point before anything else */
  1109. _prom->entry = pp;
  1110. /* Init default value for phys size */
  1111. _prom->root_size_cells = 1;
  1112. _prom->root_addr_cells = 2;
  1113. /* get a handle for the stdout device */
  1114. _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
  1115. if (!PHANDLE_VALID(_prom->chosen))
  1116. prom_panic("cannot find chosen"); /* msg won't be printed :( */
  1117. /* get device tree root */
  1118. _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
  1119. if (!PHANDLE_VALID(_prom->root))
  1120. prom_panic("cannot find device tree root"); /* msg won't be printed :( */
  1121. }
  1122. static void __init prom_init_stdout(void)
  1123. {
  1124. unsigned long offset = reloc_offset();
  1125. struct prom_t *_prom = PTRRELOC(&prom);
  1126. char *path = RELOC(of_stdout_device);
  1127. char type[16];
  1128. u32 val;
  1129. if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
  1130. prom_panic("cannot find stdout");
  1131. _prom->stdout = val;
  1132. /* Get the full OF pathname of the stdout device */
  1133. memset(path, 0, 256);
  1134. call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
  1135. val = call_prom("instance-to-package", 1, 1, _prom->stdout);
  1136. prom_setprop(_prom->chosen, "linux,stdout-package", &val, sizeof(val));
  1137. prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
  1138. prom_setprop(_prom->chosen, "linux,stdout-path",
  1139. RELOC(of_stdout_device), strlen(RELOC(of_stdout_device))+1);
  1140. /* If it's a display, note it */
  1141. memset(type, 0, sizeof(type));
  1142. prom_getprop(val, "device_type", type, sizeof(type));
  1143. if (strcmp(type, RELOC("display")) == 0) {
  1144. _prom->disp_node = val;
  1145. prom_setprop(val, "linux,boot-display", NULL, 0);
  1146. }
  1147. }
  1148. static void __init prom_close_stdin(void)
  1149. {
  1150. unsigned long offset = reloc_offset();
  1151. struct prom_t *_prom = PTRRELOC(&prom);
  1152. ihandle val;
  1153. if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
  1154. call_prom("close", 1, 0, val);
  1155. }
  1156. static int __init prom_find_machine_type(void)
  1157. {
  1158. unsigned long offset = reloc_offset();
  1159. struct prom_t *_prom = PTRRELOC(&prom);
  1160. char compat[256];
  1161. int len, i = 0;
  1162. phandle rtas;
  1163. len = prom_getprop(_prom->root, "compatible",
  1164. compat, sizeof(compat)-1);
  1165. if (len > 0) {
  1166. compat[len] = 0;
  1167. while (i < len) {
  1168. char *p = &compat[i];
  1169. int sl = strlen(p);
  1170. if (sl == 0)
  1171. break;
  1172. if (strstr(p, RELOC("Power Macintosh")) ||
  1173. strstr(p, RELOC("MacRISC4")))
  1174. return PLATFORM_POWERMAC;
  1175. if (strstr(p, RELOC("Momentum,Maple")))
  1176. return PLATFORM_MAPLE;
  1177. i += sl + 1;
  1178. }
  1179. }
  1180. /* Default to pSeries. We need to know if we are running LPAR */
  1181. rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  1182. if (PHANDLE_VALID(rtas)) {
  1183. int x = prom_getproplen(rtas, "ibm,hypertas-functions");
  1184. if (x != PROM_ERROR) {
  1185. prom_printf("Hypertas detected, assuming LPAR !\n");
  1186. return PLATFORM_PSERIES_LPAR;
  1187. }
  1188. }
  1189. return PLATFORM_PSERIES;
  1190. }
  1191. static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
  1192. {
  1193. unsigned long offset = reloc_offset();
  1194. return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
  1195. }
  1196. /*
  1197. * If we have a display that we don't know how to drive,
  1198. * we will want to try to execute OF's open method for it
  1199. * later. However, OF will probably fall over if we do that
  1200. * we've taken over the MMU.
  1201. * So we check whether we will need to open the display,
  1202. * and if so, open it now.
  1203. */
  1204. static void __init prom_check_displays(void)
  1205. {
  1206. unsigned long offset = reloc_offset();
  1207. struct prom_t *_prom = PTRRELOC(&prom);
  1208. char type[16], *path;
  1209. phandle node;
  1210. ihandle ih;
  1211. int i;
  1212. static unsigned char default_colors[] = {
  1213. 0x00, 0x00, 0x00,
  1214. 0x00, 0x00, 0xaa,
  1215. 0x00, 0xaa, 0x00,
  1216. 0x00, 0xaa, 0xaa,
  1217. 0xaa, 0x00, 0x00,
  1218. 0xaa, 0x00, 0xaa,
  1219. 0xaa, 0xaa, 0x00,
  1220. 0xaa, 0xaa, 0xaa,
  1221. 0x55, 0x55, 0x55,
  1222. 0x55, 0x55, 0xff,
  1223. 0x55, 0xff, 0x55,
  1224. 0x55, 0xff, 0xff,
  1225. 0xff, 0x55, 0x55,
  1226. 0xff, 0x55, 0xff,
  1227. 0xff, 0xff, 0x55,
  1228. 0xff, 0xff, 0xff
  1229. };
  1230. const unsigned char *clut;
  1231. prom_printf("Looking for displays\n");
  1232. for (node = 0; prom_next_node(&node); ) {
  1233. memset(type, 0, sizeof(type));
  1234. prom_getprop(node, "device_type", type, sizeof(type));
  1235. if (strcmp(type, RELOC("display")) != 0)
  1236. continue;
  1237. /* It seems OF doesn't null-terminate the path :-( */
  1238. path = RELOC(prom_scratch);
  1239. memset(path, 0, PROM_SCRATCH_SIZE);
  1240. /*
  1241. * leave some room at the end of the path for appending extra
  1242. * arguments
  1243. */
  1244. if (call_prom("package-to-path", 3, 1, node, path,
  1245. PROM_SCRATCH_SIZE-10) == PROM_ERROR)
  1246. continue;
  1247. prom_printf("found display : %s, opening ... ", path);
  1248. ih = call_prom("open", 1, 1, path);
  1249. if (ih == 0) {
  1250. prom_printf("failed\n");
  1251. continue;
  1252. }
  1253. /* Success */
  1254. prom_printf("done\n");
  1255. prom_setprop(node, "linux,opened", NULL, 0);
  1256. /*
  1257. * stdout wasn't a display node, pick the first we can find
  1258. * for btext
  1259. */
  1260. if (_prom->disp_node == 0)
  1261. _prom->disp_node = node;
  1262. /* Setup a useable color table when the appropriate
  1263. * method is available. Should update this to set-colors */
  1264. clut = RELOC(default_colors);
  1265. for (i = 0; i < 32; i++, clut += 3)
  1266. if (prom_set_color(ih, i, clut[0], clut[1],
  1267. clut[2]) != 0)
  1268. break;
  1269. #ifdef CONFIG_LOGO_LINUX_CLUT224
  1270. clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
  1271. for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
  1272. if (prom_set_color(ih, i + 32, clut[0], clut[1],
  1273. clut[2]) != 0)
  1274. break;
  1275. #endif /* CONFIG_LOGO_LINUX_CLUT224 */
  1276. }
  1277. }
  1278. /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
  1279. static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
  1280. unsigned long needed, unsigned long align)
  1281. {
  1282. unsigned long offset = reloc_offset();
  1283. void *ret;
  1284. *mem_start = _ALIGN(*mem_start, align);
  1285. while ((*mem_start + needed) > *mem_end) {
  1286. unsigned long room, chunk;
  1287. prom_debug("Chunk exhausted, claiming more at %x...\n",
  1288. RELOC(alloc_bottom));
  1289. room = RELOC(alloc_top) - RELOC(alloc_bottom);
  1290. if (room > DEVTREE_CHUNK_SIZE)
  1291. room = DEVTREE_CHUNK_SIZE;
  1292. if (room < PAGE_SIZE)
  1293. prom_panic("No memory for flatten_device_tree (no room)");
  1294. chunk = alloc_up(room, 0);
  1295. if (chunk == 0)
  1296. prom_panic("No memory for flatten_device_tree (claim failed)");
  1297. *mem_end = RELOC(alloc_top);
  1298. }
  1299. ret = (void *)*mem_start;
  1300. *mem_start += needed;
  1301. return ret;
  1302. }
  1303. #define dt_push_token(token, mem_start, mem_end) \
  1304. do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
  1305. static unsigned long __init dt_find_string(char *str)
  1306. {
  1307. unsigned long offset = reloc_offset();
  1308. char *s, *os;
  1309. s = os = (char *)RELOC(dt_string_start);
  1310. s += 4;
  1311. while (s < (char *)RELOC(dt_string_end)) {
  1312. if (strcmp(s, str) == 0)
  1313. return s - os;
  1314. s += strlen(s) + 1;
  1315. }
  1316. return 0;
  1317. }
  1318. /*
  1319. * The Open Firmware 1275 specification states properties must be 31 bytes or
  1320. * less, however not all firmwares obey this. Make it 64 bytes to be safe.
  1321. */
  1322. #define MAX_PROPERTY_NAME 64
  1323. static void __init scan_dt_build_strings(phandle node,
  1324. unsigned long *mem_start,
  1325. unsigned long *mem_end)
  1326. {
  1327. unsigned long offset = reloc_offset();
  1328. char *prev_name, *namep, *sstart;
  1329. unsigned long soff;
  1330. phandle child;
  1331. sstart = (char *)RELOC(dt_string_start);
  1332. /* get and store all property names */
  1333. prev_name = RELOC("");
  1334. for (;;) {
  1335. /* 64 is max len of name including nul. */
  1336. namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
  1337. if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
  1338. /* No more nodes: unwind alloc */
  1339. *mem_start = (unsigned long)namep;
  1340. break;
  1341. }
  1342. /* skip "name" */
  1343. if (strcmp(namep, RELOC("name")) == 0) {
  1344. *mem_start = (unsigned long)namep;
  1345. prev_name = RELOC("name");
  1346. continue;
  1347. }
  1348. /* get/create string entry */
  1349. soff = dt_find_string(namep);
  1350. if (soff != 0) {
  1351. *mem_start = (unsigned long)namep;
  1352. namep = sstart + soff;
  1353. } else {
  1354. /* Trim off some if we can */
  1355. *mem_start = (unsigned long)namep + strlen(namep) + 1;
  1356. RELOC(dt_string_end) = *mem_start;
  1357. }
  1358. prev_name = namep;
  1359. }
  1360. /* do all our children */
  1361. child = call_prom("child", 1, 1, node);
  1362. while (child != 0) {
  1363. scan_dt_build_strings(child, mem_start, mem_end);
  1364. child = call_prom("peer", 1, 1, child);
  1365. }
  1366. }
  1367. static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
  1368. unsigned long *mem_end)
  1369. {
  1370. phandle child;
  1371. char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
  1372. unsigned long soff;
  1373. unsigned char *valp;
  1374. unsigned long offset = reloc_offset();
  1375. static char pname[MAX_PROPERTY_NAME];
  1376. int l;
  1377. dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
  1378. /* get the node's full name */
  1379. namep = (char *)*mem_start;
  1380. l = call_prom("package-to-path", 3, 1, node,
  1381. namep, *mem_end - *mem_start);
  1382. if (l >= 0) {
  1383. /* Didn't fit? Get more room. */
  1384. if ((l+1) > (*mem_end - *mem_start)) {
  1385. namep = make_room(mem_start, mem_end, l+1, 1);
  1386. call_prom("package-to-path", 3, 1, node, namep, l);
  1387. }
  1388. namep[l] = '\0';
  1389. /* Fixup an Apple bug where they have bogus \0 chars in the
  1390. * middle of the path in some properties
  1391. */
  1392. for (p = namep, ep = namep + l; p < ep; p++)
  1393. if (*p == '\0') {
  1394. memmove(p, p+1, ep - p);
  1395. ep--; l--; p--;
  1396. }
  1397. /* now try to extract the unit name in that mess */
  1398. for (p = namep, lp = NULL; *p; p++)
  1399. if (*p == '/')
  1400. lp = p + 1;
  1401. if (lp != NULL)
  1402. memmove(namep, lp, strlen(lp) + 1);
  1403. *mem_start = _ALIGN(((unsigned long) namep) +
  1404. strlen(namep) + 1, 4);
  1405. }
  1406. /* get it again for debugging */
  1407. path = RELOC(prom_scratch);
  1408. memset(path, 0, PROM_SCRATCH_SIZE);
  1409. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  1410. /* get and store all properties */
  1411. prev_name = RELOC("");
  1412. sstart = (char *)RELOC(dt_string_start);
  1413. for (;;) {
  1414. if (call_prom("nextprop", 3, 1, node, prev_name,
  1415. RELOC(pname)) != 1)
  1416. break;
  1417. /* skip "name" */
  1418. if (strcmp(RELOC(pname), RELOC("name")) == 0) {
  1419. prev_name = RELOC("name");
  1420. continue;
  1421. }
  1422. /* find string offset */
  1423. soff = dt_find_string(RELOC(pname));
  1424. if (soff == 0) {
  1425. prom_printf("WARNING: Can't find string index for"
  1426. " <%s>, node %s\n", RELOC(pname), path);
  1427. break;
  1428. }
  1429. prev_name = sstart + soff;
  1430. /* get length */
  1431. l = call_prom("getproplen", 2, 1, node, RELOC(pname));
  1432. /* sanity checks */
  1433. if (l == PROM_ERROR)
  1434. continue;
  1435. if (l > MAX_PROPERTY_LENGTH) {
  1436. prom_printf("WARNING: ignoring large property ");
  1437. /* It seems OF doesn't null-terminate the path :-( */
  1438. prom_printf("[%s] ", path);
  1439. prom_printf("%s length 0x%x\n", RELOC(pname), l);
  1440. continue;
  1441. }
  1442. /* push property head */
  1443. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1444. dt_push_token(l, mem_start, mem_end);
  1445. dt_push_token(soff, mem_start, mem_end);
  1446. /* push property content */
  1447. valp = make_room(mem_start, mem_end, l, 4);
  1448. call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
  1449. *mem_start = _ALIGN(*mem_start, 4);
  1450. }
  1451. /* Add a "linux,phandle" property. */
  1452. soff = dt_find_string(RELOC("linux,phandle"));
  1453. if (soff == 0)
  1454. prom_printf("WARNING: Can't find string index for"
  1455. " <linux-phandle> node %s\n", path);
  1456. else {
  1457. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1458. dt_push_token(4, mem_start, mem_end);
  1459. dt_push_token(soff, mem_start, mem_end);
  1460. valp = make_room(mem_start, mem_end, 4, 4);
  1461. *(u32 *)valp = node;
  1462. }
  1463. /* do all our children */
  1464. child = call_prom("child", 1, 1, node);
  1465. while (child != 0) {
  1466. scan_dt_build_struct(child, mem_start, mem_end);
  1467. child = call_prom("peer", 1, 1, child);
  1468. }
  1469. dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
  1470. }
  1471. static void __init flatten_device_tree(void)
  1472. {
  1473. phandle root;
  1474. unsigned long offset = reloc_offset();
  1475. unsigned long mem_start, mem_end, room;
  1476. struct boot_param_header *hdr;
  1477. char *namep;
  1478. u64 *rsvmap;
  1479. /*
  1480. * Check how much room we have between alloc top & bottom (+/- a
  1481. * few pages), crop to 4Mb, as this is our "chuck" size
  1482. */
  1483. room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
  1484. if (room > DEVTREE_CHUNK_SIZE)
  1485. room = DEVTREE_CHUNK_SIZE;
  1486. prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
  1487. /* Now try to claim that */
  1488. mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
  1489. if (mem_start == 0)
  1490. prom_panic("Can't allocate initial device-tree chunk\n");
  1491. mem_end = RELOC(alloc_top);
  1492. /* Get root of tree */
  1493. root = call_prom("peer", 1, 1, (phandle)0);
  1494. if (root == (phandle)0)
  1495. prom_panic ("couldn't get device tree root\n");
  1496. /* Build header and make room for mem rsv map */
  1497. mem_start = _ALIGN(mem_start, 4);
  1498. hdr = make_room(&mem_start, &mem_end,
  1499. sizeof(struct boot_param_header), 4);
  1500. RELOC(dt_header_start) = (unsigned long)hdr;
  1501. rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
  1502. /* Start of strings */
  1503. mem_start = PAGE_ALIGN(mem_start);
  1504. RELOC(dt_string_start) = mem_start;
  1505. mem_start += 4; /* hole */
  1506. /* Add "linux,phandle" in there, we'll need it */
  1507. namep = make_room(&mem_start, &mem_end, 16, 1);
  1508. strcpy(namep, RELOC("linux,phandle"));
  1509. mem_start = (unsigned long)namep + strlen(namep) + 1;
  1510. /* Build string array */
  1511. prom_printf("Building dt strings...\n");
  1512. scan_dt_build_strings(root, &mem_start, &mem_end);
  1513. RELOC(dt_string_end) = mem_start;
  1514. /* Build structure */
  1515. mem_start = PAGE_ALIGN(mem_start);
  1516. RELOC(dt_struct_start) = mem_start;
  1517. prom_printf("Building dt structure...\n");
  1518. scan_dt_build_struct(root, &mem_start, &mem_end);
  1519. dt_push_token(OF_DT_END, &mem_start, &mem_end);
  1520. RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
  1521. /* Finish header */
  1522. hdr->magic = OF_DT_HEADER;
  1523. hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
  1524. hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
  1525. hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
  1526. hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
  1527. hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
  1528. hdr->version = OF_DT_VERSION;
  1529. /* Version 16 is not backward compatible */
  1530. hdr->last_comp_version = 0x10;
  1531. /* Reserve the whole thing and copy the reserve map in, we
  1532. * also bump mem_reserve_cnt to cause further reservations to
  1533. * fail since it's too late.
  1534. */
  1535. reserve_mem(RELOC(dt_header_start), hdr->totalsize);
  1536. memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
  1537. #ifdef DEBUG_PROM
  1538. {
  1539. int i;
  1540. prom_printf("reserved memory map:\n");
  1541. for (i = 0; i < RELOC(mem_reserve_cnt); i++)
  1542. prom_printf(" %x - %x\n", RELOC(mem_reserve_map)[i].base,
  1543. RELOC(mem_reserve_map)[i].size);
  1544. }
  1545. #endif
  1546. RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
  1547. prom_printf("Device tree strings 0x%x -> 0x%x\n",
  1548. RELOC(dt_string_start), RELOC(dt_string_end));
  1549. prom_printf("Device tree struct 0x%x -> 0x%x\n",
  1550. RELOC(dt_struct_start), RELOC(dt_struct_end));
  1551. }
  1552. static void __init fixup_device_tree(void)
  1553. {
  1554. unsigned long offset = reloc_offset();
  1555. phandle u3, i2c, mpic;
  1556. u32 u3_rev;
  1557. u32 interrupts[2];
  1558. u32 parent;
  1559. /* Some G5s have a missing interrupt definition, fix it up here */
  1560. u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
  1561. if (!PHANDLE_VALID(u3))
  1562. return;
  1563. i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
  1564. if (!PHANDLE_VALID(i2c))
  1565. return;
  1566. mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
  1567. if (!PHANDLE_VALID(mpic))
  1568. return;
  1569. /* check if proper rev of u3 */
  1570. if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
  1571. == PROM_ERROR)
  1572. return;
  1573. if (u3_rev != 0x35 && u3_rev != 0x37)
  1574. return;
  1575. /* does it need fixup ? */
  1576. if (prom_getproplen(i2c, "interrupts") > 0)
  1577. return;
  1578. prom_printf("fixing up bogus interrupts for u3 i2c...\n");
  1579. /* interrupt on this revision of u3 is number 0 and level */
  1580. interrupts[0] = 0;
  1581. interrupts[1] = 1;
  1582. prom_setprop(i2c, "interrupts", &interrupts, sizeof(interrupts));
  1583. parent = (u32)mpic;
  1584. prom_setprop(i2c, "interrupt-parent", &parent, sizeof(parent));
  1585. }
  1586. static void __init prom_find_boot_cpu(void)
  1587. {
  1588. unsigned long offset = reloc_offset();
  1589. struct prom_t *_prom = PTRRELOC(&prom);
  1590. u32 getprop_rval;
  1591. ihandle prom_cpu;
  1592. phandle cpu_pkg;
  1593. if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
  1594. prom_panic("cannot find boot cpu");
  1595. cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
  1596. prom_setprop(cpu_pkg, "linux,boot-cpu", NULL, 0);
  1597. prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
  1598. _prom->cpu = getprop_rval;
  1599. prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
  1600. }
  1601. static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
  1602. {
  1603. #ifdef CONFIG_BLK_DEV_INITRD
  1604. unsigned long offset = reloc_offset();
  1605. struct prom_t *_prom = PTRRELOC(&prom);
  1606. if ( r3 && r4 && r4 != 0xdeadbeef) {
  1607. u64 val;
  1608. RELOC(prom_initrd_start) = (r3 >= KERNELBASE) ? __pa(r3) : r3;
  1609. RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
  1610. val = (u64)RELOC(prom_initrd_start);
  1611. prom_setprop(_prom->chosen, "linux,initrd-start", &val, sizeof(val));
  1612. val = (u64)RELOC(prom_initrd_end);
  1613. prom_setprop(_prom->chosen, "linux,initrd-end", &val, sizeof(val));
  1614. reserve_mem(RELOC(prom_initrd_start),
  1615. RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
  1616. prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
  1617. prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
  1618. }
  1619. #endif /* CONFIG_BLK_DEV_INITRD */
  1620. }
  1621. /*
  1622. * We enter here early on, when the Open Firmware prom is still
  1623. * handling exceptions and the MMU hash table for us.
  1624. */
  1625. unsigned long __init prom_init(unsigned long r3, unsigned long r4, unsigned long pp,
  1626. unsigned long r6, unsigned long r7)
  1627. {
  1628. unsigned long offset = reloc_offset();
  1629. struct prom_t *_prom = PTRRELOC(&prom);
  1630. unsigned long phys = KERNELBASE - offset;
  1631. u32 getprop_rval;
  1632. /*
  1633. * First zero the BSS
  1634. */
  1635. memset(PTRRELOC(&__bss_start), 0, __bss_stop - __bss_start);
  1636. /*
  1637. * Init interface to Open Firmware, get some node references,
  1638. * like /chosen
  1639. */
  1640. prom_init_client_services(pp);
  1641. /*
  1642. * Init prom stdout device
  1643. */
  1644. prom_init_stdout();
  1645. prom_debug("klimit=0x%x\n", RELOC(klimit));
  1646. prom_debug("offset=0x%x\n", offset);
  1647. /*
  1648. * Check for an initrd
  1649. */
  1650. prom_check_initrd(r3, r4);
  1651. /*
  1652. * Get default machine type. At this point, we do not differenciate
  1653. * between pSeries SMP and pSeries LPAR
  1654. */
  1655. RELOC(of_platform) = prom_find_machine_type();
  1656. getprop_rval = RELOC(of_platform);
  1657. prom_setprop(_prom->chosen, "linux,platform",
  1658. &getprop_rval, sizeof(getprop_rval));
  1659. /*
  1660. * On pSeries, inform the firmware about our capabilities
  1661. */
  1662. if (RELOC(of_platform) & PLATFORM_PSERIES)
  1663. prom_send_capabilities();
  1664. /*
  1665. * On pSeries and BPA, copy the CPU hold code
  1666. */
  1667. if (RELOC(of_platform) & (PLATFORM_PSERIES | PLATFORM_BPA))
  1668. copy_and_flush(0, KERNELBASE - offset, 0x100, 0);
  1669. /*
  1670. * Get memory cells format
  1671. */
  1672. getprop_rval = 1;
  1673. prom_getprop(_prom->root, "#size-cells",
  1674. &getprop_rval, sizeof(getprop_rval));
  1675. _prom->root_size_cells = getprop_rval;
  1676. getprop_rval = 2;
  1677. prom_getprop(_prom->root, "#address-cells",
  1678. &getprop_rval, sizeof(getprop_rval));
  1679. _prom->root_addr_cells = getprop_rval;
  1680. /*
  1681. * Do early parsing of command line
  1682. */
  1683. early_cmdline_parse();
  1684. /*
  1685. * Initialize memory management within prom_init
  1686. */
  1687. prom_init_mem();
  1688. /*
  1689. * Determine which cpu is actually running right _now_
  1690. */
  1691. prom_find_boot_cpu();
  1692. /*
  1693. * Initialize display devices
  1694. */
  1695. prom_check_displays();
  1696. /*
  1697. * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
  1698. * that uses the allocator, we need to make sure we get the top of memory
  1699. * available for us here...
  1700. */
  1701. if (RELOC(of_platform) == PLATFORM_PSERIES)
  1702. prom_initialize_tce_table();
  1703. /*
  1704. * On non-powermacs, try to instantiate RTAS and puts all CPUs
  1705. * in spin-loops. PowerMacs don't have a working RTAS and use
  1706. * a different way to spin CPUs
  1707. */
  1708. if (RELOC(of_platform) != PLATFORM_POWERMAC) {
  1709. prom_instantiate_rtas();
  1710. prom_hold_cpus();
  1711. }
  1712. /*
  1713. * Fill in some infos for use by the kernel later on
  1714. */
  1715. if (RELOC(ppc64_iommu_off))
  1716. prom_setprop(_prom->chosen, "linux,iommu-off", NULL, 0);
  1717. if (RELOC(iommu_force_on))
  1718. prom_setprop(_prom->chosen, "linux,iommu-force-on", NULL, 0);
  1719. if (RELOC(prom_memory_limit))
  1720. prom_setprop(_prom->chosen, "linux,memory-limit",
  1721. PTRRELOC(&prom_memory_limit), sizeof(RELOC(prom_memory_limit)));
  1722. if (RELOC(prom_tce_alloc_start)) {
  1723. prom_setprop(_prom->chosen, "linux,tce-alloc-start",
  1724. PTRRELOC(&prom_tce_alloc_start), sizeof(RELOC(prom_tce_alloc_start)));
  1725. prom_setprop(_prom->chosen, "linux,tce-alloc-end",
  1726. PTRRELOC(&prom_tce_alloc_end), sizeof(RELOC(prom_tce_alloc_end)));
  1727. }
  1728. /*
  1729. * Fixup any known bugs in the device-tree
  1730. */
  1731. fixup_device_tree();
  1732. /*
  1733. * Now finally create the flattened device-tree
  1734. */
  1735. prom_printf("copying OF device tree ...\n");
  1736. flatten_device_tree();
  1737. /* in case stdin is USB and still active on IBM machines... */
  1738. prom_close_stdin();
  1739. /*
  1740. * Call OF "quiesce" method to shut down pending DMA's from
  1741. * devices etc...
  1742. */
  1743. prom_printf("Calling quiesce ...\n");
  1744. call_prom("quiesce", 0, 0);
  1745. /*
  1746. * And finally, call the kernel passing it the flattened device
  1747. * tree and NULL as r5, thus triggering the new entry point which
  1748. * is common to us and kexec
  1749. */
  1750. prom_printf("returning from prom_init\n");
  1751. prom_debug("->dt_header_start=0x%x\n", RELOC(dt_header_start));
  1752. prom_debug("->phys=0x%x\n", phys);
  1753. __start(RELOC(dt_header_start), phys, 0);
  1754. return 0;
  1755. }