prom_init.c 62 KB

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