prom_init.c 68 KB

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