prom_init.c 58 KB

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