prom_init.c 88 KB

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