prom_init.c 85 KB

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