prom_init.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. /*
  2. * Procedures for interfacing to Open Firmware.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #undef DEBUG_PROM
  16. #include <stdarg.h>
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/init.h>
  20. #include <linux/threads.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/types.h>
  23. #include <linux/pci.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/stringify.h>
  26. #include <linux/delay.h>
  27. #include <linux/initrd.h>
  28. #include <linux/bitops.h>
  29. #include <asm/prom.h>
  30. #include <asm/rtas.h>
  31. #include <asm/page.h>
  32. #include <asm/processor.h>
  33. #include <asm/irq.h>
  34. #include <asm/io.h>
  35. #include <asm/smp.h>
  36. #include <asm/system.h>
  37. #include <asm/mmu.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/pci.h>
  40. #include <asm/iommu.h>
  41. #include <asm/btext.h>
  42. #include <asm/sections.h>
  43. #include <asm/machdep.h>
  44. #ifdef CONFIG_LOGO_LINUX_CLUT224
  45. #include <linux/linux_logo.h>
  46. extern const struct linux_logo logo_linux_clut224;
  47. #endif
  48. /*
  49. * Properties whose value is longer than this get excluded from our
  50. * copy of the device tree. This value does need to be big enough to
  51. * ensure that we don't lose things like the interrupt-map property
  52. * on a PCI-PCI bridge.
  53. */
  54. #define MAX_PROPERTY_LENGTH (1UL * 1024 * 1024)
  55. /*
  56. * Eventually bump that one up
  57. */
  58. #define DEVTREE_CHUNK_SIZE 0x100000
  59. /*
  60. * This is the size of the local memory reserve map that gets copied
  61. * into the boot params passed to the kernel. That size is totally
  62. * flexible as the kernel just reads the list until it encounters an
  63. * entry with size 0, so it can be changed without breaking binary
  64. * compatibility
  65. */
  66. #define MEM_RESERVE_MAP_SIZE 8
  67. /*
  68. * prom_init() is called very early on, before the kernel text
  69. * and data have been mapped to KERNELBASE. At this point the code
  70. * is running at whatever address it has been loaded at.
  71. * On ppc32 we compile with -mrelocatable, which means that references
  72. * to extern and static variables get relocated automatically.
  73. * On ppc64 we have to relocate the references explicitly with
  74. * RELOC. (Note that strings count as static variables.)
  75. *
  76. * Because OF may have mapped I/O devices into the area starting at
  77. * KERNELBASE, particularly on CHRP machines, we can't safely call
  78. * OF once the kernel has been mapped to KERNELBASE. Therefore all
  79. * OF calls must be done within prom_init().
  80. *
  81. * ADDR is used in calls to call_prom. The 4th and following
  82. * arguments to call_prom should be 32-bit values.
  83. * On ppc64, 64 bit values are truncated to 32 bits (and
  84. * fortunately don't get interpreted as two arguments).
  85. */
  86. #ifdef CONFIG_PPC64
  87. #define RELOC(x) (*PTRRELOC(&(x)))
  88. #define ADDR(x) (u32) add_reloc_offset((unsigned long)(x))
  89. #define OF_WORKAROUNDS 0
  90. #else
  91. #define RELOC(x) (x)
  92. #define ADDR(x) (u32) (x)
  93. #define OF_WORKAROUNDS of_workarounds
  94. int of_workarounds;
  95. #endif
  96. #define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */
  97. #define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */
  98. #define PROM_BUG() do { \
  99. prom_printf("kernel BUG at %s line 0x%x!\n", \
  100. RELOC(__FILE__), __LINE__); \
  101. __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \
  102. } while (0)
  103. #ifdef DEBUG_PROM
  104. #define prom_debug(x...) prom_printf(x)
  105. #else
  106. #define prom_debug(x...)
  107. #endif
  108. typedef u32 prom_arg_t;
  109. struct prom_args {
  110. u32 service;
  111. u32 nargs;
  112. u32 nret;
  113. prom_arg_t args[10];
  114. };
  115. struct prom_t {
  116. ihandle root;
  117. phandle chosen;
  118. int cpu;
  119. ihandle stdout;
  120. ihandle mmumap;
  121. ihandle memory;
  122. };
  123. struct mem_map_entry {
  124. u64 base;
  125. u64 size;
  126. };
  127. typedef u32 cell_t;
  128. extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
  129. #ifdef CONFIG_PPC64
  130. extern int enter_prom(struct prom_args *args, unsigned long entry);
  131. #else
  132. static inline int enter_prom(struct prom_args *args, unsigned long entry)
  133. {
  134. return ((int (*)(struct prom_args *))entry)(args);
  135. }
  136. #endif
  137. extern void copy_and_flush(unsigned long dest, unsigned long src,
  138. unsigned long size, unsigned long offset);
  139. /* prom structure */
  140. static struct prom_t __initdata prom;
  141. static unsigned long prom_entry __initdata;
  142. #define PROM_SCRATCH_SIZE 256
  143. static char __initdata of_stdout_device[256];
  144. static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
  145. static unsigned long __initdata dt_header_start;
  146. static unsigned long __initdata dt_struct_start, dt_struct_end;
  147. static unsigned long __initdata dt_string_start, dt_string_end;
  148. static unsigned long __initdata prom_initrd_start, prom_initrd_end;
  149. #ifdef CONFIG_PPC64
  150. static int __initdata prom_iommu_force_on;
  151. static int __initdata prom_iommu_off;
  152. static unsigned long __initdata prom_tce_alloc_start;
  153. static unsigned long __initdata prom_tce_alloc_end;
  154. #endif
  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 __initdata of_platform;
  165. static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
  166. static unsigned long __initdata alloc_top;
  167. static unsigned long __initdata alloc_top_high;
  168. static unsigned long __initdata alloc_bottom;
  169. static unsigned long __initdata rmo_top;
  170. static unsigned long __initdata ram_top;
  171. static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
  172. static int __initdata mem_reserve_cnt;
  173. static cell_t __initdata regbuf[1024];
  174. #define MAX_CPU_THREADS 2
  175. /*
  176. * Error results ... some OF calls will return "-1" on error, some
  177. * will return 0, some will return either. To simplify, here are
  178. * macros to use with any ihandle or phandle return value to check if
  179. * it is valid
  180. */
  181. #define PROM_ERROR (-1u)
  182. #define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
  183. #define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
  184. /* This is the one and *ONLY* place where we actually call open
  185. * firmware.
  186. */
  187. static int __init call_prom(const char *service, int nargs, int nret, ...)
  188. {
  189. int i;
  190. struct prom_args args;
  191. va_list list;
  192. args.service = ADDR(service);
  193. args.nargs = nargs;
  194. args.nret = nret;
  195. va_start(list, nret);
  196. for (i = 0; i < nargs; i++)
  197. args.args[i] = va_arg(list, prom_arg_t);
  198. va_end(list);
  199. for (i = 0; i < nret; i++)
  200. args.args[nargs+i] = 0;
  201. if (enter_prom(&args, RELOC(prom_entry)) < 0)
  202. return PROM_ERROR;
  203. return (nret > 0) ? args.args[nargs] : 0;
  204. }
  205. static int __init call_prom_ret(const char *service, int nargs, int nret,
  206. prom_arg_t *rets, ...)
  207. {
  208. int i;
  209. struct prom_args args;
  210. va_list list;
  211. args.service = ADDR(service);
  212. args.nargs = nargs;
  213. args.nret = nret;
  214. va_start(list, rets);
  215. for (i = 0; i < nargs; i++)
  216. args.args[i] = va_arg(list, prom_arg_t);
  217. va_end(list);
  218. for (i = 0; i < nret; i++)
  219. args.args[nargs+i] = 0;
  220. if (enter_prom(&args, RELOC(prom_entry)) < 0)
  221. return PROM_ERROR;
  222. if (rets != NULL)
  223. for (i = 1; i < nret; ++i)
  224. rets[i-1] = args.args[nargs+i];
  225. return (nret > 0) ? args.args[nargs] : 0;
  226. }
  227. static void __init prom_print(const char *msg)
  228. {
  229. const char *p, *q;
  230. struct prom_t *_prom = &RELOC(prom);
  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. static void __init prom_print_hex(unsigned long val)
  245. {
  246. int i, nibbles = sizeof(val)*2;
  247. char buf[sizeof(val)*2+1];
  248. struct prom_t *_prom = &RELOC(prom);
  249. for (i = nibbles-1; i >= 0; i--) {
  250. buf[i] = (val & 0xf) + '0';
  251. if (buf[i] > '9')
  252. buf[i] += ('a'-'0'-10);
  253. val >>= 4;
  254. }
  255. buf[nibbles] = '\0';
  256. call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
  257. }
  258. static void __init prom_printf(const char *format, ...)
  259. {
  260. const char *p, *q, *s;
  261. va_list args;
  262. unsigned long v;
  263. struct prom_t *_prom = &RELOC(prom);
  264. va_start(args, format);
  265. #ifdef CONFIG_PPC64
  266. format = PTRRELOC(format);
  267. #endif
  268. for (p = format; *p != 0; p = q) {
  269. for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
  270. ;
  271. if (q > p)
  272. call_prom("write", 3, 1, _prom->stdout, p, q - p);
  273. if (*q == 0)
  274. break;
  275. if (*q == '\n') {
  276. ++q;
  277. call_prom("write", 3, 1, _prom->stdout,
  278. ADDR("\r\n"), 2);
  279. continue;
  280. }
  281. ++q;
  282. if (*q == 0)
  283. break;
  284. switch (*q) {
  285. case 's':
  286. ++q;
  287. s = va_arg(args, const char *);
  288. prom_print(s);
  289. break;
  290. case 'x':
  291. ++q;
  292. v = va_arg(args, unsigned long);
  293. prom_print_hex(v);
  294. break;
  295. }
  296. }
  297. }
  298. static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
  299. unsigned long align)
  300. {
  301. struct prom_t *_prom = &RELOC(prom);
  302. if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
  303. /*
  304. * Old OF requires we claim physical and virtual separately
  305. * and then map explicitly (assuming virtual mode)
  306. */
  307. int ret;
  308. prom_arg_t result;
  309. ret = call_prom_ret("call-method", 5, 2, &result,
  310. ADDR("claim"), _prom->memory,
  311. align, size, virt);
  312. if (ret != 0 || result == -1)
  313. return -1;
  314. ret = call_prom_ret("call-method", 5, 2, &result,
  315. ADDR("claim"), _prom->mmumap,
  316. align, size, virt);
  317. if (ret != 0) {
  318. call_prom("call-method", 4, 1, ADDR("release"),
  319. _prom->memory, size, virt);
  320. return -1;
  321. }
  322. /* the 0x12 is M (coherence) + PP == read/write */
  323. call_prom("call-method", 6, 1,
  324. ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
  325. return virt;
  326. }
  327. return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
  328. (prom_arg_t)align);
  329. }
  330. static void __init __attribute__((noreturn)) prom_panic(const char *reason)
  331. {
  332. #ifdef CONFIG_PPC64
  333. reason = PTRRELOC(reason);
  334. #endif
  335. prom_print(reason);
  336. /* Do not call exit because it clears the screen on pmac
  337. * it also causes some sort of double-fault on early pmacs */
  338. if (RELOC(of_platform) == PLATFORM_POWERMAC)
  339. asm("trap\n");
  340. /* ToDo: should put up an SRC here on p/iSeries */
  341. call_prom("exit", 0, 0);
  342. for (;;) /* should never get here */
  343. ;
  344. }
  345. static int __init prom_next_node(phandle *nodep)
  346. {
  347. phandle node;
  348. if ((node = *nodep) != 0
  349. && (*nodep = call_prom("child", 1, 1, node)) != 0)
  350. return 1;
  351. if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
  352. return 1;
  353. for (;;) {
  354. if ((node = call_prom("parent", 1, 1, node)) == 0)
  355. return 0;
  356. if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
  357. return 1;
  358. }
  359. }
  360. static int inline prom_getprop(phandle node, const char *pname,
  361. void *value, size_t valuelen)
  362. {
  363. return call_prom("getprop", 4, 1, node, ADDR(pname),
  364. (u32)(unsigned long) value, (u32) valuelen);
  365. }
  366. static int inline prom_getproplen(phandle node, const char *pname)
  367. {
  368. return call_prom("getproplen", 2, 1, node, ADDR(pname));
  369. }
  370. static void add_string(char **str, const char *q)
  371. {
  372. char *p = *str;
  373. while (*q)
  374. *p++ = *q++;
  375. *p++ = ' ';
  376. *str = p;
  377. }
  378. static char *tohex(unsigned int x)
  379. {
  380. static char digits[] = "0123456789abcdef";
  381. static char result[9];
  382. int i;
  383. result[8] = 0;
  384. i = 8;
  385. do {
  386. --i;
  387. result[i] = digits[x & 0xf];
  388. x >>= 4;
  389. } while (x != 0 && i > 0);
  390. return &result[i];
  391. }
  392. static int __init prom_setprop(phandle node, const char *nodename,
  393. const char *pname, void *value, size_t valuelen)
  394. {
  395. char cmd[256], *p;
  396. if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
  397. return call_prom("setprop", 4, 1, node, ADDR(pname),
  398. (u32)(unsigned long) value, (u32) valuelen);
  399. /* gah... setprop doesn't work on longtrail, have to use interpret */
  400. p = cmd;
  401. add_string(&p, "dev");
  402. add_string(&p, nodename);
  403. add_string(&p, tohex((u32)(unsigned long) value));
  404. add_string(&p, tohex(valuelen));
  405. add_string(&p, tohex(ADDR(pname)));
  406. add_string(&p, tohex(strlen(RELOC(pname))));
  407. add_string(&p, "property");
  408. *p = 0;
  409. return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
  410. }
  411. /* We can't use the standard versions because of RELOC headaches. */
  412. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  413. || ('a' <= (c) && (c) <= 'f') \
  414. || ('A' <= (c) && (c) <= 'F'))
  415. #define isdigit(c) ('0' <= (c) && (c) <= '9')
  416. #define islower(c) ('a' <= (c) && (c) <= 'z')
  417. #define toupper(c) (islower(c) ? ((c) - 'a' + 'A') : (c))
  418. unsigned long prom_strtoul(const char *cp, const char **endp)
  419. {
  420. unsigned long result = 0, base = 10, value;
  421. if (*cp == '0') {
  422. base = 8;
  423. cp++;
  424. if (toupper(*cp) == 'X') {
  425. cp++;
  426. base = 16;
  427. }
  428. }
  429. while (isxdigit(*cp) &&
  430. (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
  431. result = result * base + value;
  432. cp++;
  433. }
  434. if (endp)
  435. *endp = cp;
  436. return result;
  437. }
  438. unsigned long prom_memparse(const char *ptr, const char **retptr)
  439. {
  440. unsigned long ret = prom_strtoul(ptr, retptr);
  441. int shift = 0;
  442. /*
  443. * We can't use a switch here because GCC *may* generate a
  444. * jump table which won't work, because we're not running at
  445. * the address we're linked at.
  446. */
  447. if ('G' == **retptr || 'g' == **retptr)
  448. shift = 30;
  449. if ('M' == **retptr || 'm' == **retptr)
  450. shift = 20;
  451. if ('K' == **retptr || 'k' == **retptr)
  452. shift = 10;
  453. if (shift) {
  454. ret <<= shift;
  455. (*retptr)++;
  456. }
  457. return ret;
  458. }
  459. /*
  460. * Early parsing of the command line passed to the kernel, used for
  461. * "mem=x" and the options that affect the iommu
  462. */
  463. static void __init early_cmdline_parse(void)
  464. {
  465. struct prom_t *_prom = &RELOC(prom);
  466. #ifdef CONFIG_PPC64
  467. const char *opt;
  468. #endif
  469. char *p;
  470. int l = 0;
  471. RELOC(prom_cmd_line[0]) = 0;
  472. p = RELOC(prom_cmd_line);
  473. if ((long)_prom->chosen > 0)
  474. l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
  475. #ifdef CONFIG_CMDLINE
  476. if (l <= 0 || p[0] == '\0') /* dbl check */
  477. strlcpy(RELOC(prom_cmd_line),
  478. RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
  479. #endif /* CONFIG_CMDLINE */
  480. prom_printf("command line: %s\n", RELOC(prom_cmd_line));
  481. #ifdef CONFIG_PPC64
  482. opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
  483. if (opt) {
  484. prom_printf("iommu opt is: %s\n", opt);
  485. opt += 6;
  486. while (*opt && *opt == ' ')
  487. opt++;
  488. if (!strncmp(opt, RELOC("off"), 3))
  489. RELOC(prom_iommu_off) = 1;
  490. else if (!strncmp(opt, RELOC("force"), 5))
  491. RELOC(prom_iommu_force_on) = 1;
  492. }
  493. #endif
  494. }
  495. #ifdef CONFIG_PPC_PSERIES
  496. /*
  497. * There are two methods for telling firmware what our capabilities are.
  498. * Newer machines have an "ibm,client-architecture-support" method on the
  499. * root node. For older machines, we have to call the "process-elf-header"
  500. * method in the /packages/elf-loader node, passing it a fake 32-bit
  501. * ELF header containing a couple of PT_NOTE sections that contain
  502. * structures that contain various information.
  503. */
  504. /*
  505. * New method - extensible architecture description vector.
  506. *
  507. * Because the description vector contains a mix of byte and word
  508. * values, we declare it as an unsigned char array, and use this
  509. * macro to put word values in.
  510. */
  511. #define W(x) ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
  512. ((x) >> 8) & 0xff, (x) & 0xff
  513. /* Option vector bits - generic bits in byte 1 */
  514. #define OV_IGNORE 0x80 /* ignore this vector */
  515. #define OV_CESSATION_POLICY 0x40 /* halt if unsupported option present*/
  516. /* Option vector 1: processor architectures supported */
  517. #define OV1_PPC_2_00 0x80 /* set if we support PowerPC 2.00 */
  518. #define OV1_PPC_2_01 0x40 /* set if we support PowerPC 2.01 */
  519. #define OV1_PPC_2_02 0x20 /* set if we support PowerPC 2.02 */
  520. #define OV1_PPC_2_03 0x10 /* set if we support PowerPC 2.03 */
  521. #define OV1_PPC_2_04 0x08 /* set if we support PowerPC 2.04 */
  522. #define OV1_PPC_2_05 0x04 /* set if we support PowerPC 2.05 */
  523. /* Option vector 2: Open Firmware options supported */
  524. #define OV2_REAL_MODE 0x20 /* set if we want OF in real mode */
  525. /* Option vector 3: processor options supported */
  526. #define OV3_FP 0x80 /* floating point */
  527. #define OV3_VMX 0x40 /* VMX/Altivec */
  528. #define OV3_DFP 0x20 /* decimal FP */
  529. /* Option vector 5: PAPR/OF options supported */
  530. #define OV5_LPAR 0x80 /* logical partitioning supported */
  531. #define OV5_SPLPAR 0x40 /* shared-processor LPAR supported */
  532. /* ibm,dynamic-reconfiguration-memory property supported */
  533. #define OV5_DRCONF_MEMORY 0x20
  534. #define OV5_LARGE_PAGES 0x10 /* large pages supported */
  535. /*
  536. * The architecture vector has an array of PVR mask/value pairs,
  537. * followed by # option vectors - 1, followed by the option vectors.
  538. */
  539. static unsigned char ibm_architecture_vec[] = {
  540. W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */
  541. W(0xffff0000), W(0x003e0000), /* POWER6 */
  542. W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */
  543. W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */
  544. 5 - 1, /* 5 option vectors */
  545. /* option vector 1: processor architectures supported */
  546. 3 - 2, /* length */
  547. 0, /* don't ignore, don't halt */
  548. OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
  549. OV1_PPC_2_04 | OV1_PPC_2_05,
  550. /* option vector 2: Open Firmware options supported */
  551. 34 - 2, /* length */
  552. OV2_REAL_MODE,
  553. 0, 0,
  554. W(0xffffffff), /* real_base */
  555. W(0xffffffff), /* real_size */
  556. W(0xffffffff), /* virt_base */
  557. W(0xffffffff), /* virt_size */
  558. W(0xffffffff), /* load_base */
  559. W(64), /* 128MB min RMA */
  560. W(0xffffffff), /* full client load */
  561. 0, /* min RMA percentage of total RAM */
  562. 48, /* max log_2(hash table size) */
  563. /* option vector 3: processor options supported */
  564. 3 - 2, /* length */
  565. 0, /* don't ignore, don't halt */
  566. OV3_FP | OV3_VMX | OV3_DFP,
  567. /* option vector 4: IBM PAPR implementation */
  568. 2 - 2, /* length */
  569. 0, /* don't halt */
  570. /* option vector 5: PAPR/OF options */
  571. 3 - 2, /* length */
  572. 0, /* don't ignore, don't halt */
  573. OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY,
  574. };
  575. /* Old method - ELF header with PT_NOTE sections */
  576. static struct fake_elf {
  577. Elf32_Ehdr elfhdr;
  578. Elf32_Phdr phdr[2];
  579. struct chrpnote {
  580. u32 namesz;
  581. u32 descsz;
  582. u32 type;
  583. char name[8]; /* "PowerPC" */
  584. struct chrpdesc {
  585. u32 real_mode;
  586. u32 real_base;
  587. u32 real_size;
  588. u32 virt_base;
  589. u32 virt_size;
  590. u32 load_base;
  591. } chrpdesc;
  592. } chrpnote;
  593. struct rpanote {
  594. u32 namesz;
  595. u32 descsz;
  596. u32 type;
  597. char name[24]; /* "IBM,RPA-Client-Config" */
  598. struct rpadesc {
  599. u32 lpar_affinity;
  600. u32 min_rmo_size;
  601. u32 min_rmo_percent;
  602. u32 max_pft_size;
  603. u32 splpar;
  604. u32 min_load;
  605. u32 new_mem_def;
  606. u32 ignore_me;
  607. } rpadesc;
  608. } rpanote;
  609. } fake_elf = {
  610. .elfhdr = {
  611. .e_ident = { 0x7f, 'E', 'L', 'F',
  612. ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
  613. .e_type = ET_EXEC, /* yeah right */
  614. .e_machine = EM_PPC,
  615. .e_version = EV_CURRENT,
  616. .e_phoff = offsetof(struct fake_elf, phdr),
  617. .e_phentsize = sizeof(Elf32_Phdr),
  618. .e_phnum = 2
  619. },
  620. .phdr = {
  621. [0] = {
  622. .p_type = PT_NOTE,
  623. .p_offset = offsetof(struct fake_elf, chrpnote),
  624. .p_filesz = sizeof(struct chrpnote)
  625. }, [1] = {
  626. .p_type = PT_NOTE,
  627. .p_offset = offsetof(struct fake_elf, rpanote),
  628. .p_filesz = sizeof(struct rpanote)
  629. }
  630. },
  631. .chrpnote = {
  632. .namesz = sizeof("PowerPC"),
  633. .descsz = sizeof(struct chrpdesc),
  634. .type = 0x1275,
  635. .name = "PowerPC",
  636. .chrpdesc = {
  637. .real_mode = ~0U, /* ~0 means "don't care" */
  638. .real_base = ~0U,
  639. .real_size = ~0U,
  640. .virt_base = ~0U,
  641. .virt_size = ~0U,
  642. .load_base = ~0U
  643. },
  644. },
  645. .rpanote = {
  646. .namesz = sizeof("IBM,RPA-Client-Config"),
  647. .descsz = sizeof(struct rpadesc),
  648. .type = 0x12759999,
  649. .name = "IBM,RPA-Client-Config",
  650. .rpadesc = {
  651. .lpar_affinity = 0,
  652. .min_rmo_size = 64, /* in megabytes */
  653. .min_rmo_percent = 0,
  654. .max_pft_size = 48, /* 2^48 bytes max PFT size */
  655. .splpar = 1,
  656. .min_load = ~0U,
  657. .new_mem_def = 0
  658. }
  659. }
  660. };
  661. static void __init prom_send_capabilities(void)
  662. {
  663. ihandle elfloader, root;
  664. prom_arg_t ret;
  665. root = call_prom("open", 1, 1, ADDR("/"));
  666. if (root != 0) {
  667. /* try calling the ibm,client-architecture-support method */
  668. if (call_prom_ret("call-method", 3, 2, &ret,
  669. ADDR("ibm,client-architecture-support"),
  670. root,
  671. ADDR(ibm_architecture_vec)) == 0) {
  672. /* the call exists... */
  673. if (ret)
  674. prom_printf("WARNING: ibm,client-architecture"
  675. "-support call FAILED!\n");
  676. call_prom("close", 1, 0, root);
  677. return;
  678. }
  679. call_prom("close", 1, 0, root);
  680. }
  681. /* no ibm,client-architecture-support call, try the old way */
  682. elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
  683. if (elfloader == 0) {
  684. prom_printf("couldn't open /packages/elf-loader\n");
  685. return;
  686. }
  687. call_prom("call-method", 3, 1, ADDR("process-elf-header"),
  688. elfloader, ADDR(&fake_elf));
  689. call_prom("close", 1, 0, elfloader);
  690. }
  691. #endif
  692. /*
  693. * Memory allocation strategy... our layout is normally:
  694. *
  695. * at 14Mb or more we have vmlinux, then a gap and initrd. In some
  696. * rare cases, initrd might end up being before the kernel though.
  697. * We assume this won't override the final kernel at 0, we have no
  698. * provision to handle that in this version, but it should hopefully
  699. * never happen.
  700. *
  701. * alloc_top is set to the top of RMO, eventually shrink down if the
  702. * TCEs overlap
  703. *
  704. * alloc_bottom is set to the top of kernel/initrd
  705. *
  706. * from there, allocations are done this way : rtas is allocated
  707. * topmost, and the device-tree is allocated from the bottom. We try
  708. * to grow the device-tree allocation as we progress. If we can't,
  709. * then we fail, we don't currently have a facility to restart
  710. * elsewhere, but that shouldn't be necessary.
  711. *
  712. * Note that calls to reserve_mem have to be done explicitly, memory
  713. * allocated with either alloc_up or alloc_down isn't automatically
  714. * reserved.
  715. */
  716. /*
  717. * Allocates memory in the RMO upward from the kernel/initrd
  718. *
  719. * When align is 0, this is a special case, it means to allocate in place
  720. * at the current location of alloc_bottom or fail (that is basically
  721. * extending the previous allocation). Used for the device-tree flattening
  722. */
  723. static unsigned long __init alloc_up(unsigned long size, unsigned long align)
  724. {
  725. unsigned long base = RELOC(alloc_bottom);
  726. unsigned long addr = 0;
  727. if (align)
  728. base = _ALIGN_UP(base, align);
  729. prom_debug("alloc_up(%x, %x)\n", size, align);
  730. if (RELOC(ram_top) == 0)
  731. prom_panic("alloc_up() called with mem not initialized\n");
  732. if (align)
  733. base = _ALIGN_UP(RELOC(alloc_bottom), align);
  734. else
  735. base = RELOC(alloc_bottom);
  736. for(; (base + size) <= RELOC(alloc_top);
  737. base = _ALIGN_UP(base + 0x100000, align)) {
  738. prom_debug(" trying: 0x%x\n\r", base);
  739. addr = (unsigned long)prom_claim(base, size, 0);
  740. if (addr != PROM_ERROR && addr != 0)
  741. break;
  742. addr = 0;
  743. if (align == 0)
  744. break;
  745. }
  746. if (addr == 0)
  747. return 0;
  748. RELOC(alloc_bottom) = addr;
  749. prom_debug(" -> %x\n", addr);
  750. prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  751. prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
  752. prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  753. prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
  754. prom_debug(" ram_top : %x\n", RELOC(ram_top));
  755. return addr;
  756. }
  757. /*
  758. * Allocates memory downward, either from top of RMO, or if highmem
  759. * is set, from the top of RAM. Note that this one doesn't handle
  760. * failures. It does claim memory if highmem is not set.
  761. */
  762. static unsigned long __init alloc_down(unsigned long size, unsigned long align,
  763. int highmem)
  764. {
  765. unsigned long base, addr = 0;
  766. prom_debug("alloc_down(%x, %x, %s)\n", size, align,
  767. highmem ? RELOC("(high)") : RELOC("(low)"));
  768. if (RELOC(ram_top) == 0)
  769. prom_panic("alloc_down() called with mem not initialized\n");
  770. if (highmem) {
  771. /* Carve out storage for the TCE table. */
  772. addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
  773. if (addr <= RELOC(alloc_bottom))
  774. return 0;
  775. /* Will we bump into the RMO ? If yes, check out that we
  776. * didn't overlap existing allocations there, if we did,
  777. * we are dead, we must be the first in town !
  778. */
  779. if (addr < RELOC(rmo_top)) {
  780. /* Good, we are first */
  781. if (RELOC(alloc_top) == RELOC(rmo_top))
  782. RELOC(alloc_top) = RELOC(rmo_top) = addr;
  783. else
  784. return 0;
  785. }
  786. RELOC(alloc_top_high) = addr;
  787. goto bail;
  788. }
  789. base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
  790. for (; base > RELOC(alloc_bottom);
  791. base = _ALIGN_DOWN(base - 0x100000, align)) {
  792. prom_debug(" trying: 0x%x\n\r", base);
  793. addr = (unsigned long)prom_claim(base, size, 0);
  794. if (addr != PROM_ERROR && addr != 0)
  795. break;
  796. addr = 0;
  797. }
  798. if (addr == 0)
  799. return 0;
  800. RELOC(alloc_top) = addr;
  801. bail:
  802. prom_debug(" -> %x\n", addr);
  803. prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  804. prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
  805. prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  806. prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
  807. prom_debug(" ram_top : %x\n", RELOC(ram_top));
  808. return addr;
  809. }
  810. /*
  811. * Parse a "reg" cell
  812. */
  813. static unsigned long __init prom_next_cell(int s, cell_t **cellp)
  814. {
  815. cell_t *p = *cellp;
  816. unsigned long r = 0;
  817. /* Ignore more than 2 cells */
  818. while (s > sizeof(unsigned long) / 4) {
  819. p++;
  820. s--;
  821. }
  822. r = *p++;
  823. #ifdef CONFIG_PPC64
  824. if (s > 1) {
  825. r <<= 32;
  826. r |= *(p++);
  827. }
  828. #endif
  829. *cellp = p;
  830. return r;
  831. }
  832. /*
  833. * Very dumb function for adding to the memory reserve list, but
  834. * we don't need anything smarter at this point
  835. *
  836. * XXX Eventually check for collisions. They should NEVER happen.
  837. * If problems seem to show up, it would be a good start to track
  838. * them down.
  839. */
  840. static void reserve_mem(u64 base, u64 size)
  841. {
  842. u64 top = base + size;
  843. unsigned long cnt = RELOC(mem_reserve_cnt);
  844. if (size == 0)
  845. return;
  846. /* We need to always keep one empty entry so that we
  847. * have our terminator with "size" set to 0 since we are
  848. * dumb and just copy this entire array to the boot params
  849. */
  850. base = _ALIGN_DOWN(base, PAGE_SIZE);
  851. top = _ALIGN_UP(top, PAGE_SIZE);
  852. size = top - base;
  853. if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
  854. prom_panic("Memory reserve map exhausted !\n");
  855. RELOC(mem_reserve_map)[cnt].base = base;
  856. RELOC(mem_reserve_map)[cnt].size = size;
  857. RELOC(mem_reserve_cnt) = cnt + 1;
  858. }
  859. /*
  860. * Initialize memory allocation mechanism, parse "memory" nodes and
  861. * obtain that way the top of memory and RMO to setup out local allocator
  862. */
  863. static void __init prom_init_mem(void)
  864. {
  865. phandle node;
  866. char *path, type[64];
  867. unsigned int plen;
  868. cell_t *p, *endp;
  869. struct prom_t *_prom = &RELOC(prom);
  870. u32 rac, rsc;
  871. /*
  872. * We iterate the memory nodes to find
  873. * 1) top of RMO (first node)
  874. * 2) top of memory
  875. */
  876. rac = 2;
  877. prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
  878. rsc = 1;
  879. prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
  880. prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
  881. prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
  882. prom_debug("scanning memory:\n");
  883. path = RELOC(prom_scratch);
  884. for (node = 0; prom_next_node(&node); ) {
  885. type[0] = 0;
  886. prom_getprop(node, "device_type", type, sizeof(type));
  887. if (type[0] == 0) {
  888. /*
  889. * CHRP Longtrail machines have no device_type
  890. * on the memory node, so check the name instead...
  891. */
  892. prom_getprop(node, "name", type, sizeof(type));
  893. }
  894. if (strcmp(type, RELOC("memory")))
  895. continue;
  896. plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
  897. if (plen > sizeof(regbuf)) {
  898. prom_printf("memory node too large for buffer !\n");
  899. plen = sizeof(regbuf);
  900. }
  901. p = RELOC(regbuf);
  902. endp = p + (plen / sizeof(cell_t));
  903. #ifdef DEBUG_PROM
  904. memset(path, 0, PROM_SCRATCH_SIZE);
  905. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  906. prom_debug(" node %s :\n", path);
  907. #endif /* DEBUG_PROM */
  908. while ((endp - p) >= (rac + rsc)) {
  909. unsigned long base, size;
  910. base = prom_next_cell(rac, &p);
  911. size = prom_next_cell(rsc, &p);
  912. if (size == 0)
  913. continue;
  914. prom_debug(" %x %x\n", base, size);
  915. if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
  916. RELOC(rmo_top) = size;
  917. if ((base + size) > RELOC(ram_top))
  918. RELOC(ram_top) = base + size;
  919. }
  920. }
  921. RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
  922. /* Check if we have an initrd after the kernel, if we do move our bottom
  923. * point to after it
  924. */
  925. if (RELOC(prom_initrd_start)) {
  926. if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
  927. RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
  928. }
  929. /*
  930. * Setup our top alloc point, that is top of RMO or top of
  931. * segment 0 when running non-LPAR.
  932. * Some RS64 machines have buggy firmware where claims up at
  933. * 1GB fail. Cap at 768MB as a workaround.
  934. * Since 768MB is plenty of room, and we need to cap to something
  935. * reasonable on 32-bit, cap at 768MB on all machines.
  936. */
  937. if (!RELOC(rmo_top))
  938. RELOC(rmo_top) = RELOC(ram_top);
  939. RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
  940. RELOC(alloc_top) = RELOC(rmo_top);
  941. RELOC(alloc_top_high) = RELOC(ram_top);
  942. prom_printf("memory layout at init:\n");
  943. prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  944. prom_printf(" alloc_top : %x\n", RELOC(alloc_top));
  945. prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  946. prom_printf(" rmo_top : %x\n", RELOC(rmo_top));
  947. prom_printf(" ram_top : %x\n", RELOC(ram_top));
  948. }
  949. /*
  950. * Allocate room for and instantiate RTAS
  951. */
  952. static void __init prom_instantiate_rtas(void)
  953. {
  954. phandle rtas_node;
  955. ihandle rtas_inst;
  956. u32 base, entry = 0;
  957. u32 size = 0;
  958. prom_debug("prom_instantiate_rtas: start...\n");
  959. rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  960. prom_debug("rtas_node: %x\n", rtas_node);
  961. if (!PHANDLE_VALID(rtas_node))
  962. return;
  963. prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
  964. if (size == 0)
  965. return;
  966. base = alloc_down(size, PAGE_SIZE, 0);
  967. if (base == 0) {
  968. prom_printf("RTAS allocation failed !\n");
  969. return;
  970. }
  971. rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
  972. if (!IHANDLE_VALID(rtas_inst)) {
  973. prom_printf("opening rtas package failed (%x)\n", rtas_inst);
  974. return;
  975. }
  976. prom_printf("instantiating rtas at 0x%x ...", base);
  977. if (call_prom_ret("call-method", 3, 2, &entry,
  978. ADDR("instantiate-rtas"),
  979. rtas_inst, base) != 0
  980. || entry == 0) {
  981. prom_printf(" failed\n");
  982. return;
  983. }
  984. prom_printf(" done\n");
  985. reserve_mem(base, size);
  986. prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
  987. &base, sizeof(base));
  988. prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
  989. &entry, sizeof(entry));
  990. prom_debug("rtas base = 0x%x\n", base);
  991. prom_debug("rtas entry = 0x%x\n", entry);
  992. prom_debug("rtas size = 0x%x\n", (long)size);
  993. prom_debug("prom_instantiate_rtas: end...\n");
  994. }
  995. #ifdef CONFIG_PPC64
  996. /*
  997. * Allocate room for and initialize TCE tables
  998. */
  999. static void __init prom_initialize_tce_table(void)
  1000. {
  1001. phandle node;
  1002. ihandle phb_node;
  1003. char compatible[64], type[64], model[64];
  1004. char *path = RELOC(prom_scratch);
  1005. u64 base, align;
  1006. u32 minalign, minsize;
  1007. u64 tce_entry, *tce_entryp;
  1008. u64 local_alloc_top, local_alloc_bottom;
  1009. u64 i;
  1010. if (RELOC(prom_iommu_off))
  1011. return;
  1012. prom_debug("starting prom_initialize_tce_table\n");
  1013. /* Cache current top of allocs so we reserve a single block */
  1014. local_alloc_top = RELOC(alloc_top_high);
  1015. local_alloc_bottom = local_alloc_top;
  1016. /* Search all nodes looking for PHBs. */
  1017. for (node = 0; prom_next_node(&node); ) {
  1018. compatible[0] = 0;
  1019. type[0] = 0;
  1020. model[0] = 0;
  1021. prom_getprop(node, "compatible",
  1022. compatible, sizeof(compatible));
  1023. prom_getprop(node, "device_type", type, sizeof(type));
  1024. prom_getprop(node, "model", model, sizeof(model));
  1025. if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
  1026. continue;
  1027. /* Keep the old logic in tack to avoid regression. */
  1028. if (compatible[0] != 0) {
  1029. if ((strstr(compatible, RELOC("python")) == NULL) &&
  1030. (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
  1031. (strstr(compatible, RELOC("Winnipeg")) == NULL))
  1032. continue;
  1033. } else if (model[0] != 0) {
  1034. if ((strstr(model, RELOC("ython")) == NULL) &&
  1035. (strstr(model, RELOC("peedwagon")) == NULL) &&
  1036. (strstr(model, RELOC("innipeg")) == NULL))
  1037. continue;
  1038. }
  1039. if (prom_getprop(node, "tce-table-minalign", &minalign,
  1040. sizeof(minalign)) == PROM_ERROR)
  1041. minalign = 0;
  1042. if (prom_getprop(node, "tce-table-minsize", &minsize,
  1043. sizeof(minsize)) == PROM_ERROR)
  1044. minsize = 4UL << 20;
  1045. /*
  1046. * Even though we read what OF wants, we just set the table
  1047. * size to 4 MB. This is enough to map 2GB of PCI DMA space.
  1048. * By doing this, we avoid the pitfalls of trying to DMA to
  1049. * MMIO space and the DMA alias hole.
  1050. *
  1051. * On POWER4, firmware sets the TCE region by assuming
  1052. * each TCE table is 8MB. Using this memory for anything
  1053. * else will impact performance, so we always allocate 8MB.
  1054. * Anton
  1055. */
  1056. if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
  1057. minsize = 8UL << 20;
  1058. else
  1059. minsize = 4UL << 20;
  1060. /* Align to the greater of the align or size */
  1061. align = max(minalign, minsize);
  1062. base = alloc_down(minsize, align, 1);
  1063. if (base == 0)
  1064. prom_panic("ERROR, cannot find space for TCE table.\n");
  1065. if (base < local_alloc_bottom)
  1066. local_alloc_bottom = base;
  1067. /* It seems OF doesn't null-terminate the path :-( */
  1068. memset(path, 0, sizeof(path));
  1069. /* Call OF to setup the TCE hardware */
  1070. if (call_prom("package-to-path", 3, 1, node,
  1071. path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
  1072. prom_printf("package-to-path failed\n");
  1073. }
  1074. /* Save away the TCE table attributes for later use. */
  1075. prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
  1076. prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
  1077. prom_debug("TCE table: %s\n", path);
  1078. prom_debug("\tnode = 0x%x\n", node);
  1079. prom_debug("\tbase = 0x%x\n", base);
  1080. prom_debug("\tsize = 0x%x\n", minsize);
  1081. /* Initialize the table to have a one-to-one mapping
  1082. * over the allocated size.
  1083. */
  1084. tce_entryp = (unsigned long *)base;
  1085. for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
  1086. tce_entry = (i << PAGE_SHIFT);
  1087. tce_entry |= 0x3;
  1088. *tce_entryp = tce_entry;
  1089. }
  1090. prom_printf("opening PHB %s", path);
  1091. phb_node = call_prom("open", 1, 1, path);
  1092. if (phb_node == 0)
  1093. prom_printf("... failed\n");
  1094. else
  1095. prom_printf("... done\n");
  1096. call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
  1097. phb_node, -1, minsize,
  1098. (u32) base, (u32) (base >> 32));
  1099. call_prom("close", 1, 0, phb_node);
  1100. }
  1101. reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
  1102. /* These are only really needed if there is a memory limit in
  1103. * effect, but we don't know so export them always. */
  1104. RELOC(prom_tce_alloc_start) = local_alloc_bottom;
  1105. RELOC(prom_tce_alloc_end) = local_alloc_top;
  1106. /* Flag the first invalid entry */
  1107. prom_debug("ending prom_initialize_tce_table\n");
  1108. }
  1109. #endif
  1110. /*
  1111. * With CHRP SMP we need to use the OF to start the other processors.
  1112. * We can't wait until smp_boot_cpus (the OF is trashed by then)
  1113. * so we have to put the processors into a holding pattern controlled
  1114. * by the kernel (not OF) before we destroy the OF.
  1115. *
  1116. * This uses a chunk of low memory, puts some holding pattern
  1117. * code there and sends the other processors off to there until
  1118. * smp_boot_cpus tells them to do something. The holding pattern
  1119. * checks that address until its cpu # is there, when it is that
  1120. * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
  1121. * of setting those values.
  1122. *
  1123. * We also use physical address 0x4 here to tell when a cpu
  1124. * is in its holding pattern code.
  1125. *
  1126. * -- Cort
  1127. */
  1128. extern void __secondary_hold(void);
  1129. extern unsigned long __secondary_hold_spinloop;
  1130. extern unsigned long __secondary_hold_acknowledge;
  1131. /*
  1132. * We want to reference the copy of __secondary_hold_* in the
  1133. * 0 - 0x100 address range
  1134. */
  1135. #define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
  1136. static void __init prom_hold_cpus(void)
  1137. {
  1138. unsigned long i;
  1139. unsigned int reg;
  1140. phandle node;
  1141. char type[64];
  1142. int cpuid = 0;
  1143. unsigned int interrupt_server[MAX_CPU_THREADS];
  1144. unsigned int cpu_threads, hw_cpu_num;
  1145. int propsize;
  1146. struct prom_t *_prom = &RELOC(prom);
  1147. unsigned long *spinloop
  1148. = (void *) LOW_ADDR(__secondary_hold_spinloop);
  1149. unsigned long *acknowledge
  1150. = (void *) LOW_ADDR(__secondary_hold_acknowledge);
  1151. #ifdef CONFIG_PPC64
  1152. /* __secondary_hold is actually a descriptor, not the text address */
  1153. unsigned long secondary_hold
  1154. = __pa(*PTRRELOC((unsigned long *)__secondary_hold));
  1155. #else
  1156. unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
  1157. #endif
  1158. prom_debug("prom_hold_cpus: start...\n");
  1159. prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
  1160. prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
  1161. prom_debug(" 1) acknowledge = 0x%x\n",
  1162. (unsigned long)acknowledge);
  1163. prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
  1164. prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
  1165. /* Set the common spinloop variable, so all of the secondary cpus
  1166. * will block when they are awakened from their OF spinloop.
  1167. * This must occur for both SMP and non SMP kernels, since OF will
  1168. * be trashed when we move the kernel.
  1169. */
  1170. *spinloop = 0;
  1171. /* look for cpus */
  1172. for (node = 0; prom_next_node(&node); ) {
  1173. type[0] = 0;
  1174. prom_getprop(node, "device_type", type, sizeof(type));
  1175. if (strcmp(type, RELOC("cpu")) != 0)
  1176. continue;
  1177. /* Skip non-configured cpus. */
  1178. if (prom_getprop(node, "status", type, sizeof(type)) > 0)
  1179. if (strcmp(type, RELOC("okay")) != 0)
  1180. continue;
  1181. reg = -1;
  1182. prom_getprop(node, "reg", &reg, sizeof(reg));
  1183. prom_debug("\ncpuid = 0x%x\n", cpuid);
  1184. prom_debug("cpu hw idx = 0x%x\n", reg);
  1185. /* Init the acknowledge var which will be reset by
  1186. * the secondary cpu when it awakens from its OF
  1187. * spinloop.
  1188. */
  1189. *acknowledge = (unsigned long)-1;
  1190. propsize = prom_getprop(node, "ibm,ppc-interrupt-server#s",
  1191. &interrupt_server,
  1192. sizeof(interrupt_server));
  1193. if (propsize < 0) {
  1194. /* no property. old hardware has no SMT */
  1195. cpu_threads = 1;
  1196. interrupt_server[0] = reg; /* fake it with phys id */
  1197. } else {
  1198. /* We have a threaded processor */
  1199. cpu_threads = propsize / sizeof(u32);
  1200. if (cpu_threads > MAX_CPU_THREADS) {
  1201. prom_printf("SMT: too many threads!\n"
  1202. "SMT: found %x, max is %x\n",
  1203. cpu_threads, MAX_CPU_THREADS);
  1204. cpu_threads = 1; /* ToDo: panic? */
  1205. }
  1206. }
  1207. hw_cpu_num = interrupt_server[0];
  1208. if (hw_cpu_num != _prom->cpu) {
  1209. /* Primary Thread of non-boot cpu */
  1210. prom_printf("%x : starting cpu hw idx %x... ", cpuid, reg);
  1211. call_prom("start-cpu", 3, 0, node,
  1212. secondary_hold, reg);
  1213. for (i = 0; (i < 100000000) &&
  1214. (*acknowledge == ((unsigned long)-1)); i++ )
  1215. mb();
  1216. if (*acknowledge == reg)
  1217. prom_printf("done\n");
  1218. else
  1219. prom_printf("failed: %x\n", *acknowledge);
  1220. }
  1221. #ifdef CONFIG_SMP
  1222. else
  1223. prom_printf("%x : boot cpu %x\n", cpuid, reg);
  1224. #endif /* CONFIG_SMP */
  1225. /* Reserve cpu #s for secondary threads. They start later. */
  1226. cpuid += cpu_threads;
  1227. }
  1228. if (cpuid > NR_CPUS)
  1229. prom_printf("WARNING: maximum CPUs (" __stringify(NR_CPUS)
  1230. ") exceeded: ignoring extras\n");
  1231. prom_debug("prom_hold_cpus: end...\n");
  1232. }
  1233. static void __init prom_init_client_services(unsigned long pp)
  1234. {
  1235. struct prom_t *_prom = &RELOC(prom);
  1236. /* Get a handle to the prom entry point before anything else */
  1237. RELOC(prom_entry) = pp;
  1238. /* get a handle for the stdout device */
  1239. _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
  1240. if (!PHANDLE_VALID(_prom->chosen))
  1241. prom_panic("cannot find chosen"); /* msg won't be printed :( */
  1242. /* get device tree root */
  1243. _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
  1244. if (!PHANDLE_VALID(_prom->root))
  1245. prom_panic("cannot find device tree root"); /* msg won't be printed :( */
  1246. _prom->mmumap = 0;
  1247. }
  1248. #ifdef CONFIG_PPC32
  1249. /*
  1250. * For really old powermacs, we need to map things we claim.
  1251. * For that, we need the ihandle of the mmu.
  1252. * Also, on the longtrail, we need to work around other bugs.
  1253. */
  1254. static void __init prom_find_mmu(void)
  1255. {
  1256. struct prom_t *_prom = &RELOC(prom);
  1257. phandle oprom;
  1258. char version[64];
  1259. oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
  1260. if (!PHANDLE_VALID(oprom))
  1261. return;
  1262. if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
  1263. return;
  1264. version[sizeof(version) - 1] = 0;
  1265. /* XXX might need to add other versions here */
  1266. if (strcmp(version, "Open Firmware, 1.0.5") == 0)
  1267. of_workarounds = OF_WA_CLAIM;
  1268. else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
  1269. of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
  1270. call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
  1271. } else
  1272. return;
  1273. _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
  1274. prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
  1275. sizeof(_prom->mmumap));
  1276. if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
  1277. of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
  1278. }
  1279. #else
  1280. #define prom_find_mmu()
  1281. #endif
  1282. static void __init prom_init_stdout(void)
  1283. {
  1284. struct prom_t *_prom = &RELOC(prom);
  1285. char *path = RELOC(of_stdout_device);
  1286. char type[16];
  1287. u32 val;
  1288. if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
  1289. prom_panic("cannot find stdout");
  1290. _prom->stdout = val;
  1291. /* Get the full OF pathname of the stdout device */
  1292. memset(path, 0, 256);
  1293. call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
  1294. val = call_prom("instance-to-package", 1, 1, _prom->stdout);
  1295. prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
  1296. &val, sizeof(val));
  1297. prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
  1298. prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
  1299. path, strlen(path) + 1);
  1300. /* If it's a display, note it */
  1301. memset(type, 0, sizeof(type));
  1302. prom_getprop(val, "device_type", type, sizeof(type));
  1303. if (strcmp(type, RELOC("display")) == 0)
  1304. prom_setprop(val, path, "linux,boot-display", NULL, 0);
  1305. }
  1306. static void __init prom_close_stdin(void)
  1307. {
  1308. struct prom_t *_prom = &RELOC(prom);
  1309. ihandle val;
  1310. if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
  1311. call_prom("close", 1, 0, val);
  1312. }
  1313. static int __init prom_find_machine_type(void)
  1314. {
  1315. struct prom_t *_prom = &RELOC(prom);
  1316. char compat[256];
  1317. int len, i = 0;
  1318. #ifdef CONFIG_PPC64
  1319. phandle rtas;
  1320. int x;
  1321. #endif
  1322. /* Look for a PowerMac */
  1323. len = prom_getprop(_prom->root, "compatible",
  1324. compat, sizeof(compat)-1);
  1325. if (len > 0) {
  1326. compat[len] = 0;
  1327. while (i < len) {
  1328. char *p = &compat[i];
  1329. int sl = strlen(p);
  1330. if (sl == 0)
  1331. break;
  1332. if (strstr(p, RELOC("Power Macintosh")) ||
  1333. strstr(p, RELOC("MacRISC")))
  1334. return PLATFORM_POWERMAC;
  1335. #ifdef CONFIG_PPC64
  1336. /* We must make sure we don't detect the IBM Cell
  1337. * blades as pSeries due to some firmware issues,
  1338. * so we do it here.
  1339. */
  1340. if (strstr(p, RELOC("IBM,CBEA")) ||
  1341. strstr(p, RELOC("IBM,CPBW-1.0")))
  1342. return PLATFORM_GENERIC;
  1343. #endif /* CONFIG_PPC64 */
  1344. i += sl + 1;
  1345. }
  1346. }
  1347. #ifdef CONFIG_PPC64
  1348. /* If not a mac, try to figure out if it's an IBM pSeries or any other
  1349. * PAPR compliant platform. We assume it is if :
  1350. * - /device_type is "chrp" (please, do NOT use that for future
  1351. * non-IBM designs !
  1352. * - it has /rtas
  1353. */
  1354. len = prom_getprop(_prom->root, "device_type",
  1355. compat, sizeof(compat)-1);
  1356. if (len <= 0)
  1357. return PLATFORM_GENERIC;
  1358. if (strcmp(compat, RELOC("chrp")))
  1359. return PLATFORM_GENERIC;
  1360. /* Default to pSeries. We need to know if we are running LPAR */
  1361. rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  1362. if (!PHANDLE_VALID(rtas))
  1363. return PLATFORM_GENERIC;
  1364. x = prom_getproplen(rtas, "ibm,hypertas-functions");
  1365. if (x != PROM_ERROR) {
  1366. prom_printf("Hypertas detected, assuming LPAR !\n");
  1367. return PLATFORM_PSERIES_LPAR;
  1368. }
  1369. return PLATFORM_PSERIES;
  1370. #else
  1371. return PLATFORM_GENERIC;
  1372. #endif
  1373. }
  1374. static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
  1375. {
  1376. return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
  1377. }
  1378. /*
  1379. * If we have a display that we don't know how to drive,
  1380. * we will want to try to execute OF's open method for it
  1381. * later. However, OF will probably fall over if we do that
  1382. * we've taken over the MMU.
  1383. * So we check whether we will need to open the display,
  1384. * and if so, open it now.
  1385. */
  1386. static void __init prom_check_displays(void)
  1387. {
  1388. char type[16], *path;
  1389. phandle node;
  1390. ihandle ih;
  1391. int i;
  1392. static unsigned char default_colors[] = {
  1393. 0x00, 0x00, 0x00,
  1394. 0x00, 0x00, 0xaa,
  1395. 0x00, 0xaa, 0x00,
  1396. 0x00, 0xaa, 0xaa,
  1397. 0xaa, 0x00, 0x00,
  1398. 0xaa, 0x00, 0xaa,
  1399. 0xaa, 0xaa, 0x00,
  1400. 0xaa, 0xaa, 0xaa,
  1401. 0x55, 0x55, 0x55,
  1402. 0x55, 0x55, 0xff,
  1403. 0x55, 0xff, 0x55,
  1404. 0x55, 0xff, 0xff,
  1405. 0xff, 0x55, 0x55,
  1406. 0xff, 0x55, 0xff,
  1407. 0xff, 0xff, 0x55,
  1408. 0xff, 0xff, 0xff
  1409. };
  1410. const unsigned char *clut;
  1411. prom_printf("Looking for displays\n");
  1412. for (node = 0; prom_next_node(&node); ) {
  1413. memset(type, 0, sizeof(type));
  1414. prom_getprop(node, "device_type", type, sizeof(type));
  1415. if (strcmp(type, RELOC("display")) != 0)
  1416. continue;
  1417. /* It seems OF doesn't null-terminate the path :-( */
  1418. path = RELOC(prom_scratch);
  1419. memset(path, 0, PROM_SCRATCH_SIZE);
  1420. /*
  1421. * leave some room at the end of the path for appending extra
  1422. * arguments
  1423. */
  1424. if (call_prom("package-to-path", 3, 1, node, path,
  1425. PROM_SCRATCH_SIZE-10) == PROM_ERROR)
  1426. continue;
  1427. prom_printf("found display : %s, opening ... ", path);
  1428. ih = call_prom("open", 1, 1, path);
  1429. if (ih == 0) {
  1430. prom_printf("failed\n");
  1431. continue;
  1432. }
  1433. /* Success */
  1434. prom_printf("done\n");
  1435. prom_setprop(node, path, "linux,opened", NULL, 0);
  1436. /* Setup a usable color table when the appropriate
  1437. * method is available. Should update this to set-colors */
  1438. clut = RELOC(default_colors);
  1439. for (i = 0; i < 32; i++, clut += 3)
  1440. if (prom_set_color(ih, i, clut[0], clut[1],
  1441. clut[2]) != 0)
  1442. break;
  1443. #ifdef CONFIG_LOGO_LINUX_CLUT224
  1444. clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
  1445. for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
  1446. if (prom_set_color(ih, i + 32, clut[0], clut[1],
  1447. clut[2]) != 0)
  1448. break;
  1449. #endif /* CONFIG_LOGO_LINUX_CLUT224 */
  1450. }
  1451. }
  1452. /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
  1453. static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
  1454. unsigned long needed, unsigned long align)
  1455. {
  1456. void *ret;
  1457. *mem_start = _ALIGN(*mem_start, align);
  1458. while ((*mem_start + needed) > *mem_end) {
  1459. unsigned long room, chunk;
  1460. prom_debug("Chunk exhausted, claiming more at %x...\n",
  1461. RELOC(alloc_bottom));
  1462. room = RELOC(alloc_top) - RELOC(alloc_bottom);
  1463. if (room > DEVTREE_CHUNK_SIZE)
  1464. room = DEVTREE_CHUNK_SIZE;
  1465. if (room < PAGE_SIZE)
  1466. prom_panic("No memory for flatten_device_tree (no room)");
  1467. chunk = alloc_up(room, 0);
  1468. if (chunk == 0)
  1469. prom_panic("No memory for flatten_device_tree (claim failed)");
  1470. *mem_end = RELOC(alloc_top);
  1471. }
  1472. ret = (void *)*mem_start;
  1473. *mem_start += needed;
  1474. return ret;
  1475. }
  1476. #define dt_push_token(token, mem_start, mem_end) \
  1477. do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
  1478. static unsigned long __init dt_find_string(char *str)
  1479. {
  1480. char *s, *os;
  1481. s = os = (char *)RELOC(dt_string_start);
  1482. s += 4;
  1483. while (s < (char *)RELOC(dt_string_end)) {
  1484. if (strcmp(s, str) == 0)
  1485. return s - os;
  1486. s += strlen(s) + 1;
  1487. }
  1488. return 0;
  1489. }
  1490. /*
  1491. * The Open Firmware 1275 specification states properties must be 31 bytes or
  1492. * less, however not all firmwares obey this. Make it 64 bytes to be safe.
  1493. */
  1494. #define MAX_PROPERTY_NAME 64
  1495. static void __init scan_dt_build_strings(phandle node,
  1496. unsigned long *mem_start,
  1497. unsigned long *mem_end)
  1498. {
  1499. char *prev_name, *namep, *sstart;
  1500. unsigned long soff;
  1501. phandle child;
  1502. sstart = (char *)RELOC(dt_string_start);
  1503. /* get and store all property names */
  1504. prev_name = RELOC("");
  1505. for (;;) {
  1506. /* 64 is max len of name including nul. */
  1507. namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
  1508. if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
  1509. /* No more nodes: unwind alloc */
  1510. *mem_start = (unsigned long)namep;
  1511. break;
  1512. }
  1513. /* skip "name" */
  1514. if (strcmp(namep, RELOC("name")) == 0) {
  1515. *mem_start = (unsigned long)namep;
  1516. prev_name = RELOC("name");
  1517. continue;
  1518. }
  1519. /* get/create string entry */
  1520. soff = dt_find_string(namep);
  1521. if (soff != 0) {
  1522. *mem_start = (unsigned long)namep;
  1523. namep = sstart + soff;
  1524. } else {
  1525. /* Trim off some if we can */
  1526. *mem_start = (unsigned long)namep + strlen(namep) + 1;
  1527. RELOC(dt_string_end) = *mem_start;
  1528. }
  1529. prev_name = namep;
  1530. }
  1531. /* do all our children */
  1532. child = call_prom("child", 1, 1, node);
  1533. while (child != 0) {
  1534. scan_dt_build_strings(child, mem_start, mem_end);
  1535. child = call_prom("peer", 1, 1, child);
  1536. }
  1537. }
  1538. static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
  1539. unsigned long *mem_end)
  1540. {
  1541. phandle child;
  1542. char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
  1543. unsigned long soff;
  1544. unsigned char *valp;
  1545. static char pname[MAX_PROPERTY_NAME];
  1546. int l, room;
  1547. dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
  1548. /* get the node's full name */
  1549. namep = (char *)*mem_start;
  1550. room = *mem_end - *mem_start;
  1551. if (room > 255)
  1552. room = 255;
  1553. l = call_prom("package-to-path", 3, 1, node, namep, room);
  1554. if (l >= 0) {
  1555. /* Didn't fit? Get more room. */
  1556. if (l >= room) {
  1557. if (l >= *mem_end - *mem_start)
  1558. namep = make_room(mem_start, mem_end, l+1, 1);
  1559. call_prom("package-to-path", 3, 1, node, namep, l);
  1560. }
  1561. namep[l] = '\0';
  1562. /* Fixup an Apple bug where they have bogus \0 chars in the
  1563. * middle of the path in some properties, and extract
  1564. * the unit name (everything after the last '/').
  1565. */
  1566. for (lp = p = namep, ep = namep + l; p < ep; p++) {
  1567. if (*p == '/')
  1568. lp = namep;
  1569. else if (*p != 0)
  1570. *lp++ = *p;
  1571. }
  1572. *lp = 0;
  1573. *mem_start = _ALIGN((unsigned long)lp + 1, 4);
  1574. }
  1575. /* get it again for debugging */
  1576. path = RELOC(prom_scratch);
  1577. memset(path, 0, PROM_SCRATCH_SIZE);
  1578. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  1579. /* get and store all properties */
  1580. prev_name = RELOC("");
  1581. sstart = (char *)RELOC(dt_string_start);
  1582. for (;;) {
  1583. if (call_prom("nextprop", 3, 1, node, prev_name,
  1584. RELOC(pname)) != 1)
  1585. break;
  1586. /* skip "name" */
  1587. if (strcmp(RELOC(pname), RELOC("name")) == 0) {
  1588. prev_name = RELOC("name");
  1589. continue;
  1590. }
  1591. /* find string offset */
  1592. soff = dt_find_string(RELOC(pname));
  1593. if (soff == 0) {
  1594. prom_printf("WARNING: Can't find string index for"
  1595. " <%s>, node %s\n", RELOC(pname), path);
  1596. break;
  1597. }
  1598. prev_name = sstart + soff;
  1599. /* get length */
  1600. l = call_prom("getproplen", 2, 1, node, RELOC(pname));
  1601. /* sanity checks */
  1602. if (l == PROM_ERROR)
  1603. continue;
  1604. if (l > MAX_PROPERTY_LENGTH) {
  1605. prom_printf("WARNING: ignoring large property ");
  1606. /* It seems OF doesn't null-terminate the path :-( */
  1607. prom_printf("[%s] ", path);
  1608. prom_printf("%s length 0x%x\n", RELOC(pname), l);
  1609. continue;
  1610. }
  1611. /* push property head */
  1612. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1613. dt_push_token(l, mem_start, mem_end);
  1614. dt_push_token(soff, mem_start, mem_end);
  1615. /* push property content */
  1616. valp = make_room(mem_start, mem_end, l, 4);
  1617. call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
  1618. *mem_start = _ALIGN(*mem_start, 4);
  1619. }
  1620. /* Add a "linux,phandle" property. */
  1621. soff = dt_find_string(RELOC("linux,phandle"));
  1622. if (soff == 0)
  1623. prom_printf("WARNING: Can't find string index for"
  1624. " <linux-phandle> node %s\n", path);
  1625. else {
  1626. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1627. dt_push_token(4, mem_start, mem_end);
  1628. dt_push_token(soff, mem_start, mem_end);
  1629. valp = make_room(mem_start, mem_end, 4, 4);
  1630. *(u32 *)valp = node;
  1631. }
  1632. /* do all our children */
  1633. child = call_prom("child", 1, 1, node);
  1634. while (child != 0) {
  1635. scan_dt_build_struct(child, mem_start, mem_end);
  1636. child = call_prom("peer", 1, 1, child);
  1637. }
  1638. dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
  1639. }
  1640. static void __init flatten_device_tree(void)
  1641. {
  1642. phandle root;
  1643. unsigned long mem_start, mem_end, room;
  1644. struct boot_param_header *hdr;
  1645. struct prom_t *_prom = &RELOC(prom);
  1646. char *namep;
  1647. u64 *rsvmap;
  1648. /*
  1649. * Check how much room we have between alloc top & bottom (+/- a
  1650. * few pages), crop to 4Mb, as this is our "chuck" size
  1651. */
  1652. room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
  1653. if (room > DEVTREE_CHUNK_SIZE)
  1654. room = DEVTREE_CHUNK_SIZE;
  1655. prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
  1656. /* Now try to claim that */
  1657. mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
  1658. if (mem_start == 0)
  1659. prom_panic("Can't allocate initial device-tree chunk\n");
  1660. mem_end = RELOC(alloc_top);
  1661. /* Get root of tree */
  1662. root = call_prom("peer", 1, 1, (phandle)0);
  1663. if (root == (phandle)0)
  1664. prom_panic ("couldn't get device tree root\n");
  1665. /* Build header and make room for mem rsv map */
  1666. mem_start = _ALIGN(mem_start, 4);
  1667. hdr = make_room(&mem_start, &mem_end,
  1668. sizeof(struct boot_param_header), 4);
  1669. RELOC(dt_header_start) = (unsigned long)hdr;
  1670. rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
  1671. /* Start of strings */
  1672. mem_start = PAGE_ALIGN(mem_start);
  1673. RELOC(dt_string_start) = mem_start;
  1674. mem_start += 4; /* hole */
  1675. /* Add "linux,phandle" in there, we'll need it */
  1676. namep = make_room(&mem_start, &mem_end, 16, 1);
  1677. strcpy(namep, RELOC("linux,phandle"));
  1678. mem_start = (unsigned long)namep + strlen(namep) + 1;
  1679. /* Build string array */
  1680. prom_printf("Building dt strings...\n");
  1681. scan_dt_build_strings(root, &mem_start, &mem_end);
  1682. RELOC(dt_string_end) = mem_start;
  1683. /* Build structure */
  1684. mem_start = PAGE_ALIGN(mem_start);
  1685. RELOC(dt_struct_start) = mem_start;
  1686. prom_printf("Building dt structure...\n");
  1687. scan_dt_build_struct(root, &mem_start, &mem_end);
  1688. dt_push_token(OF_DT_END, &mem_start, &mem_end);
  1689. RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
  1690. /* Finish header */
  1691. hdr->boot_cpuid_phys = _prom->cpu;
  1692. hdr->magic = OF_DT_HEADER;
  1693. hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
  1694. hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
  1695. hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
  1696. hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
  1697. hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
  1698. hdr->version = OF_DT_VERSION;
  1699. /* Version 16 is not backward compatible */
  1700. hdr->last_comp_version = 0x10;
  1701. /* Copy the reserve map in */
  1702. memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
  1703. #ifdef DEBUG_PROM
  1704. {
  1705. int i;
  1706. prom_printf("reserved memory map:\n");
  1707. for (i = 0; i < RELOC(mem_reserve_cnt); i++)
  1708. prom_printf(" %x - %x\n",
  1709. RELOC(mem_reserve_map)[i].base,
  1710. RELOC(mem_reserve_map)[i].size);
  1711. }
  1712. #endif
  1713. /* Bump mem_reserve_cnt to cause further reservations to fail
  1714. * since it's too late.
  1715. */
  1716. RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
  1717. prom_printf("Device tree strings 0x%x -> 0x%x\n",
  1718. RELOC(dt_string_start), RELOC(dt_string_end));
  1719. prom_printf("Device tree struct 0x%x -> 0x%x\n",
  1720. RELOC(dt_struct_start), RELOC(dt_struct_end));
  1721. }
  1722. #ifdef CONFIG_PPC_MAPLE
  1723. /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
  1724. * The values are bad, and it doesn't even have the right number of cells. */
  1725. static void __init fixup_device_tree_maple(void)
  1726. {
  1727. phandle isa;
  1728. u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
  1729. u32 isa_ranges[6];
  1730. char *name;
  1731. name = "/ht@0/isa@4";
  1732. isa = call_prom("finddevice", 1, 1, ADDR(name));
  1733. if (!PHANDLE_VALID(isa)) {
  1734. name = "/ht@0/isa@6";
  1735. isa = call_prom("finddevice", 1, 1, ADDR(name));
  1736. rloc = 0x01003000; /* IO space; PCI device = 6 */
  1737. }
  1738. if (!PHANDLE_VALID(isa))
  1739. return;
  1740. if (prom_getproplen(isa, "ranges") != 12)
  1741. return;
  1742. if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
  1743. == PROM_ERROR)
  1744. return;
  1745. if (isa_ranges[0] != 0x1 ||
  1746. isa_ranges[1] != 0xf4000000 ||
  1747. isa_ranges[2] != 0x00010000)
  1748. return;
  1749. prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
  1750. isa_ranges[0] = 0x1;
  1751. isa_ranges[1] = 0x0;
  1752. isa_ranges[2] = rloc;
  1753. isa_ranges[3] = 0x0;
  1754. isa_ranges[4] = 0x0;
  1755. isa_ranges[5] = 0x00010000;
  1756. prom_setprop(isa, name, "ranges",
  1757. isa_ranges, sizeof(isa_ranges));
  1758. }
  1759. #else
  1760. #define fixup_device_tree_maple()
  1761. #endif
  1762. #ifdef CONFIG_PPC_CHRP
  1763. /* Pegasos and BriQ lacks the "ranges" property in the isa node */
  1764. static void __init fixup_device_tree_chrp(void)
  1765. {
  1766. phandle isa;
  1767. u32 isa_ranges[6];
  1768. u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
  1769. char *name;
  1770. int rc;
  1771. name = "/pci@80000000/isa@c";
  1772. isa = call_prom("finddevice", 1, 1, ADDR(name));
  1773. if (!PHANDLE_VALID(isa)) {
  1774. name = "/pci@ff500000/isa@6";
  1775. isa = call_prom("finddevice", 1, 1, ADDR(name));
  1776. rloc = 0x01003000; /* IO space; PCI device = 6 */
  1777. }
  1778. if (!PHANDLE_VALID(isa))
  1779. return;
  1780. rc = prom_getproplen(isa, "ranges");
  1781. if (rc != 0 && rc != PROM_ERROR)
  1782. return;
  1783. prom_printf("Fixing up missing ISA range on Pegasos...\n");
  1784. isa_ranges[0] = 0x1;
  1785. isa_ranges[1] = 0x0;
  1786. isa_ranges[2] = rloc;
  1787. isa_ranges[3] = 0x0;
  1788. isa_ranges[4] = 0x0;
  1789. isa_ranges[5] = 0x00010000;
  1790. prom_setprop(isa, name, "ranges",
  1791. isa_ranges, sizeof(isa_ranges));
  1792. }
  1793. #else
  1794. #define fixup_device_tree_chrp()
  1795. #endif
  1796. #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
  1797. static void __init fixup_device_tree_pmac(void)
  1798. {
  1799. phandle u3, i2c, mpic;
  1800. u32 u3_rev;
  1801. u32 interrupts[2];
  1802. u32 parent;
  1803. /* Some G5s have a missing interrupt definition, fix it up here */
  1804. u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
  1805. if (!PHANDLE_VALID(u3))
  1806. return;
  1807. i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
  1808. if (!PHANDLE_VALID(i2c))
  1809. return;
  1810. mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
  1811. if (!PHANDLE_VALID(mpic))
  1812. return;
  1813. /* check if proper rev of u3 */
  1814. if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
  1815. == PROM_ERROR)
  1816. return;
  1817. if (u3_rev < 0x35 || u3_rev > 0x39)
  1818. return;
  1819. /* does it need fixup ? */
  1820. if (prom_getproplen(i2c, "interrupts") > 0)
  1821. return;
  1822. prom_printf("fixing up bogus interrupts for u3 i2c...\n");
  1823. /* interrupt on this revision of u3 is number 0 and level */
  1824. interrupts[0] = 0;
  1825. interrupts[1] = 1;
  1826. prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
  1827. &interrupts, sizeof(interrupts));
  1828. parent = (u32)mpic;
  1829. prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
  1830. &parent, sizeof(parent));
  1831. }
  1832. #else
  1833. #define fixup_device_tree_pmac()
  1834. #endif
  1835. static void __init fixup_device_tree(void)
  1836. {
  1837. fixup_device_tree_maple();
  1838. fixup_device_tree_chrp();
  1839. fixup_device_tree_pmac();
  1840. }
  1841. static void __init prom_find_boot_cpu(void)
  1842. {
  1843. struct prom_t *_prom = &RELOC(prom);
  1844. u32 getprop_rval;
  1845. ihandle prom_cpu;
  1846. phandle cpu_pkg;
  1847. _prom->cpu = 0;
  1848. if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
  1849. return;
  1850. cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
  1851. prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
  1852. _prom->cpu = getprop_rval;
  1853. prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
  1854. }
  1855. static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
  1856. {
  1857. #ifdef CONFIG_BLK_DEV_INITRD
  1858. struct prom_t *_prom = &RELOC(prom);
  1859. if (r3 && r4 && r4 != 0xdeadbeef) {
  1860. unsigned long val;
  1861. RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
  1862. RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
  1863. val = RELOC(prom_initrd_start);
  1864. prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
  1865. &val, sizeof(val));
  1866. val = RELOC(prom_initrd_end);
  1867. prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
  1868. &val, sizeof(val));
  1869. reserve_mem(RELOC(prom_initrd_start),
  1870. RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
  1871. prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
  1872. prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
  1873. }
  1874. #endif /* CONFIG_BLK_DEV_INITRD */
  1875. }
  1876. /*
  1877. * We enter here early on, when the Open Firmware prom is still
  1878. * handling exceptions and the MMU hash table for us.
  1879. */
  1880. unsigned long __init prom_init(unsigned long r3, unsigned long r4,
  1881. unsigned long pp,
  1882. unsigned long r6, unsigned long r7)
  1883. {
  1884. struct prom_t *_prom;
  1885. unsigned long hdr;
  1886. unsigned long offset = reloc_offset();
  1887. #ifdef CONFIG_PPC32
  1888. reloc_got2(offset);
  1889. #endif
  1890. _prom = &RELOC(prom);
  1891. /*
  1892. * First zero the BSS
  1893. */
  1894. memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
  1895. /*
  1896. * Init interface to Open Firmware, get some node references,
  1897. * like /chosen
  1898. */
  1899. prom_init_client_services(pp);
  1900. /*
  1901. * See if this OF is old enough that we need to do explicit maps
  1902. * and other workarounds
  1903. */
  1904. prom_find_mmu();
  1905. /*
  1906. * Init prom stdout device
  1907. */
  1908. prom_init_stdout();
  1909. /*
  1910. * Get default machine type. At this point, we do not differentiate
  1911. * between pSeries SMP and pSeries LPAR
  1912. */
  1913. RELOC(of_platform) = prom_find_machine_type();
  1914. /* Bail if this is a kdump kernel. */
  1915. if (PHYSICAL_START > 0)
  1916. prom_panic("Error: You can't boot a kdump kernel from OF!\n");
  1917. /*
  1918. * Check for an initrd
  1919. */
  1920. prom_check_initrd(r3, r4);
  1921. #ifdef CONFIG_PPC_PSERIES
  1922. /*
  1923. * On pSeries, inform the firmware about our capabilities
  1924. */
  1925. if (RELOC(of_platform) == PLATFORM_PSERIES ||
  1926. RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
  1927. prom_send_capabilities();
  1928. #endif
  1929. /*
  1930. * Copy the CPU hold code
  1931. */
  1932. if (RELOC(of_platform) != PLATFORM_POWERMAC)
  1933. copy_and_flush(0, KERNELBASE + offset, 0x100, 0);
  1934. /*
  1935. * Do early parsing of command line
  1936. */
  1937. early_cmdline_parse();
  1938. /*
  1939. * Initialize memory management within prom_init
  1940. */
  1941. prom_init_mem();
  1942. /*
  1943. * Determine which cpu is actually running right _now_
  1944. */
  1945. prom_find_boot_cpu();
  1946. /*
  1947. * Initialize display devices
  1948. */
  1949. prom_check_displays();
  1950. #ifdef CONFIG_PPC64
  1951. /*
  1952. * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
  1953. * that uses the allocator, we need to make sure we get the top of memory
  1954. * available for us here...
  1955. */
  1956. if (RELOC(of_platform) == PLATFORM_PSERIES)
  1957. prom_initialize_tce_table();
  1958. #endif
  1959. /*
  1960. * On non-powermacs, try to instantiate RTAS and puts all CPUs
  1961. * in spin-loops. PowerMacs don't have a working RTAS and use
  1962. * a different way to spin CPUs
  1963. */
  1964. if (RELOC(of_platform) != PLATFORM_POWERMAC) {
  1965. prom_instantiate_rtas();
  1966. prom_hold_cpus();
  1967. }
  1968. /*
  1969. * Fill in some infos for use by the kernel later on
  1970. */
  1971. #ifdef CONFIG_PPC64
  1972. if (RELOC(prom_iommu_off))
  1973. prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
  1974. NULL, 0);
  1975. if (RELOC(prom_iommu_force_on))
  1976. prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
  1977. NULL, 0);
  1978. if (RELOC(prom_tce_alloc_start)) {
  1979. prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
  1980. &RELOC(prom_tce_alloc_start),
  1981. sizeof(prom_tce_alloc_start));
  1982. prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
  1983. &RELOC(prom_tce_alloc_end),
  1984. sizeof(prom_tce_alloc_end));
  1985. }
  1986. #endif
  1987. /*
  1988. * Fixup any known bugs in the device-tree
  1989. */
  1990. fixup_device_tree();
  1991. /*
  1992. * Now finally create the flattened device-tree
  1993. */
  1994. prom_printf("copying OF device tree ...\n");
  1995. flatten_device_tree();
  1996. /*
  1997. * in case stdin is USB and still active on IBM machines...
  1998. * Unfortunately quiesce crashes on some powermacs if we have
  1999. * closed stdin already (in particular the powerbook 101).
  2000. */
  2001. if (RELOC(of_platform) != PLATFORM_POWERMAC)
  2002. prom_close_stdin();
  2003. /*
  2004. * Call OF "quiesce" method to shut down pending DMA's from
  2005. * devices etc...
  2006. */
  2007. prom_printf("Calling quiesce ...\n");
  2008. call_prom("quiesce", 0, 0);
  2009. /*
  2010. * And finally, call the kernel passing it the flattened device
  2011. * tree and NULL as r5, thus triggering the new entry point which
  2012. * is common to us and kexec
  2013. */
  2014. hdr = RELOC(dt_header_start);
  2015. prom_printf("returning from prom_init\n");
  2016. prom_debug("->dt_header_start=0x%x\n", hdr);
  2017. #ifdef CONFIG_PPC32
  2018. reloc_got2(-offset);
  2019. #endif
  2020. __start(hdr, KERNELBASE + offset, 0);
  2021. return 0;
  2022. }