prom_init.c 63 KB

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