prom_init.c 56 KB

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