prom_init.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574
  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. /* Option Vector 6: IBM PAPR hints */
  553. #define OV6_LINUX 0x02 /* Linux is our OS */
  554. /*
  555. * The architecture vector has an array of PVR mask/value pairs,
  556. * followed by # option vectors - 1, followed by the option vectors.
  557. */
  558. static unsigned char ibm_architecture_vec[] = {
  559. W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */
  560. W(0xffff0000), W(0x003e0000), /* POWER6 */
  561. W(0xffff0000), W(0x003f0000), /* POWER7 */
  562. W(0xffffffff), W(0x0f000003), /* all 2.06-compliant */
  563. W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */
  564. W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */
  565. 6 - 1, /* 6 option vectors */
  566. /* option vector 1: processor architectures supported */
  567. 3 - 2, /* length */
  568. 0, /* don't ignore, don't halt */
  569. OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
  570. OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
  571. /* option vector 2: Open Firmware options supported */
  572. 34 - 2, /* length */
  573. OV2_REAL_MODE,
  574. 0, 0,
  575. W(0xffffffff), /* real_base */
  576. W(0xffffffff), /* real_size */
  577. W(0xffffffff), /* virt_base */
  578. W(0xffffffff), /* virt_size */
  579. W(0xffffffff), /* load_base */
  580. W(64), /* 64MB min RMA */
  581. W(0xffffffff), /* full client load */
  582. 0, /* min RMA percentage of total RAM */
  583. 48, /* max log_2(hash table size) */
  584. /* option vector 3: processor options supported */
  585. 3 - 2, /* length */
  586. 0, /* don't ignore, don't halt */
  587. OV3_FP | OV3_VMX | OV3_DFP,
  588. /* option vector 4: IBM PAPR implementation */
  589. 2 - 2, /* length */
  590. 0, /* don't halt */
  591. /* option vector 5: PAPR/OF options */
  592. 13 - 2, /* length */
  593. 0, /* don't ignore, don't halt */
  594. OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
  595. OV5_DONATE_DEDICATE_CPU | OV5_MSI,
  596. 0,
  597. OV5_CMO,
  598. 0,
  599. 0,
  600. 0,
  601. 0,
  602. W(NR_CPUS), /* number of cores supported*/
  603. /* option vector 6: IBM PAPR hints */
  604. 4 - 2, /* length */
  605. 0,
  606. 0,
  607. OV6_LINUX,
  608. };
  609. /* Old method - ELF header with PT_NOTE sections */
  610. static struct fake_elf {
  611. Elf32_Ehdr elfhdr;
  612. Elf32_Phdr phdr[2];
  613. struct chrpnote {
  614. u32 namesz;
  615. u32 descsz;
  616. u32 type;
  617. char name[8]; /* "PowerPC" */
  618. struct chrpdesc {
  619. u32 real_mode;
  620. u32 real_base;
  621. u32 real_size;
  622. u32 virt_base;
  623. u32 virt_size;
  624. u32 load_base;
  625. } chrpdesc;
  626. } chrpnote;
  627. struct rpanote {
  628. u32 namesz;
  629. u32 descsz;
  630. u32 type;
  631. char name[24]; /* "IBM,RPA-Client-Config" */
  632. struct rpadesc {
  633. u32 lpar_affinity;
  634. u32 min_rmo_size;
  635. u32 min_rmo_percent;
  636. u32 max_pft_size;
  637. u32 splpar;
  638. u32 min_load;
  639. u32 new_mem_def;
  640. u32 ignore_me;
  641. } rpadesc;
  642. } rpanote;
  643. } fake_elf = {
  644. .elfhdr = {
  645. .e_ident = { 0x7f, 'E', 'L', 'F',
  646. ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
  647. .e_type = ET_EXEC, /* yeah right */
  648. .e_machine = EM_PPC,
  649. .e_version = EV_CURRENT,
  650. .e_phoff = offsetof(struct fake_elf, phdr),
  651. .e_phentsize = sizeof(Elf32_Phdr),
  652. .e_phnum = 2
  653. },
  654. .phdr = {
  655. [0] = {
  656. .p_type = PT_NOTE,
  657. .p_offset = offsetof(struct fake_elf, chrpnote),
  658. .p_filesz = sizeof(struct chrpnote)
  659. }, [1] = {
  660. .p_type = PT_NOTE,
  661. .p_offset = offsetof(struct fake_elf, rpanote),
  662. .p_filesz = sizeof(struct rpanote)
  663. }
  664. },
  665. .chrpnote = {
  666. .namesz = sizeof("PowerPC"),
  667. .descsz = sizeof(struct chrpdesc),
  668. .type = 0x1275,
  669. .name = "PowerPC",
  670. .chrpdesc = {
  671. .real_mode = ~0U, /* ~0 means "don't care" */
  672. .real_base = ~0U,
  673. .real_size = ~0U,
  674. .virt_base = ~0U,
  675. .virt_size = ~0U,
  676. .load_base = ~0U
  677. },
  678. },
  679. .rpanote = {
  680. .namesz = sizeof("IBM,RPA-Client-Config"),
  681. .descsz = sizeof(struct rpadesc),
  682. .type = 0x12759999,
  683. .name = "IBM,RPA-Client-Config",
  684. .rpadesc = {
  685. .lpar_affinity = 0,
  686. .min_rmo_size = 64, /* in megabytes */
  687. .min_rmo_percent = 0,
  688. .max_pft_size = 48, /* 2^48 bytes max PFT size */
  689. .splpar = 1,
  690. .min_load = ~0U,
  691. .new_mem_def = 0
  692. }
  693. }
  694. };
  695. static void __init prom_send_capabilities(void)
  696. {
  697. ihandle elfloader, root;
  698. prom_arg_t ret;
  699. root = call_prom("open", 1, 1, ADDR("/"));
  700. if (root != 0) {
  701. /* try calling the ibm,client-architecture-support method */
  702. prom_printf("Calling ibm,client-architecture-support...");
  703. if (call_prom_ret("call-method", 3, 2, &ret,
  704. ADDR("ibm,client-architecture-support"),
  705. root,
  706. ADDR(ibm_architecture_vec)) == 0) {
  707. /* the call exists... */
  708. if (ret)
  709. prom_printf("\nWARNING: ibm,client-architecture"
  710. "-support call FAILED!\n");
  711. call_prom("close", 1, 0, root);
  712. prom_printf(" done\n");
  713. return;
  714. }
  715. call_prom("close", 1, 0, root);
  716. prom_printf(" not implemented\n");
  717. }
  718. /* no ibm,client-architecture-support call, try the old way */
  719. elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
  720. if (elfloader == 0) {
  721. prom_printf("couldn't open /packages/elf-loader\n");
  722. return;
  723. }
  724. call_prom("call-method", 3, 1, ADDR("process-elf-header"),
  725. elfloader, ADDR(&fake_elf));
  726. call_prom("close", 1, 0, elfloader);
  727. }
  728. #endif
  729. /*
  730. * Memory allocation strategy... our layout is normally:
  731. *
  732. * at 14Mb or more we have vmlinux, then a gap and initrd. In some
  733. * rare cases, initrd might end up being before the kernel though.
  734. * We assume this won't override the final kernel at 0, we have no
  735. * provision to handle that in this version, but it should hopefully
  736. * never happen.
  737. *
  738. * alloc_top is set to the top of RMO, eventually shrink down if the
  739. * TCEs overlap
  740. *
  741. * alloc_bottom is set to the top of kernel/initrd
  742. *
  743. * from there, allocations are done this way : rtas is allocated
  744. * topmost, and the device-tree is allocated from the bottom. We try
  745. * to grow the device-tree allocation as we progress. If we can't,
  746. * then we fail, we don't currently have a facility to restart
  747. * elsewhere, but that shouldn't be necessary.
  748. *
  749. * Note that calls to reserve_mem have to be done explicitly, memory
  750. * allocated with either alloc_up or alloc_down isn't automatically
  751. * reserved.
  752. */
  753. /*
  754. * Allocates memory in the RMO upward from the kernel/initrd
  755. *
  756. * When align is 0, this is a special case, it means to allocate in place
  757. * at the current location of alloc_bottom or fail (that is basically
  758. * extending the previous allocation). Used for the device-tree flattening
  759. */
  760. static unsigned long __init alloc_up(unsigned long size, unsigned long align)
  761. {
  762. unsigned long base = RELOC(alloc_bottom);
  763. unsigned long addr = 0;
  764. if (align)
  765. base = _ALIGN_UP(base, align);
  766. prom_debug("alloc_up(%x, %x)\n", size, align);
  767. if (RELOC(ram_top) == 0)
  768. prom_panic("alloc_up() called with mem not initialized\n");
  769. if (align)
  770. base = _ALIGN_UP(RELOC(alloc_bottom), align);
  771. else
  772. base = RELOC(alloc_bottom);
  773. for(; (base + size) <= RELOC(alloc_top);
  774. base = _ALIGN_UP(base + 0x100000, align)) {
  775. prom_debug(" trying: 0x%x\n\r", base);
  776. addr = (unsigned long)prom_claim(base, size, 0);
  777. if (addr != PROM_ERROR && addr != 0)
  778. break;
  779. addr = 0;
  780. if (align == 0)
  781. break;
  782. }
  783. if (addr == 0)
  784. return 0;
  785. RELOC(alloc_bottom) = addr;
  786. prom_debug(" -> %x\n", addr);
  787. prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  788. prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
  789. prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  790. prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
  791. prom_debug(" ram_top : %x\n", RELOC(ram_top));
  792. return addr;
  793. }
  794. /*
  795. * Allocates memory downward, either from top of RMO, or if highmem
  796. * is set, from the top of RAM. Note that this one doesn't handle
  797. * failures. It does claim memory if highmem is not set.
  798. */
  799. static unsigned long __init alloc_down(unsigned long size, unsigned long align,
  800. int highmem)
  801. {
  802. unsigned long base, addr = 0;
  803. prom_debug("alloc_down(%x, %x, %s)\n", size, align,
  804. highmem ? RELOC("(high)") : RELOC("(low)"));
  805. if (RELOC(ram_top) == 0)
  806. prom_panic("alloc_down() called with mem not initialized\n");
  807. if (highmem) {
  808. /* Carve out storage for the TCE table. */
  809. addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
  810. if (addr <= RELOC(alloc_bottom))
  811. return 0;
  812. /* Will we bump into the RMO ? If yes, check out that we
  813. * didn't overlap existing allocations there, if we did,
  814. * we are dead, we must be the first in town !
  815. */
  816. if (addr < RELOC(rmo_top)) {
  817. /* Good, we are first */
  818. if (RELOC(alloc_top) == RELOC(rmo_top))
  819. RELOC(alloc_top) = RELOC(rmo_top) = addr;
  820. else
  821. return 0;
  822. }
  823. RELOC(alloc_top_high) = addr;
  824. goto bail;
  825. }
  826. base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
  827. for (; base > RELOC(alloc_bottom);
  828. base = _ALIGN_DOWN(base - 0x100000, align)) {
  829. prom_debug(" trying: 0x%x\n\r", base);
  830. addr = (unsigned long)prom_claim(base, size, 0);
  831. if (addr != PROM_ERROR && addr != 0)
  832. break;
  833. addr = 0;
  834. }
  835. if (addr == 0)
  836. return 0;
  837. RELOC(alloc_top) = addr;
  838. bail:
  839. prom_debug(" -> %x\n", addr);
  840. prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  841. prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
  842. prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  843. prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
  844. prom_debug(" ram_top : %x\n", RELOC(ram_top));
  845. return addr;
  846. }
  847. /*
  848. * Parse a "reg" cell
  849. */
  850. static unsigned long __init prom_next_cell(int s, cell_t **cellp)
  851. {
  852. cell_t *p = *cellp;
  853. unsigned long r = 0;
  854. /* Ignore more than 2 cells */
  855. while (s > sizeof(unsigned long) / 4) {
  856. p++;
  857. s--;
  858. }
  859. r = *p++;
  860. #ifdef CONFIG_PPC64
  861. if (s > 1) {
  862. r <<= 32;
  863. r |= *(p++);
  864. }
  865. #endif
  866. *cellp = p;
  867. return r;
  868. }
  869. /*
  870. * Very dumb function for adding to the memory reserve list, but
  871. * we don't need anything smarter at this point
  872. *
  873. * XXX Eventually check for collisions. They should NEVER happen.
  874. * If problems seem to show up, it would be a good start to track
  875. * them down.
  876. */
  877. static void __init reserve_mem(u64 base, u64 size)
  878. {
  879. u64 top = base + size;
  880. unsigned long cnt = RELOC(mem_reserve_cnt);
  881. if (size == 0)
  882. return;
  883. /* We need to always keep one empty entry so that we
  884. * have our terminator with "size" set to 0 since we are
  885. * dumb and just copy this entire array to the boot params
  886. */
  887. base = _ALIGN_DOWN(base, PAGE_SIZE);
  888. top = _ALIGN_UP(top, PAGE_SIZE);
  889. size = top - base;
  890. if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
  891. prom_panic("Memory reserve map exhausted !\n");
  892. RELOC(mem_reserve_map)[cnt].base = base;
  893. RELOC(mem_reserve_map)[cnt].size = size;
  894. RELOC(mem_reserve_cnt) = cnt + 1;
  895. }
  896. /*
  897. * Initialize memory allocation mechanism, parse "memory" nodes and
  898. * obtain that way the top of memory and RMO to setup out local allocator
  899. */
  900. static void __init prom_init_mem(void)
  901. {
  902. phandle node;
  903. char *path, type[64];
  904. unsigned int plen;
  905. cell_t *p, *endp;
  906. struct prom_t *_prom = &RELOC(prom);
  907. u32 rac, rsc;
  908. /*
  909. * We iterate the memory nodes to find
  910. * 1) top of RMO (first node)
  911. * 2) top of memory
  912. */
  913. rac = 2;
  914. prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
  915. rsc = 1;
  916. prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
  917. prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
  918. prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
  919. prom_debug("scanning memory:\n");
  920. path = RELOC(prom_scratch);
  921. for (node = 0; prom_next_node(&node); ) {
  922. type[0] = 0;
  923. prom_getprop(node, "device_type", type, sizeof(type));
  924. if (type[0] == 0) {
  925. /*
  926. * CHRP Longtrail machines have no device_type
  927. * on the memory node, so check the name instead...
  928. */
  929. prom_getprop(node, "name", type, sizeof(type));
  930. }
  931. if (strcmp(type, RELOC("memory")))
  932. continue;
  933. plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
  934. if (plen > sizeof(regbuf)) {
  935. prom_printf("memory node too large for buffer !\n");
  936. plen = sizeof(regbuf);
  937. }
  938. p = RELOC(regbuf);
  939. endp = p + (plen / sizeof(cell_t));
  940. #ifdef DEBUG_PROM
  941. memset(path, 0, PROM_SCRATCH_SIZE);
  942. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  943. prom_debug(" node %s :\n", path);
  944. #endif /* DEBUG_PROM */
  945. while ((endp - p) >= (rac + rsc)) {
  946. unsigned long base, size;
  947. base = prom_next_cell(rac, &p);
  948. size = prom_next_cell(rsc, &p);
  949. if (size == 0)
  950. continue;
  951. prom_debug(" %x %x\n", base, size);
  952. if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
  953. RELOC(rmo_top) = size;
  954. if ((base + size) > RELOC(ram_top))
  955. RELOC(ram_top) = base + size;
  956. }
  957. }
  958. RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
  959. /* Check if we have an initrd after the kernel, if we do move our bottom
  960. * point to after it
  961. */
  962. if (RELOC(prom_initrd_start)) {
  963. if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
  964. RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
  965. }
  966. /*
  967. * If prom_memory_limit is set we reduce the upper limits *except* for
  968. * alloc_top_high. This must be the real top of RAM so we can put
  969. * TCE's up there.
  970. */
  971. RELOC(alloc_top_high) = RELOC(ram_top);
  972. if (RELOC(prom_memory_limit)) {
  973. if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
  974. prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
  975. RELOC(prom_memory_limit));
  976. RELOC(prom_memory_limit) = 0;
  977. } else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
  978. prom_printf("Ignoring mem=%x >= ram_top.\n",
  979. RELOC(prom_memory_limit));
  980. RELOC(prom_memory_limit) = 0;
  981. } else {
  982. RELOC(ram_top) = RELOC(prom_memory_limit);
  983. RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
  984. }
  985. }
  986. /*
  987. * Setup our top alloc point, that is top of RMO or top of
  988. * segment 0 when running non-LPAR.
  989. * Some RS64 machines have buggy firmware where claims up at
  990. * 1GB fail. Cap at 768MB as a workaround.
  991. * Since 768MB is plenty of room, and we need to cap to something
  992. * reasonable on 32-bit, cap at 768MB on all machines.
  993. */
  994. if (!RELOC(rmo_top))
  995. RELOC(rmo_top) = RELOC(ram_top);
  996. RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
  997. RELOC(alloc_top) = RELOC(rmo_top);
  998. RELOC(alloc_top_high) = RELOC(ram_top);
  999. prom_printf("memory layout at init:\n");
  1000. prom_printf(" memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
  1001. prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  1002. prom_printf(" alloc_top : %x\n", RELOC(alloc_top));
  1003. prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  1004. prom_printf(" rmo_top : %x\n", RELOC(rmo_top));
  1005. prom_printf(" ram_top : %x\n", RELOC(ram_top));
  1006. }
  1007. /*
  1008. * Allocate room for and instantiate RTAS
  1009. */
  1010. static void __init prom_instantiate_rtas(void)
  1011. {
  1012. phandle rtas_node;
  1013. ihandle rtas_inst;
  1014. u32 base, entry = 0;
  1015. u32 size = 0;
  1016. prom_debug("prom_instantiate_rtas: start...\n");
  1017. rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  1018. prom_debug("rtas_node: %x\n", rtas_node);
  1019. if (!PHANDLE_VALID(rtas_node))
  1020. return;
  1021. prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
  1022. if (size == 0)
  1023. return;
  1024. base = alloc_down(size, PAGE_SIZE, 0);
  1025. if (base == 0) {
  1026. prom_printf("RTAS allocation failed !\n");
  1027. return;
  1028. }
  1029. rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
  1030. if (!IHANDLE_VALID(rtas_inst)) {
  1031. prom_printf("opening rtas package failed (%x)\n", rtas_inst);
  1032. return;
  1033. }
  1034. prom_printf("instantiating rtas at 0x%x...", base);
  1035. if (call_prom_ret("call-method", 3, 2, &entry,
  1036. ADDR("instantiate-rtas"),
  1037. rtas_inst, base) != 0
  1038. || entry == 0) {
  1039. prom_printf(" failed\n");
  1040. return;
  1041. }
  1042. prom_printf(" done\n");
  1043. reserve_mem(base, size);
  1044. prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
  1045. &base, sizeof(base));
  1046. prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
  1047. &entry, sizeof(entry));
  1048. prom_debug("rtas base = 0x%x\n", base);
  1049. prom_debug("rtas entry = 0x%x\n", entry);
  1050. prom_debug("rtas size = 0x%x\n", (long)size);
  1051. prom_debug("prom_instantiate_rtas: end...\n");
  1052. }
  1053. #ifdef CONFIG_PPC64
  1054. /*
  1055. * Allocate room for and initialize TCE tables
  1056. */
  1057. static void __init prom_initialize_tce_table(void)
  1058. {
  1059. phandle node;
  1060. ihandle phb_node;
  1061. char compatible[64], type[64], model[64];
  1062. char *path = RELOC(prom_scratch);
  1063. u64 base, align;
  1064. u32 minalign, minsize;
  1065. u64 tce_entry, *tce_entryp;
  1066. u64 local_alloc_top, local_alloc_bottom;
  1067. u64 i;
  1068. if (RELOC(prom_iommu_off))
  1069. return;
  1070. prom_debug("starting prom_initialize_tce_table\n");
  1071. /* Cache current top of allocs so we reserve a single block */
  1072. local_alloc_top = RELOC(alloc_top_high);
  1073. local_alloc_bottom = local_alloc_top;
  1074. /* Search all nodes looking for PHBs. */
  1075. for (node = 0; prom_next_node(&node); ) {
  1076. compatible[0] = 0;
  1077. type[0] = 0;
  1078. model[0] = 0;
  1079. prom_getprop(node, "compatible",
  1080. compatible, sizeof(compatible));
  1081. prom_getprop(node, "device_type", type, sizeof(type));
  1082. prom_getprop(node, "model", model, sizeof(model));
  1083. if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
  1084. continue;
  1085. /* Keep the old logic intact to avoid regression. */
  1086. if (compatible[0] != 0) {
  1087. if ((strstr(compatible, RELOC("python")) == NULL) &&
  1088. (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
  1089. (strstr(compatible, RELOC("Winnipeg")) == NULL))
  1090. continue;
  1091. } else if (model[0] != 0) {
  1092. if ((strstr(model, RELOC("ython")) == NULL) &&
  1093. (strstr(model, RELOC("peedwagon")) == NULL) &&
  1094. (strstr(model, RELOC("innipeg")) == NULL))
  1095. continue;
  1096. }
  1097. if (prom_getprop(node, "tce-table-minalign", &minalign,
  1098. sizeof(minalign)) == PROM_ERROR)
  1099. minalign = 0;
  1100. if (prom_getprop(node, "tce-table-minsize", &minsize,
  1101. sizeof(minsize)) == PROM_ERROR)
  1102. minsize = 4UL << 20;
  1103. /*
  1104. * Even though we read what OF wants, we just set the table
  1105. * size to 4 MB. This is enough to map 2GB of PCI DMA space.
  1106. * By doing this, we avoid the pitfalls of trying to DMA to
  1107. * MMIO space and the DMA alias hole.
  1108. *
  1109. * On POWER4, firmware sets the TCE region by assuming
  1110. * each TCE table is 8MB. Using this memory for anything
  1111. * else will impact performance, so we always allocate 8MB.
  1112. * Anton
  1113. */
  1114. if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
  1115. minsize = 8UL << 20;
  1116. else
  1117. minsize = 4UL << 20;
  1118. /* Align to the greater of the align or size */
  1119. align = max(minalign, minsize);
  1120. base = alloc_down(minsize, align, 1);
  1121. if (base == 0)
  1122. prom_panic("ERROR, cannot find space for TCE table.\n");
  1123. if (base < local_alloc_bottom)
  1124. local_alloc_bottom = base;
  1125. /* It seems OF doesn't null-terminate the path :-( */
  1126. memset(path, 0, PROM_SCRATCH_SIZE);
  1127. /* Call OF to setup the TCE hardware */
  1128. if (call_prom("package-to-path", 3, 1, node,
  1129. path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
  1130. prom_printf("package-to-path failed\n");
  1131. }
  1132. /* Save away the TCE table attributes for later use. */
  1133. prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
  1134. prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
  1135. prom_debug("TCE table: %s\n", path);
  1136. prom_debug("\tnode = 0x%x\n", node);
  1137. prom_debug("\tbase = 0x%x\n", base);
  1138. prom_debug("\tsize = 0x%x\n", minsize);
  1139. /* Initialize the table to have a one-to-one mapping
  1140. * over the allocated size.
  1141. */
  1142. tce_entryp = (u64 *)base;
  1143. for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
  1144. tce_entry = (i << PAGE_SHIFT);
  1145. tce_entry |= 0x3;
  1146. *tce_entryp = tce_entry;
  1147. }
  1148. prom_printf("opening PHB %s", path);
  1149. phb_node = call_prom("open", 1, 1, path);
  1150. if (phb_node == 0)
  1151. prom_printf("... failed\n");
  1152. else
  1153. prom_printf("... done\n");
  1154. call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
  1155. phb_node, -1, minsize,
  1156. (u32) base, (u32) (base >> 32));
  1157. call_prom("close", 1, 0, phb_node);
  1158. }
  1159. reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
  1160. /* These are only really needed if there is a memory limit in
  1161. * effect, but we don't know so export them always. */
  1162. RELOC(prom_tce_alloc_start) = local_alloc_bottom;
  1163. RELOC(prom_tce_alloc_end) = local_alloc_top;
  1164. /* Flag the first invalid entry */
  1165. prom_debug("ending prom_initialize_tce_table\n");
  1166. }
  1167. #endif
  1168. /*
  1169. * With CHRP SMP we need to use the OF to start the other processors.
  1170. * We can't wait until smp_boot_cpus (the OF is trashed by then)
  1171. * so we have to put the processors into a holding pattern controlled
  1172. * by the kernel (not OF) before we destroy the OF.
  1173. *
  1174. * This uses a chunk of low memory, puts some holding pattern
  1175. * code there and sends the other processors off to there until
  1176. * smp_boot_cpus tells them to do something. The holding pattern
  1177. * checks that address until its cpu # is there, when it is that
  1178. * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
  1179. * of setting those values.
  1180. *
  1181. * We also use physical address 0x4 here to tell when a cpu
  1182. * is in its holding pattern code.
  1183. *
  1184. * -- Cort
  1185. */
  1186. /*
  1187. * We want to reference the copy of __secondary_hold_* in the
  1188. * 0 - 0x100 address range
  1189. */
  1190. #define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
  1191. static void __init prom_hold_cpus(void)
  1192. {
  1193. unsigned long i;
  1194. unsigned int reg;
  1195. phandle node;
  1196. char type[64];
  1197. struct prom_t *_prom = &RELOC(prom);
  1198. unsigned long *spinloop
  1199. = (void *) LOW_ADDR(__secondary_hold_spinloop);
  1200. unsigned long *acknowledge
  1201. = (void *) LOW_ADDR(__secondary_hold_acknowledge);
  1202. unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
  1203. prom_debug("prom_hold_cpus: start...\n");
  1204. prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
  1205. prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
  1206. prom_debug(" 1) acknowledge = 0x%x\n",
  1207. (unsigned long)acknowledge);
  1208. prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
  1209. prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
  1210. /* Set the common spinloop variable, so all of the secondary cpus
  1211. * will block when they are awakened from their OF spinloop.
  1212. * This must occur for both SMP and non SMP kernels, since OF will
  1213. * be trashed when we move the kernel.
  1214. */
  1215. *spinloop = 0;
  1216. /* look for cpus */
  1217. for (node = 0; prom_next_node(&node); ) {
  1218. type[0] = 0;
  1219. prom_getprop(node, "device_type", type, sizeof(type));
  1220. if (strcmp(type, RELOC("cpu")) != 0)
  1221. continue;
  1222. /* Skip non-configured cpus. */
  1223. if (prom_getprop(node, "status", type, sizeof(type)) > 0)
  1224. if (strcmp(type, RELOC("okay")) != 0)
  1225. continue;
  1226. reg = -1;
  1227. prom_getprop(node, "reg", &reg, sizeof(reg));
  1228. prom_debug("cpu hw idx = 0x%x\n", reg);
  1229. /* Init the acknowledge var which will be reset by
  1230. * the secondary cpu when it awakens from its OF
  1231. * spinloop.
  1232. */
  1233. *acknowledge = (unsigned long)-1;
  1234. if (reg != _prom->cpu) {
  1235. /* Primary Thread of non-boot cpu */
  1236. prom_printf("starting cpu hw idx %x... ", reg);
  1237. call_prom("start-cpu", 3, 0, node,
  1238. secondary_hold, reg);
  1239. for (i = 0; (i < 100000000) &&
  1240. (*acknowledge == ((unsigned long)-1)); i++ )
  1241. mb();
  1242. if (*acknowledge == reg)
  1243. prom_printf("done\n");
  1244. else
  1245. prom_printf("failed: %x\n", *acknowledge);
  1246. }
  1247. #ifdef CONFIG_SMP
  1248. else
  1249. prom_printf("boot cpu hw idx %x\n", reg);
  1250. #endif /* CONFIG_SMP */
  1251. }
  1252. prom_debug("prom_hold_cpus: end...\n");
  1253. }
  1254. static void __init prom_init_client_services(unsigned long pp)
  1255. {
  1256. struct prom_t *_prom = &RELOC(prom);
  1257. /* Get a handle to the prom entry point before anything else */
  1258. RELOC(prom_entry) = pp;
  1259. /* get a handle for the stdout device */
  1260. _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
  1261. if (!PHANDLE_VALID(_prom->chosen))
  1262. prom_panic("cannot find chosen"); /* msg won't be printed :( */
  1263. /* get device tree root */
  1264. _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
  1265. if (!PHANDLE_VALID(_prom->root))
  1266. prom_panic("cannot find device tree root"); /* msg won't be printed :( */
  1267. _prom->mmumap = 0;
  1268. }
  1269. #ifdef CONFIG_PPC32
  1270. /*
  1271. * For really old powermacs, we need to map things we claim.
  1272. * For that, we need the ihandle of the mmu.
  1273. * Also, on the longtrail, we need to work around other bugs.
  1274. */
  1275. static void __init prom_find_mmu(void)
  1276. {
  1277. struct prom_t *_prom = &RELOC(prom);
  1278. phandle oprom;
  1279. char version[64];
  1280. oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
  1281. if (!PHANDLE_VALID(oprom))
  1282. return;
  1283. if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
  1284. return;
  1285. version[sizeof(version) - 1] = 0;
  1286. /* XXX might need to add other versions here */
  1287. if (strcmp(version, "Open Firmware, 1.0.5") == 0)
  1288. of_workarounds = OF_WA_CLAIM;
  1289. else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
  1290. of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
  1291. call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
  1292. } else
  1293. return;
  1294. _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
  1295. prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
  1296. sizeof(_prom->mmumap));
  1297. if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
  1298. of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
  1299. }
  1300. #else
  1301. #define prom_find_mmu()
  1302. #endif
  1303. static void __init prom_init_stdout(void)
  1304. {
  1305. struct prom_t *_prom = &RELOC(prom);
  1306. char *path = RELOC(of_stdout_device);
  1307. char type[16];
  1308. u32 val;
  1309. if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
  1310. prom_panic("cannot find stdout");
  1311. _prom->stdout = val;
  1312. /* Get the full OF pathname of the stdout device */
  1313. memset(path, 0, 256);
  1314. call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
  1315. val = call_prom("instance-to-package", 1, 1, _prom->stdout);
  1316. prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
  1317. &val, sizeof(val));
  1318. prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
  1319. prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
  1320. path, strlen(path) + 1);
  1321. /* If it's a display, note it */
  1322. memset(type, 0, sizeof(type));
  1323. prom_getprop(val, "device_type", type, sizeof(type));
  1324. if (strcmp(type, RELOC("display")) == 0)
  1325. prom_setprop(val, path, "linux,boot-display", NULL, 0);
  1326. }
  1327. static void __init prom_close_stdin(void)
  1328. {
  1329. struct prom_t *_prom = &RELOC(prom);
  1330. ihandle val;
  1331. if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
  1332. call_prom("close", 1, 0, val);
  1333. }
  1334. static int __init prom_find_machine_type(void)
  1335. {
  1336. struct prom_t *_prom = &RELOC(prom);
  1337. char compat[256];
  1338. int len, i = 0;
  1339. #ifdef CONFIG_PPC64
  1340. phandle rtas;
  1341. int x;
  1342. #endif
  1343. /* Look for a PowerMac */
  1344. len = prom_getprop(_prom->root, "compatible",
  1345. compat, sizeof(compat)-1);
  1346. if (len > 0) {
  1347. compat[len] = 0;
  1348. while (i < len) {
  1349. char *p = &compat[i];
  1350. int sl = strlen(p);
  1351. if (sl == 0)
  1352. break;
  1353. if (strstr(p, RELOC("Power Macintosh")) ||
  1354. strstr(p, RELOC("MacRISC")))
  1355. return PLATFORM_POWERMAC;
  1356. #ifdef CONFIG_PPC64
  1357. /* We must make sure we don't detect the IBM Cell
  1358. * blades as pSeries due to some firmware issues,
  1359. * so we do it here.
  1360. */
  1361. if (strstr(p, RELOC("IBM,CBEA")) ||
  1362. strstr(p, RELOC("IBM,CPBW-1.0")))
  1363. return PLATFORM_GENERIC;
  1364. #endif /* CONFIG_PPC64 */
  1365. i += sl + 1;
  1366. }
  1367. }
  1368. #ifdef CONFIG_PPC64
  1369. /* If not a mac, try to figure out if it's an IBM pSeries or any other
  1370. * PAPR compliant platform. We assume it is if :
  1371. * - /device_type is "chrp" (please, do NOT use that for future
  1372. * non-IBM designs !
  1373. * - it has /rtas
  1374. */
  1375. len = prom_getprop(_prom->root, "device_type",
  1376. compat, sizeof(compat)-1);
  1377. if (len <= 0)
  1378. return PLATFORM_GENERIC;
  1379. if (strcmp(compat, RELOC("chrp")))
  1380. return PLATFORM_GENERIC;
  1381. /* Default to pSeries. We need to know if we are running LPAR */
  1382. rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  1383. if (!PHANDLE_VALID(rtas))
  1384. return PLATFORM_GENERIC;
  1385. x = prom_getproplen(rtas, "ibm,hypertas-functions");
  1386. if (x != PROM_ERROR) {
  1387. prom_debug("Hypertas detected, assuming LPAR !\n");
  1388. return PLATFORM_PSERIES_LPAR;
  1389. }
  1390. return PLATFORM_PSERIES;
  1391. #else
  1392. return PLATFORM_GENERIC;
  1393. #endif
  1394. }
  1395. static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
  1396. {
  1397. return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
  1398. }
  1399. /*
  1400. * If we have a display that we don't know how to drive,
  1401. * we will want to try to execute OF's open method for it
  1402. * later. However, OF will probably fall over if we do that
  1403. * we've taken over the MMU.
  1404. * So we check whether we will need to open the display,
  1405. * and if so, open it now.
  1406. */
  1407. static void __init prom_check_displays(void)
  1408. {
  1409. char type[16], *path;
  1410. phandle node;
  1411. ihandle ih;
  1412. int i;
  1413. static unsigned char default_colors[] = {
  1414. 0x00, 0x00, 0x00,
  1415. 0x00, 0x00, 0xaa,
  1416. 0x00, 0xaa, 0x00,
  1417. 0x00, 0xaa, 0xaa,
  1418. 0xaa, 0x00, 0x00,
  1419. 0xaa, 0x00, 0xaa,
  1420. 0xaa, 0xaa, 0x00,
  1421. 0xaa, 0xaa, 0xaa,
  1422. 0x55, 0x55, 0x55,
  1423. 0x55, 0x55, 0xff,
  1424. 0x55, 0xff, 0x55,
  1425. 0x55, 0xff, 0xff,
  1426. 0xff, 0x55, 0x55,
  1427. 0xff, 0x55, 0xff,
  1428. 0xff, 0xff, 0x55,
  1429. 0xff, 0xff, 0xff
  1430. };
  1431. const unsigned char *clut;
  1432. prom_debug("Looking for displays\n");
  1433. for (node = 0; prom_next_node(&node); ) {
  1434. memset(type, 0, sizeof(type));
  1435. prom_getprop(node, "device_type", type, sizeof(type));
  1436. if (strcmp(type, RELOC("display")) != 0)
  1437. continue;
  1438. /* It seems OF doesn't null-terminate the path :-( */
  1439. path = RELOC(prom_scratch);
  1440. memset(path, 0, PROM_SCRATCH_SIZE);
  1441. /*
  1442. * leave some room at the end of the path for appending extra
  1443. * arguments
  1444. */
  1445. if (call_prom("package-to-path", 3, 1, node, path,
  1446. PROM_SCRATCH_SIZE-10) == PROM_ERROR)
  1447. continue;
  1448. prom_printf("found display : %s, opening... ", path);
  1449. ih = call_prom("open", 1, 1, path);
  1450. if (ih == 0) {
  1451. prom_printf("failed\n");
  1452. continue;
  1453. }
  1454. /* Success */
  1455. prom_printf("done\n");
  1456. prom_setprop(node, path, "linux,opened", NULL, 0);
  1457. /* Setup a usable color table when the appropriate
  1458. * method is available. Should update this to set-colors */
  1459. clut = RELOC(default_colors);
  1460. for (i = 0; i < 32; i++, clut += 3)
  1461. if (prom_set_color(ih, i, clut[0], clut[1],
  1462. clut[2]) != 0)
  1463. break;
  1464. #ifdef CONFIG_LOGO_LINUX_CLUT224
  1465. clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
  1466. for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
  1467. if (prom_set_color(ih, i + 32, clut[0], clut[1],
  1468. clut[2]) != 0)
  1469. break;
  1470. #endif /* CONFIG_LOGO_LINUX_CLUT224 */
  1471. }
  1472. }
  1473. /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
  1474. static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
  1475. unsigned long needed, unsigned long align)
  1476. {
  1477. void *ret;
  1478. *mem_start = _ALIGN(*mem_start, align);
  1479. while ((*mem_start + needed) > *mem_end) {
  1480. unsigned long room, chunk;
  1481. prom_debug("Chunk exhausted, claiming more at %x...\n",
  1482. RELOC(alloc_bottom));
  1483. room = RELOC(alloc_top) - RELOC(alloc_bottom);
  1484. if (room > DEVTREE_CHUNK_SIZE)
  1485. room = DEVTREE_CHUNK_SIZE;
  1486. if (room < PAGE_SIZE)
  1487. prom_panic("No memory for flatten_device_tree (no room)");
  1488. chunk = alloc_up(room, 0);
  1489. if (chunk == 0)
  1490. prom_panic("No memory for flatten_device_tree (claim failed)");
  1491. *mem_end = RELOC(alloc_top);
  1492. }
  1493. ret = (void *)*mem_start;
  1494. *mem_start += needed;
  1495. return ret;
  1496. }
  1497. #define dt_push_token(token, mem_start, mem_end) \
  1498. do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
  1499. static unsigned long __init dt_find_string(char *str)
  1500. {
  1501. char *s, *os;
  1502. s = os = (char *)RELOC(dt_string_start);
  1503. s += 4;
  1504. while (s < (char *)RELOC(dt_string_end)) {
  1505. if (strcmp(s, str) == 0)
  1506. return s - os;
  1507. s += strlen(s) + 1;
  1508. }
  1509. return 0;
  1510. }
  1511. /*
  1512. * The Open Firmware 1275 specification states properties must be 31 bytes or
  1513. * less, however not all firmwares obey this. Make it 64 bytes to be safe.
  1514. */
  1515. #define MAX_PROPERTY_NAME 64
  1516. static void __init scan_dt_build_strings(phandle node,
  1517. unsigned long *mem_start,
  1518. unsigned long *mem_end)
  1519. {
  1520. char *prev_name, *namep, *sstart;
  1521. unsigned long soff;
  1522. phandle child;
  1523. sstart = (char *)RELOC(dt_string_start);
  1524. /* get and store all property names */
  1525. prev_name = RELOC("");
  1526. for (;;) {
  1527. /* 64 is max len of name including nul. */
  1528. namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
  1529. if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
  1530. /* No more nodes: unwind alloc */
  1531. *mem_start = (unsigned long)namep;
  1532. break;
  1533. }
  1534. /* skip "name" */
  1535. if (strcmp(namep, RELOC("name")) == 0) {
  1536. *mem_start = (unsigned long)namep;
  1537. prev_name = RELOC("name");
  1538. continue;
  1539. }
  1540. /* get/create string entry */
  1541. soff = dt_find_string(namep);
  1542. if (soff != 0) {
  1543. *mem_start = (unsigned long)namep;
  1544. namep = sstart + soff;
  1545. } else {
  1546. /* Trim off some if we can */
  1547. *mem_start = (unsigned long)namep + strlen(namep) + 1;
  1548. RELOC(dt_string_end) = *mem_start;
  1549. }
  1550. prev_name = namep;
  1551. }
  1552. /* do all our children */
  1553. child = call_prom("child", 1, 1, node);
  1554. while (child != 0) {
  1555. scan_dt_build_strings(child, mem_start, mem_end);
  1556. child = call_prom("peer", 1, 1, child);
  1557. }
  1558. }
  1559. static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
  1560. unsigned long *mem_end)
  1561. {
  1562. phandle child;
  1563. char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
  1564. unsigned long soff;
  1565. unsigned char *valp;
  1566. static char pname[MAX_PROPERTY_NAME];
  1567. int l, room;
  1568. dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
  1569. /* get the node's full name */
  1570. namep = (char *)*mem_start;
  1571. room = *mem_end - *mem_start;
  1572. if (room > 255)
  1573. room = 255;
  1574. l = call_prom("package-to-path", 3, 1, node, namep, room);
  1575. if (l >= 0) {
  1576. /* Didn't fit? Get more room. */
  1577. if (l >= room) {
  1578. if (l >= *mem_end - *mem_start)
  1579. namep = make_room(mem_start, mem_end, l+1, 1);
  1580. call_prom("package-to-path", 3, 1, node, namep, l);
  1581. }
  1582. namep[l] = '\0';
  1583. /* Fixup an Apple bug where they have bogus \0 chars in the
  1584. * middle of the path in some properties, and extract
  1585. * the unit name (everything after the last '/').
  1586. */
  1587. for (lp = p = namep, ep = namep + l; p < ep; p++) {
  1588. if (*p == '/')
  1589. lp = namep;
  1590. else if (*p != 0)
  1591. *lp++ = *p;
  1592. }
  1593. *lp = 0;
  1594. *mem_start = _ALIGN((unsigned long)lp + 1, 4);
  1595. }
  1596. /* get it again for debugging */
  1597. path = RELOC(prom_scratch);
  1598. memset(path, 0, PROM_SCRATCH_SIZE);
  1599. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  1600. /* get and store all properties */
  1601. prev_name = RELOC("");
  1602. sstart = (char *)RELOC(dt_string_start);
  1603. for (;;) {
  1604. if (call_prom("nextprop", 3, 1, node, prev_name,
  1605. RELOC(pname)) != 1)
  1606. break;
  1607. /* skip "name" */
  1608. if (strcmp(RELOC(pname), RELOC("name")) == 0) {
  1609. prev_name = RELOC("name");
  1610. continue;
  1611. }
  1612. /* find string offset */
  1613. soff = dt_find_string(RELOC(pname));
  1614. if (soff == 0) {
  1615. prom_printf("WARNING: Can't find string index for"
  1616. " <%s>, node %s\n", RELOC(pname), path);
  1617. break;
  1618. }
  1619. prev_name = sstart + soff;
  1620. /* get length */
  1621. l = call_prom("getproplen", 2, 1, node, RELOC(pname));
  1622. /* sanity checks */
  1623. if (l == PROM_ERROR)
  1624. continue;
  1625. if (l > MAX_PROPERTY_LENGTH) {
  1626. prom_printf("WARNING: ignoring large property ");
  1627. /* It seems OF doesn't null-terminate the path :-( */
  1628. prom_printf("[%s] ", path);
  1629. prom_printf("%s length 0x%x\n", RELOC(pname), l);
  1630. continue;
  1631. }
  1632. /* push property head */
  1633. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1634. dt_push_token(l, mem_start, mem_end);
  1635. dt_push_token(soff, mem_start, mem_end);
  1636. /* push property content */
  1637. valp = make_room(mem_start, mem_end, l, 4);
  1638. call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
  1639. *mem_start = _ALIGN(*mem_start, 4);
  1640. }
  1641. /* Add a "linux,phandle" property. */
  1642. soff = dt_find_string(RELOC("linux,phandle"));
  1643. if (soff == 0)
  1644. prom_printf("WARNING: Can't find string index for"
  1645. " <linux-phandle> node %s\n", path);
  1646. else {
  1647. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1648. dt_push_token(4, mem_start, mem_end);
  1649. dt_push_token(soff, mem_start, mem_end);
  1650. valp = make_room(mem_start, mem_end, 4, 4);
  1651. *(u32 *)valp = node;
  1652. }
  1653. /* do all our children */
  1654. child = call_prom("child", 1, 1, node);
  1655. while (child != 0) {
  1656. scan_dt_build_struct(child, mem_start, mem_end);
  1657. child = call_prom("peer", 1, 1, child);
  1658. }
  1659. dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
  1660. }
  1661. static void __init flatten_device_tree(void)
  1662. {
  1663. phandle root;
  1664. unsigned long mem_start, mem_end, room;
  1665. struct boot_param_header *hdr;
  1666. struct prom_t *_prom = &RELOC(prom);
  1667. char *namep;
  1668. u64 *rsvmap;
  1669. /*
  1670. * Check how much room we have between alloc top & bottom (+/- a
  1671. * few pages), crop to 4Mb, as this is our "chuck" size
  1672. */
  1673. room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
  1674. if (room > DEVTREE_CHUNK_SIZE)
  1675. room = DEVTREE_CHUNK_SIZE;
  1676. prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
  1677. /* Now try to claim that */
  1678. mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
  1679. if (mem_start == 0)
  1680. prom_panic("Can't allocate initial device-tree chunk\n");
  1681. mem_end = RELOC(alloc_top);
  1682. /* Get root of tree */
  1683. root = call_prom("peer", 1, 1, (phandle)0);
  1684. if (root == (phandle)0)
  1685. prom_panic ("couldn't get device tree root\n");
  1686. /* Build header and make room for mem rsv map */
  1687. mem_start = _ALIGN(mem_start, 4);
  1688. hdr = make_room(&mem_start, &mem_end,
  1689. sizeof(struct boot_param_header), 4);
  1690. RELOC(dt_header_start) = (unsigned long)hdr;
  1691. rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
  1692. /* Start of strings */
  1693. mem_start = PAGE_ALIGN(mem_start);
  1694. RELOC(dt_string_start) = mem_start;
  1695. mem_start += 4; /* hole */
  1696. /* Add "linux,phandle" in there, we'll need it */
  1697. namep = make_room(&mem_start, &mem_end, 16, 1);
  1698. strcpy(namep, RELOC("linux,phandle"));
  1699. mem_start = (unsigned long)namep + strlen(namep) + 1;
  1700. /* Build string array */
  1701. prom_printf("Building dt strings...\n");
  1702. scan_dt_build_strings(root, &mem_start, &mem_end);
  1703. RELOC(dt_string_end) = mem_start;
  1704. /* Build structure */
  1705. mem_start = PAGE_ALIGN(mem_start);
  1706. RELOC(dt_struct_start) = mem_start;
  1707. prom_printf("Building dt structure...\n");
  1708. scan_dt_build_struct(root, &mem_start, &mem_end);
  1709. dt_push_token(OF_DT_END, &mem_start, &mem_end);
  1710. RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
  1711. /* Finish header */
  1712. hdr->boot_cpuid_phys = _prom->cpu;
  1713. hdr->magic = OF_DT_HEADER;
  1714. hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
  1715. hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
  1716. hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
  1717. hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
  1718. hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
  1719. hdr->version = OF_DT_VERSION;
  1720. /* Version 16 is not backward compatible */
  1721. hdr->last_comp_version = 0x10;
  1722. /* Copy the reserve map in */
  1723. memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
  1724. #ifdef DEBUG_PROM
  1725. {
  1726. int i;
  1727. prom_printf("reserved memory map:\n");
  1728. for (i = 0; i < RELOC(mem_reserve_cnt); i++)
  1729. prom_printf(" %x - %x\n",
  1730. RELOC(mem_reserve_map)[i].base,
  1731. RELOC(mem_reserve_map)[i].size);
  1732. }
  1733. #endif
  1734. /* Bump mem_reserve_cnt to cause further reservations to fail
  1735. * since it's too late.
  1736. */
  1737. RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
  1738. prom_printf("Device tree strings 0x%x -> 0x%x\n",
  1739. RELOC(dt_string_start), RELOC(dt_string_end));
  1740. prom_printf("Device tree struct 0x%x -> 0x%x\n",
  1741. RELOC(dt_struct_start), RELOC(dt_struct_end));
  1742. }
  1743. #ifdef CONFIG_PPC_MAPLE
  1744. /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
  1745. * The values are bad, and it doesn't even have the right number of cells. */
  1746. static void __init fixup_device_tree_maple(void)
  1747. {
  1748. phandle isa;
  1749. u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
  1750. u32 isa_ranges[6];
  1751. char *name;
  1752. name = "/ht@0/isa@4";
  1753. isa = call_prom("finddevice", 1, 1, ADDR(name));
  1754. if (!PHANDLE_VALID(isa)) {
  1755. name = "/ht@0/isa@6";
  1756. isa = call_prom("finddevice", 1, 1, ADDR(name));
  1757. rloc = 0x01003000; /* IO space; PCI device = 6 */
  1758. }
  1759. if (!PHANDLE_VALID(isa))
  1760. return;
  1761. if (prom_getproplen(isa, "ranges") != 12)
  1762. return;
  1763. if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
  1764. == PROM_ERROR)
  1765. return;
  1766. if (isa_ranges[0] != 0x1 ||
  1767. isa_ranges[1] != 0xf4000000 ||
  1768. isa_ranges[2] != 0x00010000)
  1769. return;
  1770. prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
  1771. isa_ranges[0] = 0x1;
  1772. isa_ranges[1] = 0x0;
  1773. isa_ranges[2] = rloc;
  1774. isa_ranges[3] = 0x0;
  1775. isa_ranges[4] = 0x0;
  1776. isa_ranges[5] = 0x00010000;
  1777. prom_setprop(isa, name, "ranges",
  1778. isa_ranges, sizeof(isa_ranges));
  1779. }
  1780. #define CPC925_MC_START 0xf8000000
  1781. #define CPC925_MC_LENGTH 0x1000000
  1782. /* The values for memory-controller don't have right number of cells */
  1783. static void __init fixup_device_tree_maple_memory_controller(void)
  1784. {
  1785. phandle mc;
  1786. u32 mc_reg[4];
  1787. char *name = "/hostbridge@f8000000";
  1788. struct prom_t *_prom = &RELOC(prom);
  1789. u32 ac, sc;
  1790. mc = call_prom("finddevice", 1, 1, ADDR(name));
  1791. if (!PHANDLE_VALID(mc))
  1792. return;
  1793. if (prom_getproplen(mc, "reg") != 8)
  1794. return;
  1795. prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
  1796. prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
  1797. if ((ac != 2) || (sc != 2))
  1798. return;
  1799. if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
  1800. return;
  1801. if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
  1802. return;
  1803. prom_printf("Fixing up bogus hostbridge on Maple...\n");
  1804. mc_reg[0] = 0x0;
  1805. mc_reg[1] = CPC925_MC_START;
  1806. mc_reg[2] = 0x0;
  1807. mc_reg[3] = CPC925_MC_LENGTH;
  1808. prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
  1809. }
  1810. #else
  1811. #define fixup_device_tree_maple()
  1812. #define fixup_device_tree_maple_memory_controller()
  1813. #endif
  1814. #ifdef CONFIG_PPC_CHRP
  1815. /*
  1816. * Pegasos and BriQ lacks the "ranges" property in the isa node
  1817. * Pegasos needs decimal IRQ 14/15, not hexadecimal
  1818. * Pegasos has the IDE configured in legacy mode, but advertised as native
  1819. */
  1820. static void __init fixup_device_tree_chrp(void)
  1821. {
  1822. phandle ph;
  1823. u32 prop[6];
  1824. u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
  1825. char *name;
  1826. int rc;
  1827. name = "/pci@80000000/isa@c";
  1828. ph = call_prom("finddevice", 1, 1, ADDR(name));
  1829. if (!PHANDLE_VALID(ph)) {
  1830. name = "/pci@ff500000/isa@6";
  1831. ph = call_prom("finddevice", 1, 1, ADDR(name));
  1832. rloc = 0x01003000; /* IO space; PCI device = 6 */
  1833. }
  1834. if (PHANDLE_VALID(ph)) {
  1835. rc = prom_getproplen(ph, "ranges");
  1836. if (rc == 0 || rc == PROM_ERROR) {
  1837. prom_printf("Fixing up missing ISA range on Pegasos...\n");
  1838. prop[0] = 0x1;
  1839. prop[1] = 0x0;
  1840. prop[2] = rloc;
  1841. prop[3] = 0x0;
  1842. prop[4] = 0x0;
  1843. prop[5] = 0x00010000;
  1844. prom_setprop(ph, name, "ranges", prop, sizeof(prop));
  1845. }
  1846. }
  1847. name = "/pci@80000000/ide@C,1";
  1848. ph = call_prom("finddevice", 1, 1, ADDR(name));
  1849. if (PHANDLE_VALID(ph)) {
  1850. prom_printf("Fixing up IDE interrupt on Pegasos...\n");
  1851. prop[0] = 14;
  1852. prop[1] = 0x0;
  1853. prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
  1854. prom_printf("Fixing up IDE class-code on Pegasos...\n");
  1855. rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
  1856. if (rc == sizeof(u32)) {
  1857. prop[0] &= ~0x5;
  1858. prom_setprop(ph, name, "class-code", prop, sizeof(u32));
  1859. }
  1860. }
  1861. }
  1862. #else
  1863. #define fixup_device_tree_chrp()
  1864. #endif
  1865. #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
  1866. static void __init fixup_device_tree_pmac(void)
  1867. {
  1868. phandle u3, i2c, mpic;
  1869. u32 u3_rev;
  1870. u32 interrupts[2];
  1871. u32 parent;
  1872. /* Some G5s have a missing interrupt definition, fix it up here */
  1873. u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
  1874. if (!PHANDLE_VALID(u3))
  1875. return;
  1876. i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
  1877. if (!PHANDLE_VALID(i2c))
  1878. return;
  1879. mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
  1880. if (!PHANDLE_VALID(mpic))
  1881. return;
  1882. /* check if proper rev of u3 */
  1883. if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
  1884. == PROM_ERROR)
  1885. return;
  1886. if (u3_rev < 0x35 || u3_rev > 0x39)
  1887. return;
  1888. /* does it need fixup ? */
  1889. if (prom_getproplen(i2c, "interrupts") > 0)
  1890. return;
  1891. prom_printf("fixing up bogus interrupts for u3 i2c...\n");
  1892. /* interrupt on this revision of u3 is number 0 and level */
  1893. interrupts[0] = 0;
  1894. interrupts[1] = 1;
  1895. prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
  1896. &interrupts, sizeof(interrupts));
  1897. parent = (u32)mpic;
  1898. prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
  1899. &parent, sizeof(parent));
  1900. }
  1901. #else
  1902. #define fixup_device_tree_pmac()
  1903. #endif
  1904. #ifdef CONFIG_PPC_EFIKA
  1905. /*
  1906. * The MPC5200 FEC driver requires an phy-handle property to tell it how
  1907. * to talk to the phy. If the phy-handle property is missing, then this
  1908. * function is called to add the appropriate nodes and link it to the
  1909. * ethernet node.
  1910. */
  1911. static void __init fixup_device_tree_efika_add_phy(void)
  1912. {
  1913. u32 node;
  1914. char prop[64];
  1915. int rv;
  1916. /* Check if /builtin/ethernet exists - bail if it doesn't */
  1917. node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
  1918. if (!PHANDLE_VALID(node))
  1919. return;
  1920. /* Check if the phy-handle property exists - bail if it does */
  1921. rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
  1922. if (!rv)
  1923. return;
  1924. /*
  1925. * At this point the ethernet device doesn't have a phy described.
  1926. * Now we need to add the missing phy node and linkage
  1927. */
  1928. /* Check for an MDIO bus node - if missing then create one */
  1929. node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
  1930. if (!PHANDLE_VALID(node)) {
  1931. prom_printf("Adding Ethernet MDIO node\n");
  1932. call_prom("interpret", 1, 1,
  1933. " s\" /builtin\" find-device"
  1934. " new-device"
  1935. " 1 encode-int s\" #address-cells\" property"
  1936. " 0 encode-int s\" #size-cells\" property"
  1937. " s\" mdio\" device-name"
  1938. " s\" fsl,mpc5200b-mdio\" encode-string"
  1939. " s\" compatible\" property"
  1940. " 0xf0003000 0x400 reg"
  1941. " 0x2 encode-int"
  1942. " 0x5 encode-int encode+"
  1943. " 0x3 encode-int encode+"
  1944. " s\" interrupts\" property"
  1945. " finish-device");
  1946. };
  1947. /* Check for a PHY device node - if missing then create one and
  1948. * give it's phandle to the ethernet node */
  1949. node = call_prom("finddevice", 1, 1,
  1950. ADDR("/builtin/mdio/ethernet-phy"));
  1951. if (!PHANDLE_VALID(node)) {
  1952. prom_printf("Adding Ethernet PHY node\n");
  1953. call_prom("interpret", 1, 1,
  1954. " s\" /builtin/mdio\" find-device"
  1955. " new-device"
  1956. " s\" ethernet-phy\" device-name"
  1957. " 0x10 encode-int s\" reg\" property"
  1958. " my-self"
  1959. " ihandle>phandle"
  1960. " finish-device"
  1961. " s\" /builtin/ethernet\" find-device"
  1962. " encode-int"
  1963. " s\" phy-handle\" property"
  1964. " device-end");
  1965. }
  1966. }
  1967. static void __init fixup_device_tree_efika(void)
  1968. {
  1969. int sound_irq[3] = { 2, 2, 0 };
  1970. int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
  1971. 3,4,0, 3,5,0, 3,6,0, 3,7,0,
  1972. 3,8,0, 3,9,0, 3,10,0, 3,11,0,
  1973. 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
  1974. u32 node;
  1975. char prop[64];
  1976. int rv, len;
  1977. /* Check if we're really running on a EFIKA */
  1978. node = call_prom("finddevice", 1, 1, ADDR("/"));
  1979. if (!PHANDLE_VALID(node))
  1980. return;
  1981. rv = prom_getprop(node, "model", prop, sizeof(prop));
  1982. if (rv == PROM_ERROR)
  1983. return;
  1984. if (strcmp(prop, "EFIKA5K2"))
  1985. return;
  1986. prom_printf("Applying EFIKA device tree fixups\n");
  1987. /* Claiming to be 'chrp' is death */
  1988. node = call_prom("finddevice", 1, 1, ADDR("/"));
  1989. rv = prom_getprop(node, "device_type", prop, sizeof(prop));
  1990. if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
  1991. prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
  1992. /* CODEGEN,description is exposed in /proc/cpuinfo so
  1993. fix that too */
  1994. rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
  1995. if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
  1996. prom_setprop(node, "/", "CODEGEN,description",
  1997. "Efika 5200B PowerPC System",
  1998. sizeof("Efika 5200B PowerPC System"));
  1999. /* Fixup bestcomm interrupts property */
  2000. node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
  2001. if (PHANDLE_VALID(node)) {
  2002. len = prom_getproplen(node, "interrupts");
  2003. if (len == 12) {
  2004. prom_printf("Fixing bestcomm interrupts property\n");
  2005. prom_setprop(node, "/builtin/bestcom", "interrupts",
  2006. bcomm_irq, sizeof(bcomm_irq));
  2007. }
  2008. }
  2009. /* Fixup sound interrupts property */
  2010. node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
  2011. if (PHANDLE_VALID(node)) {
  2012. rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
  2013. if (rv == PROM_ERROR) {
  2014. prom_printf("Adding sound interrupts property\n");
  2015. prom_setprop(node, "/builtin/sound", "interrupts",
  2016. sound_irq, sizeof(sound_irq));
  2017. }
  2018. }
  2019. /* Make sure ethernet phy-handle property exists */
  2020. fixup_device_tree_efika_add_phy();
  2021. }
  2022. #else
  2023. #define fixup_device_tree_efika()
  2024. #endif
  2025. static void __init fixup_device_tree(void)
  2026. {
  2027. fixup_device_tree_maple();
  2028. fixup_device_tree_maple_memory_controller();
  2029. fixup_device_tree_chrp();
  2030. fixup_device_tree_pmac();
  2031. fixup_device_tree_efika();
  2032. }
  2033. static void __init prom_find_boot_cpu(void)
  2034. {
  2035. struct prom_t *_prom = &RELOC(prom);
  2036. u32 getprop_rval;
  2037. ihandle prom_cpu;
  2038. phandle cpu_pkg;
  2039. _prom->cpu = 0;
  2040. if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
  2041. return;
  2042. cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
  2043. prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
  2044. _prom->cpu = getprop_rval;
  2045. prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
  2046. }
  2047. static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
  2048. {
  2049. #ifdef CONFIG_BLK_DEV_INITRD
  2050. struct prom_t *_prom = &RELOC(prom);
  2051. if (r3 && r4 && r4 != 0xdeadbeef) {
  2052. unsigned long val;
  2053. RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
  2054. RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
  2055. val = RELOC(prom_initrd_start);
  2056. prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
  2057. &val, sizeof(val));
  2058. val = RELOC(prom_initrd_end);
  2059. prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
  2060. &val, sizeof(val));
  2061. reserve_mem(RELOC(prom_initrd_start),
  2062. RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
  2063. prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
  2064. prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
  2065. }
  2066. #endif /* CONFIG_BLK_DEV_INITRD */
  2067. }
  2068. /*
  2069. * We enter here early on, when the Open Firmware prom is still
  2070. * handling exceptions and the MMU hash table for us.
  2071. */
  2072. unsigned long __init prom_init(unsigned long r3, unsigned long r4,
  2073. unsigned long pp,
  2074. unsigned long r6, unsigned long r7,
  2075. unsigned long kbase)
  2076. {
  2077. struct prom_t *_prom;
  2078. unsigned long hdr;
  2079. #ifdef CONFIG_PPC32
  2080. unsigned long offset = reloc_offset();
  2081. reloc_got2(offset);
  2082. #endif
  2083. _prom = &RELOC(prom);
  2084. /*
  2085. * First zero the BSS
  2086. */
  2087. memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
  2088. /*
  2089. * Init interface to Open Firmware, get some node references,
  2090. * like /chosen
  2091. */
  2092. prom_init_client_services(pp);
  2093. /*
  2094. * See if this OF is old enough that we need to do explicit maps
  2095. * and other workarounds
  2096. */
  2097. prom_find_mmu();
  2098. /*
  2099. * Init prom stdout device
  2100. */
  2101. prom_init_stdout();
  2102. prom_printf("Preparing to boot %s", RELOC(linux_banner));
  2103. /*
  2104. * Get default machine type. At this point, we do not differentiate
  2105. * between pSeries SMP and pSeries LPAR
  2106. */
  2107. RELOC(of_platform) = prom_find_machine_type();
  2108. #ifndef CONFIG_RELOCATABLE
  2109. /* Bail if this is a kdump kernel. */
  2110. if (PHYSICAL_START > 0)
  2111. prom_panic("Error: You can't boot a kdump kernel from OF!\n");
  2112. #endif
  2113. /*
  2114. * Check for an initrd
  2115. */
  2116. prom_check_initrd(r3, r4);
  2117. #ifdef CONFIG_PPC_PSERIES
  2118. /*
  2119. * On pSeries, inform the firmware about our capabilities
  2120. */
  2121. if (RELOC(of_platform) == PLATFORM_PSERIES ||
  2122. RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
  2123. prom_send_capabilities();
  2124. #endif
  2125. /*
  2126. * Copy the CPU hold code
  2127. */
  2128. if (RELOC(of_platform) != PLATFORM_POWERMAC)
  2129. copy_and_flush(0, kbase, 0x100, 0);
  2130. /*
  2131. * Do early parsing of command line
  2132. */
  2133. early_cmdline_parse();
  2134. /*
  2135. * Initialize memory management within prom_init
  2136. */
  2137. prom_init_mem();
  2138. /*
  2139. * Determine which cpu is actually running right _now_
  2140. */
  2141. prom_find_boot_cpu();
  2142. /*
  2143. * Initialize display devices
  2144. */
  2145. prom_check_displays();
  2146. #ifdef CONFIG_PPC64
  2147. /*
  2148. * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
  2149. * that uses the allocator, we need to make sure we get the top of memory
  2150. * available for us here...
  2151. */
  2152. if (RELOC(of_platform) == PLATFORM_PSERIES)
  2153. prom_initialize_tce_table();
  2154. #endif
  2155. /*
  2156. * On non-powermacs, try to instantiate RTAS and puts all CPUs
  2157. * in spin-loops. PowerMacs don't have a working RTAS and use
  2158. * a different way to spin CPUs
  2159. */
  2160. if (RELOC(of_platform) != PLATFORM_POWERMAC) {
  2161. prom_instantiate_rtas();
  2162. prom_hold_cpus();
  2163. }
  2164. /*
  2165. * Fill in some infos for use by the kernel later on
  2166. */
  2167. if (RELOC(prom_memory_limit))
  2168. prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit",
  2169. &RELOC(prom_memory_limit),
  2170. sizeof(prom_memory_limit));
  2171. #ifdef CONFIG_PPC64
  2172. if (RELOC(prom_iommu_off))
  2173. prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
  2174. NULL, 0);
  2175. if (RELOC(prom_iommu_force_on))
  2176. prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
  2177. NULL, 0);
  2178. if (RELOC(prom_tce_alloc_start)) {
  2179. prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
  2180. &RELOC(prom_tce_alloc_start),
  2181. sizeof(prom_tce_alloc_start));
  2182. prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
  2183. &RELOC(prom_tce_alloc_end),
  2184. sizeof(prom_tce_alloc_end));
  2185. }
  2186. #endif
  2187. /*
  2188. * Fixup any known bugs in the device-tree
  2189. */
  2190. fixup_device_tree();
  2191. /*
  2192. * Now finally create the flattened device-tree
  2193. */
  2194. prom_printf("copying OF device tree...\n");
  2195. flatten_device_tree();
  2196. /*
  2197. * in case stdin is USB and still active on IBM machines...
  2198. * Unfortunately quiesce crashes on some powermacs if we have
  2199. * closed stdin already (in particular the powerbook 101).
  2200. */
  2201. if (RELOC(of_platform) != PLATFORM_POWERMAC)
  2202. prom_close_stdin();
  2203. /*
  2204. * Call OF "quiesce" method to shut down pending DMA's from
  2205. * devices etc...
  2206. */
  2207. prom_printf("Calling quiesce...\n");
  2208. call_prom("quiesce", 0, 0);
  2209. /*
  2210. * And finally, call the kernel passing it the flattened device
  2211. * tree and NULL as r5, thus triggering the new entry point which
  2212. * is common to us and kexec
  2213. */
  2214. hdr = RELOC(dt_header_start);
  2215. prom_printf("returning from prom_init\n");
  2216. prom_debug("->dt_header_start=0x%x\n", hdr);
  2217. #ifdef CONFIG_PPC32
  2218. reloc_got2(-offset);
  2219. #endif
  2220. __start(hdr, kbase, 0);
  2221. return 0;
  2222. }