prom_init.c 77 KB

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