prom_init.c 66 KB

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