prom_init.c 72 KB

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