prom_init.c 81 KB

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