prom_init.c 59 KB

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