prom_init.c 83 KB

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