prom_init.c 81 KB

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