prom_init.c 84 KB

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