prom_init.c 63 KB

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