prom_init.c 71 KB

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