prom_init.c 67 KB

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