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