prom_init.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509
  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. #define OV5_DONATE_DEDICATE_CPU 0x02 /* donate dedicated CPU support */
  536. /* PCIe/MSI support. Without MSI full PCIe is not supported */
  537. #ifdef CONFIG_PCI_MSI
  538. #define OV5_MSI 0x01 /* PCIe/MSI support */
  539. #else
  540. #define OV5_MSI 0x00
  541. #endif /* CONFIG_PCI_MSI */
  542. /*
  543. * The architecture vector has an array of PVR mask/value pairs,
  544. * followed by # option vectors - 1, followed by the option vectors.
  545. */
  546. static unsigned char ibm_architecture_vec[] = {
  547. W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */
  548. W(0xffff0000), W(0x003e0000), /* POWER6 */
  549. W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */
  550. W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */
  551. 5 - 1, /* 5 option vectors */
  552. /* option vector 1: processor architectures supported */
  553. 3 - 2, /* length */
  554. 0, /* don't ignore, don't halt */
  555. OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
  556. OV1_PPC_2_04 | OV1_PPC_2_05,
  557. /* option vector 2: Open Firmware options supported */
  558. 34 - 2, /* length */
  559. OV2_REAL_MODE,
  560. 0, 0,
  561. W(0xffffffff), /* real_base */
  562. W(0xffffffff), /* real_size */
  563. W(0xffffffff), /* virt_base */
  564. W(0xffffffff), /* virt_size */
  565. W(0xffffffff), /* load_base */
  566. W(64), /* 128MB min RMA */
  567. W(0xffffffff), /* full client load */
  568. 0, /* min RMA percentage of total RAM */
  569. 48, /* max log_2(hash table size) */
  570. /* option vector 3: processor options supported */
  571. 3 - 2, /* length */
  572. 0, /* don't ignore, don't halt */
  573. OV3_FP | OV3_VMX | OV3_DFP,
  574. /* option vector 4: IBM PAPR implementation */
  575. 2 - 2, /* length */
  576. 0, /* don't halt */
  577. /* option vector 5: PAPR/OF options */
  578. 3 - 2, /* length */
  579. 0, /* don't ignore, don't halt */
  580. OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
  581. OV5_DONATE_DEDICATE_CPU | OV5_MSI,
  582. };
  583. /* Old method - ELF header with PT_NOTE sections */
  584. static struct fake_elf {
  585. Elf32_Ehdr elfhdr;
  586. Elf32_Phdr phdr[2];
  587. struct chrpnote {
  588. u32 namesz;
  589. u32 descsz;
  590. u32 type;
  591. char name[8]; /* "PowerPC" */
  592. struct chrpdesc {
  593. u32 real_mode;
  594. u32 real_base;
  595. u32 real_size;
  596. u32 virt_base;
  597. u32 virt_size;
  598. u32 load_base;
  599. } chrpdesc;
  600. } chrpnote;
  601. struct rpanote {
  602. u32 namesz;
  603. u32 descsz;
  604. u32 type;
  605. char name[24]; /* "IBM,RPA-Client-Config" */
  606. struct rpadesc {
  607. u32 lpar_affinity;
  608. u32 min_rmo_size;
  609. u32 min_rmo_percent;
  610. u32 max_pft_size;
  611. u32 splpar;
  612. u32 min_load;
  613. u32 new_mem_def;
  614. u32 ignore_me;
  615. } rpadesc;
  616. } rpanote;
  617. } fake_elf = {
  618. .elfhdr = {
  619. .e_ident = { 0x7f, 'E', 'L', 'F',
  620. ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
  621. .e_type = ET_EXEC, /* yeah right */
  622. .e_machine = EM_PPC,
  623. .e_version = EV_CURRENT,
  624. .e_phoff = offsetof(struct fake_elf, phdr),
  625. .e_phentsize = sizeof(Elf32_Phdr),
  626. .e_phnum = 2
  627. },
  628. .phdr = {
  629. [0] = {
  630. .p_type = PT_NOTE,
  631. .p_offset = offsetof(struct fake_elf, chrpnote),
  632. .p_filesz = sizeof(struct chrpnote)
  633. }, [1] = {
  634. .p_type = PT_NOTE,
  635. .p_offset = offsetof(struct fake_elf, rpanote),
  636. .p_filesz = sizeof(struct rpanote)
  637. }
  638. },
  639. .chrpnote = {
  640. .namesz = sizeof("PowerPC"),
  641. .descsz = sizeof(struct chrpdesc),
  642. .type = 0x1275,
  643. .name = "PowerPC",
  644. .chrpdesc = {
  645. .real_mode = ~0U, /* ~0 means "don't care" */
  646. .real_base = ~0U,
  647. .real_size = ~0U,
  648. .virt_base = ~0U,
  649. .virt_size = ~0U,
  650. .load_base = ~0U
  651. },
  652. },
  653. .rpanote = {
  654. .namesz = sizeof("IBM,RPA-Client-Config"),
  655. .descsz = sizeof(struct rpadesc),
  656. .type = 0x12759999,
  657. .name = "IBM,RPA-Client-Config",
  658. .rpadesc = {
  659. .lpar_affinity = 0,
  660. .min_rmo_size = 64, /* in megabytes */
  661. .min_rmo_percent = 0,
  662. .max_pft_size = 48, /* 2^48 bytes max PFT size */
  663. .splpar = 1,
  664. .min_load = ~0U,
  665. .new_mem_def = 0
  666. }
  667. }
  668. };
  669. static void __init prom_send_capabilities(void)
  670. {
  671. ihandle elfloader, root;
  672. prom_arg_t ret;
  673. root = call_prom("open", 1, 1, ADDR("/"));
  674. if (root != 0) {
  675. /* try calling the ibm,client-architecture-support method */
  676. if (call_prom_ret("call-method", 3, 2, &ret,
  677. ADDR("ibm,client-architecture-support"),
  678. root,
  679. ADDR(ibm_architecture_vec)) == 0) {
  680. /* the call exists... */
  681. if (ret)
  682. prom_printf("WARNING: ibm,client-architecture"
  683. "-support call FAILED!\n");
  684. call_prom("close", 1, 0, root);
  685. return;
  686. }
  687. call_prom("close", 1, 0, root);
  688. }
  689. /* no ibm,client-architecture-support call, try the old way */
  690. elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
  691. if (elfloader == 0) {
  692. prom_printf("couldn't open /packages/elf-loader\n");
  693. return;
  694. }
  695. call_prom("call-method", 3, 1, ADDR("process-elf-header"),
  696. elfloader, ADDR(&fake_elf));
  697. call_prom("close", 1, 0, elfloader);
  698. }
  699. #endif
  700. /*
  701. * Memory allocation strategy... our layout is normally:
  702. *
  703. * at 14Mb or more we have vmlinux, then a gap and initrd. In some
  704. * rare cases, initrd might end up being before the kernel though.
  705. * We assume this won't override the final kernel at 0, we have no
  706. * provision to handle that in this version, but it should hopefully
  707. * never happen.
  708. *
  709. * alloc_top is set to the top of RMO, eventually shrink down if the
  710. * TCEs overlap
  711. *
  712. * alloc_bottom is set to the top of kernel/initrd
  713. *
  714. * from there, allocations are done this way : rtas is allocated
  715. * topmost, and the device-tree is allocated from the bottom. We try
  716. * to grow the device-tree allocation as we progress. If we can't,
  717. * then we fail, we don't currently have a facility to restart
  718. * elsewhere, but that shouldn't be necessary.
  719. *
  720. * Note that calls to reserve_mem have to be done explicitly, memory
  721. * allocated with either alloc_up or alloc_down isn't automatically
  722. * reserved.
  723. */
  724. /*
  725. * Allocates memory in the RMO upward from the kernel/initrd
  726. *
  727. * When align is 0, this is a special case, it means to allocate in place
  728. * at the current location of alloc_bottom or fail (that is basically
  729. * extending the previous allocation). Used for the device-tree flattening
  730. */
  731. static unsigned long __init alloc_up(unsigned long size, unsigned long align)
  732. {
  733. unsigned long base = RELOC(alloc_bottom);
  734. unsigned long addr = 0;
  735. if (align)
  736. base = _ALIGN_UP(base, align);
  737. prom_debug("alloc_up(%x, %x)\n", size, align);
  738. if (RELOC(ram_top) == 0)
  739. prom_panic("alloc_up() called with mem not initialized\n");
  740. if (align)
  741. base = _ALIGN_UP(RELOC(alloc_bottom), align);
  742. else
  743. base = RELOC(alloc_bottom);
  744. for(; (base + size) <= RELOC(alloc_top);
  745. base = _ALIGN_UP(base + 0x100000, align)) {
  746. prom_debug(" trying: 0x%x\n\r", base);
  747. addr = (unsigned long)prom_claim(base, size, 0);
  748. if (addr != PROM_ERROR && addr != 0)
  749. break;
  750. addr = 0;
  751. if (align == 0)
  752. break;
  753. }
  754. if (addr == 0)
  755. return 0;
  756. RELOC(alloc_bottom) = addr;
  757. prom_debug(" -> %x\n", addr);
  758. prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  759. prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
  760. prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  761. prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
  762. prom_debug(" ram_top : %x\n", RELOC(ram_top));
  763. return addr;
  764. }
  765. /*
  766. * Allocates memory downward, either from top of RMO, or if highmem
  767. * is set, from the top of RAM. Note that this one doesn't handle
  768. * failures. It does claim memory if highmem is not set.
  769. */
  770. static unsigned long __init alloc_down(unsigned long size, unsigned long align,
  771. int highmem)
  772. {
  773. unsigned long base, addr = 0;
  774. prom_debug("alloc_down(%x, %x, %s)\n", size, align,
  775. highmem ? RELOC("(high)") : RELOC("(low)"));
  776. if (RELOC(ram_top) == 0)
  777. prom_panic("alloc_down() called with mem not initialized\n");
  778. if (highmem) {
  779. /* Carve out storage for the TCE table. */
  780. addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
  781. if (addr <= RELOC(alloc_bottom))
  782. return 0;
  783. /* Will we bump into the RMO ? If yes, check out that we
  784. * didn't overlap existing allocations there, if we did,
  785. * we are dead, we must be the first in town !
  786. */
  787. if (addr < RELOC(rmo_top)) {
  788. /* Good, we are first */
  789. if (RELOC(alloc_top) == RELOC(rmo_top))
  790. RELOC(alloc_top) = RELOC(rmo_top) = addr;
  791. else
  792. return 0;
  793. }
  794. RELOC(alloc_top_high) = addr;
  795. goto bail;
  796. }
  797. base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
  798. for (; base > RELOC(alloc_bottom);
  799. base = _ALIGN_DOWN(base - 0x100000, align)) {
  800. prom_debug(" trying: 0x%x\n\r", base);
  801. addr = (unsigned long)prom_claim(base, size, 0);
  802. if (addr != PROM_ERROR && addr != 0)
  803. break;
  804. addr = 0;
  805. }
  806. if (addr == 0)
  807. return 0;
  808. RELOC(alloc_top) = addr;
  809. bail:
  810. prom_debug(" -> %x\n", addr);
  811. prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  812. prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
  813. prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  814. prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
  815. prom_debug(" ram_top : %x\n", RELOC(ram_top));
  816. return addr;
  817. }
  818. /*
  819. * Parse a "reg" cell
  820. */
  821. static unsigned long __init prom_next_cell(int s, cell_t **cellp)
  822. {
  823. cell_t *p = *cellp;
  824. unsigned long r = 0;
  825. /* Ignore more than 2 cells */
  826. while (s > sizeof(unsigned long) / 4) {
  827. p++;
  828. s--;
  829. }
  830. r = *p++;
  831. #ifdef CONFIG_PPC64
  832. if (s > 1) {
  833. r <<= 32;
  834. r |= *(p++);
  835. }
  836. #endif
  837. *cellp = p;
  838. return r;
  839. }
  840. /*
  841. * Very dumb function for adding to the memory reserve list, but
  842. * we don't need anything smarter at this point
  843. *
  844. * XXX Eventually check for collisions. They should NEVER happen.
  845. * If problems seem to show up, it would be a good start to track
  846. * them down.
  847. */
  848. static void __init reserve_mem(u64 base, u64 size)
  849. {
  850. u64 top = base + size;
  851. unsigned long cnt = RELOC(mem_reserve_cnt);
  852. if (size == 0)
  853. return;
  854. /* We need to always keep one empty entry so that we
  855. * have our terminator with "size" set to 0 since we are
  856. * dumb and just copy this entire array to the boot params
  857. */
  858. base = _ALIGN_DOWN(base, PAGE_SIZE);
  859. top = _ALIGN_UP(top, PAGE_SIZE);
  860. size = top - base;
  861. if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
  862. prom_panic("Memory reserve map exhausted !\n");
  863. RELOC(mem_reserve_map)[cnt].base = base;
  864. RELOC(mem_reserve_map)[cnt].size = size;
  865. RELOC(mem_reserve_cnt) = cnt + 1;
  866. }
  867. /*
  868. * Initialize memory allocation mechanism, parse "memory" nodes and
  869. * obtain that way the top of memory and RMO to setup out local allocator
  870. */
  871. static void __init prom_init_mem(void)
  872. {
  873. phandle node;
  874. char *path, type[64];
  875. unsigned int plen;
  876. cell_t *p, *endp;
  877. struct prom_t *_prom = &RELOC(prom);
  878. u32 rac, rsc;
  879. /*
  880. * We iterate the memory nodes to find
  881. * 1) top of RMO (first node)
  882. * 2) top of memory
  883. */
  884. rac = 2;
  885. prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
  886. rsc = 1;
  887. prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
  888. prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
  889. prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
  890. prom_debug("scanning memory:\n");
  891. path = RELOC(prom_scratch);
  892. for (node = 0; prom_next_node(&node); ) {
  893. type[0] = 0;
  894. prom_getprop(node, "device_type", type, sizeof(type));
  895. if (type[0] == 0) {
  896. /*
  897. * CHRP Longtrail machines have no device_type
  898. * on the memory node, so check the name instead...
  899. */
  900. prom_getprop(node, "name", type, sizeof(type));
  901. }
  902. if (strcmp(type, RELOC("memory")))
  903. continue;
  904. plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
  905. if (plen > sizeof(regbuf)) {
  906. prom_printf("memory node too large for buffer !\n");
  907. plen = sizeof(regbuf);
  908. }
  909. p = RELOC(regbuf);
  910. endp = p + (plen / sizeof(cell_t));
  911. #ifdef DEBUG_PROM
  912. memset(path, 0, PROM_SCRATCH_SIZE);
  913. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  914. prom_debug(" node %s :\n", path);
  915. #endif /* DEBUG_PROM */
  916. while ((endp - p) >= (rac + rsc)) {
  917. unsigned long base, size;
  918. base = prom_next_cell(rac, &p);
  919. size = prom_next_cell(rsc, &p);
  920. if (size == 0)
  921. continue;
  922. prom_debug(" %x %x\n", base, size);
  923. if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
  924. RELOC(rmo_top) = size;
  925. if ((base + size) > RELOC(ram_top))
  926. RELOC(ram_top) = base + size;
  927. }
  928. }
  929. RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
  930. /* Check if we have an initrd after the kernel, if we do move our bottom
  931. * point to after it
  932. */
  933. if (RELOC(prom_initrd_start)) {
  934. if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
  935. RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
  936. }
  937. /*
  938. * Setup our top alloc point, that is top of RMO or top of
  939. * segment 0 when running non-LPAR.
  940. * Some RS64 machines have buggy firmware where claims up at
  941. * 1GB fail. Cap at 768MB as a workaround.
  942. * Since 768MB is plenty of room, and we need to cap to something
  943. * reasonable on 32-bit, cap at 768MB on all machines.
  944. */
  945. if (!RELOC(rmo_top))
  946. RELOC(rmo_top) = RELOC(ram_top);
  947. RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
  948. RELOC(alloc_top) = RELOC(rmo_top);
  949. RELOC(alloc_top_high) = RELOC(ram_top);
  950. prom_printf("memory layout at init:\n");
  951. prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom));
  952. prom_printf(" alloc_top : %x\n", RELOC(alloc_top));
  953. prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
  954. prom_printf(" rmo_top : %x\n", RELOC(rmo_top));
  955. prom_printf(" ram_top : %x\n", RELOC(ram_top));
  956. }
  957. /*
  958. * Allocate room for and instantiate RTAS
  959. */
  960. static void __init prom_instantiate_rtas(void)
  961. {
  962. phandle rtas_node;
  963. ihandle rtas_inst;
  964. u32 base, entry = 0;
  965. u32 size = 0;
  966. prom_debug("prom_instantiate_rtas: start...\n");
  967. rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  968. prom_debug("rtas_node: %x\n", rtas_node);
  969. if (!PHANDLE_VALID(rtas_node))
  970. return;
  971. prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
  972. if (size == 0)
  973. return;
  974. base = alloc_down(size, PAGE_SIZE, 0);
  975. if (base == 0) {
  976. prom_printf("RTAS allocation failed !\n");
  977. return;
  978. }
  979. rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
  980. if (!IHANDLE_VALID(rtas_inst)) {
  981. prom_printf("opening rtas package failed (%x)\n", rtas_inst);
  982. return;
  983. }
  984. prom_printf("instantiating rtas at 0x%x ...", base);
  985. if (call_prom_ret("call-method", 3, 2, &entry,
  986. ADDR("instantiate-rtas"),
  987. rtas_inst, base) != 0
  988. || entry == 0) {
  989. prom_printf(" failed\n");
  990. return;
  991. }
  992. prom_printf(" done\n");
  993. reserve_mem(base, size);
  994. prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
  995. &base, sizeof(base));
  996. prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
  997. &entry, sizeof(entry));
  998. prom_debug("rtas base = 0x%x\n", base);
  999. prom_debug("rtas entry = 0x%x\n", entry);
  1000. prom_debug("rtas size = 0x%x\n", (long)size);
  1001. prom_debug("prom_instantiate_rtas: end...\n");
  1002. }
  1003. #ifdef CONFIG_PPC64
  1004. /*
  1005. * Allocate room for and initialize TCE tables
  1006. */
  1007. static void __init prom_initialize_tce_table(void)
  1008. {
  1009. phandle node;
  1010. ihandle phb_node;
  1011. char compatible[64], type[64], model[64];
  1012. char *path = RELOC(prom_scratch);
  1013. u64 base, align;
  1014. u32 minalign, minsize;
  1015. u64 tce_entry, *tce_entryp;
  1016. u64 local_alloc_top, local_alloc_bottom;
  1017. u64 i;
  1018. if (RELOC(prom_iommu_off))
  1019. return;
  1020. prom_debug("starting prom_initialize_tce_table\n");
  1021. /* Cache current top of allocs so we reserve a single block */
  1022. local_alloc_top = RELOC(alloc_top_high);
  1023. local_alloc_bottom = local_alloc_top;
  1024. /* Search all nodes looking for PHBs. */
  1025. for (node = 0; prom_next_node(&node); ) {
  1026. compatible[0] = 0;
  1027. type[0] = 0;
  1028. model[0] = 0;
  1029. prom_getprop(node, "compatible",
  1030. compatible, sizeof(compatible));
  1031. prom_getprop(node, "device_type", type, sizeof(type));
  1032. prom_getprop(node, "model", model, sizeof(model));
  1033. if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
  1034. continue;
  1035. /* Keep the old logic intact to avoid regression. */
  1036. if (compatible[0] != 0) {
  1037. if ((strstr(compatible, RELOC("python")) == NULL) &&
  1038. (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
  1039. (strstr(compatible, RELOC("Winnipeg")) == NULL))
  1040. continue;
  1041. } else if (model[0] != 0) {
  1042. if ((strstr(model, RELOC("ython")) == NULL) &&
  1043. (strstr(model, RELOC("peedwagon")) == NULL) &&
  1044. (strstr(model, RELOC("innipeg")) == NULL))
  1045. continue;
  1046. }
  1047. if (prom_getprop(node, "tce-table-minalign", &minalign,
  1048. sizeof(minalign)) == PROM_ERROR)
  1049. minalign = 0;
  1050. if (prom_getprop(node, "tce-table-minsize", &minsize,
  1051. sizeof(minsize)) == PROM_ERROR)
  1052. minsize = 4UL << 20;
  1053. /*
  1054. * Even though we read what OF wants, we just set the table
  1055. * size to 4 MB. This is enough to map 2GB of PCI DMA space.
  1056. * By doing this, we avoid the pitfalls of trying to DMA to
  1057. * MMIO space and the DMA alias hole.
  1058. *
  1059. * On POWER4, firmware sets the TCE region by assuming
  1060. * each TCE table is 8MB. Using this memory for anything
  1061. * else will impact performance, so we always allocate 8MB.
  1062. * Anton
  1063. */
  1064. if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
  1065. minsize = 8UL << 20;
  1066. else
  1067. minsize = 4UL << 20;
  1068. /* Align to the greater of the align or size */
  1069. align = max(minalign, minsize);
  1070. base = alloc_down(minsize, align, 1);
  1071. if (base == 0)
  1072. prom_panic("ERROR, cannot find space for TCE table.\n");
  1073. if (base < local_alloc_bottom)
  1074. local_alloc_bottom = base;
  1075. /* It seems OF doesn't null-terminate the path :-( */
  1076. memset(path, 0, PROM_SCRATCH_SIZE);
  1077. /* Call OF to setup the TCE hardware */
  1078. if (call_prom("package-to-path", 3, 1, node,
  1079. path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
  1080. prom_printf("package-to-path failed\n");
  1081. }
  1082. /* Save away the TCE table attributes for later use. */
  1083. prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
  1084. prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
  1085. prom_debug("TCE table: %s\n", path);
  1086. prom_debug("\tnode = 0x%x\n", node);
  1087. prom_debug("\tbase = 0x%x\n", base);
  1088. prom_debug("\tsize = 0x%x\n", minsize);
  1089. /* Initialize the table to have a one-to-one mapping
  1090. * over the allocated size.
  1091. */
  1092. tce_entryp = (unsigned long *)base;
  1093. for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
  1094. tce_entry = (i << PAGE_SHIFT);
  1095. tce_entry |= 0x3;
  1096. *tce_entryp = tce_entry;
  1097. }
  1098. prom_printf("opening PHB %s", path);
  1099. phb_node = call_prom("open", 1, 1, path);
  1100. if (phb_node == 0)
  1101. prom_printf("... failed\n");
  1102. else
  1103. prom_printf("... done\n");
  1104. call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
  1105. phb_node, -1, minsize,
  1106. (u32) base, (u32) (base >> 32));
  1107. call_prom("close", 1, 0, phb_node);
  1108. }
  1109. reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
  1110. /* These are only really needed if there is a memory limit in
  1111. * effect, but we don't know so export them always. */
  1112. RELOC(prom_tce_alloc_start) = local_alloc_bottom;
  1113. RELOC(prom_tce_alloc_end) = local_alloc_top;
  1114. /* Flag the first invalid entry */
  1115. prom_debug("ending prom_initialize_tce_table\n");
  1116. }
  1117. #endif
  1118. /*
  1119. * With CHRP SMP we need to use the OF to start the other processors.
  1120. * We can't wait until smp_boot_cpus (the OF is trashed by then)
  1121. * so we have to put the processors into a holding pattern controlled
  1122. * by the kernel (not OF) before we destroy the OF.
  1123. *
  1124. * This uses a chunk of low memory, puts some holding pattern
  1125. * code there and sends the other processors off to there until
  1126. * smp_boot_cpus tells them to do something. The holding pattern
  1127. * checks that address until its cpu # is there, when it is that
  1128. * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
  1129. * of setting those values.
  1130. *
  1131. * We also use physical address 0x4 here to tell when a cpu
  1132. * is in its holding pattern code.
  1133. *
  1134. * -- Cort
  1135. */
  1136. extern void __secondary_hold(void);
  1137. extern unsigned long __secondary_hold_spinloop;
  1138. extern unsigned long __secondary_hold_acknowledge;
  1139. /*
  1140. * We want to reference the copy of __secondary_hold_* in the
  1141. * 0 - 0x100 address range
  1142. */
  1143. #define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
  1144. static void __init prom_hold_cpus(void)
  1145. {
  1146. unsigned long i;
  1147. unsigned int reg;
  1148. phandle node;
  1149. char type[64];
  1150. int cpuid = 0;
  1151. unsigned int interrupt_server[MAX_CPU_THREADS];
  1152. unsigned int cpu_threads, hw_cpu_num;
  1153. int propsize;
  1154. struct prom_t *_prom = &RELOC(prom);
  1155. unsigned long *spinloop
  1156. = (void *) LOW_ADDR(__secondary_hold_spinloop);
  1157. unsigned long *acknowledge
  1158. = (void *) LOW_ADDR(__secondary_hold_acknowledge);
  1159. #ifdef CONFIG_PPC64
  1160. /* __secondary_hold is actually a descriptor, not the text address */
  1161. unsigned long secondary_hold
  1162. = __pa(*PTRRELOC((unsigned long *)__secondary_hold));
  1163. #else
  1164. unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
  1165. #endif
  1166. prom_debug("prom_hold_cpus: start...\n");
  1167. prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
  1168. prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
  1169. prom_debug(" 1) acknowledge = 0x%x\n",
  1170. (unsigned long)acknowledge);
  1171. prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
  1172. prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
  1173. /* Set the common spinloop variable, so all of the secondary cpus
  1174. * will block when they are awakened from their OF spinloop.
  1175. * This must occur for both SMP and non SMP kernels, since OF will
  1176. * be trashed when we move the kernel.
  1177. */
  1178. *spinloop = 0;
  1179. /* look for cpus */
  1180. for (node = 0; prom_next_node(&node); ) {
  1181. type[0] = 0;
  1182. prom_getprop(node, "device_type", type, sizeof(type));
  1183. if (strcmp(type, RELOC("cpu")) != 0)
  1184. continue;
  1185. /* Skip non-configured cpus. */
  1186. if (prom_getprop(node, "status", type, sizeof(type)) > 0)
  1187. if (strcmp(type, RELOC("okay")) != 0)
  1188. continue;
  1189. reg = -1;
  1190. prom_getprop(node, "reg", &reg, sizeof(reg));
  1191. prom_debug("\ncpuid = 0x%x\n", cpuid);
  1192. prom_debug("cpu hw idx = 0x%x\n", reg);
  1193. /* Init the acknowledge var which will be reset by
  1194. * the secondary cpu when it awakens from its OF
  1195. * spinloop.
  1196. */
  1197. *acknowledge = (unsigned long)-1;
  1198. propsize = prom_getprop(node, "ibm,ppc-interrupt-server#s",
  1199. &interrupt_server,
  1200. sizeof(interrupt_server));
  1201. if (propsize < 0) {
  1202. /* no property. old hardware has no SMT */
  1203. cpu_threads = 1;
  1204. interrupt_server[0] = reg; /* fake it with phys id */
  1205. } else {
  1206. /* We have a threaded processor */
  1207. cpu_threads = propsize / sizeof(u32);
  1208. if (cpu_threads > MAX_CPU_THREADS) {
  1209. prom_printf("SMT: too many threads!\n"
  1210. "SMT: found %x, max is %x\n",
  1211. cpu_threads, MAX_CPU_THREADS);
  1212. cpu_threads = 1; /* ToDo: panic? */
  1213. }
  1214. }
  1215. hw_cpu_num = interrupt_server[0];
  1216. if (hw_cpu_num != _prom->cpu) {
  1217. /* Primary Thread of non-boot cpu */
  1218. prom_printf("%x : starting cpu hw idx %x... ", cpuid, reg);
  1219. call_prom("start-cpu", 3, 0, node,
  1220. secondary_hold, reg);
  1221. for (i = 0; (i < 100000000) &&
  1222. (*acknowledge == ((unsigned long)-1)); i++ )
  1223. mb();
  1224. if (*acknowledge == reg)
  1225. prom_printf("done\n");
  1226. else
  1227. prom_printf("failed: %x\n", *acknowledge);
  1228. }
  1229. #ifdef CONFIG_SMP
  1230. else
  1231. prom_printf("%x : boot cpu %x\n", cpuid, reg);
  1232. #endif /* CONFIG_SMP */
  1233. /* Reserve cpu #s for secondary threads. They start later. */
  1234. cpuid += cpu_threads;
  1235. }
  1236. if (cpuid > NR_CPUS)
  1237. prom_printf("WARNING: maximum CPUs (" __stringify(NR_CPUS)
  1238. ") exceeded: ignoring extras\n");
  1239. prom_debug("prom_hold_cpus: end...\n");
  1240. }
  1241. static void __init prom_init_client_services(unsigned long pp)
  1242. {
  1243. struct prom_t *_prom = &RELOC(prom);
  1244. /* Get a handle to the prom entry point before anything else */
  1245. RELOC(prom_entry) = pp;
  1246. /* get a handle for the stdout device */
  1247. _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
  1248. if (!PHANDLE_VALID(_prom->chosen))
  1249. prom_panic("cannot find chosen"); /* msg won't be printed :( */
  1250. /* get device tree root */
  1251. _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
  1252. if (!PHANDLE_VALID(_prom->root))
  1253. prom_panic("cannot find device tree root"); /* msg won't be printed :( */
  1254. _prom->mmumap = 0;
  1255. }
  1256. #ifdef CONFIG_PPC32
  1257. /*
  1258. * For really old powermacs, we need to map things we claim.
  1259. * For that, we need the ihandle of the mmu.
  1260. * Also, on the longtrail, we need to work around other bugs.
  1261. */
  1262. static void __init prom_find_mmu(void)
  1263. {
  1264. struct prom_t *_prom = &RELOC(prom);
  1265. phandle oprom;
  1266. char version[64];
  1267. oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
  1268. if (!PHANDLE_VALID(oprom))
  1269. return;
  1270. if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
  1271. return;
  1272. version[sizeof(version) - 1] = 0;
  1273. /* XXX might need to add other versions here */
  1274. if (strcmp(version, "Open Firmware, 1.0.5") == 0)
  1275. of_workarounds = OF_WA_CLAIM;
  1276. else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
  1277. of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
  1278. call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
  1279. } else
  1280. return;
  1281. _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
  1282. prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
  1283. sizeof(_prom->mmumap));
  1284. if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
  1285. of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
  1286. }
  1287. #else
  1288. #define prom_find_mmu()
  1289. #endif
  1290. static void __init prom_init_stdout(void)
  1291. {
  1292. struct prom_t *_prom = &RELOC(prom);
  1293. char *path = RELOC(of_stdout_device);
  1294. char type[16];
  1295. u32 val;
  1296. if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
  1297. prom_panic("cannot find stdout");
  1298. _prom->stdout = val;
  1299. /* Get the full OF pathname of the stdout device */
  1300. memset(path, 0, 256);
  1301. call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
  1302. val = call_prom("instance-to-package", 1, 1, _prom->stdout);
  1303. prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
  1304. &val, sizeof(val));
  1305. prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
  1306. prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
  1307. path, strlen(path) + 1);
  1308. /* If it's a display, note it */
  1309. memset(type, 0, sizeof(type));
  1310. prom_getprop(val, "device_type", type, sizeof(type));
  1311. if (strcmp(type, RELOC("display")) == 0)
  1312. prom_setprop(val, path, "linux,boot-display", NULL, 0);
  1313. }
  1314. static void __init prom_close_stdin(void)
  1315. {
  1316. struct prom_t *_prom = &RELOC(prom);
  1317. ihandle val;
  1318. if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
  1319. call_prom("close", 1, 0, val);
  1320. }
  1321. static int __init prom_find_machine_type(void)
  1322. {
  1323. struct prom_t *_prom = &RELOC(prom);
  1324. char compat[256];
  1325. int len, i = 0;
  1326. #ifdef CONFIG_PPC64
  1327. phandle rtas;
  1328. int x;
  1329. #endif
  1330. /* Look for a PowerMac */
  1331. len = prom_getprop(_prom->root, "compatible",
  1332. compat, sizeof(compat)-1);
  1333. if (len > 0) {
  1334. compat[len] = 0;
  1335. while (i < len) {
  1336. char *p = &compat[i];
  1337. int sl = strlen(p);
  1338. if (sl == 0)
  1339. break;
  1340. if (strstr(p, RELOC("Power Macintosh")) ||
  1341. strstr(p, RELOC("MacRISC")))
  1342. return PLATFORM_POWERMAC;
  1343. #ifdef CONFIG_PPC64
  1344. /* We must make sure we don't detect the IBM Cell
  1345. * blades as pSeries due to some firmware issues,
  1346. * so we do it here.
  1347. */
  1348. if (strstr(p, RELOC("IBM,CBEA")) ||
  1349. strstr(p, RELOC("IBM,CPBW-1.0")))
  1350. return PLATFORM_GENERIC;
  1351. #endif /* CONFIG_PPC64 */
  1352. i += sl + 1;
  1353. }
  1354. }
  1355. #ifdef CONFIG_PPC64
  1356. /* If not a mac, try to figure out if it's an IBM pSeries or any other
  1357. * PAPR compliant platform. We assume it is if :
  1358. * - /device_type is "chrp" (please, do NOT use that for future
  1359. * non-IBM designs !
  1360. * - it has /rtas
  1361. */
  1362. len = prom_getprop(_prom->root, "device_type",
  1363. compat, sizeof(compat)-1);
  1364. if (len <= 0)
  1365. return PLATFORM_GENERIC;
  1366. if (strcmp(compat, RELOC("chrp")))
  1367. return PLATFORM_GENERIC;
  1368. /* Default to pSeries. We need to know if we are running LPAR */
  1369. rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
  1370. if (!PHANDLE_VALID(rtas))
  1371. return PLATFORM_GENERIC;
  1372. x = prom_getproplen(rtas, "ibm,hypertas-functions");
  1373. if (x != PROM_ERROR) {
  1374. prom_printf("Hypertas detected, assuming LPAR !\n");
  1375. return PLATFORM_PSERIES_LPAR;
  1376. }
  1377. return PLATFORM_PSERIES;
  1378. #else
  1379. return PLATFORM_GENERIC;
  1380. #endif
  1381. }
  1382. static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
  1383. {
  1384. return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
  1385. }
  1386. /*
  1387. * If we have a display that we don't know how to drive,
  1388. * we will want to try to execute OF's open method for it
  1389. * later. However, OF will probably fall over if we do that
  1390. * we've taken over the MMU.
  1391. * So we check whether we will need to open the display,
  1392. * and if so, open it now.
  1393. */
  1394. static void __init prom_check_displays(void)
  1395. {
  1396. char type[16], *path;
  1397. phandle node;
  1398. ihandle ih;
  1399. int i;
  1400. static unsigned char default_colors[] = {
  1401. 0x00, 0x00, 0x00,
  1402. 0x00, 0x00, 0xaa,
  1403. 0x00, 0xaa, 0x00,
  1404. 0x00, 0xaa, 0xaa,
  1405. 0xaa, 0x00, 0x00,
  1406. 0xaa, 0x00, 0xaa,
  1407. 0xaa, 0xaa, 0x00,
  1408. 0xaa, 0xaa, 0xaa,
  1409. 0x55, 0x55, 0x55,
  1410. 0x55, 0x55, 0xff,
  1411. 0x55, 0xff, 0x55,
  1412. 0x55, 0xff, 0xff,
  1413. 0xff, 0x55, 0x55,
  1414. 0xff, 0x55, 0xff,
  1415. 0xff, 0xff, 0x55,
  1416. 0xff, 0xff, 0xff
  1417. };
  1418. const unsigned char *clut;
  1419. prom_printf("Looking for displays\n");
  1420. for (node = 0; prom_next_node(&node); ) {
  1421. memset(type, 0, sizeof(type));
  1422. prom_getprop(node, "device_type", type, sizeof(type));
  1423. if (strcmp(type, RELOC("display")) != 0)
  1424. continue;
  1425. /* It seems OF doesn't null-terminate the path :-( */
  1426. path = RELOC(prom_scratch);
  1427. memset(path, 0, PROM_SCRATCH_SIZE);
  1428. /*
  1429. * leave some room at the end of the path for appending extra
  1430. * arguments
  1431. */
  1432. if (call_prom("package-to-path", 3, 1, node, path,
  1433. PROM_SCRATCH_SIZE-10) == PROM_ERROR)
  1434. continue;
  1435. prom_printf("found display : %s, opening ... ", path);
  1436. ih = call_prom("open", 1, 1, path);
  1437. if (ih == 0) {
  1438. prom_printf("failed\n");
  1439. continue;
  1440. }
  1441. /* Success */
  1442. prom_printf("done\n");
  1443. prom_setprop(node, path, "linux,opened", NULL, 0);
  1444. /* Setup a usable color table when the appropriate
  1445. * method is available. Should update this to set-colors */
  1446. clut = RELOC(default_colors);
  1447. for (i = 0; i < 32; i++, clut += 3)
  1448. if (prom_set_color(ih, i, clut[0], clut[1],
  1449. clut[2]) != 0)
  1450. break;
  1451. #ifdef CONFIG_LOGO_LINUX_CLUT224
  1452. clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
  1453. for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
  1454. if (prom_set_color(ih, i + 32, clut[0], clut[1],
  1455. clut[2]) != 0)
  1456. break;
  1457. #endif /* CONFIG_LOGO_LINUX_CLUT224 */
  1458. }
  1459. }
  1460. /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
  1461. static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
  1462. unsigned long needed, unsigned long align)
  1463. {
  1464. void *ret;
  1465. *mem_start = _ALIGN(*mem_start, align);
  1466. while ((*mem_start + needed) > *mem_end) {
  1467. unsigned long room, chunk;
  1468. prom_debug("Chunk exhausted, claiming more at %x...\n",
  1469. RELOC(alloc_bottom));
  1470. room = RELOC(alloc_top) - RELOC(alloc_bottom);
  1471. if (room > DEVTREE_CHUNK_SIZE)
  1472. room = DEVTREE_CHUNK_SIZE;
  1473. if (room < PAGE_SIZE)
  1474. prom_panic("No memory for flatten_device_tree (no room)");
  1475. chunk = alloc_up(room, 0);
  1476. if (chunk == 0)
  1477. prom_panic("No memory for flatten_device_tree (claim failed)");
  1478. *mem_end = RELOC(alloc_top);
  1479. }
  1480. ret = (void *)*mem_start;
  1481. *mem_start += needed;
  1482. return ret;
  1483. }
  1484. #define dt_push_token(token, mem_start, mem_end) \
  1485. do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
  1486. static unsigned long __init dt_find_string(char *str)
  1487. {
  1488. char *s, *os;
  1489. s = os = (char *)RELOC(dt_string_start);
  1490. s += 4;
  1491. while (s < (char *)RELOC(dt_string_end)) {
  1492. if (strcmp(s, str) == 0)
  1493. return s - os;
  1494. s += strlen(s) + 1;
  1495. }
  1496. return 0;
  1497. }
  1498. /*
  1499. * The Open Firmware 1275 specification states properties must be 31 bytes or
  1500. * less, however not all firmwares obey this. Make it 64 bytes to be safe.
  1501. */
  1502. #define MAX_PROPERTY_NAME 64
  1503. static void __init scan_dt_build_strings(phandle node,
  1504. unsigned long *mem_start,
  1505. unsigned long *mem_end)
  1506. {
  1507. char *prev_name, *namep, *sstart;
  1508. unsigned long soff;
  1509. phandle child;
  1510. sstart = (char *)RELOC(dt_string_start);
  1511. /* get and store all property names */
  1512. prev_name = RELOC("");
  1513. for (;;) {
  1514. /* 64 is max len of name including nul. */
  1515. namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
  1516. if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
  1517. /* No more nodes: unwind alloc */
  1518. *mem_start = (unsigned long)namep;
  1519. break;
  1520. }
  1521. /* skip "name" */
  1522. if (strcmp(namep, RELOC("name")) == 0) {
  1523. *mem_start = (unsigned long)namep;
  1524. prev_name = RELOC("name");
  1525. continue;
  1526. }
  1527. /* get/create string entry */
  1528. soff = dt_find_string(namep);
  1529. if (soff != 0) {
  1530. *mem_start = (unsigned long)namep;
  1531. namep = sstart + soff;
  1532. } else {
  1533. /* Trim off some if we can */
  1534. *mem_start = (unsigned long)namep + strlen(namep) + 1;
  1535. RELOC(dt_string_end) = *mem_start;
  1536. }
  1537. prev_name = namep;
  1538. }
  1539. /* do all our children */
  1540. child = call_prom("child", 1, 1, node);
  1541. while (child != 0) {
  1542. scan_dt_build_strings(child, mem_start, mem_end);
  1543. child = call_prom("peer", 1, 1, child);
  1544. }
  1545. }
  1546. static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
  1547. unsigned long *mem_end)
  1548. {
  1549. phandle child;
  1550. char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
  1551. unsigned long soff;
  1552. unsigned char *valp;
  1553. static char pname[MAX_PROPERTY_NAME];
  1554. int l, room;
  1555. dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
  1556. /* get the node's full name */
  1557. namep = (char *)*mem_start;
  1558. room = *mem_end - *mem_start;
  1559. if (room > 255)
  1560. room = 255;
  1561. l = call_prom("package-to-path", 3, 1, node, namep, room);
  1562. if (l >= 0) {
  1563. /* Didn't fit? Get more room. */
  1564. if (l >= room) {
  1565. if (l >= *mem_end - *mem_start)
  1566. namep = make_room(mem_start, mem_end, l+1, 1);
  1567. call_prom("package-to-path", 3, 1, node, namep, l);
  1568. }
  1569. namep[l] = '\0';
  1570. /* Fixup an Apple bug where they have bogus \0 chars in the
  1571. * middle of the path in some properties, and extract
  1572. * the unit name (everything after the last '/').
  1573. */
  1574. for (lp = p = namep, ep = namep + l; p < ep; p++) {
  1575. if (*p == '/')
  1576. lp = namep;
  1577. else if (*p != 0)
  1578. *lp++ = *p;
  1579. }
  1580. *lp = 0;
  1581. *mem_start = _ALIGN((unsigned long)lp + 1, 4);
  1582. }
  1583. /* get it again for debugging */
  1584. path = RELOC(prom_scratch);
  1585. memset(path, 0, PROM_SCRATCH_SIZE);
  1586. call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
  1587. /* get and store all properties */
  1588. prev_name = RELOC("");
  1589. sstart = (char *)RELOC(dt_string_start);
  1590. for (;;) {
  1591. if (call_prom("nextprop", 3, 1, node, prev_name,
  1592. RELOC(pname)) != 1)
  1593. break;
  1594. /* skip "name" */
  1595. if (strcmp(RELOC(pname), RELOC("name")) == 0) {
  1596. prev_name = RELOC("name");
  1597. continue;
  1598. }
  1599. /* find string offset */
  1600. soff = dt_find_string(RELOC(pname));
  1601. if (soff == 0) {
  1602. prom_printf("WARNING: Can't find string index for"
  1603. " <%s>, node %s\n", RELOC(pname), path);
  1604. break;
  1605. }
  1606. prev_name = sstart + soff;
  1607. /* get length */
  1608. l = call_prom("getproplen", 2, 1, node, RELOC(pname));
  1609. /* sanity checks */
  1610. if (l == PROM_ERROR)
  1611. continue;
  1612. if (l > MAX_PROPERTY_LENGTH) {
  1613. prom_printf("WARNING: ignoring large property ");
  1614. /* It seems OF doesn't null-terminate the path :-( */
  1615. prom_printf("[%s] ", path);
  1616. prom_printf("%s length 0x%x\n", RELOC(pname), l);
  1617. continue;
  1618. }
  1619. /* push property head */
  1620. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1621. dt_push_token(l, mem_start, mem_end);
  1622. dt_push_token(soff, mem_start, mem_end);
  1623. /* push property content */
  1624. valp = make_room(mem_start, mem_end, l, 4);
  1625. call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
  1626. *mem_start = _ALIGN(*mem_start, 4);
  1627. }
  1628. /* Add a "linux,phandle" property. */
  1629. soff = dt_find_string(RELOC("linux,phandle"));
  1630. if (soff == 0)
  1631. prom_printf("WARNING: Can't find string index for"
  1632. " <linux-phandle> node %s\n", path);
  1633. else {
  1634. dt_push_token(OF_DT_PROP, mem_start, mem_end);
  1635. dt_push_token(4, mem_start, mem_end);
  1636. dt_push_token(soff, mem_start, mem_end);
  1637. valp = make_room(mem_start, mem_end, 4, 4);
  1638. *(u32 *)valp = node;
  1639. }
  1640. /* do all our children */
  1641. child = call_prom("child", 1, 1, node);
  1642. while (child != 0) {
  1643. scan_dt_build_struct(child, mem_start, mem_end);
  1644. child = call_prom("peer", 1, 1, child);
  1645. }
  1646. dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
  1647. }
  1648. static void __init flatten_device_tree(void)
  1649. {
  1650. phandle root;
  1651. unsigned long mem_start, mem_end, room;
  1652. struct boot_param_header *hdr;
  1653. struct prom_t *_prom = &RELOC(prom);
  1654. char *namep;
  1655. u64 *rsvmap;
  1656. /*
  1657. * Check how much room we have between alloc top & bottom (+/- a
  1658. * few pages), crop to 4Mb, as this is our "chuck" size
  1659. */
  1660. room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
  1661. if (room > DEVTREE_CHUNK_SIZE)
  1662. room = DEVTREE_CHUNK_SIZE;
  1663. prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
  1664. /* Now try to claim that */
  1665. mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
  1666. if (mem_start == 0)
  1667. prom_panic("Can't allocate initial device-tree chunk\n");
  1668. mem_end = RELOC(alloc_top);
  1669. /* Get root of tree */
  1670. root = call_prom("peer", 1, 1, (phandle)0);
  1671. if (root == (phandle)0)
  1672. prom_panic ("couldn't get device tree root\n");
  1673. /* Build header and make room for mem rsv map */
  1674. mem_start = _ALIGN(mem_start, 4);
  1675. hdr = make_room(&mem_start, &mem_end,
  1676. sizeof(struct boot_param_header), 4);
  1677. RELOC(dt_header_start) = (unsigned long)hdr;
  1678. rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
  1679. /* Start of strings */
  1680. mem_start = PAGE_ALIGN(mem_start);
  1681. RELOC(dt_string_start) = mem_start;
  1682. mem_start += 4; /* hole */
  1683. /* Add "linux,phandle" in there, we'll need it */
  1684. namep = make_room(&mem_start, &mem_end, 16, 1);
  1685. strcpy(namep, RELOC("linux,phandle"));
  1686. mem_start = (unsigned long)namep + strlen(namep) + 1;
  1687. /* Build string array */
  1688. prom_printf("Building dt strings...\n");
  1689. scan_dt_build_strings(root, &mem_start, &mem_end);
  1690. RELOC(dt_string_end) = mem_start;
  1691. /* Build structure */
  1692. mem_start = PAGE_ALIGN(mem_start);
  1693. RELOC(dt_struct_start) = mem_start;
  1694. prom_printf("Building dt structure...\n");
  1695. scan_dt_build_struct(root, &mem_start, &mem_end);
  1696. dt_push_token(OF_DT_END, &mem_start, &mem_end);
  1697. RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
  1698. /* Finish header */
  1699. hdr->boot_cpuid_phys = _prom->cpu;
  1700. hdr->magic = OF_DT_HEADER;
  1701. hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
  1702. hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
  1703. hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
  1704. hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
  1705. hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
  1706. hdr->version = OF_DT_VERSION;
  1707. /* Version 16 is not backward compatible */
  1708. hdr->last_comp_version = 0x10;
  1709. /* Copy the reserve map in */
  1710. memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
  1711. #ifdef DEBUG_PROM
  1712. {
  1713. int i;
  1714. prom_printf("reserved memory map:\n");
  1715. for (i = 0; i < RELOC(mem_reserve_cnt); i++)
  1716. prom_printf(" %x - %x\n",
  1717. RELOC(mem_reserve_map)[i].base,
  1718. RELOC(mem_reserve_map)[i].size);
  1719. }
  1720. #endif
  1721. /* Bump mem_reserve_cnt to cause further reservations to fail
  1722. * since it's too late.
  1723. */
  1724. RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
  1725. prom_printf("Device tree strings 0x%x -> 0x%x\n",
  1726. RELOC(dt_string_start), RELOC(dt_string_end));
  1727. prom_printf("Device tree struct 0x%x -> 0x%x\n",
  1728. RELOC(dt_struct_start), RELOC(dt_struct_end));
  1729. }
  1730. #ifdef CONFIG_PPC_MAPLE
  1731. /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
  1732. * The values are bad, and it doesn't even have the right number of cells. */
  1733. static void __init fixup_device_tree_maple(void)
  1734. {
  1735. phandle isa;
  1736. u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
  1737. u32 isa_ranges[6];
  1738. char *name;
  1739. name = "/ht@0/isa@4";
  1740. isa = call_prom("finddevice", 1, 1, ADDR(name));
  1741. if (!PHANDLE_VALID(isa)) {
  1742. name = "/ht@0/isa@6";
  1743. isa = call_prom("finddevice", 1, 1, ADDR(name));
  1744. rloc = 0x01003000; /* IO space; PCI device = 6 */
  1745. }
  1746. if (!PHANDLE_VALID(isa))
  1747. return;
  1748. if (prom_getproplen(isa, "ranges") != 12)
  1749. return;
  1750. if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
  1751. == PROM_ERROR)
  1752. return;
  1753. if (isa_ranges[0] != 0x1 ||
  1754. isa_ranges[1] != 0xf4000000 ||
  1755. isa_ranges[2] != 0x00010000)
  1756. return;
  1757. prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
  1758. isa_ranges[0] = 0x1;
  1759. isa_ranges[1] = 0x0;
  1760. isa_ranges[2] = rloc;
  1761. isa_ranges[3] = 0x0;
  1762. isa_ranges[4] = 0x0;
  1763. isa_ranges[5] = 0x00010000;
  1764. prom_setprop(isa, name, "ranges",
  1765. isa_ranges, sizeof(isa_ranges));
  1766. }
  1767. #else
  1768. #define fixup_device_tree_maple()
  1769. #endif
  1770. #ifdef CONFIG_PPC_CHRP
  1771. /*
  1772. * Pegasos and BriQ lacks the "ranges" property in the isa node
  1773. * Pegasos needs decimal IRQ 14/15, not hexadecimal
  1774. * Pegasos has the IDE configured in legacy mode, but advertised as native
  1775. */
  1776. static void __init fixup_device_tree_chrp(void)
  1777. {
  1778. phandle ph;
  1779. u32 prop[6];
  1780. u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
  1781. char *name;
  1782. int rc;
  1783. name = "/pci@80000000/isa@c";
  1784. ph = call_prom("finddevice", 1, 1, ADDR(name));
  1785. if (!PHANDLE_VALID(ph)) {
  1786. name = "/pci@ff500000/isa@6";
  1787. ph = call_prom("finddevice", 1, 1, ADDR(name));
  1788. rloc = 0x01003000; /* IO space; PCI device = 6 */
  1789. }
  1790. if (PHANDLE_VALID(ph)) {
  1791. rc = prom_getproplen(ph, "ranges");
  1792. if (rc == 0 || rc == PROM_ERROR) {
  1793. prom_printf("Fixing up missing ISA range on Pegasos...\n");
  1794. prop[0] = 0x1;
  1795. prop[1] = 0x0;
  1796. prop[2] = rloc;
  1797. prop[3] = 0x0;
  1798. prop[4] = 0x0;
  1799. prop[5] = 0x00010000;
  1800. prom_setprop(ph, name, "ranges", prop, sizeof(prop));
  1801. }
  1802. }
  1803. name = "/pci@80000000/ide@C,1";
  1804. ph = call_prom("finddevice", 1, 1, ADDR(name));
  1805. if (PHANDLE_VALID(ph)) {
  1806. prom_printf("Fixing up IDE interrupt on Pegasos...\n");
  1807. prop[0] = 14;
  1808. prop[1] = 0x0;
  1809. prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
  1810. prom_printf("Fixing up IDE class-code on Pegasos...\n");
  1811. rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
  1812. if (rc == sizeof(u32)) {
  1813. prop[0] &= ~0x5;
  1814. prom_setprop(ph, name, "class-code", prop, sizeof(u32));
  1815. }
  1816. }
  1817. }
  1818. #else
  1819. #define fixup_device_tree_chrp()
  1820. #endif
  1821. #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
  1822. static void __init fixup_device_tree_pmac(void)
  1823. {
  1824. phandle u3, i2c, mpic;
  1825. u32 u3_rev;
  1826. u32 interrupts[2];
  1827. u32 parent;
  1828. /* Some G5s have a missing interrupt definition, fix it up here */
  1829. u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
  1830. if (!PHANDLE_VALID(u3))
  1831. return;
  1832. i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
  1833. if (!PHANDLE_VALID(i2c))
  1834. return;
  1835. mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
  1836. if (!PHANDLE_VALID(mpic))
  1837. return;
  1838. /* check if proper rev of u3 */
  1839. if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
  1840. == PROM_ERROR)
  1841. return;
  1842. if (u3_rev < 0x35 || u3_rev > 0x39)
  1843. return;
  1844. /* does it need fixup ? */
  1845. if (prom_getproplen(i2c, "interrupts") > 0)
  1846. return;
  1847. prom_printf("fixing up bogus interrupts for u3 i2c...\n");
  1848. /* interrupt on this revision of u3 is number 0 and level */
  1849. interrupts[0] = 0;
  1850. interrupts[1] = 1;
  1851. prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
  1852. &interrupts, sizeof(interrupts));
  1853. parent = (u32)mpic;
  1854. prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
  1855. &parent, sizeof(parent));
  1856. }
  1857. #else
  1858. #define fixup_device_tree_pmac()
  1859. #endif
  1860. #ifdef CONFIG_PPC_EFIKA
  1861. /*
  1862. * The MPC5200 FEC driver requires an phy-handle property to tell it how
  1863. * to talk to the phy. If the phy-handle property is missing, then this
  1864. * function is called to add the appropriate nodes and link it to the
  1865. * ethernet node.
  1866. */
  1867. static void __init fixup_device_tree_efika_add_phy(void)
  1868. {
  1869. u32 node;
  1870. char prop[64];
  1871. int rv;
  1872. /* Check if /builtin/ethernet exists - bail if it doesn't */
  1873. node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
  1874. if (!PHANDLE_VALID(node))
  1875. return;
  1876. /* Check if the phy-handle property exists - bail if it does */
  1877. rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
  1878. if (!rv)
  1879. return;
  1880. /*
  1881. * At this point the ethernet device doesn't have a phy described.
  1882. * Now we need to add the missing phy node and linkage
  1883. */
  1884. /* Check for an MDIO bus node - if missing then create one */
  1885. node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
  1886. if (!PHANDLE_VALID(node)) {
  1887. prom_printf("Adding Ethernet MDIO node\n");
  1888. call_prom("interpret", 1, 1,
  1889. " s\" /builtin\" find-device"
  1890. " new-device"
  1891. " 1 encode-int s\" #address-cells\" property"
  1892. " 0 encode-int s\" #size-cells\" property"
  1893. " s\" mdio\" device-name"
  1894. " s\" fsl,mpc5200b-mdio\" encode-string"
  1895. " s\" compatible\" property"
  1896. " 0xf0003000 0x400 reg"
  1897. " 0x2 encode-int"
  1898. " 0x5 encode-int encode+"
  1899. " 0x3 encode-int encode+"
  1900. " s\" interrupts\" property"
  1901. " finish-device");
  1902. };
  1903. /* Check for a PHY device node - if missing then create one and
  1904. * give it's phandle to the ethernet node */
  1905. node = call_prom("finddevice", 1, 1,
  1906. ADDR("/builtin/mdio/ethernet-phy"));
  1907. if (!PHANDLE_VALID(node)) {
  1908. prom_printf("Adding Ethernet PHY node\n");
  1909. call_prom("interpret", 1, 1,
  1910. " s\" /builtin/mdio\" find-device"
  1911. " new-device"
  1912. " s\" ethernet-phy\" device-name"
  1913. " 0x10 encode-int s\" reg\" property"
  1914. " my-self"
  1915. " ihandle>phandle"
  1916. " finish-device"
  1917. " s\" /builtin/ethernet\" find-device"
  1918. " encode-int"
  1919. " s\" phy-handle\" property"
  1920. " device-end");
  1921. }
  1922. }
  1923. static void __init fixup_device_tree_efika(void)
  1924. {
  1925. int sound_irq[3] = { 2, 2, 0 };
  1926. int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
  1927. 3,4,0, 3,5,0, 3,6,0, 3,7,0,
  1928. 3,8,0, 3,9,0, 3,10,0, 3,11,0,
  1929. 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
  1930. u32 node;
  1931. char prop[64];
  1932. int rv, len;
  1933. /* Check if we're really running on a EFIKA */
  1934. node = call_prom("finddevice", 1, 1, ADDR("/"));
  1935. if (!PHANDLE_VALID(node))
  1936. return;
  1937. rv = prom_getprop(node, "model", prop, sizeof(prop));
  1938. if (rv == PROM_ERROR)
  1939. return;
  1940. if (strcmp(prop, "EFIKA5K2"))
  1941. return;
  1942. prom_printf("Applying EFIKA device tree fixups\n");
  1943. /* Claiming to be 'chrp' is death */
  1944. node = call_prom("finddevice", 1, 1, ADDR("/"));
  1945. rv = prom_getprop(node, "device_type", prop, sizeof(prop));
  1946. if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
  1947. prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
  1948. /* CODEGEN,description is exposed in /proc/cpuinfo so
  1949. fix that too */
  1950. rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
  1951. if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
  1952. prom_setprop(node, "/", "CODEGEN,description",
  1953. "Efika 5200B PowerPC System",
  1954. sizeof("Efika 5200B PowerPC System"));
  1955. /* Fixup bestcomm interrupts property */
  1956. node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
  1957. if (PHANDLE_VALID(node)) {
  1958. len = prom_getproplen(node, "interrupts");
  1959. if (len == 12) {
  1960. prom_printf("Fixing bestcomm interrupts property\n");
  1961. prom_setprop(node, "/builtin/bestcom", "interrupts",
  1962. bcomm_irq, sizeof(bcomm_irq));
  1963. }
  1964. }
  1965. /* Fixup sound interrupts property */
  1966. node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
  1967. if (PHANDLE_VALID(node)) {
  1968. rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
  1969. if (rv == PROM_ERROR) {
  1970. prom_printf("Adding sound interrupts property\n");
  1971. prom_setprop(node, "/builtin/sound", "interrupts",
  1972. sound_irq, sizeof(sound_irq));
  1973. }
  1974. }
  1975. /* Make sure ethernet phy-handle property exists */
  1976. fixup_device_tree_efika_add_phy();
  1977. }
  1978. #else
  1979. #define fixup_device_tree_efika()
  1980. #endif
  1981. static void __init fixup_device_tree(void)
  1982. {
  1983. fixup_device_tree_maple();
  1984. fixup_device_tree_chrp();
  1985. fixup_device_tree_pmac();
  1986. fixup_device_tree_efika();
  1987. }
  1988. static void __init prom_find_boot_cpu(void)
  1989. {
  1990. struct prom_t *_prom = &RELOC(prom);
  1991. u32 getprop_rval;
  1992. ihandle prom_cpu;
  1993. phandle cpu_pkg;
  1994. _prom->cpu = 0;
  1995. if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
  1996. return;
  1997. cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
  1998. prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
  1999. _prom->cpu = getprop_rval;
  2000. prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
  2001. }
  2002. static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
  2003. {
  2004. #ifdef CONFIG_BLK_DEV_INITRD
  2005. struct prom_t *_prom = &RELOC(prom);
  2006. if (r3 && r4 && r4 != 0xdeadbeef) {
  2007. unsigned long val;
  2008. RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
  2009. RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
  2010. val = RELOC(prom_initrd_start);
  2011. prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
  2012. &val, sizeof(val));
  2013. val = RELOC(prom_initrd_end);
  2014. prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
  2015. &val, sizeof(val));
  2016. reserve_mem(RELOC(prom_initrd_start),
  2017. RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
  2018. prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
  2019. prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
  2020. }
  2021. #endif /* CONFIG_BLK_DEV_INITRD */
  2022. }
  2023. /*
  2024. * We enter here early on, when the Open Firmware prom is still
  2025. * handling exceptions and the MMU hash table for us.
  2026. */
  2027. unsigned long __init prom_init(unsigned long r3, unsigned long r4,
  2028. unsigned long pp,
  2029. unsigned long r6, unsigned long r7)
  2030. {
  2031. struct prom_t *_prom;
  2032. unsigned long hdr;
  2033. unsigned long offset = reloc_offset();
  2034. #ifdef CONFIG_PPC32
  2035. reloc_got2(offset);
  2036. #endif
  2037. _prom = &RELOC(prom);
  2038. /*
  2039. * First zero the BSS
  2040. */
  2041. memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
  2042. /*
  2043. * Init interface to Open Firmware, get some node references,
  2044. * like /chosen
  2045. */
  2046. prom_init_client_services(pp);
  2047. /*
  2048. * See if this OF is old enough that we need to do explicit maps
  2049. * and other workarounds
  2050. */
  2051. prom_find_mmu();
  2052. /*
  2053. * Init prom stdout device
  2054. */
  2055. prom_init_stdout();
  2056. /*
  2057. * Get default machine type. At this point, we do not differentiate
  2058. * between pSeries SMP and pSeries LPAR
  2059. */
  2060. RELOC(of_platform) = prom_find_machine_type();
  2061. /* Bail if this is a kdump kernel. */
  2062. if (PHYSICAL_START > 0)
  2063. prom_panic("Error: You can't boot a kdump kernel from OF!\n");
  2064. /*
  2065. * Check for an initrd
  2066. */
  2067. prom_check_initrd(r3, r4);
  2068. #ifdef CONFIG_PPC_PSERIES
  2069. /*
  2070. * On pSeries, inform the firmware about our capabilities
  2071. */
  2072. if (RELOC(of_platform) == PLATFORM_PSERIES ||
  2073. RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
  2074. prom_send_capabilities();
  2075. #endif
  2076. /*
  2077. * Copy the CPU hold code
  2078. */
  2079. if (RELOC(of_platform) != PLATFORM_POWERMAC)
  2080. copy_and_flush(0, KERNELBASE + offset, 0x100, 0);
  2081. /*
  2082. * Do early parsing of command line
  2083. */
  2084. early_cmdline_parse();
  2085. /*
  2086. * Initialize memory management within prom_init
  2087. */
  2088. prom_init_mem();
  2089. /*
  2090. * Determine which cpu is actually running right _now_
  2091. */
  2092. prom_find_boot_cpu();
  2093. /*
  2094. * Initialize display devices
  2095. */
  2096. prom_check_displays();
  2097. #ifdef CONFIG_PPC64
  2098. /*
  2099. * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
  2100. * that uses the allocator, we need to make sure we get the top of memory
  2101. * available for us here...
  2102. */
  2103. if (RELOC(of_platform) == PLATFORM_PSERIES)
  2104. prom_initialize_tce_table();
  2105. #endif
  2106. /*
  2107. * On non-powermacs, try to instantiate RTAS and puts all CPUs
  2108. * in spin-loops. PowerMacs don't have a working RTAS and use
  2109. * a different way to spin CPUs
  2110. */
  2111. if (RELOC(of_platform) != PLATFORM_POWERMAC) {
  2112. prom_instantiate_rtas();
  2113. prom_hold_cpus();
  2114. }
  2115. /*
  2116. * Fill in some infos for use by the kernel later on
  2117. */
  2118. #ifdef CONFIG_PPC64
  2119. if (RELOC(prom_iommu_off))
  2120. prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
  2121. NULL, 0);
  2122. if (RELOC(prom_iommu_force_on))
  2123. prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
  2124. NULL, 0);
  2125. if (RELOC(prom_tce_alloc_start)) {
  2126. prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
  2127. &RELOC(prom_tce_alloc_start),
  2128. sizeof(prom_tce_alloc_start));
  2129. prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
  2130. &RELOC(prom_tce_alloc_end),
  2131. sizeof(prom_tce_alloc_end));
  2132. }
  2133. #endif
  2134. /*
  2135. * Fixup any known bugs in the device-tree
  2136. */
  2137. fixup_device_tree();
  2138. /*
  2139. * Now finally create the flattened device-tree
  2140. */
  2141. prom_printf("copying OF device tree ...\n");
  2142. flatten_device_tree();
  2143. /*
  2144. * in case stdin is USB and still active on IBM machines...
  2145. * Unfortunately quiesce crashes on some powermacs if we have
  2146. * closed stdin already (in particular the powerbook 101).
  2147. */
  2148. if (RELOC(of_platform) != PLATFORM_POWERMAC)
  2149. prom_close_stdin();
  2150. /*
  2151. * Call OF "quiesce" method to shut down pending DMA's from
  2152. * devices etc...
  2153. */
  2154. prom_printf("Calling quiesce ...\n");
  2155. call_prom("quiesce", 0, 0);
  2156. /*
  2157. * And finally, call the kernel passing it the flattened device
  2158. * tree and NULL as r5, thus triggering the new entry point which
  2159. * is common to us and kexec
  2160. */
  2161. hdr = RELOC(dt_header_start);
  2162. prom_printf("returning from prom_init\n");
  2163. prom_debug("->dt_header_start=0x%x\n", hdr);
  2164. #ifdef CONFIG_PPC32
  2165. reloc_got2(-offset);
  2166. #endif
  2167. __start(hdr, KERNELBASE + offset, 0);
  2168. return 0;
  2169. }