prom_init.c 76 KB

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