prom_init.c 83 KB

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