prom_init.c 81 KB

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