prom_init.c 81 KB

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