prom_init.c 59 KB

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