prom_init.c 85 KB

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