prom_init.c 63 KB

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