prom_init.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  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. static int __initdata of_platform;
  157. static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
  158. static unsigned long __initdata prom_memory_limit;
  159. static unsigned long __initdata alloc_top;
  160. static unsigned long __initdata alloc_top_high;
  161. static unsigned long __initdata alloc_bottom;
  162. static unsigned long __initdata rmo_top;
  163. static unsigned long __initdata ram_top;
  164. #ifdef CONFIG_KEXEC
  165. static unsigned long __initdata prom_crashk_base;
  166. static unsigned long __initdata prom_crashk_size;
  167. #endif
  168. static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
  169. static int __initdata mem_reserve_cnt;
  170. static cell_t __initdata regbuf[1024];
  171. #define MAX_CPU_THREADS 2
  172. /* TO GO */
  173. #ifdef CONFIG_HMT
  174. struct {
  175. unsigned int pir;
  176. unsigned int threadid;
  177. } hmt_thread_data[NR_CPUS];
  178. #endif /* CONFIG_HMT */
  179. /*
  180. * Error results ... some OF calls will return "-1" on error, some
  181. * will return 0, some will return either. To simplify, here are
  182. * macros to use with any ihandle or phandle return value to check if
  183. * it is valid
  184. */
  185. #define PROM_ERROR (-1u)
  186. #define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
  187. #define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
  188. /* This is the one and *ONLY* place where we actually call open
  189. * firmware.
  190. */
  191. static int __init call_prom(const char *service, int nargs, int nret, ...)
  192. {
  193. int i;
  194. struct prom_args args;
  195. va_list list;
  196. args.service = ADDR(service);
  197. args.nargs = nargs;
  198. args.nret = nret;
  199. va_start(list, nret);
  200. for (i = 0; i < nargs; i++)
  201. args.args[i] = va_arg(list, prom_arg_t);
  202. va_end(list);
  203. for (i = 0; i < nret; i++)
  204. args.args[nargs+i] = 0;
  205. if (enter_prom(&args, RELOC(prom_entry)) < 0)
  206. return PROM_ERROR;
  207. return (nret > 0) ? args.args[nargs] : 0;
  208. }
  209. static int __init call_prom_ret(const char *service, int nargs, int nret,
  210. prom_arg_t *rets, ...)
  211. {
  212. int i;
  213. struct prom_args args;
  214. va_list list;
  215. args.service = ADDR(service);
  216. args.nargs = nargs;
  217. args.nret = nret;
  218. va_start(list, rets);
  219. for (i = 0; i < nargs; i++)
  220. args.args[i] = va_arg(list, prom_arg_t);
  221. va_end(list);
  222. for (i = 0; i < nret; i++)
  223. args.args[nargs+i] = 0;
  224. if (enter_prom(&args, RELOC(prom_entry)) < 0)
  225. return PROM_ERROR;
  226. if (rets != NULL)
  227. for (i = 1; i < nret; ++i)
  228. rets[i-1] = args.args[nargs+i];
  229. return (nret > 0) ? args.args[nargs] : 0;
  230. }
  231. static void __init prom_print(const char *msg)
  232. {
  233. const char *p, *q;
  234. struct prom_t *_prom = &RELOC(prom);
  235. if (_prom->stdout == 0)
  236. return;
  237. for (p = msg; *p != 0; p = q) {
  238. for (q = p; *q != 0 && *q != '\n'; ++q)
  239. ;
  240. if (q > p)
  241. call_prom("write", 3, 1, _prom->stdout, p, q - p);
  242. if (*q == 0)
  243. break;
  244. ++q;
  245. call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
  246. }
  247. }
  248. static void __init prom_print_hex(unsigned long val)
  249. {
  250. int i, nibbles = sizeof(val)*2;
  251. char buf[sizeof(val)*2+1];
  252. struct prom_t *_prom = &RELOC(prom);
  253. for (i = nibbles-1; i >= 0; i--) {
  254. buf[i] = (val & 0xf) + '0';
  255. if (buf[i] > '9')
  256. buf[i] += ('a'-'0'-10);
  257. val >>= 4;
  258. }
  259. buf[nibbles] = '\0';
  260. call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
  261. }
  262. static void __init prom_printf(const char *format, ...)
  263. {
  264. const char *p, *q, *s;
  265. va_list args;
  266. unsigned long v;
  267. struct prom_t *_prom = &RELOC(prom);
  268. va_start(args, format);
  269. #ifdef CONFIG_PPC64
  270. format = PTRRELOC(format);
  271. #endif
  272. for (p = format; *p != 0; p = q) {
  273. for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
  274. ;
  275. if (q > p)
  276. call_prom("write", 3, 1, _prom->stdout, p, q - p);
  277. if (*q == 0)
  278. break;
  279. if (*q == '\n') {
  280. ++q;
  281. call_prom("write", 3, 1, _prom->stdout,
  282. ADDR("\r\n"), 2);
  283. continue;
  284. }
  285. ++q;
  286. if (*q == 0)
  287. break;
  288. switch (*q) {
  289. case 's':
  290. ++q;
  291. s = va_arg(args, const char *);
  292. prom_print(s);
  293. break;
  294. case 'x':
  295. ++q;
  296. v = va_arg(args, unsigned long);
  297. prom_print_hex(v);
  298. break;
  299. }
  300. }
  301. }
  302. static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
  303. unsigned long align)
  304. {
  305. struct prom_t *_prom = &RELOC(prom);
  306. if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
  307. /*
  308. * Old OF requires we claim physical and virtual separately
  309. * and then map explicitly (assuming virtual mode)
  310. */
  311. int ret;
  312. prom_arg_t result;
  313. ret = call_prom_ret("call-method", 5, 2, &result,
  314. ADDR("claim"), _prom->memory,
  315. align, size, virt);
  316. if (ret != 0 || result == -1)
  317. return -1;
  318. ret = call_prom_ret("call-method", 5, 2, &result,
  319. ADDR("claim"), _prom->mmumap,
  320. align, size, virt);
  321. if (ret != 0) {
  322. call_prom("call-method", 4, 1, ADDR("release"),
  323. _prom->memory, size, virt);
  324. return -1;
  325. }
  326. /* the 0x12 is M (coherence) + PP == read/write */
  327. call_prom("call-method", 6, 1,
  328. ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
  329. return virt;
  330. }
  331. return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
  332. (prom_arg_t)align);
  333. }
  334. static void __init __attribute__((noreturn)) prom_panic(const char *reason)
  335. {
  336. #ifdef CONFIG_PPC64
  337. reason = PTRRELOC(reason);
  338. #endif
  339. prom_print(reason);
  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. const char *opt;
  467. char *p;
  468. int l = 0;
  469. RELOC(prom_cmd_line[0]) = 0;
  470. p = RELOC(prom_cmd_line);
  471. if ((long)_prom->chosen > 0)
  472. l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
  473. #ifdef CONFIG_CMDLINE
  474. if (l == 0) /* dbl check */
  475. strlcpy(RELOC(prom_cmd_line),
  476. RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
  477. #endif /* CONFIG_CMDLINE */
  478. prom_printf("command line: %s\n", RELOC(prom_cmd_line));
  479. #ifdef CONFIG_PPC64
  480. opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
  481. if (opt) {
  482. prom_printf("iommu opt is: %s\n", opt);
  483. opt += 6;
  484. while (*opt && *opt == ' ')
  485. opt++;
  486. if (!strncmp(opt, RELOC("off"), 3))
  487. RELOC(ppc64_iommu_off) = 1;
  488. else if (!strncmp(opt, RELOC("force"), 5))
  489. RELOC(iommu_force_on) = 1;
  490. }
  491. #endif
  492. opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
  493. if (opt) {
  494. opt += 4;
  495. RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
  496. #ifdef CONFIG_PPC64
  497. /* Align to 16 MB == size of ppc64 large page */
  498. RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
  499. #endif
  500. }
  501. #ifdef CONFIG_KEXEC
  502. /*
  503. * crashkernel=size@addr specifies the location to reserve for
  504. * crash kernel.
  505. */
  506. opt = strstr(RELOC(prom_cmd_line), RELOC("crashkernel="));
  507. if (opt) {
  508. opt += 12;
  509. RELOC(prom_crashk_size) =
  510. prom_memparse(opt, (const char **)&opt);
  511. if (ALIGN(RELOC(prom_crashk_size), 0x1000000) !=
  512. RELOC(prom_crashk_size)) {
  513. prom_printf("Warning: crashkernel size is not "
  514. "aligned to 16MB\n");
  515. }
  516. /*
  517. * At present, the crash kernel always run at 32MB.
  518. * Just ignore whatever user passed.
  519. */
  520. RELOC(prom_crashk_base) = 0x2000000;
  521. if (*opt == '@') {
  522. prom_printf("Warning: PPC64 kdump kernel always runs "
  523. "at 32 MB\n");
  524. }
  525. }
  526. #endif
  527. }
  528. #ifdef CONFIG_PPC_PSERIES
  529. /*
  530. * To tell the firmware what our capabilities are, we have to pass
  531. * it a fake 32-bit ELF header containing a couple of PT_NOTE sections
  532. * that contain structures that contain the actual values.
  533. */
  534. static struct fake_elf {
  535. Elf32_Ehdr elfhdr;
  536. Elf32_Phdr phdr[2];
  537. struct chrpnote {
  538. u32 namesz;
  539. u32 descsz;
  540. u32 type;
  541. char name[8]; /* "PowerPC" */
  542. struct chrpdesc {
  543. u32 real_mode;
  544. u32 real_base;
  545. u32 real_size;
  546. u32 virt_base;
  547. u32 virt_size;
  548. u32 load_base;
  549. } chrpdesc;
  550. } chrpnote;
  551. struct rpanote {
  552. u32 namesz;
  553. u32 descsz;
  554. u32 type;
  555. char name[24]; /* "IBM,RPA-Client-Config" */
  556. struct rpadesc {
  557. u32 lpar_affinity;
  558. u32 min_rmo_size;
  559. u32 min_rmo_percent;
  560. u32 max_pft_size;
  561. u32 splpar;
  562. u32 min_load;
  563. u32 new_mem_def;
  564. u32 ignore_me;
  565. } rpadesc;
  566. } rpanote;
  567. } fake_elf = {
  568. .elfhdr = {
  569. .e_ident = { 0x7f, 'E', 'L', 'F',
  570. ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
  571. .e_type = ET_EXEC, /* yeah right */
  572. .e_machine = EM_PPC,
  573. .e_version = EV_CURRENT,
  574. .e_phoff = offsetof(struct fake_elf, phdr),
  575. .e_phentsize = sizeof(Elf32_Phdr),
  576. .e_phnum = 2
  577. },
  578. .phdr = {
  579. [0] = {
  580. .p_type = PT_NOTE,
  581. .p_offset = offsetof(struct fake_elf, chrpnote),
  582. .p_filesz = sizeof(struct chrpnote)
  583. }, [1] = {
  584. .p_type = PT_NOTE,
  585. .p_offset = offsetof(struct fake_elf, rpanote),
  586. .p_filesz = sizeof(struct rpanote)
  587. }
  588. },
  589. .chrpnote = {
  590. .namesz = sizeof("PowerPC"),
  591. .descsz = sizeof(struct chrpdesc),
  592. .type = 0x1275,
  593. .name = "PowerPC",
  594. .chrpdesc = {
  595. .real_mode = ~0U, /* ~0 means "don't care" */
  596. .real_base = ~0U,
  597. .real_size = ~0U,
  598. .virt_base = ~0U,
  599. .virt_size = ~0U,
  600. .load_base = ~0U
  601. },
  602. },
  603. .rpanote = {
  604. .namesz = sizeof("IBM,RPA-Client-Config"),
  605. .descsz = sizeof(struct rpadesc),
  606. .type = 0x12759999,
  607. .name = "IBM,RPA-Client-Config",
  608. .rpadesc = {
  609. .lpar_affinity = 0,
  610. .min_rmo_size = 64, /* in megabytes */
  611. .min_rmo_percent = 0,
  612. .max_pft_size = 48, /* 2^48 bytes max PFT size */
  613. .splpar = 1,
  614. .min_load = ~0U,
  615. .new_mem_def = 0
  616. }
  617. }
  618. };
  619. static void __init prom_send_capabilities(void)
  620. {
  621. ihandle elfloader;
  622. elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
  623. if (elfloader == 0) {
  624. prom_printf("couldn't open /packages/elf-loader\n");
  625. return;
  626. }
  627. call_prom("call-method", 3, 1, ADDR("process-elf-header"),
  628. elfloader, ADDR(&fake_elf));
  629. call_prom("close", 1, 0, elfloader);
  630. }
  631. #endif
  632. /*
  633. * Memory allocation strategy... our layout is normally:
  634. *
  635. * at 14Mb or more we have vmlinux, then a gap and initrd. In some
  636. * rare cases, initrd might end up being before the kernel though.
  637. * We assume this won't override the final kernel at 0, we have no
  638. * provision to handle that in this version, but it should hopefully
  639. * never happen.
  640. *
  641. * alloc_top is set to the top of RMO, eventually shrink down if the
  642. * TCEs overlap
  643. *
  644. * alloc_bottom is set to the top of kernel/initrd
  645. *
  646. * from there, allocations are done this way : rtas is allocated
  647. * topmost, and the device-tree is allocated from the bottom. We try
  648. * to grow the device-tree allocation as we progress. If we can't,
  649. * then we fail, we don't currently have a facility to restart
  650. * elsewhere, but that shouldn't be necessary.
  651. *
  652. * Note that calls to reserve_mem have to be done explicitly, memory
  653. * allocated with either alloc_up or alloc_down isn't automatically
  654. * reserved.
  655. */
  656. /*
  657. * Allocates memory in the RMO upward from the kernel/initrd
  658. *
  659. * When align is 0, this is a special case, it means to allocate in place
  660. * at the current location of alloc_bottom or fail (that is basically
  661. * extending the previous allocation). Used for the device-tree flattening
  662. */
  663. static unsigned long __init alloc_up(unsigned long size, unsigned long align)
  664. {
  665. unsigned long base = RELOC(alloc_bottom);
  666. unsigned long addr = 0;
  667. if (align)
  668. base = _ALIGN_UP(base, align);
  669. prom_debug("alloc_up(%x, %x)\n", size, align);
  670. if (RELOC(ram_top) == 0)
  671. prom_panic("alloc_up() called with mem not initialized\n");
  672. if (align)
  673. base = _ALIGN_UP(RELOC(alloc_bottom), align);
  674. else
  675. base = RELOC(alloc_bottom);
  676. for(; (base + size) <= RELOC(alloc_top);
  677. base = _ALIGN_UP(base + 0x100000, align)) {
  678. prom_debug(" trying: 0x%x\n\r", base);
  679. addr = (unsigned long)prom_claim(base, size, 0);
  680. if (addr != PROM_ERROR && addr != 0)
  681. break;
  682. addr = 0;
  683. if (align == 0)
  684. break;
  685. }
  686. if (addr == 0)
  687. return 0;
  688. RELOC(alloc_bottom) = addr;
  689. prom_debug(" -> %x\n", addr);
  690. prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  691. prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
  692. prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  693. prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
  694. prom_debug(" ram_top : %x\n", RELOC(ram_top));
  695. return addr;
  696. }
  697. /*
  698. * Allocates memory downward, either from top of RMO, or if highmem
  699. * is set, from the top of RAM. Note that this one doesn't handle
  700. * failures. It does claim memory if highmem is not set.
  701. */
  702. static unsigned long __init alloc_down(unsigned long size, unsigned long align,
  703. int highmem)
  704. {
  705. unsigned long base, addr = 0;
  706. prom_debug("alloc_down(%x, %x, %s)\n", size, align,
  707. highmem ? RELOC("(high)") : RELOC("(low)"));
  708. if (RELOC(ram_top) == 0)
  709. prom_panic("alloc_down() called with mem not initialized\n");
  710. if (highmem) {
  711. /* Carve out storage for the TCE table. */
  712. addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
  713. if (addr <= RELOC(alloc_bottom))
  714. return 0;
  715. /* Will we bump into the RMO ? If yes, check out that we
  716. * didn't overlap existing allocations there, if we did,
  717. * we are dead, we must be the first in town !
  718. */
  719. if (addr < RELOC(rmo_top)) {
  720. /* Good, we are first */
  721. if (RELOC(alloc_top) == RELOC(rmo_top))
  722. RELOC(alloc_top) = RELOC(rmo_top) = addr;
  723. else
  724. return 0;
  725. }
  726. RELOC(alloc_top_high) = addr;
  727. goto bail;
  728. }
  729. base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
  730. for (; base > RELOC(alloc_bottom);
  731. base = _ALIGN_DOWN(base - 0x100000, align)) {
  732. prom_debug(" trying: 0x%x\n\r", base);
  733. addr = (unsigned long)prom_claim(base, size, 0);
  734. if (addr != PROM_ERROR && addr != 0)
  735. break;
  736. addr = 0;
  737. }
  738. if (addr == 0)
  739. return 0;
  740. RELOC(alloc_top) = addr;
  741. bail:
  742. prom_debug(" -> %x\n", addr);
  743. prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  744. prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
  745. prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  746. prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
  747. prom_debug(" ram_top : %x\n", RELOC(ram_top));
  748. return addr;
  749. }
  750. /*
  751. * Parse a "reg" cell
  752. */
  753. static unsigned long __init prom_next_cell(int s, cell_t **cellp)
  754. {
  755. cell_t *p = *cellp;
  756. unsigned long r = 0;
  757. /* Ignore more than 2 cells */
  758. while (s > sizeof(unsigned long) / 4) {
  759. p++;
  760. s--;
  761. }
  762. r = *p++;
  763. #ifdef CONFIG_PPC64
  764. if (s > 1) {
  765. r <<= 32;
  766. r |= *(p++);
  767. }
  768. #endif
  769. *cellp = p;
  770. return r;
  771. }
  772. /*
  773. * Very dumb function for adding to the memory reserve list, but
  774. * we don't need anything smarter at this point
  775. *
  776. * XXX Eventually check for collisions. They should NEVER happen.
  777. * If problems seem to show up, it would be a good start to track
  778. * them down.
  779. */
  780. static void reserve_mem(u64 base, u64 size)
  781. {
  782. u64 top = base + size;
  783. unsigned long cnt = RELOC(mem_reserve_cnt);
  784. if (size == 0)
  785. return;
  786. /* We need to always keep one empty entry so that we
  787. * have our terminator with "size" set to 0 since we are
  788. * dumb and just copy this entire array to the boot params
  789. */
  790. base = _ALIGN_DOWN(base, PAGE_SIZE);
  791. top = _ALIGN_UP(top, PAGE_SIZE);
  792. size = top - base;
  793. if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
  794. prom_panic("Memory reserve map exhausted !\n");
  795. RELOC(mem_reserve_map)[cnt].base = base;
  796. RELOC(mem_reserve_map)[cnt].size = size;
  797. RELOC(mem_reserve_cnt) = cnt + 1;
  798. }
  799. /*
  800. * Initialize memory allocation mecanism, parse "memory" nodes and
  801. * obtain that way the top of memory and RMO to setup out local allocator
  802. */
  803. static void __init prom_init_mem(void)
  804. {
  805. phandle node;
  806. char *path, type[64];
  807. unsigned int plen;
  808. cell_t *p, *endp;
  809. struct prom_t *_prom = &RELOC(prom);
  810. u32 rac, rsc;
  811. /*
  812. * We iterate the memory nodes to find
  813. * 1) top of RMO (first node)
  814. * 2) top of memory
  815. */
  816. rac = 2;
  817. prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
  818. rsc = 1;
  819. prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
  820. prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
  821. prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
  822. prom_debug("scanning memory:\n");
  823. path = RELOC(prom_scratch);
  824. for (node = 0; prom_next_node(&node); ) {
  825. type[0] = 0;
  826. prom_getprop(node, "device_type", type, sizeof(type));
  827. if (type[0] == 0) {
  828. /*
  829. * CHRP Longtrail machines have no device_type
  830. * on the memory node, so check the name instead...
  831. */
  832. prom_getprop(node, "name", type, sizeof(type));
  833. }
  834. if (strcmp(type, RELOC("memory")))
  835. continue;
  836. plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
  837. if (plen > sizeof(regbuf)) {
  838. prom_printf("memory node too large for buffer !\n");
  839. plen = sizeof(regbuf);
  840. }
  841. p = RELOC(regbuf);
  842. endp = p + (plen / sizeof(cell_t));
  843. #ifdef DEBUG_PROM
  844. memset(path, 0, PROM_SCRATCH_SIZE);
  845. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  846. prom_debug(" node %s :\n", path);
  847. #endif /* DEBUG_PROM */
  848. while ((endp - p) >= (rac + rsc)) {
  849. unsigned long base, size;
  850. base = prom_next_cell(rac, &p);
  851. size = prom_next_cell(rsc, &p);
  852. if (size == 0)
  853. continue;
  854. prom_debug(" %x %x\n", base, size);
  855. if (base == 0)
  856. RELOC(rmo_top) = size;
  857. if ((base + size) > RELOC(ram_top))
  858. RELOC(ram_top) = base + size;
  859. }
  860. }
  861. RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
  862. /* Check if we have an initrd after the kernel, if we do move our bottom
  863. * point to after it
  864. */
  865. if (RELOC(prom_initrd_start)) {
  866. if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
  867. RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
  868. }
  869. /*
  870. * If prom_memory_limit is set we reduce the upper limits *except* for
  871. * alloc_top_high. This must be the real top of RAM so we can put
  872. * TCE's up there.
  873. */
  874. RELOC(alloc_top_high) = RELOC(ram_top);
  875. if (RELOC(prom_memory_limit)) {
  876. if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
  877. prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
  878. RELOC(prom_memory_limit));
  879. RELOC(prom_memory_limit) = 0;
  880. } else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
  881. prom_printf("Ignoring mem=%x >= ram_top.\n",
  882. RELOC(prom_memory_limit));
  883. RELOC(prom_memory_limit) = 0;
  884. } else {
  885. RELOC(ram_top) = RELOC(prom_memory_limit);
  886. RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
  887. }
  888. }
  889. /*
  890. * Setup our top alloc point, that is top of RMO or top of
  891. * segment 0 when running non-LPAR.
  892. * Some RS64 machines have buggy firmware where claims up at
  893. * 1GB fail. Cap at 768MB as a workaround.
  894. * Since 768MB is plenty of room, and we need to cap to something
  895. * reasonable on 32-bit, cap at 768MB on all machines.
  896. */
  897. if (!RELOC(rmo_top))
  898. RELOC(rmo_top) = RELOC(ram_top);
  899. RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
  900. RELOC(alloc_top) = RELOC(rmo_top);
  901. prom_printf("memory layout at init:\n");
  902. prom_printf(" memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
  903. prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  904. prom_printf(" alloc_top : %x\n", RELOC(alloc_top));
  905. prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  906. prom_printf(" rmo_top : %x\n", RELOC(rmo_top));
  907. prom_printf(" ram_top : %x\n", RELOC(ram_top));
  908. #ifdef CONFIG_KEXEC
  909. if (RELOC(prom_crashk_base)) {
  910. prom_printf(" crashk_base : %x\n", RELOC(prom_crashk_base));
  911. prom_printf(" crashk_size : %x\n", RELOC(prom_crashk_size));
  912. }
  913. #endif
  914. }
  915. /*
  916. * Allocate room for and instantiate RTAS
  917. */
  918. static void __init prom_instantiate_rtas(void)
  919. {
  920. phandle rtas_node;
  921. ihandle rtas_inst;
  922. u32 base, entry = 0;
  923. u32 size = 0;
  924. prom_debug("prom_instantiate_rtas: start...\n");
  925. rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  926. prom_debug("rtas_node: %x\n", rtas_node);
  927. if (!PHANDLE_VALID(rtas_node))
  928. return;
  929. prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
  930. if (size == 0)
  931. return;
  932. base = alloc_down(size, PAGE_SIZE, 0);
  933. if (base == 0) {
  934. prom_printf("RTAS allocation failed !\n");
  935. return;
  936. }
  937. rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
  938. if (!IHANDLE_VALID(rtas_inst)) {
  939. prom_printf("opening rtas package failed (%x)\n", rtas_inst);
  940. return;
  941. }
  942. prom_printf("instantiating rtas at 0x%x ...", base);
  943. if (call_prom_ret("call-method", 3, 2, &entry,
  944. ADDR("instantiate-rtas"),
  945. rtas_inst, base) != 0
  946. || entry == 0) {
  947. prom_printf(" failed\n");
  948. return;
  949. }
  950. prom_printf(" done\n");
  951. reserve_mem(base, size);
  952. prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
  953. &base, sizeof(base));
  954. prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
  955. &entry, sizeof(entry));
  956. prom_debug("rtas base = 0x%x\n", base);
  957. prom_debug("rtas entry = 0x%x\n", entry);
  958. prom_debug("rtas size = 0x%x\n", (long)size);
  959. prom_debug("prom_instantiate_rtas: end...\n");
  960. }
  961. #ifdef CONFIG_PPC64
  962. /*
  963. * Allocate room for and initialize TCE tables
  964. */
  965. static void __init prom_initialize_tce_table(void)
  966. {
  967. phandle node;
  968. ihandle phb_node;
  969. char compatible[64], type[64], model[64];
  970. char *path = RELOC(prom_scratch);
  971. u64 base, align;
  972. u32 minalign, minsize;
  973. u64 tce_entry, *tce_entryp;
  974. u64 local_alloc_top, local_alloc_bottom;
  975. u64 i;
  976. if (RELOC(ppc64_iommu_off))
  977. return;
  978. prom_debug("starting prom_initialize_tce_table\n");
  979. /* Cache current top of allocs so we reserve a single block */
  980. local_alloc_top = RELOC(alloc_top_high);
  981. local_alloc_bottom = local_alloc_top;
  982. /* Search all nodes looking for PHBs. */
  983. for (node = 0; prom_next_node(&node); ) {
  984. compatible[0] = 0;
  985. type[0] = 0;
  986. model[0] = 0;
  987. prom_getprop(node, "compatible",
  988. compatible, sizeof(compatible));
  989. prom_getprop(node, "device_type", type, sizeof(type));
  990. prom_getprop(node, "model", model, sizeof(model));
  991. if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
  992. continue;
  993. /* Keep the old logic in tack to avoid regression. */
  994. if (compatible[0] != 0) {
  995. if ((strstr(compatible, RELOC("python")) == NULL) &&
  996. (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
  997. (strstr(compatible, RELOC("Winnipeg")) == NULL))
  998. continue;
  999. } else if (model[0] != 0) {
  1000. if ((strstr(model, RELOC("ython")) == NULL) &&
  1001. (strstr(model, RELOC("peedwagon")) == NULL) &&
  1002. (strstr(model, RELOC("innipeg")) == NULL))
  1003. continue;
  1004. }
  1005. if (prom_getprop(node, "tce-table-minalign", &minalign,
  1006. sizeof(minalign)) == PROM_ERROR)
  1007. minalign = 0;
  1008. if (prom_getprop(node, "tce-table-minsize", &minsize,
  1009. sizeof(minsize)) == PROM_ERROR)
  1010. minsize = 4UL << 20;
  1011. /*
  1012. * Even though we read what OF wants, we just set the table
  1013. * size to 4 MB. This is enough to map 2GB of PCI DMA space.
  1014. * By doing this, we avoid the pitfalls of trying to DMA to
  1015. * MMIO space and the DMA alias hole.
  1016. *
  1017. * On POWER4, firmware sets the TCE region by assuming
  1018. * each TCE table is 8MB. Using this memory for anything
  1019. * else will impact performance, so we always allocate 8MB.
  1020. * Anton
  1021. */
  1022. if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
  1023. minsize = 8UL << 20;
  1024. else
  1025. minsize = 4UL << 20;
  1026. /* Align to the greater of the align or size */
  1027. align = max(minalign, minsize);
  1028. base = alloc_down(minsize, align, 1);
  1029. if (base == 0)
  1030. prom_panic("ERROR, cannot find space for TCE table.\n");
  1031. if (base < local_alloc_bottom)
  1032. local_alloc_bottom = base;
  1033. /* It seems OF doesn't null-terminate the path :-( */
  1034. memset(path, 0, sizeof(path));
  1035. /* Call OF to setup the TCE hardware */
  1036. if (call_prom("package-to-path", 3, 1, node,
  1037. path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
  1038. prom_printf("package-to-path failed\n");
  1039. }
  1040. /* Save away the TCE table attributes for later use. */
  1041. prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
  1042. prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
  1043. prom_debug("TCE table: %s\n", path);
  1044. prom_debug("\tnode = 0x%x\n", node);
  1045. prom_debug("\tbase = 0x%x\n", base);
  1046. prom_debug("\tsize = 0x%x\n", minsize);
  1047. /* Initialize the table to have a one-to-one mapping
  1048. * over the allocated size.
  1049. */
  1050. tce_entryp = (unsigned long *)base;
  1051. for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
  1052. tce_entry = (i << PAGE_SHIFT);
  1053. tce_entry |= 0x3;
  1054. *tce_entryp = tce_entry;
  1055. }
  1056. prom_printf("opening PHB %s", path);
  1057. phb_node = call_prom("open", 1, 1, path);
  1058. if (phb_node == 0)
  1059. prom_printf("... failed\n");
  1060. else
  1061. prom_printf("... done\n");
  1062. call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
  1063. phb_node, -1, minsize,
  1064. (u32) base, (u32) (base >> 32));
  1065. call_prom("close", 1, 0, phb_node);
  1066. }
  1067. reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
  1068. if (RELOC(prom_memory_limit)) {
  1069. /*
  1070. * We align the start to a 16MB boundary so we can map
  1071. * the TCE area using large pages if possible.
  1072. * The end should be the top of RAM so no need to align it.
  1073. */
  1074. RELOC(prom_tce_alloc_start) = _ALIGN_DOWN(local_alloc_bottom,
  1075. 0x1000000);
  1076. RELOC(prom_tce_alloc_end) = local_alloc_top;
  1077. }
  1078. /* Flag the first invalid entry */
  1079. prom_debug("ending prom_initialize_tce_table\n");
  1080. }
  1081. #endif
  1082. /*
  1083. * With CHRP SMP we need to use the OF to start the other processors.
  1084. * We can't wait until smp_boot_cpus (the OF is trashed by then)
  1085. * so we have to put the processors into a holding pattern controlled
  1086. * by the kernel (not OF) before we destroy the OF.
  1087. *
  1088. * This uses a chunk of low memory, puts some holding pattern
  1089. * code there and sends the other processors off to there until
  1090. * smp_boot_cpus tells them to do something. The holding pattern
  1091. * checks that address until its cpu # is there, when it is that
  1092. * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
  1093. * of setting those values.
  1094. *
  1095. * We also use physical address 0x4 here to tell when a cpu
  1096. * is in its holding pattern code.
  1097. *
  1098. * -- Cort
  1099. */
  1100. extern void __secondary_hold(void);
  1101. extern unsigned long __secondary_hold_spinloop;
  1102. extern unsigned long __secondary_hold_acknowledge;
  1103. /*
  1104. * We want to reference the copy of __secondary_hold_* in the
  1105. * 0 - 0x100 address range
  1106. */
  1107. #define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
  1108. static void __init prom_hold_cpus(void)
  1109. {
  1110. unsigned long i;
  1111. unsigned int reg;
  1112. phandle node;
  1113. char type[64];
  1114. int cpuid = 0;
  1115. unsigned int interrupt_server[MAX_CPU_THREADS];
  1116. unsigned int cpu_threads, hw_cpu_num;
  1117. int propsize;
  1118. struct prom_t *_prom = &RELOC(prom);
  1119. unsigned long *spinloop
  1120. = (void *) LOW_ADDR(__secondary_hold_spinloop);
  1121. unsigned long *acknowledge
  1122. = (void *) LOW_ADDR(__secondary_hold_acknowledge);
  1123. #ifdef CONFIG_PPC64
  1124. /* __secondary_hold is actually a descriptor, not the text address */
  1125. unsigned long secondary_hold
  1126. = __pa(*PTRRELOC((unsigned long *)__secondary_hold));
  1127. #else
  1128. unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
  1129. #endif
  1130. prom_debug("prom_hold_cpus: start...\n");
  1131. prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
  1132. prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
  1133. prom_debug(" 1) acknowledge = 0x%x\n",
  1134. (unsigned long)acknowledge);
  1135. prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
  1136. prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
  1137. /* Set the common spinloop variable, so all of the secondary cpus
  1138. * will block when they are awakened from their OF spinloop.
  1139. * This must occur for both SMP and non SMP kernels, since OF will
  1140. * be trashed when we move the kernel.
  1141. */
  1142. *spinloop = 0;
  1143. #ifdef CONFIG_HMT
  1144. for (i = 0; i < NR_CPUS; i++)
  1145. RELOC(hmt_thread_data)[i].pir = 0xdeadbeef;
  1146. #endif
  1147. /* look for cpus */
  1148. for (node = 0; prom_next_node(&node); ) {
  1149. type[0] = 0;
  1150. prom_getprop(node, "device_type", type, sizeof(type));
  1151. if (strcmp(type, RELOC("cpu")) != 0)
  1152. continue;
  1153. /* Skip non-configured cpus. */
  1154. if (prom_getprop(node, "status", type, sizeof(type)) > 0)
  1155. if (strcmp(type, RELOC("okay")) != 0)
  1156. continue;
  1157. reg = -1;
  1158. prom_getprop(node, "reg", &reg, sizeof(reg));
  1159. prom_debug("\ncpuid = 0x%x\n", cpuid);
  1160. prom_debug("cpu hw idx = 0x%x\n", reg);
  1161. /* Init the acknowledge var which will be reset by
  1162. * the secondary cpu when it awakens from its OF
  1163. * spinloop.
  1164. */
  1165. *acknowledge = (unsigned long)-1;
  1166. propsize = prom_getprop(node, "ibm,ppc-interrupt-server#s",
  1167. &interrupt_server,
  1168. sizeof(interrupt_server));
  1169. if (propsize < 0) {
  1170. /* no property. old hardware has no SMT */
  1171. cpu_threads = 1;
  1172. interrupt_server[0] = reg; /* fake it with phys id */
  1173. } else {
  1174. /* We have a threaded processor */
  1175. cpu_threads = propsize / sizeof(u32);
  1176. if (cpu_threads > MAX_CPU_THREADS) {
  1177. prom_printf("SMT: too many threads!\n"
  1178. "SMT: found %x, max is %x\n",
  1179. cpu_threads, MAX_CPU_THREADS);
  1180. cpu_threads = 1; /* ToDo: panic? */
  1181. }
  1182. }
  1183. hw_cpu_num = interrupt_server[0];
  1184. if (hw_cpu_num != _prom->cpu) {
  1185. /* Primary Thread of non-boot cpu */
  1186. prom_printf("%x : starting cpu hw idx %x... ", cpuid, reg);
  1187. call_prom("start-cpu", 3, 0, node,
  1188. secondary_hold, reg);
  1189. for (i = 0; (i < 100000000) &&
  1190. (*acknowledge == ((unsigned long)-1)); i++ )
  1191. mb();
  1192. if (*acknowledge == reg)
  1193. prom_printf("done\n");
  1194. else
  1195. prom_printf("failed: %x\n", *acknowledge);
  1196. }
  1197. #ifdef CONFIG_SMP
  1198. else
  1199. prom_printf("%x : boot cpu %x\n", cpuid, reg);
  1200. #endif /* CONFIG_SMP */
  1201. /* Reserve cpu #s for secondary threads. They start later. */
  1202. cpuid += cpu_threads;
  1203. }
  1204. #ifdef CONFIG_HMT
  1205. /* Only enable HMT on processors that provide support. */
  1206. if (__is_processor(PV_PULSAR) ||
  1207. __is_processor(PV_ICESTAR) ||
  1208. __is_processor(PV_SSTAR)) {
  1209. prom_printf(" starting secondary threads\n");
  1210. for (i = 0; i < NR_CPUS; i += 2) {
  1211. if (!cpu_online(i))
  1212. continue;
  1213. if (i == 0) {
  1214. unsigned long pir = mfspr(SPRN_PIR);
  1215. if (__is_processor(PV_PULSAR)) {
  1216. RELOC(hmt_thread_data)[i].pir =
  1217. pir & 0x1f;
  1218. } else {
  1219. RELOC(hmt_thread_data)[i].pir =
  1220. pir & 0x3ff;
  1221. }
  1222. }
  1223. }
  1224. } else {
  1225. prom_printf("Processor is not HMT capable\n");
  1226. }
  1227. #endif
  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. #endif
  1321. len = prom_getprop(_prom->root, "compatible",
  1322. compat, sizeof(compat)-1);
  1323. if (len > 0) {
  1324. compat[len] = 0;
  1325. while (i < len) {
  1326. char *p = &compat[i];
  1327. int sl = strlen(p);
  1328. if (sl == 0)
  1329. break;
  1330. if (strstr(p, RELOC("Power Macintosh")) ||
  1331. strstr(p, RELOC("MacRISC")))
  1332. return PLATFORM_POWERMAC;
  1333. #ifdef CONFIG_PPC64
  1334. if (strstr(p, RELOC("Momentum,Maple")))
  1335. return PLATFORM_MAPLE;
  1336. if (strstr(p, RELOC("IBM,CPB")))
  1337. return PLATFORM_CELL;
  1338. #endif
  1339. i += sl + 1;
  1340. }
  1341. }
  1342. #ifdef CONFIG_PPC64
  1343. /* Default to pSeries. We need to know if we are running LPAR */
  1344. rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  1345. if (PHANDLE_VALID(rtas)) {
  1346. int x = prom_getproplen(rtas, "ibm,hypertas-functions");
  1347. if (x != PROM_ERROR) {
  1348. prom_printf("Hypertas detected, assuming LPAR !\n");
  1349. return PLATFORM_PSERIES_LPAR;
  1350. }
  1351. }
  1352. return PLATFORM_PSERIES;
  1353. #else
  1354. return PLATFORM_CHRP;
  1355. #endif
  1356. }
  1357. static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
  1358. {
  1359. return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
  1360. }
  1361. /*
  1362. * If we have a display that we don't know how to drive,
  1363. * we will want to try to execute OF's open method for it
  1364. * later. However, OF will probably fall over if we do that
  1365. * we've taken over the MMU.
  1366. * So we check whether we will need to open the display,
  1367. * and if so, open it now.
  1368. */
  1369. static void __init prom_check_displays(void)
  1370. {
  1371. char type[16], *path;
  1372. phandle node;
  1373. ihandle ih;
  1374. int i;
  1375. static unsigned char default_colors[] = {
  1376. 0x00, 0x00, 0x00,
  1377. 0x00, 0x00, 0xaa,
  1378. 0x00, 0xaa, 0x00,
  1379. 0x00, 0xaa, 0xaa,
  1380. 0xaa, 0x00, 0x00,
  1381. 0xaa, 0x00, 0xaa,
  1382. 0xaa, 0xaa, 0x00,
  1383. 0xaa, 0xaa, 0xaa,
  1384. 0x55, 0x55, 0x55,
  1385. 0x55, 0x55, 0xff,
  1386. 0x55, 0xff, 0x55,
  1387. 0x55, 0xff, 0xff,
  1388. 0xff, 0x55, 0x55,
  1389. 0xff, 0x55, 0xff,
  1390. 0xff, 0xff, 0x55,
  1391. 0xff, 0xff, 0xff
  1392. };
  1393. const unsigned char *clut;
  1394. prom_printf("Looking for displays\n");
  1395. for (node = 0; prom_next_node(&node); ) {
  1396. memset(type, 0, sizeof(type));
  1397. prom_getprop(node, "device_type", type, sizeof(type));
  1398. if (strcmp(type, RELOC("display")) != 0)
  1399. continue;
  1400. /* It seems OF doesn't null-terminate the path :-( */
  1401. path = RELOC(prom_scratch);
  1402. memset(path, 0, PROM_SCRATCH_SIZE);
  1403. /*
  1404. * leave some room at the end of the path for appending extra
  1405. * arguments
  1406. */
  1407. if (call_prom("package-to-path", 3, 1, node, path,
  1408. PROM_SCRATCH_SIZE-10) == PROM_ERROR)
  1409. continue;
  1410. prom_printf("found display : %s, opening ... ", path);
  1411. ih = call_prom("open", 1, 1, path);
  1412. if (ih == 0) {
  1413. prom_printf("failed\n");
  1414. continue;
  1415. }
  1416. /* Success */
  1417. prom_printf("done\n");
  1418. prom_setprop(node, path, "linux,opened", NULL, 0);
  1419. /* Setup a usable color table when the appropriate
  1420. * method is available. Should update this to set-colors */
  1421. clut = RELOC(default_colors);
  1422. for (i = 0; i < 32; i++, clut += 3)
  1423. if (prom_set_color(ih, i, clut[0], clut[1],
  1424. clut[2]) != 0)
  1425. break;
  1426. #ifdef CONFIG_LOGO_LINUX_CLUT224
  1427. clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
  1428. for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
  1429. if (prom_set_color(ih, i + 32, clut[0], clut[1],
  1430. clut[2]) != 0)
  1431. break;
  1432. #endif /* CONFIG_LOGO_LINUX_CLUT224 */
  1433. }
  1434. }
  1435. /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
  1436. static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
  1437. unsigned long needed, unsigned long align)
  1438. {
  1439. void *ret;
  1440. *mem_start = _ALIGN(*mem_start, align);
  1441. while ((*mem_start + needed) > *mem_end) {
  1442. unsigned long room, chunk;
  1443. prom_debug("Chunk exhausted, claiming more at %x...\n",
  1444. RELOC(alloc_bottom));
  1445. room = RELOC(alloc_top) - RELOC(alloc_bottom);
  1446. if (room > DEVTREE_CHUNK_SIZE)
  1447. room = DEVTREE_CHUNK_SIZE;
  1448. if (room < PAGE_SIZE)
  1449. prom_panic("No memory for flatten_device_tree (no room)");
  1450. chunk = alloc_up(room, 0);
  1451. if (chunk == 0)
  1452. prom_panic("No memory for flatten_device_tree (claim failed)");
  1453. *mem_end = RELOC(alloc_top);
  1454. }
  1455. ret = (void *)*mem_start;
  1456. *mem_start += needed;
  1457. return ret;
  1458. }
  1459. #define dt_push_token(token, mem_start, mem_end) \
  1460. do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
  1461. static unsigned long __init dt_find_string(char *str)
  1462. {
  1463. char *s, *os;
  1464. s = os = (char *)RELOC(dt_string_start);
  1465. s += 4;
  1466. while (s < (char *)RELOC(dt_string_end)) {
  1467. if (strcmp(s, str) == 0)
  1468. return s - os;
  1469. s += strlen(s) + 1;
  1470. }
  1471. return 0;
  1472. }
  1473. /*
  1474. * The Open Firmware 1275 specification states properties must be 31 bytes or
  1475. * less, however not all firmwares obey this. Make it 64 bytes to be safe.
  1476. */
  1477. #define MAX_PROPERTY_NAME 64
  1478. static void __init scan_dt_build_strings(phandle node,
  1479. unsigned long *mem_start,
  1480. unsigned long *mem_end)
  1481. {
  1482. char *prev_name, *namep, *sstart;
  1483. unsigned long soff;
  1484. phandle child;
  1485. sstart = (char *)RELOC(dt_string_start);
  1486. /* get and store all property names */
  1487. prev_name = RELOC("");
  1488. for (;;) {
  1489. /* 64 is max len of name including nul. */
  1490. namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
  1491. if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
  1492. /* No more nodes: unwind alloc */
  1493. *mem_start = (unsigned long)namep;
  1494. break;
  1495. }
  1496. /* skip "name" */
  1497. if (strcmp(namep, RELOC("name")) == 0) {
  1498. *mem_start = (unsigned long)namep;
  1499. prev_name = RELOC("name");
  1500. continue;
  1501. }
  1502. /* get/create string entry */
  1503. soff = dt_find_string(namep);
  1504. if (soff != 0) {
  1505. *mem_start = (unsigned long)namep;
  1506. namep = sstart + soff;
  1507. } else {
  1508. /* Trim off some if we can */
  1509. *mem_start = (unsigned long)namep + strlen(namep) + 1;
  1510. RELOC(dt_string_end) = *mem_start;
  1511. }
  1512. prev_name = namep;
  1513. }
  1514. /* do all our children */
  1515. child = call_prom("child", 1, 1, node);
  1516. while (child != 0) {
  1517. scan_dt_build_strings(child, mem_start, mem_end);
  1518. child = call_prom("peer", 1, 1, child);
  1519. }
  1520. }
  1521. static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
  1522. unsigned long *mem_end)
  1523. {
  1524. phandle child;
  1525. char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
  1526. unsigned long soff;
  1527. unsigned char *valp;
  1528. static char pname[MAX_PROPERTY_NAME];
  1529. int l, room;
  1530. dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
  1531. /* get the node's full name */
  1532. namep = (char *)*mem_start;
  1533. room = *mem_end - *mem_start;
  1534. if (room > 255)
  1535. room = 255;
  1536. l = call_prom("package-to-path", 3, 1, node, namep, room);
  1537. if (l >= 0) {
  1538. /* Didn't fit? Get more room. */
  1539. if (l >= room) {
  1540. if (l >= *mem_end - *mem_start)
  1541. namep = make_room(mem_start, mem_end, l+1, 1);
  1542. call_prom("package-to-path", 3, 1, node, namep, l);
  1543. }
  1544. namep[l] = '\0';
  1545. /* Fixup an Apple bug where they have bogus \0 chars in the
  1546. * middle of the path in some properties, and extract
  1547. * the unit name (everything after the last '/').
  1548. */
  1549. for (lp = p = namep, ep = namep + l; p < ep; p++) {
  1550. if (*p == '/')
  1551. lp = namep;
  1552. else if (*p != 0)
  1553. *lp++ = *p;
  1554. }
  1555. *lp = 0;
  1556. *mem_start = _ALIGN((unsigned long)lp + 1, 4);
  1557. }
  1558. /* get it again for debugging */
  1559. path = RELOC(prom_scratch);
  1560. memset(path, 0, PROM_SCRATCH_SIZE);
  1561. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  1562. /* get and store all properties */
  1563. prev_name = RELOC("");
  1564. sstart = (char *)RELOC(dt_string_start);
  1565. for (;;) {
  1566. if (call_prom("nextprop", 3, 1, node, prev_name,
  1567. RELOC(pname)) != 1)
  1568. break;
  1569. /* skip "name" */
  1570. if (strcmp(RELOC(pname), RELOC("name")) == 0) {
  1571. prev_name = RELOC("name");
  1572. continue;
  1573. }
  1574. /* find string offset */
  1575. soff = dt_find_string(RELOC(pname));
  1576. if (soff == 0) {
  1577. prom_printf("WARNING: Can't find string index for"
  1578. " <%s>, node %s\n", RELOC(pname), path);
  1579. break;
  1580. }
  1581. prev_name = sstart + soff;
  1582. /* get length */
  1583. l = call_prom("getproplen", 2, 1, node, RELOC(pname));
  1584. /* sanity checks */
  1585. if (l == PROM_ERROR)
  1586. continue;
  1587. if (l > MAX_PROPERTY_LENGTH) {
  1588. prom_printf("WARNING: ignoring large property ");
  1589. /* It seems OF doesn't null-terminate the path :-( */
  1590. prom_printf("[%s] ", path);
  1591. prom_printf("%s length 0x%x\n", RELOC(pname), l);
  1592. continue;
  1593. }
  1594. /* push property head */
  1595. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1596. dt_push_token(l, mem_start, mem_end);
  1597. dt_push_token(soff, mem_start, mem_end);
  1598. /* push property content */
  1599. valp = make_room(mem_start, mem_end, l, 4);
  1600. call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
  1601. *mem_start = _ALIGN(*mem_start, 4);
  1602. }
  1603. /* Add a "linux,phandle" property. */
  1604. soff = dt_find_string(RELOC("linux,phandle"));
  1605. if (soff == 0)
  1606. prom_printf("WARNING: Can't find string index for"
  1607. " <linux-phandle> node %s\n", path);
  1608. else {
  1609. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1610. dt_push_token(4, mem_start, mem_end);
  1611. dt_push_token(soff, mem_start, mem_end);
  1612. valp = make_room(mem_start, mem_end, 4, 4);
  1613. *(u32 *)valp = node;
  1614. }
  1615. /* do all our children */
  1616. child = call_prom("child", 1, 1, node);
  1617. while (child != 0) {
  1618. scan_dt_build_struct(child, mem_start, mem_end);
  1619. child = call_prom("peer", 1, 1, child);
  1620. }
  1621. dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
  1622. }
  1623. static void __init flatten_device_tree(void)
  1624. {
  1625. phandle root;
  1626. unsigned long mem_start, mem_end, room;
  1627. struct boot_param_header *hdr;
  1628. struct prom_t *_prom = &RELOC(prom);
  1629. char *namep;
  1630. u64 *rsvmap;
  1631. /*
  1632. * Check how much room we have between alloc top & bottom (+/- a
  1633. * few pages), crop to 4Mb, as this is our "chuck" size
  1634. */
  1635. room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
  1636. if (room > DEVTREE_CHUNK_SIZE)
  1637. room = DEVTREE_CHUNK_SIZE;
  1638. prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
  1639. /* Now try to claim that */
  1640. mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
  1641. if (mem_start == 0)
  1642. prom_panic("Can't allocate initial device-tree chunk\n");
  1643. mem_end = RELOC(alloc_top);
  1644. /* Get root of tree */
  1645. root = call_prom("peer", 1, 1, (phandle)0);
  1646. if (root == (phandle)0)
  1647. prom_panic ("couldn't get device tree root\n");
  1648. /* Build header and make room for mem rsv map */
  1649. mem_start = _ALIGN(mem_start, 4);
  1650. hdr = make_room(&mem_start, &mem_end,
  1651. sizeof(struct boot_param_header), 4);
  1652. RELOC(dt_header_start) = (unsigned long)hdr;
  1653. rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
  1654. /* Start of strings */
  1655. mem_start = PAGE_ALIGN(mem_start);
  1656. RELOC(dt_string_start) = mem_start;
  1657. mem_start += 4; /* hole */
  1658. /* Add "linux,phandle" in there, we'll need it */
  1659. namep = make_room(&mem_start, &mem_end, 16, 1);
  1660. strcpy(namep, RELOC("linux,phandle"));
  1661. mem_start = (unsigned long)namep + strlen(namep) + 1;
  1662. /* Build string array */
  1663. prom_printf("Building dt strings...\n");
  1664. scan_dt_build_strings(root, &mem_start, &mem_end);
  1665. RELOC(dt_string_end) = mem_start;
  1666. /* Build structure */
  1667. mem_start = PAGE_ALIGN(mem_start);
  1668. RELOC(dt_struct_start) = mem_start;
  1669. prom_printf("Building dt structure...\n");
  1670. scan_dt_build_struct(root, &mem_start, &mem_end);
  1671. dt_push_token(OF_DT_END, &mem_start, &mem_end);
  1672. RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
  1673. /* Finish header */
  1674. hdr->boot_cpuid_phys = _prom->cpu;
  1675. hdr->magic = OF_DT_HEADER;
  1676. hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
  1677. hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
  1678. hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
  1679. hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
  1680. hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
  1681. hdr->version = OF_DT_VERSION;
  1682. /* Version 16 is not backward compatible */
  1683. hdr->last_comp_version = 0x10;
  1684. /* Reserve the whole thing and copy the reserve map in, we
  1685. * also bump mem_reserve_cnt to cause further reservations to
  1686. * fail since it's too late.
  1687. */
  1688. reserve_mem(RELOC(dt_header_start), hdr->totalsize);
  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. RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
  1701. prom_printf("Device tree strings 0x%x -> 0x%x\n",
  1702. RELOC(dt_string_start), RELOC(dt_string_end));
  1703. prom_printf("Device tree struct 0x%x -> 0x%x\n",
  1704. RELOC(dt_struct_start), RELOC(dt_struct_end));
  1705. }
  1706. static void __init fixup_device_tree(void)
  1707. {
  1708. #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
  1709. phandle u3, i2c, mpic;
  1710. u32 u3_rev;
  1711. u32 interrupts[2];
  1712. u32 parent;
  1713. /* Some G5s have a missing interrupt definition, fix it up here */
  1714. u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
  1715. if (!PHANDLE_VALID(u3))
  1716. return;
  1717. i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
  1718. if (!PHANDLE_VALID(i2c))
  1719. return;
  1720. mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
  1721. if (!PHANDLE_VALID(mpic))
  1722. return;
  1723. /* check if proper rev of u3 */
  1724. if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
  1725. == PROM_ERROR)
  1726. return;
  1727. if (u3_rev < 0x35 || u3_rev > 0x39)
  1728. return;
  1729. /* does it need fixup ? */
  1730. if (prom_getproplen(i2c, "interrupts") > 0)
  1731. return;
  1732. prom_printf("fixing up bogus interrupts for u3 i2c...\n");
  1733. /* interrupt on this revision of u3 is number 0 and level */
  1734. interrupts[0] = 0;
  1735. interrupts[1] = 1;
  1736. prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
  1737. &interrupts, sizeof(interrupts));
  1738. parent = (u32)mpic;
  1739. prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
  1740. &parent, sizeof(parent));
  1741. #endif
  1742. }
  1743. static void __init prom_find_boot_cpu(void)
  1744. {
  1745. struct prom_t *_prom = &RELOC(prom);
  1746. u32 getprop_rval;
  1747. ihandle prom_cpu;
  1748. phandle cpu_pkg;
  1749. _prom->cpu = 0;
  1750. if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
  1751. return;
  1752. cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
  1753. prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
  1754. _prom->cpu = getprop_rval;
  1755. prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
  1756. }
  1757. static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
  1758. {
  1759. #ifdef CONFIG_BLK_DEV_INITRD
  1760. struct prom_t *_prom = &RELOC(prom);
  1761. if (r3 && r4 && r4 != 0xdeadbeef) {
  1762. unsigned long val;
  1763. RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
  1764. RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
  1765. val = RELOC(prom_initrd_start);
  1766. prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
  1767. &val, sizeof(val));
  1768. val = RELOC(prom_initrd_end);
  1769. prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
  1770. &val, sizeof(val));
  1771. reserve_mem(RELOC(prom_initrd_start),
  1772. RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
  1773. prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
  1774. prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
  1775. }
  1776. #endif /* CONFIG_BLK_DEV_INITRD */
  1777. }
  1778. /*
  1779. * We enter here early on, when the Open Firmware prom is still
  1780. * handling exceptions and the MMU hash table for us.
  1781. */
  1782. unsigned long __init prom_init(unsigned long r3, unsigned long r4,
  1783. unsigned long pp,
  1784. unsigned long r6, unsigned long r7)
  1785. {
  1786. struct prom_t *_prom;
  1787. unsigned long hdr;
  1788. u32 getprop_rval;
  1789. unsigned long offset = reloc_offset();
  1790. #ifdef CONFIG_PPC32
  1791. reloc_got2(offset);
  1792. #endif
  1793. _prom = &RELOC(prom);
  1794. /*
  1795. * First zero the BSS
  1796. */
  1797. memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
  1798. /*
  1799. * Init interface to Open Firmware, get some node references,
  1800. * like /chosen
  1801. */
  1802. prom_init_client_services(pp);
  1803. /*
  1804. * See if this OF is old enough that we need to do explicit maps
  1805. * and other workarounds
  1806. */
  1807. prom_find_mmu();
  1808. /*
  1809. * Init prom stdout device
  1810. */
  1811. prom_init_stdout();
  1812. /*
  1813. * Check for an initrd
  1814. */
  1815. prom_check_initrd(r3, r4);
  1816. /*
  1817. * Get default machine type. At this point, we do not differentiate
  1818. * between pSeries SMP and pSeries LPAR
  1819. */
  1820. RELOC(of_platform) = prom_find_machine_type();
  1821. getprop_rval = RELOC(of_platform);
  1822. prom_setprop(_prom->chosen, "/chosen", "linux,platform",
  1823. &getprop_rval, sizeof(getprop_rval));
  1824. #ifdef CONFIG_PPC_PSERIES
  1825. /*
  1826. * On pSeries, inform the firmware about our capabilities
  1827. */
  1828. if (RELOC(of_platform) == PLATFORM_PSERIES ||
  1829. RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
  1830. prom_send_capabilities();
  1831. #endif
  1832. /*
  1833. * Copy the CPU hold code
  1834. */
  1835. if (RELOC(of_platform) != PLATFORM_POWERMAC)
  1836. copy_and_flush(0, KERNELBASE + offset, 0x100, 0);
  1837. /*
  1838. * Do early parsing of command line
  1839. */
  1840. early_cmdline_parse();
  1841. /*
  1842. * Initialize memory management within prom_init
  1843. */
  1844. prom_init_mem();
  1845. #ifdef CONFIG_KEXEC
  1846. if (RELOC(prom_crashk_base))
  1847. reserve_mem(RELOC(prom_crashk_base), RELOC(prom_crashk_size));
  1848. #endif
  1849. /*
  1850. * Determine which cpu is actually running right _now_
  1851. */
  1852. prom_find_boot_cpu();
  1853. /*
  1854. * Initialize display devices
  1855. */
  1856. prom_check_displays();
  1857. #ifdef CONFIG_PPC64
  1858. /*
  1859. * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
  1860. * that uses the allocator, we need to make sure we get the top of memory
  1861. * available for us here...
  1862. */
  1863. if (RELOC(of_platform) == PLATFORM_PSERIES)
  1864. prom_initialize_tce_table();
  1865. #endif
  1866. /*
  1867. * On non-powermacs, try to instantiate RTAS and puts all CPUs
  1868. * in spin-loops. PowerMacs don't have a working RTAS and use
  1869. * a different way to spin CPUs
  1870. */
  1871. if (RELOC(of_platform) != PLATFORM_POWERMAC) {
  1872. prom_instantiate_rtas();
  1873. prom_hold_cpus();
  1874. }
  1875. /*
  1876. * Fill in some infos for use by the kernel later on
  1877. */
  1878. if (RELOC(prom_memory_limit))
  1879. prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit",
  1880. &RELOC(prom_memory_limit),
  1881. sizeof(prom_memory_limit));
  1882. #ifdef CONFIG_PPC64
  1883. if (RELOC(ppc64_iommu_off))
  1884. prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
  1885. NULL, 0);
  1886. if (RELOC(iommu_force_on))
  1887. prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
  1888. NULL, 0);
  1889. if (RELOC(prom_tce_alloc_start)) {
  1890. prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
  1891. &RELOC(prom_tce_alloc_start),
  1892. sizeof(prom_tce_alloc_start));
  1893. prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
  1894. &RELOC(prom_tce_alloc_end),
  1895. sizeof(prom_tce_alloc_end));
  1896. }
  1897. #endif
  1898. #ifdef CONFIG_KEXEC
  1899. if (RELOC(prom_crashk_base)) {
  1900. prom_setprop(_prom->chosen, "/chosen", "linux,crashkernel-base",
  1901. PTRRELOC(&prom_crashk_base),
  1902. sizeof(RELOC(prom_crashk_base)));
  1903. prom_setprop(_prom->chosen, "/chosen", "linux,crashkernel-size",
  1904. PTRRELOC(&prom_crashk_size),
  1905. sizeof(RELOC(prom_crashk_size)));
  1906. }
  1907. #endif
  1908. /*
  1909. * Fixup any known bugs in the device-tree
  1910. */
  1911. fixup_device_tree();
  1912. /*
  1913. * Now finally create the flattened device-tree
  1914. */
  1915. prom_printf("copying OF device tree ...\n");
  1916. flatten_device_tree();
  1917. /*
  1918. * in case stdin is USB and still active on IBM machines...
  1919. * Unfortunately quiesce crashes on some powermacs if we have
  1920. * closed stdin already (in particular the powerbook 101).
  1921. */
  1922. if (RELOC(of_platform) != PLATFORM_POWERMAC)
  1923. prom_close_stdin();
  1924. /*
  1925. * Call OF "quiesce" method to shut down pending DMA's from
  1926. * devices etc...
  1927. */
  1928. prom_printf("Calling quiesce ...\n");
  1929. call_prom("quiesce", 0, 0);
  1930. /*
  1931. * And finally, call the kernel passing it the flattened device
  1932. * tree and NULL as r5, thus triggering the new entry point which
  1933. * is common to us and kexec
  1934. */
  1935. hdr = RELOC(dt_header_start);
  1936. prom_printf("returning from prom_init\n");
  1937. prom_debug("->dt_header_start=0x%x\n", hdr);
  1938. #ifdef CONFIG_PPC32
  1939. reloc_got2(-offset);
  1940. #endif
  1941. __start(hdr, KERNELBASE + offset, 0);
  1942. return 0;
  1943. }