prom_init.c 77 KB

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