prom_init.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463
  1. /*
  2. * Procedures for interfacing to Open Firmware.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #undef DEBUG_PROM
  16. #include <stdarg.h>
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/init.h>
  20. #include <linux/threads.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/types.h>
  23. #include <linux/pci.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/stringify.h>
  26. #include <linux/delay.h>
  27. #include <linux/initrd.h>
  28. #include <linux/bitops.h>
  29. #include <asm/prom.h>
  30. #include <asm/rtas.h>
  31. #include <asm/page.h>
  32. #include <asm/processor.h>
  33. #include <asm/irq.h>
  34. #include <asm/io.h>
  35. #include <asm/smp.h>
  36. #include <asm/system.h>
  37. #include <asm/mmu.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/pci.h>
  40. #include <asm/iommu.h>
  41. #include <asm/btext.h>
  42. #include <asm/sections.h>
  43. #include <asm/machdep.h>
  44. #include <linux/linux_logo.h>
  45. /*
  46. * Properties whose value is longer than this get excluded from our
  47. * copy of the device tree. This value does need to be big enough to
  48. * ensure that we don't lose things like the interrupt-map property
  49. * on a PCI-PCI bridge.
  50. */
  51. #define MAX_PROPERTY_LENGTH (1UL * 1024 * 1024)
  52. /*
  53. * Eventually bump that one up
  54. */
  55. #define DEVTREE_CHUNK_SIZE 0x100000
  56. /*
  57. * This is the size of the local memory reserve map that gets copied
  58. * into the boot params passed to the kernel. That size is totally
  59. * flexible as the kernel just reads the list until it encounters an
  60. * entry with size 0, so it can be changed without breaking binary
  61. * compatibility
  62. */
  63. #define MEM_RESERVE_MAP_SIZE 8
  64. /*
  65. * prom_init() is called very early on, before the kernel text
  66. * and data have been mapped to KERNELBASE. At this point the code
  67. * is running at whatever address it has been loaded at.
  68. * On ppc32 we compile with -mrelocatable, which means that references
  69. * to extern and static variables get relocated automatically.
  70. * On ppc64 we have to relocate the references explicitly with
  71. * RELOC. (Note that strings count as static variables.)
  72. *
  73. * Because OF may have mapped I/O devices into the area starting at
  74. * KERNELBASE, particularly on CHRP machines, we can't safely call
  75. * OF once the kernel has been mapped to KERNELBASE. Therefore all
  76. * OF calls must be done within prom_init().
  77. *
  78. * ADDR is used in calls to call_prom. The 4th and following
  79. * arguments to call_prom should be 32-bit values.
  80. * On ppc64, 64 bit values are truncated to 32 bits (and
  81. * fortunately don't get interpreted as two arguments).
  82. */
  83. #ifdef CONFIG_PPC64
  84. #define RELOC(x) (*PTRRELOC(&(x)))
  85. #define ADDR(x) (u32) add_reloc_offset((unsigned long)(x))
  86. #define OF_WORKAROUNDS 0
  87. #else
  88. #define RELOC(x) (x)
  89. #define ADDR(x) (u32) (x)
  90. #define OF_WORKAROUNDS of_workarounds
  91. int of_workarounds;
  92. #endif
  93. #define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */
  94. #define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */
  95. #define PROM_BUG() do { \
  96. prom_printf("kernel BUG at %s line 0x%x!\n", \
  97. RELOC(__FILE__), __LINE__); \
  98. __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \
  99. } while (0)
  100. #ifdef DEBUG_PROM
  101. #define prom_debug(x...) prom_printf(x)
  102. #else
  103. #define prom_debug(x...)
  104. #endif
  105. typedef u32 prom_arg_t;
  106. struct prom_args {
  107. u32 service;
  108. u32 nargs;
  109. u32 nret;
  110. prom_arg_t args[10];
  111. };
  112. struct prom_t {
  113. ihandle root;
  114. phandle chosen;
  115. int cpu;
  116. ihandle stdout;
  117. ihandle mmumap;
  118. ihandle memory;
  119. };
  120. struct mem_map_entry {
  121. u64 base;
  122. u64 size;
  123. };
  124. typedef u32 cell_t;
  125. extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
  126. #ifdef CONFIG_PPC64
  127. extern int enter_prom(struct prom_args *args, unsigned long entry);
  128. #else
  129. static inline int enter_prom(struct prom_args *args, unsigned long entry)
  130. {
  131. return ((int (*)(struct prom_args *))entry)(args);
  132. }
  133. #endif
  134. extern void copy_and_flush(unsigned long dest, unsigned long src,
  135. unsigned long size, unsigned long offset);
  136. /* prom structure */
  137. static struct prom_t __initdata prom;
  138. static unsigned long prom_entry __initdata;
  139. #define PROM_SCRATCH_SIZE 256
  140. static char __initdata of_stdout_device[256];
  141. static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
  142. static unsigned long __initdata dt_header_start;
  143. static unsigned long __initdata dt_struct_start, dt_struct_end;
  144. static unsigned long __initdata dt_string_start, dt_string_end;
  145. static unsigned long __initdata prom_initrd_start, prom_initrd_end;
  146. #ifdef CONFIG_PPC64
  147. static int __initdata prom_iommu_force_on;
  148. static int __initdata prom_iommu_off;
  149. static unsigned long __initdata prom_tce_alloc_start;
  150. static unsigned long __initdata prom_tce_alloc_end;
  151. #endif
  152. /* Platforms codes are now obsolete in the kernel. Now only used within this
  153. * file and ultimately gone too. Feel free to change them if you need, they
  154. * are not shared with anything outside of this file anymore
  155. */
  156. #define PLATFORM_PSERIES 0x0100
  157. #define PLATFORM_PSERIES_LPAR 0x0101
  158. #define PLATFORM_LPAR 0x0001
  159. #define PLATFORM_POWERMAC 0x0400
  160. #define PLATFORM_GENERIC 0x0500
  161. static int __initdata of_platform;
  162. static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
  163. static unsigned long __initdata alloc_top;
  164. static unsigned long __initdata alloc_top_high;
  165. static unsigned long __initdata alloc_bottom;
  166. static unsigned long __initdata rmo_top;
  167. static unsigned long __initdata ram_top;
  168. static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
  169. static int __initdata mem_reserve_cnt;
  170. static cell_t __initdata regbuf[1024];
  171. /*
  172. * Error results ... some OF calls will return "-1" on error, some
  173. * will return 0, some will return either. To simplify, here are
  174. * macros to use with any ihandle or phandle return value to check if
  175. * it is valid
  176. */
  177. #define PROM_ERROR (-1u)
  178. #define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
  179. #define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
  180. /* This is the one and *ONLY* place where we actually call open
  181. * firmware.
  182. */
  183. static int __init call_prom(const char *service, int nargs, int nret, ...)
  184. {
  185. int i;
  186. struct prom_args args;
  187. va_list list;
  188. args.service = ADDR(service);
  189. args.nargs = nargs;
  190. args.nret = nret;
  191. va_start(list, nret);
  192. for (i = 0; i < nargs; i++)
  193. args.args[i] = va_arg(list, prom_arg_t);
  194. va_end(list);
  195. for (i = 0; i < nret; i++)
  196. args.args[nargs+i] = 0;
  197. if (enter_prom(&args, RELOC(prom_entry)) < 0)
  198. return PROM_ERROR;
  199. return (nret > 0) ? args.args[nargs] : 0;
  200. }
  201. static int __init call_prom_ret(const char *service, int nargs, int nret,
  202. prom_arg_t *rets, ...)
  203. {
  204. int i;
  205. struct prom_args args;
  206. va_list list;
  207. args.service = ADDR(service);
  208. args.nargs = nargs;
  209. args.nret = nret;
  210. va_start(list, rets);
  211. for (i = 0; i < nargs; i++)
  212. args.args[i] = va_arg(list, prom_arg_t);
  213. va_end(list);
  214. for (i = 0; i < nret; i++)
  215. args.args[nargs+i] = 0;
  216. if (enter_prom(&args, RELOC(prom_entry)) < 0)
  217. return PROM_ERROR;
  218. if (rets != NULL)
  219. for (i = 1; i < nret; ++i)
  220. rets[i-1] = args.args[nargs+i];
  221. return (nret > 0) ? args.args[nargs] : 0;
  222. }
  223. static void __init prom_print(const char *msg)
  224. {
  225. const char *p, *q;
  226. struct prom_t *_prom = &RELOC(prom);
  227. if (_prom->stdout == 0)
  228. return;
  229. for (p = msg; *p != 0; p = q) {
  230. for (q = p; *q != 0 && *q != '\n'; ++q)
  231. ;
  232. if (q > p)
  233. call_prom("write", 3, 1, _prom->stdout, p, q - p);
  234. if (*q == 0)
  235. break;
  236. ++q;
  237. call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
  238. }
  239. }
  240. static void __init prom_print_hex(unsigned long val)
  241. {
  242. int i, nibbles = sizeof(val)*2;
  243. char buf[sizeof(val)*2+1];
  244. struct prom_t *_prom = &RELOC(prom);
  245. for (i = nibbles-1; i >= 0; i--) {
  246. buf[i] = (val & 0xf) + '0';
  247. if (buf[i] > '9')
  248. buf[i] += ('a'-'0'-10);
  249. val >>= 4;
  250. }
  251. buf[nibbles] = '\0';
  252. call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
  253. }
  254. static void __init prom_printf(const char *format, ...)
  255. {
  256. const char *p, *q, *s;
  257. va_list args;
  258. unsigned long v;
  259. struct prom_t *_prom = &RELOC(prom);
  260. va_start(args, format);
  261. #ifdef CONFIG_PPC64
  262. format = PTRRELOC(format);
  263. #endif
  264. for (p = format; *p != 0; p = q) {
  265. for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
  266. ;
  267. if (q > p)
  268. call_prom("write", 3, 1, _prom->stdout, p, q - p);
  269. if (*q == 0)
  270. break;
  271. if (*q == '\n') {
  272. ++q;
  273. call_prom("write", 3, 1, _prom->stdout,
  274. ADDR("\r\n"), 2);
  275. continue;
  276. }
  277. ++q;
  278. if (*q == 0)
  279. break;
  280. switch (*q) {
  281. case 's':
  282. ++q;
  283. s = va_arg(args, const char *);
  284. prom_print(s);
  285. break;
  286. case 'x':
  287. ++q;
  288. v = va_arg(args, unsigned long);
  289. prom_print_hex(v);
  290. break;
  291. }
  292. }
  293. }
  294. static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
  295. unsigned long align)
  296. {
  297. struct prom_t *_prom = &RELOC(prom);
  298. if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
  299. /*
  300. * Old OF requires we claim physical and virtual separately
  301. * and then map explicitly (assuming virtual mode)
  302. */
  303. int ret;
  304. prom_arg_t result;
  305. ret = call_prom_ret("call-method", 5, 2, &result,
  306. ADDR("claim"), _prom->memory,
  307. align, size, virt);
  308. if (ret != 0 || result == -1)
  309. return -1;
  310. ret = call_prom_ret("call-method", 5, 2, &result,
  311. ADDR("claim"), _prom->mmumap,
  312. align, size, virt);
  313. if (ret != 0) {
  314. call_prom("call-method", 4, 1, ADDR("release"),
  315. _prom->memory, size, virt);
  316. return -1;
  317. }
  318. /* the 0x12 is M (coherence) + PP == read/write */
  319. call_prom("call-method", 6, 1,
  320. ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
  321. return virt;
  322. }
  323. return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
  324. (prom_arg_t)align);
  325. }
  326. static void __init __attribute__((noreturn)) prom_panic(const char *reason)
  327. {
  328. #ifdef CONFIG_PPC64
  329. reason = PTRRELOC(reason);
  330. #endif
  331. prom_print(reason);
  332. /* Do not call exit because it clears the screen on pmac
  333. * it also causes some sort of double-fault on early pmacs */
  334. if (RELOC(of_platform) == PLATFORM_POWERMAC)
  335. asm("trap\n");
  336. /* ToDo: should put up an SRC here on p/iSeries */
  337. call_prom("exit", 0, 0);
  338. for (;;) /* should never get here */
  339. ;
  340. }
  341. static int __init prom_next_node(phandle *nodep)
  342. {
  343. phandle node;
  344. if ((node = *nodep) != 0
  345. && (*nodep = call_prom("child", 1, 1, node)) != 0)
  346. return 1;
  347. if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
  348. return 1;
  349. for (;;) {
  350. if ((node = call_prom("parent", 1, 1, node)) == 0)
  351. return 0;
  352. if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
  353. return 1;
  354. }
  355. }
  356. static int inline prom_getprop(phandle node, const char *pname,
  357. void *value, size_t valuelen)
  358. {
  359. return call_prom("getprop", 4, 1, node, ADDR(pname),
  360. (u32)(unsigned long) value, (u32) valuelen);
  361. }
  362. static int inline prom_getproplen(phandle node, const char *pname)
  363. {
  364. return call_prom("getproplen", 2, 1, node, ADDR(pname));
  365. }
  366. static void add_string(char **str, const char *q)
  367. {
  368. char *p = *str;
  369. while (*q)
  370. *p++ = *q++;
  371. *p++ = ' ';
  372. *str = p;
  373. }
  374. static char *tohex(unsigned int x)
  375. {
  376. static char digits[] = "0123456789abcdef";
  377. static char result[9];
  378. int i;
  379. result[8] = 0;
  380. i = 8;
  381. do {
  382. --i;
  383. result[i] = digits[x & 0xf];
  384. x >>= 4;
  385. } while (x != 0 && i > 0);
  386. return &result[i];
  387. }
  388. static int __init prom_setprop(phandle node, const char *nodename,
  389. const char *pname, void *value, size_t valuelen)
  390. {
  391. char cmd[256], *p;
  392. if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
  393. return call_prom("setprop", 4, 1, node, ADDR(pname),
  394. (u32)(unsigned long) value, (u32) valuelen);
  395. /* gah... setprop doesn't work on longtrail, have to use interpret */
  396. p = cmd;
  397. add_string(&p, "dev");
  398. add_string(&p, nodename);
  399. add_string(&p, tohex((u32)(unsigned long) value));
  400. add_string(&p, tohex(valuelen));
  401. add_string(&p, tohex(ADDR(pname)));
  402. add_string(&p, tohex(strlen(RELOC(pname))));
  403. add_string(&p, "property");
  404. *p = 0;
  405. return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
  406. }
  407. /*
  408. * Early parsing of the command line passed to the kernel, used for
  409. * "mem=x" and the options that affect the iommu
  410. */
  411. static void __init early_cmdline_parse(void)
  412. {
  413. struct prom_t *_prom = &RELOC(prom);
  414. #ifdef CONFIG_PPC64
  415. const char *opt;
  416. #endif
  417. char *p;
  418. int l = 0;
  419. RELOC(prom_cmd_line[0]) = 0;
  420. p = RELOC(prom_cmd_line);
  421. if ((long)_prom->chosen > 0)
  422. l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
  423. #ifdef CONFIG_CMDLINE
  424. if (l <= 0 || p[0] == '\0') /* dbl check */
  425. strlcpy(RELOC(prom_cmd_line),
  426. RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
  427. #endif /* CONFIG_CMDLINE */
  428. prom_printf("command line: %s\n", RELOC(prom_cmd_line));
  429. #ifdef CONFIG_PPC64
  430. opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
  431. if (opt) {
  432. prom_printf("iommu opt is: %s\n", opt);
  433. opt += 6;
  434. while (*opt && *opt == ' ')
  435. opt++;
  436. if (!strncmp(opt, RELOC("off"), 3))
  437. RELOC(prom_iommu_off) = 1;
  438. else if (!strncmp(opt, RELOC("force"), 5))
  439. RELOC(prom_iommu_force_on) = 1;
  440. }
  441. #endif
  442. }
  443. #ifdef CONFIG_PPC_PSERIES
  444. /*
  445. * There are two methods for telling firmware what our capabilities are.
  446. * Newer machines have an "ibm,client-architecture-support" method on the
  447. * root node. For older machines, we have to call the "process-elf-header"
  448. * method in the /packages/elf-loader node, passing it a fake 32-bit
  449. * ELF header containing a couple of PT_NOTE sections that contain
  450. * structures that contain various information.
  451. */
  452. /*
  453. * New method - extensible architecture description vector.
  454. *
  455. * Because the description vector contains a mix of byte and word
  456. * values, we declare it as an unsigned char array, and use this
  457. * macro to put word values in.
  458. */
  459. #define W(x) ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
  460. ((x) >> 8) & 0xff, (x) & 0xff
  461. /* Option vector bits - generic bits in byte 1 */
  462. #define OV_IGNORE 0x80 /* ignore this vector */
  463. #define OV_CESSATION_POLICY 0x40 /* halt if unsupported option present*/
  464. /* Option vector 1: processor architectures supported */
  465. #define OV1_PPC_2_00 0x80 /* set if we support PowerPC 2.00 */
  466. #define OV1_PPC_2_01 0x40 /* set if we support PowerPC 2.01 */
  467. #define OV1_PPC_2_02 0x20 /* set if we support PowerPC 2.02 */
  468. #define OV1_PPC_2_03 0x10 /* set if we support PowerPC 2.03 */
  469. #define OV1_PPC_2_04 0x08 /* set if we support PowerPC 2.04 */
  470. #define OV1_PPC_2_05 0x04 /* set if we support PowerPC 2.05 */
  471. #define OV1_PPC_2_06 0x02 /* set if we support PowerPC 2.06 */
  472. /* Option vector 2: Open Firmware options supported */
  473. #define OV2_REAL_MODE 0x20 /* set if we want OF in real mode */
  474. /* Option vector 3: processor options supported */
  475. #define OV3_FP 0x80 /* floating point */
  476. #define OV3_VMX 0x40 /* VMX/Altivec */
  477. #define OV3_DFP 0x20 /* decimal FP */
  478. /* Option vector 5: PAPR/OF options supported */
  479. #define OV5_LPAR 0x80 /* logical partitioning supported */
  480. #define OV5_SPLPAR 0x40 /* shared-processor LPAR supported */
  481. /* ibm,dynamic-reconfiguration-memory property supported */
  482. #define OV5_DRCONF_MEMORY 0x20
  483. #define OV5_LARGE_PAGES 0x10 /* large pages supported */
  484. #define OV5_DONATE_DEDICATE_CPU 0x02 /* donate dedicated CPU support */
  485. /* PCIe/MSI support. Without MSI full PCIe is not supported */
  486. #ifdef CONFIG_PCI_MSI
  487. #define OV5_MSI 0x01 /* PCIe/MSI support */
  488. #else
  489. #define OV5_MSI 0x00
  490. #endif /* CONFIG_PCI_MSI */
  491. #ifdef CONFIG_PPC_SMLPAR
  492. #define OV5_CMO 0x80 /* Cooperative Memory Overcommitment */
  493. #else
  494. #define OV5_CMO 0x00
  495. #endif
  496. /*
  497. * The architecture vector has an array of PVR mask/value pairs,
  498. * followed by # option vectors - 1, followed by the option vectors.
  499. */
  500. static unsigned char ibm_architecture_vec[] = {
  501. W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */
  502. W(0xffff0000), W(0x003e0000), /* POWER6 */
  503. W(0xffff0000), W(0x003f0000), /* POWER7 */
  504. W(0xffffffff), W(0x0f000003), /* all 2.06-compliant */
  505. W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */
  506. W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */
  507. 5 - 1, /* 5 option vectors */
  508. /* option vector 1: processor architectures supported */
  509. 3 - 2, /* length */
  510. 0, /* don't ignore, don't halt */
  511. OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
  512. OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
  513. /* option vector 2: Open Firmware options supported */
  514. 34 - 2, /* length */
  515. OV2_REAL_MODE,
  516. 0, 0,
  517. W(0xffffffff), /* real_base */
  518. W(0xffffffff), /* real_size */
  519. W(0xffffffff), /* virt_base */
  520. W(0xffffffff), /* virt_size */
  521. W(0xffffffff), /* load_base */
  522. W(64), /* 64MB min RMA */
  523. W(0xffffffff), /* full client load */
  524. 0, /* min RMA percentage of total RAM */
  525. 48, /* max log_2(hash table size) */
  526. /* option vector 3: processor options supported */
  527. 3 - 2, /* length */
  528. 0, /* don't ignore, don't halt */
  529. OV3_FP | OV3_VMX | OV3_DFP,
  530. /* option vector 4: IBM PAPR implementation */
  531. 2 - 2, /* length */
  532. 0, /* don't halt */
  533. /* option vector 5: PAPR/OF options */
  534. 5 - 2, /* length */
  535. 0, /* don't ignore, don't halt */
  536. OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
  537. OV5_DONATE_DEDICATE_CPU | OV5_MSI,
  538. 0,
  539. OV5_CMO,
  540. };
  541. /* Old method - ELF header with PT_NOTE sections */
  542. static struct fake_elf {
  543. Elf32_Ehdr elfhdr;
  544. Elf32_Phdr phdr[2];
  545. struct chrpnote {
  546. u32 namesz;
  547. u32 descsz;
  548. u32 type;
  549. char name[8]; /* "PowerPC" */
  550. struct chrpdesc {
  551. u32 real_mode;
  552. u32 real_base;
  553. u32 real_size;
  554. u32 virt_base;
  555. u32 virt_size;
  556. u32 load_base;
  557. } chrpdesc;
  558. } chrpnote;
  559. struct rpanote {
  560. u32 namesz;
  561. u32 descsz;
  562. u32 type;
  563. char name[24]; /* "IBM,RPA-Client-Config" */
  564. struct rpadesc {
  565. u32 lpar_affinity;
  566. u32 min_rmo_size;
  567. u32 min_rmo_percent;
  568. u32 max_pft_size;
  569. u32 splpar;
  570. u32 min_load;
  571. u32 new_mem_def;
  572. u32 ignore_me;
  573. } rpadesc;
  574. } rpanote;
  575. } fake_elf = {
  576. .elfhdr = {
  577. .e_ident = { 0x7f, 'E', 'L', 'F',
  578. ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
  579. .e_type = ET_EXEC, /* yeah right */
  580. .e_machine = EM_PPC,
  581. .e_version = EV_CURRENT,
  582. .e_phoff = offsetof(struct fake_elf, phdr),
  583. .e_phentsize = sizeof(Elf32_Phdr),
  584. .e_phnum = 2
  585. },
  586. .phdr = {
  587. [0] = {
  588. .p_type = PT_NOTE,
  589. .p_offset = offsetof(struct fake_elf, chrpnote),
  590. .p_filesz = sizeof(struct chrpnote)
  591. }, [1] = {
  592. .p_type = PT_NOTE,
  593. .p_offset = offsetof(struct fake_elf, rpanote),
  594. .p_filesz = sizeof(struct rpanote)
  595. }
  596. },
  597. .chrpnote = {
  598. .namesz = sizeof("PowerPC"),
  599. .descsz = sizeof(struct chrpdesc),
  600. .type = 0x1275,
  601. .name = "PowerPC",
  602. .chrpdesc = {
  603. .real_mode = ~0U, /* ~0 means "don't care" */
  604. .real_base = ~0U,
  605. .real_size = ~0U,
  606. .virt_base = ~0U,
  607. .virt_size = ~0U,
  608. .load_base = ~0U
  609. },
  610. },
  611. .rpanote = {
  612. .namesz = sizeof("IBM,RPA-Client-Config"),
  613. .descsz = sizeof(struct rpadesc),
  614. .type = 0x12759999,
  615. .name = "IBM,RPA-Client-Config",
  616. .rpadesc = {
  617. .lpar_affinity = 0,
  618. .min_rmo_size = 64, /* in megabytes */
  619. .min_rmo_percent = 0,
  620. .max_pft_size = 48, /* 2^48 bytes max PFT size */
  621. .splpar = 1,
  622. .min_load = ~0U,
  623. .new_mem_def = 0
  624. }
  625. }
  626. };
  627. static void __init prom_send_capabilities(void)
  628. {
  629. ihandle elfloader, root;
  630. prom_arg_t ret;
  631. root = call_prom("open", 1, 1, ADDR("/"));
  632. if (root != 0) {
  633. /* try calling the ibm,client-architecture-support method */
  634. prom_printf("Calling ibm,client-architecture...");
  635. if (call_prom_ret("call-method", 3, 2, &ret,
  636. ADDR("ibm,client-architecture-support"),
  637. root,
  638. ADDR(ibm_architecture_vec)) == 0) {
  639. /* the call exists... */
  640. if (ret)
  641. prom_printf("\nWARNING: ibm,client-architecture"
  642. "-support call FAILED!\n");
  643. call_prom("close", 1, 0, root);
  644. prom_printf(" done\n");
  645. return;
  646. }
  647. call_prom("close", 1, 0, root);
  648. }
  649. /* no ibm,client-architecture-support call, try the old way */
  650. elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
  651. if (elfloader == 0) {
  652. prom_printf("couldn't open /packages/elf-loader\n");
  653. return;
  654. }
  655. call_prom("call-method", 3, 1, ADDR("process-elf-header"),
  656. elfloader, ADDR(&fake_elf));
  657. call_prom("close", 1, 0, elfloader);
  658. }
  659. #endif
  660. /*
  661. * Memory allocation strategy... our layout is normally:
  662. *
  663. * at 14Mb or more we have vmlinux, then a gap and initrd. In some
  664. * rare cases, initrd might end up being before the kernel though.
  665. * We assume this won't override the final kernel at 0, we have no
  666. * provision to handle that in this version, but it should hopefully
  667. * never happen.
  668. *
  669. * alloc_top is set to the top of RMO, eventually shrink down if the
  670. * TCEs overlap
  671. *
  672. * alloc_bottom is set to the top of kernel/initrd
  673. *
  674. * from there, allocations are done this way : rtas is allocated
  675. * topmost, and the device-tree is allocated from the bottom. We try
  676. * to grow the device-tree allocation as we progress. If we can't,
  677. * then we fail, we don't currently have a facility to restart
  678. * elsewhere, but that shouldn't be necessary.
  679. *
  680. * Note that calls to reserve_mem have to be done explicitly, memory
  681. * allocated with either alloc_up or alloc_down isn't automatically
  682. * reserved.
  683. */
  684. /*
  685. * Allocates memory in the RMO upward from the kernel/initrd
  686. *
  687. * When align is 0, this is a special case, it means to allocate in place
  688. * at the current location of alloc_bottom or fail (that is basically
  689. * extending the previous allocation). Used for the device-tree flattening
  690. */
  691. static unsigned long __init alloc_up(unsigned long size, unsigned long align)
  692. {
  693. unsigned long base = RELOC(alloc_bottom);
  694. unsigned long addr = 0;
  695. if (align)
  696. base = _ALIGN_UP(base, align);
  697. prom_debug("alloc_up(%x, %x)\n", size, align);
  698. if (RELOC(ram_top) == 0)
  699. prom_panic("alloc_up() called with mem not initialized\n");
  700. if (align)
  701. base = _ALIGN_UP(RELOC(alloc_bottom), align);
  702. else
  703. base = RELOC(alloc_bottom);
  704. for(; (base + size) <= RELOC(alloc_top);
  705. base = _ALIGN_UP(base + 0x100000, align)) {
  706. prom_debug(" trying: 0x%x\n\r", base);
  707. addr = (unsigned long)prom_claim(base, size, 0);
  708. if (addr != PROM_ERROR && addr != 0)
  709. break;
  710. addr = 0;
  711. if (align == 0)
  712. break;
  713. }
  714. if (addr == 0)
  715. return 0;
  716. RELOC(alloc_bottom) = addr;
  717. prom_debug(" -> %x\n", addr);
  718. prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  719. prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
  720. prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  721. prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
  722. prom_debug(" ram_top : %x\n", RELOC(ram_top));
  723. return addr;
  724. }
  725. /*
  726. * Allocates memory downward, either from top of RMO, or if highmem
  727. * is set, from the top of RAM. Note that this one doesn't handle
  728. * failures. It does claim memory if highmem is not set.
  729. */
  730. static unsigned long __init alloc_down(unsigned long size, unsigned long align,
  731. int highmem)
  732. {
  733. unsigned long base, addr = 0;
  734. prom_debug("alloc_down(%x, %x, %s)\n", size, align,
  735. highmem ? RELOC("(high)") : RELOC("(low)"));
  736. if (RELOC(ram_top) == 0)
  737. prom_panic("alloc_down() called with mem not initialized\n");
  738. if (highmem) {
  739. /* Carve out storage for the TCE table. */
  740. addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
  741. if (addr <= RELOC(alloc_bottom))
  742. return 0;
  743. /* Will we bump into the RMO ? If yes, check out that we
  744. * didn't overlap existing allocations there, if we did,
  745. * we are dead, we must be the first in town !
  746. */
  747. if (addr < RELOC(rmo_top)) {
  748. /* Good, we are first */
  749. if (RELOC(alloc_top) == RELOC(rmo_top))
  750. RELOC(alloc_top) = RELOC(rmo_top) = addr;
  751. else
  752. return 0;
  753. }
  754. RELOC(alloc_top_high) = addr;
  755. goto bail;
  756. }
  757. base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
  758. for (; base > RELOC(alloc_bottom);
  759. base = _ALIGN_DOWN(base - 0x100000, align)) {
  760. prom_debug(" trying: 0x%x\n\r", base);
  761. addr = (unsigned long)prom_claim(base, size, 0);
  762. if (addr != PROM_ERROR && addr != 0)
  763. break;
  764. addr = 0;
  765. }
  766. if (addr == 0)
  767. return 0;
  768. RELOC(alloc_top) = addr;
  769. bail:
  770. prom_debug(" -> %x\n", addr);
  771. prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  772. prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
  773. prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  774. prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
  775. prom_debug(" ram_top : %x\n", RELOC(ram_top));
  776. return addr;
  777. }
  778. /*
  779. * Parse a "reg" cell
  780. */
  781. static unsigned long __init prom_next_cell(int s, cell_t **cellp)
  782. {
  783. cell_t *p = *cellp;
  784. unsigned long r = 0;
  785. /* Ignore more than 2 cells */
  786. while (s > sizeof(unsigned long) / 4) {
  787. p++;
  788. s--;
  789. }
  790. r = *p++;
  791. #ifdef CONFIG_PPC64
  792. if (s > 1) {
  793. r <<= 32;
  794. r |= *(p++);
  795. }
  796. #endif
  797. *cellp = p;
  798. return r;
  799. }
  800. /*
  801. * Very dumb function for adding to the memory reserve list, but
  802. * we don't need anything smarter at this point
  803. *
  804. * XXX Eventually check for collisions. They should NEVER happen.
  805. * If problems seem to show up, it would be a good start to track
  806. * them down.
  807. */
  808. static void __init reserve_mem(u64 base, u64 size)
  809. {
  810. u64 top = base + size;
  811. unsigned long cnt = RELOC(mem_reserve_cnt);
  812. if (size == 0)
  813. return;
  814. /* We need to always keep one empty entry so that we
  815. * have our terminator with "size" set to 0 since we are
  816. * dumb and just copy this entire array to the boot params
  817. */
  818. base = _ALIGN_DOWN(base, PAGE_SIZE);
  819. top = _ALIGN_UP(top, PAGE_SIZE);
  820. size = top - base;
  821. if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
  822. prom_panic("Memory reserve map exhausted !\n");
  823. RELOC(mem_reserve_map)[cnt].base = base;
  824. RELOC(mem_reserve_map)[cnt].size = size;
  825. RELOC(mem_reserve_cnt) = cnt + 1;
  826. }
  827. /*
  828. * Initialize memory allocation mechanism, parse "memory" nodes and
  829. * obtain that way the top of memory and RMO to setup out local allocator
  830. */
  831. static void __init prom_init_mem(void)
  832. {
  833. phandle node;
  834. char *path, type[64];
  835. unsigned int plen;
  836. cell_t *p, *endp;
  837. struct prom_t *_prom = &RELOC(prom);
  838. u32 rac, rsc;
  839. /*
  840. * We iterate the memory nodes to find
  841. * 1) top of RMO (first node)
  842. * 2) top of memory
  843. */
  844. rac = 2;
  845. prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
  846. rsc = 1;
  847. prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
  848. prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
  849. prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
  850. prom_debug("scanning memory:\n");
  851. path = RELOC(prom_scratch);
  852. for (node = 0; prom_next_node(&node); ) {
  853. type[0] = 0;
  854. prom_getprop(node, "device_type", type, sizeof(type));
  855. if (type[0] == 0) {
  856. /*
  857. * CHRP Longtrail machines have no device_type
  858. * on the memory node, so check the name instead...
  859. */
  860. prom_getprop(node, "name", type, sizeof(type));
  861. }
  862. if (strcmp(type, RELOC("memory")))
  863. continue;
  864. plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
  865. if (plen > sizeof(regbuf)) {
  866. prom_printf("memory node too large for buffer !\n");
  867. plen = sizeof(regbuf);
  868. }
  869. p = RELOC(regbuf);
  870. endp = p + (plen / sizeof(cell_t));
  871. #ifdef DEBUG_PROM
  872. memset(path, 0, PROM_SCRATCH_SIZE);
  873. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  874. prom_debug(" node %s :\n", path);
  875. #endif /* DEBUG_PROM */
  876. while ((endp - p) >= (rac + rsc)) {
  877. unsigned long base, size;
  878. base = prom_next_cell(rac, &p);
  879. size = prom_next_cell(rsc, &p);
  880. if (size == 0)
  881. continue;
  882. prom_debug(" %x %x\n", base, size);
  883. if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
  884. RELOC(rmo_top) = size;
  885. if ((base + size) > RELOC(ram_top))
  886. RELOC(ram_top) = base + size;
  887. }
  888. }
  889. RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
  890. /* Check if we have an initrd after the kernel, if we do move our bottom
  891. * point to after it
  892. */
  893. if (RELOC(prom_initrd_start)) {
  894. if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
  895. RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
  896. }
  897. /*
  898. * Setup our top alloc point, that is top of RMO or top of
  899. * segment 0 when running non-LPAR.
  900. * Some RS64 machines have buggy firmware where claims up at
  901. * 1GB fail. Cap at 768MB as a workaround.
  902. * Since 768MB is plenty of room, and we need to cap to something
  903. * reasonable on 32-bit, cap at 768MB on all machines.
  904. */
  905. if (!RELOC(rmo_top))
  906. RELOC(rmo_top) = RELOC(ram_top);
  907. RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
  908. RELOC(alloc_top) = RELOC(rmo_top);
  909. RELOC(alloc_top_high) = RELOC(ram_top);
  910. prom_printf("memory layout at init:\n");
  911. prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  912. prom_printf(" alloc_top : %x\n", RELOC(alloc_top));
  913. prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  914. prom_printf(" rmo_top : %x\n", RELOC(rmo_top));
  915. prom_printf(" ram_top : %x\n", RELOC(ram_top));
  916. }
  917. /*
  918. * Allocate room for and instantiate RTAS
  919. */
  920. static void __init prom_instantiate_rtas(void)
  921. {
  922. phandle rtas_node;
  923. ihandle rtas_inst;
  924. u32 base, entry = 0;
  925. u32 size = 0;
  926. prom_debug("prom_instantiate_rtas: start...\n");
  927. rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  928. prom_debug("rtas_node: %x\n", rtas_node);
  929. if (!PHANDLE_VALID(rtas_node))
  930. return;
  931. prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
  932. if (size == 0)
  933. return;
  934. base = alloc_down(size, PAGE_SIZE, 0);
  935. if (base == 0) {
  936. prom_printf("RTAS allocation failed !\n");
  937. return;
  938. }
  939. rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
  940. if (!IHANDLE_VALID(rtas_inst)) {
  941. prom_printf("opening rtas package failed (%x)\n", rtas_inst);
  942. return;
  943. }
  944. prom_printf("instantiating rtas at 0x%x...", base);
  945. if (call_prom_ret("call-method", 3, 2, &entry,
  946. ADDR("instantiate-rtas"),
  947. rtas_inst, base) != 0
  948. || entry == 0) {
  949. prom_printf(" failed\n");
  950. return;
  951. }
  952. prom_printf(" done\n");
  953. reserve_mem(base, size);
  954. prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
  955. &base, sizeof(base));
  956. prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
  957. &entry, sizeof(entry));
  958. prom_debug("rtas base = 0x%x\n", base);
  959. prom_debug("rtas entry = 0x%x\n", entry);
  960. prom_debug("rtas size = 0x%x\n", (long)size);
  961. prom_debug("prom_instantiate_rtas: end...\n");
  962. }
  963. #ifdef CONFIG_PPC64
  964. /*
  965. * Allocate room for and initialize TCE tables
  966. */
  967. static void __init prom_initialize_tce_table(void)
  968. {
  969. phandle node;
  970. ihandle phb_node;
  971. char compatible[64], type[64], model[64];
  972. char *path = RELOC(prom_scratch);
  973. u64 base, align;
  974. u32 minalign, minsize;
  975. u64 tce_entry, *tce_entryp;
  976. u64 local_alloc_top, local_alloc_bottom;
  977. u64 i;
  978. if (RELOC(prom_iommu_off))
  979. return;
  980. prom_debug("starting prom_initialize_tce_table\n");
  981. /* Cache current top of allocs so we reserve a single block */
  982. local_alloc_top = RELOC(alloc_top_high);
  983. local_alloc_bottom = local_alloc_top;
  984. /* Search all nodes looking for PHBs. */
  985. for (node = 0; prom_next_node(&node); ) {
  986. compatible[0] = 0;
  987. type[0] = 0;
  988. model[0] = 0;
  989. prom_getprop(node, "compatible",
  990. compatible, sizeof(compatible));
  991. prom_getprop(node, "device_type", type, sizeof(type));
  992. prom_getprop(node, "model", model, sizeof(model));
  993. if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
  994. continue;
  995. /* Keep the old logic intact to avoid regression. */
  996. if (compatible[0] != 0) {
  997. if ((strstr(compatible, RELOC("python")) == NULL) &&
  998. (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
  999. (strstr(compatible, RELOC("Winnipeg")) == NULL))
  1000. continue;
  1001. } else if (model[0] != 0) {
  1002. if ((strstr(model, RELOC("ython")) == NULL) &&
  1003. (strstr(model, RELOC("peedwagon")) == NULL) &&
  1004. (strstr(model, RELOC("innipeg")) == NULL))
  1005. continue;
  1006. }
  1007. if (prom_getprop(node, "tce-table-minalign", &minalign,
  1008. sizeof(minalign)) == PROM_ERROR)
  1009. minalign = 0;
  1010. if (prom_getprop(node, "tce-table-minsize", &minsize,
  1011. sizeof(minsize)) == PROM_ERROR)
  1012. minsize = 4UL << 20;
  1013. /*
  1014. * Even though we read what OF wants, we just set the table
  1015. * size to 4 MB. This is enough to map 2GB of PCI DMA space.
  1016. * By doing this, we avoid the pitfalls of trying to DMA to
  1017. * MMIO space and the DMA alias hole.
  1018. *
  1019. * On POWER4, firmware sets the TCE region by assuming
  1020. * each TCE table is 8MB. Using this memory for anything
  1021. * else will impact performance, so we always allocate 8MB.
  1022. * Anton
  1023. */
  1024. if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
  1025. minsize = 8UL << 20;
  1026. else
  1027. minsize = 4UL << 20;
  1028. /* Align to the greater of the align or size */
  1029. align = max(minalign, minsize);
  1030. base = alloc_down(minsize, align, 1);
  1031. if (base == 0)
  1032. prom_panic("ERROR, cannot find space for TCE table.\n");
  1033. if (base < local_alloc_bottom)
  1034. local_alloc_bottom = base;
  1035. /* It seems OF doesn't null-terminate the path :-( */
  1036. memset(path, 0, PROM_SCRATCH_SIZE);
  1037. /* Call OF to setup the TCE hardware */
  1038. if (call_prom("package-to-path", 3, 1, node,
  1039. path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
  1040. prom_printf("package-to-path failed\n");
  1041. }
  1042. /* Save away the TCE table attributes for later use. */
  1043. prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
  1044. prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
  1045. prom_debug("TCE table: %s\n", path);
  1046. prom_debug("\tnode = 0x%x\n", node);
  1047. prom_debug("\tbase = 0x%x\n", base);
  1048. prom_debug("\tsize = 0x%x\n", minsize);
  1049. /* Initialize the table to have a one-to-one mapping
  1050. * over the allocated size.
  1051. */
  1052. tce_entryp = (u64 *)base;
  1053. for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
  1054. tce_entry = (i << PAGE_SHIFT);
  1055. tce_entry |= 0x3;
  1056. *tce_entryp = tce_entry;
  1057. }
  1058. prom_printf("opening PHB %s", path);
  1059. phb_node = call_prom("open", 1, 1, path);
  1060. if (phb_node == 0)
  1061. prom_printf("... failed\n");
  1062. else
  1063. prom_printf("... done\n");
  1064. call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
  1065. phb_node, -1, minsize,
  1066. (u32) base, (u32) (base >> 32));
  1067. call_prom("close", 1, 0, phb_node);
  1068. }
  1069. reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
  1070. /* These are only really needed if there is a memory limit in
  1071. * effect, but we don't know so export them always. */
  1072. RELOC(prom_tce_alloc_start) = local_alloc_bottom;
  1073. RELOC(prom_tce_alloc_end) = local_alloc_top;
  1074. /* Flag the first invalid entry */
  1075. prom_debug("ending prom_initialize_tce_table\n");
  1076. }
  1077. #endif
  1078. /*
  1079. * With CHRP SMP we need to use the OF to start the other processors.
  1080. * We can't wait until smp_boot_cpus (the OF is trashed by then)
  1081. * so we have to put the processors into a holding pattern controlled
  1082. * by the kernel (not OF) before we destroy the OF.
  1083. *
  1084. * This uses a chunk of low memory, puts some holding pattern
  1085. * code there and sends the other processors off to there until
  1086. * smp_boot_cpus tells them to do something. The holding pattern
  1087. * checks that address until its cpu # is there, when it is that
  1088. * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
  1089. * of setting those values.
  1090. *
  1091. * We also use physical address 0x4 here to tell when a cpu
  1092. * is in its holding pattern code.
  1093. *
  1094. * -- Cort
  1095. */
  1096. extern char __secondary_hold;
  1097. extern unsigned long __secondary_hold_spinloop;
  1098. extern unsigned long __secondary_hold_acknowledge;
  1099. /*
  1100. * We want to reference the copy of __secondary_hold_* in the
  1101. * 0 - 0x100 address range
  1102. */
  1103. #define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
  1104. static void __init prom_hold_cpus(void)
  1105. {
  1106. unsigned long i;
  1107. unsigned int reg;
  1108. phandle node;
  1109. char type[64];
  1110. struct prom_t *_prom = &RELOC(prom);
  1111. unsigned long *spinloop
  1112. = (void *) LOW_ADDR(__secondary_hold_spinloop);
  1113. unsigned long *acknowledge
  1114. = (void *) LOW_ADDR(__secondary_hold_acknowledge);
  1115. unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
  1116. prom_debug("prom_hold_cpus: start...\n");
  1117. prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
  1118. prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
  1119. prom_debug(" 1) acknowledge = 0x%x\n",
  1120. (unsigned long)acknowledge);
  1121. prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
  1122. prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
  1123. /* Set the common spinloop variable, so all of the secondary cpus
  1124. * will block when they are awakened from their OF spinloop.
  1125. * This must occur for both SMP and non SMP kernels, since OF will
  1126. * be trashed when we move the kernel.
  1127. */
  1128. *spinloop = 0;
  1129. /* look for cpus */
  1130. for (node = 0; prom_next_node(&node); ) {
  1131. type[0] = 0;
  1132. prom_getprop(node, "device_type", type, sizeof(type));
  1133. if (strcmp(type, RELOC("cpu")) != 0)
  1134. continue;
  1135. /* Skip non-configured cpus. */
  1136. if (prom_getprop(node, "status", type, sizeof(type)) > 0)
  1137. if (strcmp(type, RELOC("okay")) != 0)
  1138. continue;
  1139. reg = -1;
  1140. prom_getprop(node, "reg", &reg, sizeof(reg));
  1141. prom_debug("cpu hw idx = 0x%x\n", reg);
  1142. /* Init the acknowledge var which will be reset by
  1143. * the secondary cpu when it awakens from its OF
  1144. * spinloop.
  1145. */
  1146. *acknowledge = (unsigned long)-1;
  1147. if (reg != _prom->cpu) {
  1148. /* Primary Thread of non-boot cpu */
  1149. prom_printf("starting cpu hw idx %x... ", reg);
  1150. call_prom("start-cpu", 3, 0, node,
  1151. secondary_hold, reg);
  1152. for (i = 0; (i < 100000000) &&
  1153. (*acknowledge == ((unsigned long)-1)); i++ )
  1154. mb();
  1155. if (*acknowledge == reg)
  1156. prom_printf("done\n");
  1157. else
  1158. prom_printf("failed: %x\n", *acknowledge);
  1159. }
  1160. #ifdef CONFIG_SMP
  1161. else
  1162. prom_printf("boot cpu hw idx %x\n", reg);
  1163. #endif /* CONFIG_SMP */
  1164. }
  1165. prom_debug("prom_hold_cpus: end...\n");
  1166. }
  1167. static void __init prom_init_client_services(unsigned long pp)
  1168. {
  1169. struct prom_t *_prom = &RELOC(prom);
  1170. /* Get a handle to the prom entry point before anything else */
  1171. RELOC(prom_entry) = pp;
  1172. /* get a handle for the stdout device */
  1173. _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
  1174. if (!PHANDLE_VALID(_prom->chosen))
  1175. prom_panic("cannot find chosen"); /* msg won't be printed :( */
  1176. /* get device tree root */
  1177. _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
  1178. if (!PHANDLE_VALID(_prom->root))
  1179. prom_panic("cannot find device tree root"); /* msg won't be printed :( */
  1180. _prom->mmumap = 0;
  1181. }
  1182. #ifdef CONFIG_PPC32
  1183. /*
  1184. * For really old powermacs, we need to map things we claim.
  1185. * For that, we need the ihandle of the mmu.
  1186. * Also, on the longtrail, we need to work around other bugs.
  1187. */
  1188. static void __init prom_find_mmu(void)
  1189. {
  1190. struct prom_t *_prom = &RELOC(prom);
  1191. phandle oprom;
  1192. char version[64];
  1193. oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
  1194. if (!PHANDLE_VALID(oprom))
  1195. return;
  1196. if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
  1197. return;
  1198. version[sizeof(version) - 1] = 0;
  1199. /* XXX might need to add other versions here */
  1200. if (strcmp(version, "Open Firmware, 1.0.5") == 0)
  1201. of_workarounds = OF_WA_CLAIM;
  1202. else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
  1203. of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
  1204. call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
  1205. } else
  1206. return;
  1207. _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
  1208. prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
  1209. sizeof(_prom->mmumap));
  1210. if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
  1211. of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
  1212. }
  1213. #else
  1214. #define prom_find_mmu()
  1215. #endif
  1216. static void __init prom_init_stdout(void)
  1217. {
  1218. struct prom_t *_prom = &RELOC(prom);
  1219. char *path = RELOC(of_stdout_device);
  1220. char type[16];
  1221. u32 val;
  1222. if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
  1223. prom_panic("cannot find stdout");
  1224. _prom->stdout = val;
  1225. /* Get the full OF pathname of the stdout device */
  1226. memset(path, 0, 256);
  1227. call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
  1228. val = call_prom("instance-to-package", 1, 1, _prom->stdout);
  1229. prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
  1230. &val, sizeof(val));
  1231. prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
  1232. prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
  1233. path, strlen(path) + 1);
  1234. /* If it's a display, note it */
  1235. memset(type, 0, sizeof(type));
  1236. prom_getprop(val, "device_type", type, sizeof(type));
  1237. if (strcmp(type, RELOC("display")) == 0)
  1238. prom_setprop(val, path, "linux,boot-display", NULL, 0);
  1239. }
  1240. static void __init prom_close_stdin(void)
  1241. {
  1242. struct prom_t *_prom = &RELOC(prom);
  1243. ihandle val;
  1244. if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
  1245. call_prom("close", 1, 0, val);
  1246. }
  1247. static int __init prom_find_machine_type(void)
  1248. {
  1249. struct prom_t *_prom = &RELOC(prom);
  1250. char compat[256];
  1251. int len, i = 0;
  1252. #ifdef CONFIG_PPC64
  1253. phandle rtas;
  1254. int x;
  1255. #endif
  1256. /* Look for a PowerMac */
  1257. len = prom_getprop(_prom->root, "compatible",
  1258. compat, sizeof(compat)-1);
  1259. if (len > 0) {
  1260. compat[len] = 0;
  1261. while (i < len) {
  1262. char *p = &compat[i];
  1263. int sl = strlen(p);
  1264. if (sl == 0)
  1265. break;
  1266. if (strstr(p, RELOC("Power Macintosh")) ||
  1267. strstr(p, RELOC("MacRISC")))
  1268. return PLATFORM_POWERMAC;
  1269. #ifdef CONFIG_PPC64
  1270. /* We must make sure we don't detect the IBM Cell
  1271. * blades as pSeries due to some firmware issues,
  1272. * so we do it here.
  1273. */
  1274. if (strstr(p, RELOC("IBM,CBEA")) ||
  1275. strstr(p, RELOC("IBM,CPBW-1.0")))
  1276. return PLATFORM_GENERIC;
  1277. #endif /* CONFIG_PPC64 */
  1278. i += sl + 1;
  1279. }
  1280. }
  1281. #ifdef CONFIG_PPC64
  1282. /* If not a mac, try to figure out if it's an IBM pSeries or any other
  1283. * PAPR compliant platform. We assume it is if :
  1284. * - /device_type is "chrp" (please, do NOT use that for future
  1285. * non-IBM designs !
  1286. * - it has /rtas
  1287. */
  1288. len = prom_getprop(_prom->root, "device_type",
  1289. compat, sizeof(compat)-1);
  1290. if (len <= 0)
  1291. return PLATFORM_GENERIC;
  1292. if (strcmp(compat, RELOC("chrp")))
  1293. return PLATFORM_GENERIC;
  1294. /* Default to pSeries. We need to know if we are running LPAR */
  1295. rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  1296. if (!PHANDLE_VALID(rtas))
  1297. return PLATFORM_GENERIC;
  1298. x = prom_getproplen(rtas, "ibm,hypertas-functions");
  1299. if (x != PROM_ERROR) {
  1300. prom_debug("Hypertas detected, assuming LPAR !\n");
  1301. return PLATFORM_PSERIES_LPAR;
  1302. }
  1303. return PLATFORM_PSERIES;
  1304. #else
  1305. return PLATFORM_GENERIC;
  1306. #endif
  1307. }
  1308. static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
  1309. {
  1310. return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
  1311. }
  1312. /*
  1313. * If we have a display that we don't know how to drive,
  1314. * we will want to try to execute OF's open method for it
  1315. * later. However, OF will probably fall over if we do that
  1316. * we've taken over the MMU.
  1317. * So we check whether we will need to open the display,
  1318. * and if so, open it now.
  1319. */
  1320. static void __init prom_check_displays(void)
  1321. {
  1322. char type[16], *path;
  1323. phandle node;
  1324. ihandle ih;
  1325. int i;
  1326. static unsigned char default_colors[] = {
  1327. 0x00, 0x00, 0x00,
  1328. 0x00, 0x00, 0xaa,
  1329. 0x00, 0xaa, 0x00,
  1330. 0x00, 0xaa, 0xaa,
  1331. 0xaa, 0x00, 0x00,
  1332. 0xaa, 0x00, 0xaa,
  1333. 0xaa, 0xaa, 0x00,
  1334. 0xaa, 0xaa, 0xaa,
  1335. 0x55, 0x55, 0x55,
  1336. 0x55, 0x55, 0xff,
  1337. 0x55, 0xff, 0x55,
  1338. 0x55, 0xff, 0xff,
  1339. 0xff, 0x55, 0x55,
  1340. 0xff, 0x55, 0xff,
  1341. 0xff, 0xff, 0x55,
  1342. 0xff, 0xff, 0xff
  1343. };
  1344. const unsigned char *clut;
  1345. prom_debug("Looking for displays\n");
  1346. for (node = 0; prom_next_node(&node); ) {
  1347. memset(type, 0, sizeof(type));
  1348. prom_getprop(node, "device_type", type, sizeof(type));
  1349. if (strcmp(type, RELOC("display")) != 0)
  1350. continue;
  1351. /* It seems OF doesn't null-terminate the path :-( */
  1352. path = RELOC(prom_scratch);
  1353. memset(path, 0, PROM_SCRATCH_SIZE);
  1354. /*
  1355. * leave some room at the end of the path for appending extra
  1356. * arguments
  1357. */
  1358. if (call_prom("package-to-path", 3, 1, node, path,
  1359. PROM_SCRATCH_SIZE-10) == PROM_ERROR)
  1360. continue;
  1361. prom_printf("found display : %s, opening... ", path);
  1362. ih = call_prom("open", 1, 1, path);
  1363. if (ih == 0) {
  1364. prom_printf("failed\n");
  1365. continue;
  1366. }
  1367. /* Success */
  1368. prom_printf("done\n");
  1369. prom_setprop(node, path, "linux,opened", NULL, 0);
  1370. /* Setup a usable color table when the appropriate
  1371. * method is available. Should update this to set-colors */
  1372. clut = RELOC(default_colors);
  1373. for (i = 0; i < 32; i++, clut += 3)
  1374. if (prom_set_color(ih, i, clut[0], clut[1],
  1375. clut[2]) != 0)
  1376. break;
  1377. #ifdef CONFIG_LOGO_LINUX_CLUT224
  1378. clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
  1379. for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
  1380. if (prom_set_color(ih, i + 32, clut[0], clut[1],
  1381. clut[2]) != 0)
  1382. break;
  1383. #endif /* CONFIG_LOGO_LINUX_CLUT224 */
  1384. }
  1385. }
  1386. /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
  1387. static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
  1388. unsigned long needed, unsigned long align)
  1389. {
  1390. void *ret;
  1391. *mem_start = _ALIGN(*mem_start, align);
  1392. while ((*mem_start + needed) > *mem_end) {
  1393. unsigned long room, chunk;
  1394. prom_debug("Chunk exhausted, claiming more at %x...\n",
  1395. RELOC(alloc_bottom));
  1396. room = RELOC(alloc_top) - RELOC(alloc_bottom);
  1397. if (room > DEVTREE_CHUNK_SIZE)
  1398. room = DEVTREE_CHUNK_SIZE;
  1399. if (room < PAGE_SIZE)
  1400. prom_panic("No memory for flatten_device_tree (no room)");
  1401. chunk = alloc_up(room, 0);
  1402. if (chunk == 0)
  1403. prom_panic("No memory for flatten_device_tree (claim failed)");
  1404. *mem_end = RELOC(alloc_top);
  1405. }
  1406. ret = (void *)*mem_start;
  1407. *mem_start += needed;
  1408. return ret;
  1409. }
  1410. #define dt_push_token(token, mem_start, mem_end) \
  1411. do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
  1412. static unsigned long __init dt_find_string(char *str)
  1413. {
  1414. char *s, *os;
  1415. s = os = (char *)RELOC(dt_string_start);
  1416. s += 4;
  1417. while (s < (char *)RELOC(dt_string_end)) {
  1418. if (strcmp(s, str) == 0)
  1419. return s - os;
  1420. s += strlen(s) + 1;
  1421. }
  1422. return 0;
  1423. }
  1424. /*
  1425. * The Open Firmware 1275 specification states properties must be 31 bytes or
  1426. * less, however not all firmwares obey this. Make it 64 bytes to be safe.
  1427. */
  1428. #define MAX_PROPERTY_NAME 64
  1429. static void __init scan_dt_build_strings(phandle node,
  1430. unsigned long *mem_start,
  1431. unsigned long *mem_end)
  1432. {
  1433. char *prev_name, *namep, *sstart;
  1434. unsigned long soff;
  1435. phandle child;
  1436. sstart = (char *)RELOC(dt_string_start);
  1437. /* get and store all property names */
  1438. prev_name = RELOC("");
  1439. for (;;) {
  1440. /* 64 is max len of name including nul. */
  1441. namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
  1442. if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
  1443. /* No more nodes: unwind alloc */
  1444. *mem_start = (unsigned long)namep;
  1445. break;
  1446. }
  1447. /* skip "name" */
  1448. if (strcmp(namep, RELOC("name")) == 0) {
  1449. *mem_start = (unsigned long)namep;
  1450. prev_name = RELOC("name");
  1451. continue;
  1452. }
  1453. /* get/create string entry */
  1454. soff = dt_find_string(namep);
  1455. if (soff != 0) {
  1456. *mem_start = (unsigned long)namep;
  1457. namep = sstart + soff;
  1458. } else {
  1459. /* Trim off some if we can */
  1460. *mem_start = (unsigned long)namep + strlen(namep) + 1;
  1461. RELOC(dt_string_end) = *mem_start;
  1462. }
  1463. prev_name = namep;
  1464. }
  1465. /* do all our children */
  1466. child = call_prom("child", 1, 1, node);
  1467. while (child != 0) {
  1468. scan_dt_build_strings(child, mem_start, mem_end);
  1469. child = call_prom("peer", 1, 1, child);
  1470. }
  1471. }
  1472. static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
  1473. unsigned long *mem_end)
  1474. {
  1475. phandle child;
  1476. char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
  1477. unsigned long soff;
  1478. unsigned char *valp;
  1479. static char pname[MAX_PROPERTY_NAME];
  1480. int l, room;
  1481. dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
  1482. /* get the node's full name */
  1483. namep = (char *)*mem_start;
  1484. room = *mem_end - *mem_start;
  1485. if (room > 255)
  1486. room = 255;
  1487. l = call_prom("package-to-path", 3, 1, node, namep, room);
  1488. if (l >= 0) {
  1489. /* Didn't fit? Get more room. */
  1490. if (l >= room) {
  1491. if (l >= *mem_end - *mem_start)
  1492. namep = make_room(mem_start, mem_end, l+1, 1);
  1493. call_prom("package-to-path", 3, 1, node, namep, l);
  1494. }
  1495. namep[l] = '\0';
  1496. /* Fixup an Apple bug where they have bogus \0 chars in the
  1497. * middle of the path in some properties, and extract
  1498. * the unit name (everything after the last '/').
  1499. */
  1500. for (lp = p = namep, ep = namep + l; p < ep; p++) {
  1501. if (*p == '/')
  1502. lp = namep;
  1503. else if (*p != 0)
  1504. *lp++ = *p;
  1505. }
  1506. *lp = 0;
  1507. *mem_start = _ALIGN((unsigned long)lp + 1, 4);
  1508. }
  1509. /* get it again for debugging */
  1510. path = RELOC(prom_scratch);
  1511. memset(path, 0, PROM_SCRATCH_SIZE);
  1512. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  1513. /* get and store all properties */
  1514. prev_name = RELOC("");
  1515. sstart = (char *)RELOC(dt_string_start);
  1516. for (;;) {
  1517. if (call_prom("nextprop", 3, 1, node, prev_name,
  1518. RELOC(pname)) != 1)
  1519. break;
  1520. /* skip "name" */
  1521. if (strcmp(RELOC(pname), RELOC("name")) == 0) {
  1522. prev_name = RELOC("name");
  1523. continue;
  1524. }
  1525. /* find string offset */
  1526. soff = dt_find_string(RELOC(pname));
  1527. if (soff == 0) {
  1528. prom_printf("WARNING: Can't find string index for"
  1529. " <%s>, node %s\n", RELOC(pname), path);
  1530. break;
  1531. }
  1532. prev_name = sstart + soff;
  1533. /* get length */
  1534. l = call_prom("getproplen", 2, 1, node, RELOC(pname));
  1535. /* sanity checks */
  1536. if (l == PROM_ERROR)
  1537. continue;
  1538. if (l > MAX_PROPERTY_LENGTH) {
  1539. prom_printf("WARNING: ignoring large property ");
  1540. /* It seems OF doesn't null-terminate the path :-( */
  1541. prom_printf("[%s] ", path);
  1542. prom_printf("%s length 0x%x\n", RELOC(pname), l);
  1543. continue;
  1544. }
  1545. /* push property head */
  1546. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1547. dt_push_token(l, mem_start, mem_end);
  1548. dt_push_token(soff, mem_start, mem_end);
  1549. /* push property content */
  1550. valp = make_room(mem_start, mem_end, l, 4);
  1551. call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
  1552. *mem_start = _ALIGN(*mem_start, 4);
  1553. }
  1554. /* Add a "linux,phandle" property. */
  1555. soff = dt_find_string(RELOC("linux,phandle"));
  1556. if (soff == 0)
  1557. prom_printf("WARNING: Can't find string index for"
  1558. " <linux-phandle> node %s\n", path);
  1559. else {
  1560. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1561. dt_push_token(4, mem_start, mem_end);
  1562. dt_push_token(soff, mem_start, mem_end);
  1563. valp = make_room(mem_start, mem_end, 4, 4);
  1564. *(u32 *)valp = node;
  1565. }
  1566. /* do all our children */
  1567. child = call_prom("child", 1, 1, node);
  1568. while (child != 0) {
  1569. scan_dt_build_struct(child, mem_start, mem_end);
  1570. child = call_prom("peer", 1, 1, child);
  1571. }
  1572. dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
  1573. }
  1574. static void __init flatten_device_tree(void)
  1575. {
  1576. phandle root;
  1577. unsigned long mem_start, mem_end, room;
  1578. struct boot_param_header *hdr;
  1579. struct prom_t *_prom = &RELOC(prom);
  1580. char *namep;
  1581. u64 *rsvmap;
  1582. /*
  1583. * Check how much room we have between alloc top & bottom (+/- a
  1584. * few pages), crop to 4Mb, as this is our "chuck" size
  1585. */
  1586. room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
  1587. if (room > DEVTREE_CHUNK_SIZE)
  1588. room = DEVTREE_CHUNK_SIZE;
  1589. prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
  1590. /* Now try to claim that */
  1591. mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
  1592. if (mem_start == 0)
  1593. prom_panic("Can't allocate initial device-tree chunk\n");
  1594. mem_end = RELOC(alloc_top);
  1595. /* Get root of tree */
  1596. root = call_prom("peer", 1, 1, (phandle)0);
  1597. if (root == (phandle)0)
  1598. prom_panic ("couldn't get device tree root\n");
  1599. /* Build header and make room for mem rsv map */
  1600. mem_start = _ALIGN(mem_start, 4);
  1601. hdr = make_room(&mem_start, &mem_end,
  1602. sizeof(struct boot_param_header), 4);
  1603. RELOC(dt_header_start) = (unsigned long)hdr;
  1604. rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
  1605. /* Start of strings */
  1606. mem_start = PAGE_ALIGN(mem_start);
  1607. RELOC(dt_string_start) = mem_start;
  1608. mem_start += 4; /* hole */
  1609. /* Add "linux,phandle" in there, we'll need it */
  1610. namep = make_room(&mem_start, &mem_end, 16, 1);
  1611. strcpy(namep, RELOC("linux,phandle"));
  1612. mem_start = (unsigned long)namep + strlen(namep) + 1;
  1613. /* Build string array */
  1614. prom_printf("Building dt strings...\n");
  1615. scan_dt_build_strings(root, &mem_start, &mem_end);
  1616. RELOC(dt_string_end) = mem_start;
  1617. /* Build structure */
  1618. mem_start = PAGE_ALIGN(mem_start);
  1619. RELOC(dt_struct_start) = mem_start;
  1620. prom_printf("Building dt structure...\n");
  1621. scan_dt_build_struct(root, &mem_start, &mem_end);
  1622. dt_push_token(OF_DT_END, &mem_start, &mem_end);
  1623. RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
  1624. /* Finish header */
  1625. hdr->boot_cpuid_phys = _prom->cpu;
  1626. hdr->magic = OF_DT_HEADER;
  1627. hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
  1628. hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
  1629. hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
  1630. hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
  1631. hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
  1632. hdr->version = OF_DT_VERSION;
  1633. /* Version 16 is not backward compatible */
  1634. hdr->last_comp_version = 0x10;
  1635. /* Copy the reserve map in */
  1636. memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
  1637. #ifdef DEBUG_PROM
  1638. {
  1639. int i;
  1640. prom_printf("reserved memory map:\n");
  1641. for (i = 0; i < RELOC(mem_reserve_cnt); i++)
  1642. prom_printf(" %x - %x\n",
  1643. RELOC(mem_reserve_map)[i].base,
  1644. RELOC(mem_reserve_map)[i].size);
  1645. }
  1646. #endif
  1647. /* Bump mem_reserve_cnt to cause further reservations to fail
  1648. * since it's too late.
  1649. */
  1650. RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
  1651. prom_printf("Device tree strings 0x%x -> 0x%x\n",
  1652. RELOC(dt_string_start), RELOC(dt_string_end));
  1653. prom_printf("Device tree struct 0x%x -> 0x%x\n",
  1654. RELOC(dt_struct_start), RELOC(dt_struct_end));
  1655. }
  1656. #ifdef CONFIG_PPC_MAPLE
  1657. /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
  1658. * The values are bad, and it doesn't even have the right number of cells. */
  1659. static void __init fixup_device_tree_maple(void)
  1660. {
  1661. phandle isa;
  1662. u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
  1663. u32 isa_ranges[6];
  1664. char *name;
  1665. name = "/ht@0/isa@4";
  1666. isa = call_prom("finddevice", 1, 1, ADDR(name));
  1667. if (!PHANDLE_VALID(isa)) {
  1668. name = "/ht@0/isa@6";
  1669. isa = call_prom("finddevice", 1, 1, ADDR(name));
  1670. rloc = 0x01003000; /* IO space; PCI device = 6 */
  1671. }
  1672. if (!PHANDLE_VALID(isa))
  1673. return;
  1674. if (prom_getproplen(isa, "ranges") != 12)
  1675. return;
  1676. if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
  1677. == PROM_ERROR)
  1678. return;
  1679. if (isa_ranges[0] != 0x1 ||
  1680. isa_ranges[1] != 0xf4000000 ||
  1681. isa_ranges[2] != 0x00010000)
  1682. return;
  1683. prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
  1684. isa_ranges[0] = 0x1;
  1685. isa_ranges[1] = 0x0;
  1686. isa_ranges[2] = rloc;
  1687. isa_ranges[3] = 0x0;
  1688. isa_ranges[4] = 0x0;
  1689. isa_ranges[5] = 0x00010000;
  1690. prom_setprop(isa, name, "ranges",
  1691. isa_ranges, sizeof(isa_ranges));
  1692. }
  1693. #define CPC925_MC_START 0xf8000000
  1694. #define CPC925_MC_LENGTH 0x1000000
  1695. /* The values for memory-controller don't have right number of cells */
  1696. static void __init fixup_device_tree_maple_memory_controller(void)
  1697. {
  1698. phandle mc;
  1699. u32 mc_reg[4];
  1700. char *name = "/hostbridge@f8000000";
  1701. struct prom_t *_prom = &RELOC(prom);
  1702. u32 ac, sc;
  1703. mc = call_prom("finddevice", 1, 1, ADDR(name));
  1704. if (!PHANDLE_VALID(mc))
  1705. return;
  1706. if (prom_getproplen(mc, "reg") != 8)
  1707. return;
  1708. prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
  1709. prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
  1710. if ((ac != 2) || (sc != 2))
  1711. return;
  1712. if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
  1713. return;
  1714. if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
  1715. return;
  1716. prom_printf("Fixing up bogus hostbridge on Maple...\n");
  1717. mc_reg[0] = 0x0;
  1718. mc_reg[1] = CPC925_MC_START;
  1719. mc_reg[2] = 0x0;
  1720. mc_reg[3] = CPC925_MC_LENGTH;
  1721. prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
  1722. }
  1723. #else
  1724. #define fixup_device_tree_maple()
  1725. #define fixup_device_tree_maple_memory_controller()
  1726. #endif
  1727. #ifdef CONFIG_PPC_CHRP
  1728. /*
  1729. * Pegasos and BriQ lacks the "ranges" property in the isa node
  1730. * Pegasos needs decimal IRQ 14/15, not hexadecimal
  1731. * Pegasos has the IDE configured in legacy mode, but advertised as native
  1732. */
  1733. static void __init fixup_device_tree_chrp(void)
  1734. {
  1735. phandle ph;
  1736. u32 prop[6];
  1737. u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
  1738. char *name;
  1739. int rc;
  1740. name = "/pci@80000000/isa@c";
  1741. ph = call_prom("finddevice", 1, 1, ADDR(name));
  1742. if (!PHANDLE_VALID(ph)) {
  1743. name = "/pci@ff500000/isa@6";
  1744. ph = call_prom("finddevice", 1, 1, ADDR(name));
  1745. rloc = 0x01003000; /* IO space; PCI device = 6 */
  1746. }
  1747. if (PHANDLE_VALID(ph)) {
  1748. rc = prom_getproplen(ph, "ranges");
  1749. if (rc == 0 || rc == PROM_ERROR) {
  1750. prom_printf("Fixing up missing ISA range on Pegasos...\n");
  1751. prop[0] = 0x1;
  1752. prop[1] = 0x0;
  1753. prop[2] = rloc;
  1754. prop[3] = 0x0;
  1755. prop[4] = 0x0;
  1756. prop[5] = 0x00010000;
  1757. prom_setprop(ph, name, "ranges", prop, sizeof(prop));
  1758. }
  1759. }
  1760. name = "/pci@80000000/ide@C,1";
  1761. ph = call_prom("finddevice", 1, 1, ADDR(name));
  1762. if (PHANDLE_VALID(ph)) {
  1763. prom_printf("Fixing up IDE interrupt on Pegasos...\n");
  1764. prop[0] = 14;
  1765. prop[1] = 0x0;
  1766. prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
  1767. prom_printf("Fixing up IDE class-code on Pegasos...\n");
  1768. rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
  1769. if (rc == sizeof(u32)) {
  1770. prop[0] &= ~0x5;
  1771. prom_setprop(ph, name, "class-code", prop, sizeof(u32));
  1772. }
  1773. }
  1774. }
  1775. #else
  1776. #define fixup_device_tree_chrp()
  1777. #endif
  1778. #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
  1779. static void __init fixup_device_tree_pmac(void)
  1780. {
  1781. phandle u3, i2c, mpic;
  1782. u32 u3_rev;
  1783. u32 interrupts[2];
  1784. u32 parent;
  1785. /* Some G5s have a missing interrupt definition, fix it up here */
  1786. u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
  1787. if (!PHANDLE_VALID(u3))
  1788. return;
  1789. i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
  1790. if (!PHANDLE_VALID(i2c))
  1791. return;
  1792. mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
  1793. if (!PHANDLE_VALID(mpic))
  1794. return;
  1795. /* check if proper rev of u3 */
  1796. if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
  1797. == PROM_ERROR)
  1798. return;
  1799. if (u3_rev < 0x35 || u3_rev > 0x39)
  1800. return;
  1801. /* does it need fixup ? */
  1802. if (prom_getproplen(i2c, "interrupts") > 0)
  1803. return;
  1804. prom_printf("fixing up bogus interrupts for u3 i2c...\n");
  1805. /* interrupt on this revision of u3 is number 0 and level */
  1806. interrupts[0] = 0;
  1807. interrupts[1] = 1;
  1808. prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
  1809. &interrupts, sizeof(interrupts));
  1810. parent = (u32)mpic;
  1811. prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
  1812. &parent, sizeof(parent));
  1813. }
  1814. #else
  1815. #define fixup_device_tree_pmac()
  1816. #endif
  1817. #ifdef CONFIG_PPC_EFIKA
  1818. /*
  1819. * The MPC5200 FEC driver requires an phy-handle property to tell it how
  1820. * to talk to the phy. If the phy-handle property is missing, then this
  1821. * function is called to add the appropriate nodes and link it to the
  1822. * ethernet node.
  1823. */
  1824. static void __init fixup_device_tree_efika_add_phy(void)
  1825. {
  1826. u32 node;
  1827. char prop[64];
  1828. int rv;
  1829. /* Check if /builtin/ethernet exists - bail if it doesn't */
  1830. node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
  1831. if (!PHANDLE_VALID(node))
  1832. return;
  1833. /* Check if the phy-handle property exists - bail if it does */
  1834. rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
  1835. if (!rv)
  1836. return;
  1837. /*
  1838. * At this point the ethernet device doesn't have a phy described.
  1839. * Now we need to add the missing phy node and linkage
  1840. */
  1841. /* Check for an MDIO bus node - if missing then create one */
  1842. node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
  1843. if (!PHANDLE_VALID(node)) {
  1844. prom_printf("Adding Ethernet MDIO node\n");
  1845. call_prom("interpret", 1, 1,
  1846. " s\" /builtin\" find-device"
  1847. " new-device"
  1848. " 1 encode-int s\" #address-cells\" property"
  1849. " 0 encode-int s\" #size-cells\" property"
  1850. " s\" mdio\" device-name"
  1851. " s\" fsl,mpc5200b-mdio\" encode-string"
  1852. " s\" compatible\" property"
  1853. " 0xf0003000 0x400 reg"
  1854. " 0x2 encode-int"
  1855. " 0x5 encode-int encode+"
  1856. " 0x3 encode-int encode+"
  1857. " s\" interrupts\" property"
  1858. " finish-device");
  1859. };
  1860. /* Check for a PHY device node - if missing then create one and
  1861. * give it's phandle to the ethernet node */
  1862. node = call_prom("finddevice", 1, 1,
  1863. ADDR("/builtin/mdio/ethernet-phy"));
  1864. if (!PHANDLE_VALID(node)) {
  1865. prom_printf("Adding Ethernet PHY node\n");
  1866. call_prom("interpret", 1, 1,
  1867. " s\" /builtin/mdio\" find-device"
  1868. " new-device"
  1869. " s\" ethernet-phy\" device-name"
  1870. " 0x10 encode-int s\" reg\" property"
  1871. " my-self"
  1872. " ihandle>phandle"
  1873. " finish-device"
  1874. " s\" /builtin/ethernet\" find-device"
  1875. " encode-int"
  1876. " s\" phy-handle\" property"
  1877. " device-end");
  1878. }
  1879. }
  1880. static void __init fixup_device_tree_efika(void)
  1881. {
  1882. int sound_irq[3] = { 2, 2, 0 };
  1883. int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
  1884. 3,4,0, 3,5,0, 3,6,0, 3,7,0,
  1885. 3,8,0, 3,9,0, 3,10,0, 3,11,0,
  1886. 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
  1887. u32 node;
  1888. char prop[64];
  1889. int rv, len;
  1890. /* Check if we're really running on a EFIKA */
  1891. node = call_prom("finddevice", 1, 1, ADDR("/"));
  1892. if (!PHANDLE_VALID(node))
  1893. return;
  1894. rv = prom_getprop(node, "model", prop, sizeof(prop));
  1895. if (rv == PROM_ERROR)
  1896. return;
  1897. if (strcmp(prop, "EFIKA5K2"))
  1898. return;
  1899. prom_printf("Applying EFIKA device tree fixups\n");
  1900. /* Claiming to be 'chrp' is death */
  1901. node = call_prom("finddevice", 1, 1, ADDR("/"));
  1902. rv = prom_getprop(node, "device_type", prop, sizeof(prop));
  1903. if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
  1904. prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
  1905. /* CODEGEN,description is exposed in /proc/cpuinfo so
  1906. fix that too */
  1907. rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
  1908. if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
  1909. prom_setprop(node, "/", "CODEGEN,description",
  1910. "Efika 5200B PowerPC System",
  1911. sizeof("Efika 5200B PowerPC System"));
  1912. /* Fixup bestcomm interrupts property */
  1913. node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
  1914. if (PHANDLE_VALID(node)) {
  1915. len = prom_getproplen(node, "interrupts");
  1916. if (len == 12) {
  1917. prom_printf("Fixing bestcomm interrupts property\n");
  1918. prom_setprop(node, "/builtin/bestcom", "interrupts",
  1919. bcomm_irq, sizeof(bcomm_irq));
  1920. }
  1921. }
  1922. /* Fixup sound interrupts property */
  1923. node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
  1924. if (PHANDLE_VALID(node)) {
  1925. rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
  1926. if (rv == PROM_ERROR) {
  1927. prom_printf("Adding sound interrupts property\n");
  1928. prom_setprop(node, "/builtin/sound", "interrupts",
  1929. sound_irq, sizeof(sound_irq));
  1930. }
  1931. }
  1932. /* Make sure ethernet phy-handle property exists */
  1933. fixup_device_tree_efika_add_phy();
  1934. }
  1935. #else
  1936. #define fixup_device_tree_efika()
  1937. #endif
  1938. static void __init fixup_device_tree(void)
  1939. {
  1940. fixup_device_tree_maple();
  1941. fixup_device_tree_maple_memory_controller();
  1942. fixup_device_tree_chrp();
  1943. fixup_device_tree_pmac();
  1944. fixup_device_tree_efika();
  1945. }
  1946. static void __init prom_find_boot_cpu(void)
  1947. {
  1948. struct prom_t *_prom = &RELOC(prom);
  1949. u32 getprop_rval;
  1950. ihandle prom_cpu;
  1951. phandle cpu_pkg;
  1952. _prom->cpu = 0;
  1953. if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
  1954. return;
  1955. cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
  1956. prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
  1957. _prom->cpu = getprop_rval;
  1958. prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
  1959. }
  1960. static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
  1961. {
  1962. #ifdef CONFIG_BLK_DEV_INITRD
  1963. struct prom_t *_prom = &RELOC(prom);
  1964. if (r3 && r4 && r4 != 0xdeadbeef) {
  1965. unsigned long val;
  1966. RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
  1967. RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
  1968. val = RELOC(prom_initrd_start);
  1969. prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
  1970. &val, sizeof(val));
  1971. val = RELOC(prom_initrd_end);
  1972. prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
  1973. &val, sizeof(val));
  1974. reserve_mem(RELOC(prom_initrd_start),
  1975. RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
  1976. prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
  1977. prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
  1978. }
  1979. #endif /* CONFIG_BLK_DEV_INITRD */
  1980. }
  1981. /*
  1982. * We enter here early on, when the Open Firmware prom is still
  1983. * handling exceptions and the MMU hash table for us.
  1984. */
  1985. unsigned long __init prom_init(unsigned long r3, unsigned long r4,
  1986. unsigned long pp,
  1987. unsigned long r6, unsigned long r7,
  1988. unsigned long kbase)
  1989. {
  1990. struct prom_t *_prom;
  1991. unsigned long hdr;
  1992. #ifdef CONFIG_PPC32
  1993. unsigned long offset = reloc_offset();
  1994. reloc_got2(offset);
  1995. #endif
  1996. _prom = &RELOC(prom);
  1997. /*
  1998. * First zero the BSS
  1999. */
  2000. memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
  2001. /*
  2002. * Init interface to Open Firmware, get some node references,
  2003. * like /chosen
  2004. */
  2005. prom_init_client_services(pp);
  2006. /*
  2007. * See if this OF is old enough that we need to do explicit maps
  2008. * and other workarounds
  2009. */
  2010. prom_find_mmu();
  2011. /*
  2012. * Init prom stdout device
  2013. */
  2014. prom_init_stdout();
  2015. prom_printf("Preparing to boot %s", RELOC(linux_banner));
  2016. /*
  2017. * Get default machine type. At this point, we do not differentiate
  2018. * between pSeries SMP and pSeries LPAR
  2019. */
  2020. RELOC(of_platform) = prom_find_machine_type();
  2021. #ifndef CONFIG_RELOCATABLE
  2022. /* Bail if this is a kdump kernel. */
  2023. if (PHYSICAL_START > 0)
  2024. prom_panic("Error: You can't boot a kdump kernel from OF!\n");
  2025. #endif
  2026. /*
  2027. * Check for an initrd
  2028. */
  2029. prom_check_initrd(r3, r4);
  2030. #ifdef CONFIG_PPC_PSERIES
  2031. /*
  2032. * On pSeries, inform the firmware about our capabilities
  2033. */
  2034. if (RELOC(of_platform) == PLATFORM_PSERIES ||
  2035. RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
  2036. prom_send_capabilities();
  2037. #endif
  2038. /*
  2039. * Copy the CPU hold code
  2040. */
  2041. if (RELOC(of_platform) != PLATFORM_POWERMAC)
  2042. copy_and_flush(0, kbase, 0x100, 0);
  2043. /*
  2044. * Do early parsing of command line
  2045. */
  2046. early_cmdline_parse();
  2047. /*
  2048. * Initialize memory management within prom_init
  2049. */
  2050. prom_init_mem();
  2051. /*
  2052. * Determine which cpu is actually running right _now_
  2053. */
  2054. prom_find_boot_cpu();
  2055. /*
  2056. * Initialize display devices
  2057. */
  2058. prom_check_displays();
  2059. #ifdef CONFIG_PPC64
  2060. /*
  2061. * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
  2062. * that uses the allocator, we need to make sure we get the top of memory
  2063. * available for us here...
  2064. */
  2065. if (RELOC(of_platform) == PLATFORM_PSERIES)
  2066. prom_initialize_tce_table();
  2067. #endif
  2068. /*
  2069. * On non-powermacs, try to instantiate RTAS and puts all CPUs
  2070. * in spin-loops. PowerMacs don't have a working RTAS and use
  2071. * a different way to spin CPUs
  2072. */
  2073. if (RELOC(of_platform) != PLATFORM_POWERMAC) {
  2074. prom_instantiate_rtas();
  2075. prom_hold_cpus();
  2076. }
  2077. /*
  2078. * Fill in some infos for use by the kernel later on
  2079. */
  2080. #ifdef CONFIG_PPC64
  2081. if (RELOC(prom_iommu_off))
  2082. prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
  2083. NULL, 0);
  2084. if (RELOC(prom_iommu_force_on))
  2085. prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
  2086. NULL, 0);
  2087. if (RELOC(prom_tce_alloc_start)) {
  2088. prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
  2089. &RELOC(prom_tce_alloc_start),
  2090. sizeof(prom_tce_alloc_start));
  2091. prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
  2092. &RELOC(prom_tce_alloc_end),
  2093. sizeof(prom_tce_alloc_end));
  2094. }
  2095. #endif
  2096. /*
  2097. * Fixup any known bugs in the device-tree
  2098. */
  2099. fixup_device_tree();
  2100. /*
  2101. * Now finally create the flattened device-tree
  2102. */
  2103. prom_printf("copying OF device tree...\n");
  2104. flatten_device_tree();
  2105. /*
  2106. * in case stdin is USB and still active on IBM machines...
  2107. * Unfortunately quiesce crashes on some powermacs if we have
  2108. * closed stdin already (in particular the powerbook 101).
  2109. */
  2110. if (RELOC(of_platform) != PLATFORM_POWERMAC)
  2111. prom_close_stdin();
  2112. /*
  2113. * Call OF "quiesce" method to shut down pending DMA's from
  2114. * devices etc...
  2115. */
  2116. prom_printf("Calling quiesce...\n");
  2117. call_prom("quiesce", 0, 0);
  2118. /*
  2119. * And finally, call the kernel passing it the flattened device
  2120. * tree and NULL as r5, thus triggering the new entry point which
  2121. * is common to us and kexec
  2122. */
  2123. hdr = RELOC(dt_header_start);
  2124. prom_printf("returning from prom_init\n");
  2125. prom_debug("->dt_header_start=0x%x\n", hdr);
  2126. #ifdef CONFIG_PPC32
  2127. reloc_got2(-offset);
  2128. #endif
  2129. __start(hdr, kbase, 0);
  2130. return 0;
  2131. }