prom_init.c 81 KB

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