prom_init.c 81 KB

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