prom_init.c 81 KB

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