prom_init.c 59 KB

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