prom_init.c 68 KB

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