prom_init.c 55 KB

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