prom.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  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
  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/stringify.h>
  25. #include <linux/delay.h>
  26. #include <linux/initrd.h>
  27. #include <linux/bitops.h>
  28. #include <linux/module.h>
  29. #include <linux/kexec.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/irq.h>
  32. #include <linux/lmb.h>
  33. #include <asm/prom.h>
  34. #include <asm/rtas.h>
  35. #include <asm/page.h>
  36. #include <asm/processor.h>
  37. #include <asm/irq.h>
  38. #include <asm/io.h>
  39. #include <asm/kdump.h>
  40. #include <asm/smp.h>
  41. #include <asm/system.h>
  42. #include <asm/mmu.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/pci.h>
  45. #include <asm/iommu.h>
  46. #include <asm/btext.h>
  47. #include <asm/sections.h>
  48. #include <asm/machdep.h>
  49. #include <asm/pSeries_reconfig.h>
  50. #include <asm/pci-bridge.h>
  51. #include <asm/phyp_dump.h>
  52. #include <asm/kexec.h>
  53. #include <mm/mmu_decl.h>
  54. #ifdef DEBUG
  55. #define DBG(fmt...) printk(KERN_ERR fmt)
  56. #else
  57. #define DBG(fmt...)
  58. #endif
  59. static int __initdata dt_root_addr_cells;
  60. static int __initdata dt_root_size_cells;
  61. #ifdef CONFIG_PPC64
  62. int __initdata iommu_is_off;
  63. int __initdata iommu_force_on;
  64. unsigned long tce_alloc_start, tce_alloc_end;
  65. #endif
  66. typedef u32 cell_t;
  67. extern rwlock_t devtree_lock; /* temporary while merging */
  68. /* export that to outside world */
  69. struct device_node *of_chosen;
  70. static int __init early_parse_mem(char *p)
  71. {
  72. if (!p)
  73. return 1;
  74. memory_limit = PAGE_ALIGN(memparse(p, &p));
  75. DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit);
  76. return 0;
  77. }
  78. early_param("mem", early_parse_mem);
  79. /**
  80. * move_device_tree - move tree to an unused area, if needed.
  81. *
  82. * The device tree may be allocated beyond our memory limit, or inside the
  83. * crash kernel region for kdump. If so, move it out of the way.
  84. */
  85. static void __init move_device_tree(void)
  86. {
  87. unsigned long start, size;
  88. void *p;
  89. DBG("-> move_device_tree\n");
  90. start = __pa(initial_boot_params);
  91. size = initial_boot_params->totalsize;
  92. if ((memory_limit && (start + size) > memory_limit) ||
  93. overlaps_crashkernel(start, size)) {
  94. p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
  95. memcpy(p, initial_boot_params, size);
  96. initial_boot_params = (struct boot_param_header *)p;
  97. DBG("Moved device tree to 0x%p\n", p);
  98. }
  99. DBG("<- move_device_tree\n");
  100. }
  101. /*
  102. * ibm,pa-features is a per-cpu property that contains a string of
  103. * attribute descriptors, each of which has a 2 byte header plus up
  104. * to 254 bytes worth of processor attribute bits. First header
  105. * byte specifies the number of bytes following the header.
  106. * Second header byte is an "attribute-specifier" type, of which
  107. * zero is the only currently-defined value.
  108. * Implementation: Pass in the byte and bit offset for the feature
  109. * that we are interested in. The function will return -1 if the
  110. * pa-features property is missing, or a 1/0 to indicate if the feature
  111. * is supported/not supported. Note that the bit numbers are
  112. * big-endian to match the definition in PAPR.
  113. */
  114. static struct ibm_pa_feature {
  115. unsigned long cpu_features; /* CPU_FTR_xxx bit */
  116. unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
  117. unsigned char pabyte; /* byte number in ibm,pa-features */
  118. unsigned char pabit; /* bit number (big-endian) */
  119. unsigned char invert; /* if 1, pa bit set => clear feature */
  120. } ibm_pa_features[] __initdata = {
  121. {0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
  122. {0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
  123. {CPU_FTR_SLB, 0, 0, 2, 0},
  124. {CPU_FTR_CTRL, 0, 0, 3, 0},
  125. {CPU_FTR_NOEXECUTE, 0, 0, 6, 0},
  126. {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1},
  127. {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
  128. {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
  129. };
  130. static void __init scan_features(unsigned long node, unsigned char *ftrs,
  131. unsigned long tablelen,
  132. struct ibm_pa_feature *fp,
  133. unsigned long ft_size)
  134. {
  135. unsigned long i, len, bit;
  136. /* find descriptor with type == 0 */
  137. for (;;) {
  138. if (tablelen < 3)
  139. return;
  140. len = 2 + ftrs[0];
  141. if (tablelen < len)
  142. return; /* descriptor 0 not found */
  143. if (ftrs[1] == 0)
  144. break;
  145. tablelen -= len;
  146. ftrs += len;
  147. }
  148. /* loop over bits we know about */
  149. for (i = 0; i < ft_size; ++i, ++fp) {
  150. if (fp->pabyte >= ftrs[0])
  151. continue;
  152. bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
  153. if (bit ^ fp->invert) {
  154. cur_cpu_spec->cpu_features |= fp->cpu_features;
  155. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
  156. } else {
  157. cur_cpu_spec->cpu_features &= ~fp->cpu_features;
  158. cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
  159. }
  160. }
  161. }
  162. static void __init check_cpu_pa_features(unsigned long node)
  163. {
  164. unsigned char *pa_ftrs;
  165. unsigned long tablelen;
  166. pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
  167. if (pa_ftrs == NULL)
  168. return;
  169. scan_features(node, pa_ftrs, tablelen,
  170. ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
  171. }
  172. #ifdef CONFIG_PPC_STD_MMU_64
  173. static void __init check_cpu_slb_size(unsigned long node)
  174. {
  175. u32 *slb_size_ptr;
  176. slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
  177. if (slb_size_ptr != NULL) {
  178. mmu_slb_size = *slb_size_ptr;
  179. return;
  180. }
  181. slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
  182. if (slb_size_ptr != NULL) {
  183. mmu_slb_size = *slb_size_ptr;
  184. }
  185. }
  186. #else
  187. #define check_cpu_slb_size(node) do { } while(0)
  188. #endif
  189. static struct feature_property {
  190. const char *name;
  191. u32 min_value;
  192. unsigned long cpu_feature;
  193. unsigned long cpu_user_ftr;
  194. } feature_properties[] __initdata = {
  195. #ifdef CONFIG_ALTIVEC
  196. {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  197. {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  198. #endif /* CONFIG_ALTIVEC */
  199. #ifdef CONFIG_VSX
  200. /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
  201. {"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
  202. #endif /* CONFIG_VSX */
  203. #ifdef CONFIG_PPC64
  204. {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
  205. {"ibm,purr", 1, CPU_FTR_PURR, 0},
  206. {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
  207. #endif /* CONFIG_PPC64 */
  208. };
  209. #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
  210. static inline void identical_pvr_fixup(unsigned long node)
  211. {
  212. unsigned int pvr;
  213. char *model = of_get_flat_dt_prop(node, "model", NULL);
  214. /*
  215. * Since 440GR(x)/440EP(x) processors have the same pvr,
  216. * we check the node path and set bit 28 in the cur_cpu_spec
  217. * pvr for EP(x) processor version. This bit is always 0 in
  218. * the "real" pvr. Then we call identify_cpu again with
  219. * the new logical pvr to enable FPU support.
  220. */
  221. if (model && strstr(model, "440EP")) {
  222. pvr = cur_cpu_spec->pvr_value | 0x8;
  223. identify_cpu(0, pvr);
  224. DBG("Using logical pvr %x for %s\n", pvr, model);
  225. }
  226. }
  227. #else
  228. #define identical_pvr_fixup(node) do { } while(0)
  229. #endif
  230. static void __init check_cpu_feature_properties(unsigned long node)
  231. {
  232. unsigned long i;
  233. struct feature_property *fp = feature_properties;
  234. const u32 *prop;
  235. for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
  236. prop = of_get_flat_dt_prop(node, fp->name, NULL);
  237. if (prop && *prop >= fp->min_value) {
  238. cur_cpu_spec->cpu_features |= fp->cpu_feature;
  239. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
  240. }
  241. }
  242. }
  243. static int __init early_init_dt_scan_cpus(unsigned long node,
  244. const char *uname, int depth,
  245. void *data)
  246. {
  247. static int logical_cpuid = 0;
  248. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  249. const u32 *prop;
  250. const u32 *intserv;
  251. int i, nthreads;
  252. unsigned long len;
  253. int found = 0;
  254. /* We are scanning "cpu" nodes only */
  255. if (type == NULL || strcmp(type, "cpu") != 0)
  256. return 0;
  257. /* Get physical cpuid */
  258. intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
  259. if (intserv) {
  260. nthreads = len / sizeof(int);
  261. } else {
  262. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  263. nthreads = 1;
  264. }
  265. /*
  266. * Now see if any of these threads match our boot cpu.
  267. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  268. */
  269. for (i = 0; i < nthreads; i++) {
  270. /*
  271. * version 2 of the kexec param format adds the phys cpuid of
  272. * booted proc.
  273. */
  274. if (initial_boot_params && initial_boot_params->version >= 2) {
  275. if (intserv[i] ==
  276. initial_boot_params->boot_cpuid_phys) {
  277. found = 1;
  278. break;
  279. }
  280. } else {
  281. /*
  282. * Check if it's the boot-cpu, set it's hw index now,
  283. * unfortunately this format did not support booting
  284. * off secondary threads.
  285. */
  286. if (of_get_flat_dt_prop(node,
  287. "linux,boot-cpu", NULL) != NULL) {
  288. found = 1;
  289. break;
  290. }
  291. }
  292. #ifdef CONFIG_SMP
  293. /* logical cpu id is always 0 on UP kernels */
  294. logical_cpuid++;
  295. #endif
  296. }
  297. if (found) {
  298. DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
  299. intserv[i]);
  300. boot_cpuid = logical_cpuid;
  301. set_hard_smp_processor_id(boot_cpuid, intserv[i]);
  302. /*
  303. * PAPR defines "logical" PVR values for cpus that
  304. * meet various levels of the architecture:
  305. * 0x0f000001 Architecture version 2.04
  306. * 0x0f000002 Architecture version 2.05
  307. * If the cpu-version property in the cpu node contains
  308. * such a value, we call identify_cpu again with the
  309. * logical PVR value in order to use the cpu feature
  310. * bits appropriate for the architecture level.
  311. *
  312. * A POWER6 partition in "POWER6 architected" mode
  313. * uses the 0x0f000002 PVR value; in POWER5+ mode
  314. * it uses 0x0f000001.
  315. */
  316. prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
  317. if (prop && (*prop & 0xff000000) == 0x0f000000)
  318. identify_cpu(0, *prop);
  319. identical_pvr_fixup(node);
  320. }
  321. check_cpu_feature_properties(node);
  322. check_cpu_pa_features(node);
  323. check_cpu_slb_size(node);
  324. #ifdef CONFIG_PPC_PSERIES
  325. if (nthreads > 1)
  326. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  327. else
  328. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  329. #endif
  330. return 0;
  331. }
  332. #ifdef CONFIG_BLK_DEV_INITRD
  333. static void __init early_init_dt_check_for_initrd(unsigned long node)
  334. {
  335. unsigned long l;
  336. u32 *prop;
  337. DBG("Looking for initrd properties... ");
  338. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
  339. if (prop) {
  340. initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
  341. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
  342. if (prop) {
  343. initrd_end = (unsigned long)
  344. __va(of_read_ulong(prop, l/4));
  345. initrd_below_start_ok = 1;
  346. } else {
  347. initrd_start = 0;
  348. }
  349. }
  350. DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start, initrd_end);
  351. }
  352. #else
  353. static inline void early_init_dt_check_for_initrd(unsigned long node)
  354. {
  355. }
  356. #endif /* CONFIG_BLK_DEV_INITRD */
  357. static int __init early_init_dt_scan_chosen(unsigned long node,
  358. const char *uname, int depth, void *data)
  359. {
  360. unsigned long *lprop;
  361. unsigned long l;
  362. char *p;
  363. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  364. if (depth != 1 ||
  365. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  366. return 0;
  367. #ifdef CONFIG_PPC64
  368. /* check if iommu is forced on or off */
  369. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  370. iommu_is_off = 1;
  371. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  372. iommu_force_on = 1;
  373. #endif
  374. /* mem=x on the command line is the preferred mechanism */
  375. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  376. if (lprop)
  377. memory_limit = *lprop;
  378. #ifdef CONFIG_PPC64
  379. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  380. if (lprop)
  381. tce_alloc_start = *lprop;
  382. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  383. if (lprop)
  384. tce_alloc_end = *lprop;
  385. #endif
  386. #ifdef CONFIG_KEXEC
  387. lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  388. if (lprop)
  389. crashk_res.start = *lprop;
  390. lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  391. if (lprop)
  392. crashk_res.end = crashk_res.start + *lprop - 1;
  393. #endif
  394. early_init_dt_check_for_initrd(node);
  395. /* Retreive command line */
  396. p = of_get_flat_dt_prop(node, "bootargs", &l);
  397. if (p != NULL && l > 0)
  398. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  399. #ifdef CONFIG_CMDLINE
  400. if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
  401. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  402. #endif /* CONFIG_CMDLINE */
  403. DBG("Command line is: %s\n", cmd_line);
  404. /* break now */
  405. return 1;
  406. }
  407. static int __init early_init_dt_scan_root(unsigned long node,
  408. const char *uname, int depth, void *data)
  409. {
  410. u32 *prop;
  411. if (depth != 0)
  412. return 0;
  413. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  414. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  415. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  416. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  417. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  418. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  419. /* break now */
  420. return 1;
  421. }
  422. static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
  423. {
  424. cell_t *p = *cellp;
  425. *cellp = p + s;
  426. return of_read_number(p, s);
  427. }
  428. #ifdef CONFIG_PPC_PSERIES
  429. /*
  430. * Interpret the ibm,dynamic-memory property in the
  431. * /ibm,dynamic-reconfiguration-memory node.
  432. * This contains a list of memory blocks along with NUMA affinity
  433. * information.
  434. */
  435. static int __init early_init_dt_scan_drconf_memory(unsigned long node)
  436. {
  437. cell_t *dm, *ls, *usm;
  438. unsigned long l, n, flags;
  439. u64 base, size, lmb_size;
  440. unsigned int is_kexec_kdump = 0, rngs;
  441. ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
  442. if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
  443. return 0;
  444. lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
  445. dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
  446. if (dm == NULL || l < sizeof(cell_t))
  447. return 0;
  448. n = *dm++; /* number of entries */
  449. if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
  450. return 0;
  451. /* check if this is a kexec/kdump kernel. */
  452. usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
  453. &l);
  454. if (usm != NULL)
  455. is_kexec_kdump = 1;
  456. for (; n != 0; --n) {
  457. base = dt_mem_next_cell(dt_root_addr_cells, &dm);
  458. flags = dm[3];
  459. /* skip DRC index, pad, assoc. list index, flags */
  460. dm += 4;
  461. /* skip this block if the reserved bit is set in flags (0x80)
  462. or if the block is not assigned to this partition (0x8) */
  463. if ((flags & 0x80) || !(flags & 0x8))
  464. continue;
  465. size = lmb_size;
  466. rngs = 1;
  467. if (is_kexec_kdump) {
  468. /*
  469. * For each lmb in ibm,dynamic-memory, a corresponding
  470. * entry in linux,drconf-usable-memory property contains
  471. * a counter 'p' followed by 'p' (base, size) duple.
  472. * Now read the counter from
  473. * linux,drconf-usable-memory property
  474. */
  475. rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
  476. if (!rngs) /* there are no (base, size) duple */
  477. continue;
  478. }
  479. do {
  480. if (is_kexec_kdump) {
  481. base = dt_mem_next_cell(dt_root_addr_cells,
  482. &usm);
  483. size = dt_mem_next_cell(dt_root_size_cells,
  484. &usm);
  485. }
  486. if (iommu_is_off) {
  487. if (base >= 0x80000000ul)
  488. continue;
  489. if ((base + size) > 0x80000000ul)
  490. size = 0x80000000ul - base;
  491. }
  492. lmb_add(base, size);
  493. } while (--rngs);
  494. }
  495. lmb_dump_all();
  496. return 0;
  497. }
  498. #else
  499. #define early_init_dt_scan_drconf_memory(node) 0
  500. #endif /* CONFIG_PPC_PSERIES */
  501. static int __init early_init_dt_scan_memory(unsigned long node,
  502. const char *uname, int depth, void *data)
  503. {
  504. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  505. cell_t *reg, *endp;
  506. unsigned long l;
  507. /* Look for the ibm,dynamic-reconfiguration-memory node */
  508. if (depth == 1 &&
  509. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  510. return early_init_dt_scan_drconf_memory(node);
  511. /* We are scanning "memory" nodes only */
  512. if (type == NULL) {
  513. /*
  514. * The longtrail doesn't have a device_type on the
  515. * /memory node, so look for the node called /memory@0.
  516. */
  517. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  518. return 0;
  519. } else if (strcmp(type, "memory") != 0)
  520. return 0;
  521. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  522. if (reg == NULL)
  523. reg = of_get_flat_dt_prop(node, "reg", &l);
  524. if (reg == NULL)
  525. return 0;
  526. endp = reg + (l / sizeof(cell_t));
  527. DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  528. uname, l, reg[0], reg[1], reg[2], reg[3]);
  529. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  530. u64 base, size;
  531. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  532. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  533. if (size == 0)
  534. continue;
  535. DBG(" - %llx , %llx\n", (unsigned long long)base,
  536. (unsigned long long)size);
  537. #ifdef CONFIG_PPC64
  538. if (iommu_is_off) {
  539. if (base >= 0x80000000ul)
  540. continue;
  541. if ((base + size) > 0x80000000ul)
  542. size = 0x80000000ul - base;
  543. }
  544. #endif
  545. lmb_add(base, size);
  546. memstart_addr = min((u64)memstart_addr, base);
  547. }
  548. return 0;
  549. }
  550. static void __init early_reserve_mem(void)
  551. {
  552. u64 base, size;
  553. u64 *reserve_map;
  554. unsigned long self_base;
  555. unsigned long self_size;
  556. reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  557. initial_boot_params->off_mem_rsvmap);
  558. /* before we do anything, lets reserve the dt blob */
  559. self_base = __pa((unsigned long)initial_boot_params);
  560. self_size = initial_boot_params->totalsize;
  561. lmb_reserve(self_base, self_size);
  562. #ifdef CONFIG_BLK_DEV_INITRD
  563. /* then reserve the initrd, if any */
  564. if (initrd_start && (initrd_end > initrd_start))
  565. lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
  566. #endif /* CONFIG_BLK_DEV_INITRD */
  567. #ifdef CONFIG_PPC32
  568. /*
  569. * Handle the case where we might be booting from an old kexec
  570. * image that setup the mem_rsvmap as pairs of 32-bit values
  571. */
  572. if (*reserve_map > 0xffffffffull) {
  573. u32 base_32, size_32;
  574. u32 *reserve_map_32 = (u32 *)reserve_map;
  575. while (1) {
  576. base_32 = *(reserve_map_32++);
  577. size_32 = *(reserve_map_32++);
  578. if (size_32 == 0)
  579. break;
  580. /* skip if the reservation is for the blob */
  581. if (base_32 == self_base && size_32 == self_size)
  582. continue;
  583. DBG("reserving: %x -> %x\n", base_32, size_32);
  584. lmb_reserve(base_32, size_32);
  585. }
  586. return;
  587. }
  588. #endif
  589. while (1) {
  590. base = *(reserve_map++);
  591. size = *(reserve_map++);
  592. if (size == 0)
  593. break;
  594. DBG("reserving: %llx -> %llx\n", base, size);
  595. lmb_reserve(base, size);
  596. }
  597. }
  598. #ifdef CONFIG_PHYP_DUMP
  599. /**
  600. * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
  601. *
  602. * Function to find the largest size we need to reserve
  603. * during early boot process.
  604. *
  605. * It either looks for boot param and returns that OR
  606. * returns larger of 256 or 5% rounded down to multiples of 256MB.
  607. *
  608. */
  609. static inline unsigned long phyp_dump_calculate_reserve_size(void)
  610. {
  611. unsigned long tmp;
  612. if (phyp_dump_info->reserve_bootvar)
  613. return phyp_dump_info->reserve_bootvar;
  614. /* divide by 20 to get 5% of value */
  615. tmp = lmb_end_of_DRAM();
  616. do_div(tmp, 20);
  617. /* round it down in multiples of 256 */
  618. tmp = tmp & ~0x0FFFFFFFUL;
  619. return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
  620. }
  621. /**
  622. * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
  623. *
  624. * This routine may reserve memory regions in the kernel only
  625. * if the system is supported and a dump was taken in last
  626. * boot instance or if the hardware is supported and the
  627. * scratch area needs to be setup. In other instances it returns
  628. * without reserving anything. The memory in case of dump being
  629. * active is freed when the dump is collected (by userland tools).
  630. */
  631. static void __init phyp_dump_reserve_mem(void)
  632. {
  633. unsigned long base, size;
  634. unsigned long variable_reserve_size;
  635. if (!phyp_dump_info->phyp_dump_configured) {
  636. printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
  637. return;
  638. }
  639. if (!phyp_dump_info->phyp_dump_at_boot) {
  640. printk(KERN_INFO "Phyp-dump disabled at boot time\n");
  641. return;
  642. }
  643. variable_reserve_size = phyp_dump_calculate_reserve_size();
  644. if (phyp_dump_info->phyp_dump_is_active) {
  645. /* Reserve *everything* above RMR.Area freed by userland tools*/
  646. base = variable_reserve_size;
  647. size = lmb_end_of_DRAM() - base;
  648. /* XXX crashed_ram_end is wrong, since it may be beyond
  649. * the memory_limit, it will need to be adjusted. */
  650. lmb_reserve(base, size);
  651. phyp_dump_info->init_reserve_start = base;
  652. phyp_dump_info->init_reserve_size = size;
  653. } else {
  654. size = phyp_dump_info->cpu_state_size +
  655. phyp_dump_info->hpte_region_size +
  656. variable_reserve_size;
  657. base = lmb_end_of_DRAM() - size;
  658. lmb_reserve(base, size);
  659. phyp_dump_info->init_reserve_start = base;
  660. phyp_dump_info->init_reserve_size = size;
  661. }
  662. }
  663. #else
  664. static inline void __init phyp_dump_reserve_mem(void) {}
  665. #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
  666. void __init early_init_devtree(void *params)
  667. {
  668. phys_addr_t limit;
  669. DBG(" -> early_init_devtree(%p)\n", params);
  670. /* Setup flat device-tree pointer */
  671. initial_boot_params = params;
  672. #ifdef CONFIG_PPC_RTAS
  673. /* Some machines might need RTAS info for debugging, grab it now. */
  674. of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
  675. #endif
  676. #ifdef CONFIG_PHYP_DUMP
  677. /* scan tree to see if dump occured during last boot */
  678. of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
  679. #endif
  680. /* Retrieve various informations from the /chosen node of the
  681. * device-tree, including the platform type, initrd location and
  682. * size, TCE reserve, and more ...
  683. */
  684. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  685. /* Scan memory nodes and rebuild LMBs */
  686. lmb_init();
  687. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  688. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  689. /* Save command line for /proc/cmdline and then parse parameters */
  690. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  691. parse_early_param();
  692. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  693. lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  694. /* If relocatable, reserve first 32k for interrupt vectors etc. */
  695. if (PHYSICAL_START > MEMORY_START)
  696. lmb_reserve(MEMORY_START, 0x8000);
  697. reserve_kdump_trampoline();
  698. reserve_crashkernel();
  699. early_reserve_mem();
  700. phyp_dump_reserve_mem();
  701. limit = memory_limit;
  702. if (! limit) {
  703. phys_addr_t memsize;
  704. /* Ensure that total memory size is page-aligned, because
  705. * otherwise mark_bootmem() gets upset. */
  706. lmb_analyze();
  707. memsize = lmb_phys_mem_size();
  708. if ((memsize & PAGE_MASK) != memsize)
  709. limit = memsize & PAGE_MASK;
  710. }
  711. lmb_enforce_memory_limit(limit);
  712. lmb_analyze();
  713. lmb_dump_all();
  714. DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
  715. /* We may need to relocate the flat tree, do it now.
  716. * FIXME .. and the initrd too? */
  717. move_device_tree();
  718. DBG("Scanning CPUs ...\n");
  719. /* Retreive CPU related informations from the flat tree
  720. * (altivec support, boot CPU ID, ...)
  721. */
  722. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  723. DBG(" <- early_init_devtree()\n");
  724. }
  725. /**
  726. * Indicates whether the root node has a given value in its
  727. * compatible property.
  728. */
  729. int machine_is_compatible(const char *compat)
  730. {
  731. struct device_node *root;
  732. int rc = 0;
  733. root = of_find_node_by_path("/");
  734. if (root) {
  735. rc = of_device_is_compatible(root, compat);
  736. of_node_put(root);
  737. }
  738. return rc;
  739. }
  740. EXPORT_SYMBOL(machine_is_compatible);
  741. /*******
  742. *
  743. * New implementation of the OF "find" APIs, return a refcounted
  744. * object, call of_node_put() when done. The device tree and list
  745. * are protected by a rw_lock.
  746. *
  747. * Note that property management will need some locking as well,
  748. * this isn't dealt with yet.
  749. *
  750. *******/
  751. /**
  752. * of_find_node_by_phandle - Find a node given a phandle
  753. * @handle: phandle of the node to find
  754. *
  755. * Returns a node pointer with refcount incremented, use
  756. * of_node_put() on it when done.
  757. */
  758. struct device_node *of_find_node_by_phandle(phandle handle)
  759. {
  760. struct device_node *np;
  761. read_lock(&devtree_lock);
  762. for (np = allnodes; np != 0; np = np->allnext)
  763. if (np->linux_phandle == handle)
  764. break;
  765. of_node_get(np);
  766. read_unlock(&devtree_lock);
  767. return np;
  768. }
  769. EXPORT_SYMBOL(of_find_node_by_phandle);
  770. /**
  771. * of_find_next_cache_node - Find a node's subsidiary cache
  772. * @np: node of type "cpu" or "cache"
  773. *
  774. * Returns a node pointer with refcount incremented, use
  775. * of_node_put() on it when done. Caller should hold a reference
  776. * to np.
  777. */
  778. struct device_node *of_find_next_cache_node(struct device_node *np)
  779. {
  780. struct device_node *child;
  781. const phandle *handle;
  782. handle = of_get_property(np, "l2-cache", NULL);
  783. if (!handle)
  784. handle = of_get_property(np, "next-level-cache", NULL);
  785. if (handle)
  786. return of_find_node_by_phandle(*handle);
  787. /* OF on pmac has nodes instead of properties named "l2-cache"
  788. * beneath CPU nodes.
  789. */
  790. if (!strcmp(np->type, "cpu"))
  791. for_each_child_of_node(np, child)
  792. if (!strcmp(child->type, "cache"))
  793. return child;
  794. return NULL;
  795. }
  796. /**
  797. * of_node_get - Increment refcount of a node
  798. * @node: Node to inc refcount, NULL is supported to
  799. * simplify writing of callers
  800. *
  801. * Returns node.
  802. */
  803. struct device_node *of_node_get(struct device_node *node)
  804. {
  805. if (node)
  806. kref_get(&node->kref);
  807. return node;
  808. }
  809. EXPORT_SYMBOL(of_node_get);
  810. static inline struct device_node * kref_to_device_node(struct kref *kref)
  811. {
  812. return container_of(kref, struct device_node, kref);
  813. }
  814. /**
  815. * of_node_release - release a dynamically allocated node
  816. * @kref: kref element of the node to be released
  817. *
  818. * In of_node_put() this function is passed to kref_put()
  819. * as the destructor.
  820. */
  821. static void of_node_release(struct kref *kref)
  822. {
  823. struct device_node *node = kref_to_device_node(kref);
  824. struct property *prop = node->properties;
  825. /* We should never be releasing nodes that haven't been detached. */
  826. if (!of_node_check_flag(node, OF_DETACHED)) {
  827. printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
  828. dump_stack();
  829. kref_init(&node->kref);
  830. return;
  831. }
  832. if (!of_node_check_flag(node, OF_DYNAMIC))
  833. return;
  834. while (prop) {
  835. struct property *next = prop->next;
  836. kfree(prop->name);
  837. kfree(prop->value);
  838. kfree(prop);
  839. prop = next;
  840. if (!prop) {
  841. prop = node->deadprops;
  842. node->deadprops = NULL;
  843. }
  844. }
  845. kfree(node->full_name);
  846. kfree(node->data);
  847. kfree(node);
  848. }
  849. /**
  850. * of_node_put - Decrement refcount of a node
  851. * @node: Node to dec refcount, NULL is supported to
  852. * simplify writing of callers
  853. *
  854. */
  855. void of_node_put(struct device_node *node)
  856. {
  857. if (node)
  858. kref_put(&node->kref, of_node_release);
  859. }
  860. EXPORT_SYMBOL(of_node_put);
  861. /*
  862. * Plug a device node into the tree and global list.
  863. */
  864. void of_attach_node(struct device_node *np)
  865. {
  866. unsigned long flags;
  867. write_lock_irqsave(&devtree_lock, flags);
  868. np->sibling = np->parent->child;
  869. np->allnext = allnodes;
  870. np->parent->child = np;
  871. allnodes = np;
  872. write_unlock_irqrestore(&devtree_lock, flags);
  873. }
  874. /*
  875. * "Unplug" a node from the device tree. The caller must hold
  876. * a reference to the node. The memory associated with the node
  877. * is not freed until its refcount goes to zero.
  878. */
  879. void of_detach_node(struct device_node *np)
  880. {
  881. struct device_node *parent;
  882. unsigned long flags;
  883. write_lock_irqsave(&devtree_lock, flags);
  884. parent = np->parent;
  885. if (!parent)
  886. goto out_unlock;
  887. if (allnodes == np)
  888. allnodes = np->allnext;
  889. else {
  890. struct device_node *prev;
  891. for (prev = allnodes;
  892. prev->allnext != np;
  893. prev = prev->allnext)
  894. ;
  895. prev->allnext = np->allnext;
  896. }
  897. if (parent->child == np)
  898. parent->child = np->sibling;
  899. else {
  900. struct device_node *prevsib;
  901. for (prevsib = np->parent->child;
  902. prevsib->sibling != np;
  903. prevsib = prevsib->sibling)
  904. ;
  905. prevsib->sibling = np->sibling;
  906. }
  907. of_node_set_flag(np, OF_DETACHED);
  908. out_unlock:
  909. write_unlock_irqrestore(&devtree_lock, flags);
  910. }
  911. #ifdef CONFIG_PPC_PSERIES
  912. /*
  913. * Fix up the uninitialized fields in a new device node:
  914. * name, type and pci-specific fields
  915. */
  916. static int of_finish_dynamic_node(struct device_node *node)
  917. {
  918. struct device_node *parent = of_get_parent(node);
  919. int err = 0;
  920. const phandle *ibm_phandle;
  921. node->name = of_get_property(node, "name", NULL);
  922. node->type = of_get_property(node, "device_type", NULL);
  923. if (!node->name)
  924. node->name = "<NULL>";
  925. if (!node->type)
  926. node->type = "<NULL>";
  927. if (!parent) {
  928. err = -ENODEV;
  929. goto out;
  930. }
  931. /* We don't support that function on PowerMac, at least
  932. * not yet
  933. */
  934. if (machine_is(powermac))
  935. return -ENODEV;
  936. /* fix up new node's linux_phandle field */
  937. if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
  938. node->linux_phandle = *ibm_phandle;
  939. out:
  940. of_node_put(parent);
  941. return err;
  942. }
  943. static int prom_reconfig_notifier(struct notifier_block *nb,
  944. unsigned long action, void *node)
  945. {
  946. int err;
  947. switch (action) {
  948. case PSERIES_RECONFIG_ADD:
  949. err = of_finish_dynamic_node(node);
  950. if (err < 0) {
  951. printk(KERN_ERR "finish_node returned %d\n", err);
  952. err = NOTIFY_BAD;
  953. }
  954. break;
  955. default:
  956. err = NOTIFY_DONE;
  957. break;
  958. }
  959. return err;
  960. }
  961. static struct notifier_block prom_reconfig_nb = {
  962. .notifier_call = prom_reconfig_notifier,
  963. .priority = 10, /* This one needs to run first */
  964. };
  965. static int __init prom_reconfig_setup(void)
  966. {
  967. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  968. }
  969. __initcall(prom_reconfig_setup);
  970. #endif
  971. /* Find the device node for a given logical cpu number, also returns the cpu
  972. * local thread number (index in ibm,interrupt-server#s) if relevant and
  973. * asked for (non NULL)
  974. */
  975. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  976. {
  977. int hardid;
  978. struct device_node *np;
  979. hardid = get_hard_smp_processor_id(cpu);
  980. for_each_node_by_type(np, "cpu") {
  981. const u32 *intserv;
  982. unsigned int plen, t;
  983. /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
  984. * fallback to "reg" property and assume no threads
  985. */
  986. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
  987. &plen);
  988. if (intserv == NULL) {
  989. const u32 *reg = of_get_property(np, "reg", NULL);
  990. if (reg == NULL)
  991. continue;
  992. if (*reg == hardid) {
  993. if (thread)
  994. *thread = 0;
  995. return np;
  996. }
  997. } else {
  998. plen /= sizeof(u32);
  999. for (t = 0; t < plen; t++) {
  1000. if (hardid == intserv[t]) {
  1001. if (thread)
  1002. *thread = t;
  1003. return np;
  1004. }
  1005. }
  1006. }
  1007. }
  1008. return NULL;
  1009. }
  1010. EXPORT_SYMBOL(of_get_cpu_node);
  1011. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  1012. static struct debugfs_blob_wrapper flat_dt_blob;
  1013. static int __init export_flat_device_tree(void)
  1014. {
  1015. struct dentry *d;
  1016. flat_dt_blob.data = initial_boot_params;
  1017. flat_dt_blob.size = initial_boot_params->totalsize;
  1018. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  1019. powerpc_debugfs_root, &flat_dt_blob);
  1020. if (!d)
  1021. return 1;
  1022. return 0;
  1023. }
  1024. __initcall(export_flat_device_tree);
  1025. #endif