prom_init.c 81 KB

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