prom_init.c 58 KB

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