prom_init.c 77 KB

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