prom_init.c 55 KB

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