prom_init.c 85 KB

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