prom_init.c 54 KB

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