prom_init.c 71 KB

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