prom_init.c 62 KB

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