prom_init.c 72 KB

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