prom_init.c 83 KB

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