prom_init.c 72 KB

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