prom_init.c 61 KB

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