prom_init.c 55 KB

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