prom_init.c 69 KB

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