prom_init.c 58 KB

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