prom_init.c 61 KB

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