prom.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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/export.h>
  29. #include <linux/kexec.h>
  30. #include <linux/irq.h>
  31. #include <linux/memblock.h>
  32. #include <linux/of.h>
  33. #include <linux/of_fdt.h>
  34. #include <linux/libfdt.h>
  35. #include <asm/prom.h>
  36. #include <asm/rtas.h>
  37. #include <asm/page.h>
  38. #include <asm/processor.h>
  39. #include <asm/irq.h>
  40. #include <asm/io.h>
  41. #include <asm/kdump.h>
  42. #include <asm/smp.h>
  43. #include <asm/mmu.h>
  44. #include <asm/paca.h>
  45. #include <asm/pgtable.h>
  46. #include <asm/pci.h>
  47. #include <asm/iommu.h>
  48. #include <asm/btext.h>
  49. #include <asm/sections.h>
  50. #include <asm/machdep.h>
  51. #include <asm/pci-bridge.h>
  52. #include <asm/kexec.h>
  53. #include <asm/opal.h>
  54. #include <asm/fadump.h>
  55. #include <asm/debug.h>
  56. #include <mm/mmu_decl.h>
  57. #ifdef DEBUG
  58. #define DBG(fmt...) printk(KERN_ERR fmt)
  59. #else
  60. #define DBG(fmt...)
  61. #endif
  62. #ifdef CONFIG_PPC64
  63. int __initdata iommu_is_off;
  64. int __initdata iommu_force_on;
  65. unsigned long tce_alloc_start, tce_alloc_end;
  66. u64 ppc64_rma_size;
  67. #endif
  68. static phys_addr_t first_memblock_size;
  69. static int __initdata boot_cpu_count;
  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", memory_limit);
  76. return 0;
  77. }
  78. early_param("mem", early_parse_mem);
  79. /*
  80. * overlaps_initrd - check for overlap with page aligned extension of
  81. * initrd.
  82. */
  83. static inline int overlaps_initrd(unsigned long start, unsigned long size)
  84. {
  85. #ifdef CONFIG_BLK_DEV_INITRD
  86. if (!initrd_start)
  87. return 0;
  88. return (start + size) > _ALIGN_DOWN(initrd_start, PAGE_SIZE) &&
  89. start <= _ALIGN_UP(initrd_end, PAGE_SIZE);
  90. #else
  91. return 0;
  92. #endif
  93. }
  94. /**
  95. * move_device_tree - move tree to an unused area, if needed.
  96. *
  97. * The device tree may be allocated beyond our memory limit, or inside the
  98. * crash kernel region for kdump, or within the page aligned range of initrd.
  99. * If so, move it out of the way.
  100. */
  101. static void __init move_device_tree(void)
  102. {
  103. unsigned long start, size;
  104. void *p;
  105. DBG("-> move_device_tree\n");
  106. start = __pa(initial_boot_params);
  107. size = fdt_totalsize(initial_boot_params);
  108. if ((memory_limit && (start + size) > PHYSICAL_START + memory_limit) ||
  109. overlaps_crashkernel(start, size) ||
  110. overlaps_initrd(start, size)) {
  111. p = __va(memblock_alloc(size, PAGE_SIZE));
  112. memcpy(p, initial_boot_params, size);
  113. initial_boot_params = p;
  114. DBG("Moved device tree to 0x%p\n", p);
  115. }
  116. DBG("<- move_device_tree\n");
  117. }
  118. /*
  119. * ibm,pa-features is a per-cpu property that contains a string of
  120. * attribute descriptors, each of which has a 2 byte header plus up
  121. * to 254 bytes worth of processor attribute bits. First header
  122. * byte specifies the number of bytes following the header.
  123. * Second header byte is an "attribute-specifier" type, of which
  124. * zero is the only currently-defined value.
  125. * Implementation: Pass in the byte and bit offset for the feature
  126. * that we are interested in. The function will return -1 if the
  127. * pa-features property is missing, or a 1/0 to indicate if the feature
  128. * is supported/not supported. Note that the bit numbers are
  129. * big-endian to match the definition in PAPR.
  130. */
  131. static struct ibm_pa_feature {
  132. unsigned long cpu_features; /* CPU_FTR_xxx bit */
  133. unsigned long mmu_features; /* MMU_FTR_xxx bit */
  134. unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
  135. unsigned char pabyte; /* byte number in ibm,pa-features */
  136. unsigned char pabit; /* bit number (big-endian) */
  137. unsigned char invert; /* if 1, pa bit set => clear feature */
  138. } ibm_pa_features[] __initdata = {
  139. {0, 0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
  140. {0, 0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
  141. {CPU_FTR_CTRL, 0, 0, 0, 3, 0},
  142. {CPU_FTR_NOEXECUTE, 0, 0, 0, 6, 0},
  143. {CPU_FTR_NODSISRALIGN, 0, 0, 1, 1, 1},
  144. {0, MMU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
  145. {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
  146. /*
  147. * If the kernel doesn't support TM (ie. CONFIG_PPC_TRANSACTIONAL_MEM=n),
  148. * we don't want to turn on CPU_FTR_TM here, so we use CPU_FTR_TM_COMP
  149. * which is 0 if the kernel doesn't support TM.
  150. */
  151. {CPU_FTR_TM_COMP, 0, 0, 22, 0, 0},
  152. };
  153. static void __init scan_features(unsigned long node, const unsigned char *ftrs,
  154. unsigned long tablelen,
  155. struct ibm_pa_feature *fp,
  156. unsigned long ft_size)
  157. {
  158. unsigned long i, len, bit;
  159. /* find descriptor with type == 0 */
  160. for (;;) {
  161. if (tablelen < 3)
  162. return;
  163. len = 2 + ftrs[0];
  164. if (tablelen < len)
  165. return; /* descriptor 0 not found */
  166. if (ftrs[1] == 0)
  167. break;
  168. tablelen -= len;
  169. ftrs += len;
  170. }
  171. /* loop over bits we know about */
  172. for (i = 0; i < ft_size; ++i, ++fp) {
  173. if (fp->pabyte >= ftrs[0])
  174. continue;
  175. bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
  176. if (bit ^ fp->invert) {
  177. cur_cpu_spec->cpu_features |= fp->cpu_features;
  178. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
  179. cur_cpu_spec->mmu_features |= fp->mmu_features;
  180. } else {
  181. cur_cpu_spec->cpu_features &= ~fp->cpu_features;
  182. cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
  183. cur_cpu_spec->mmu_features &= ~fp->mmu_features;
  184. }
  185. }
  186. }
  187. static void __init check_cpu_pa_features(unsigned long node)
  188. {
  189. const unsigned char *pa_ftrs;
  190. int tablelen;
  191. pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
  192. if (pa_ftrs == NULL)
  193. return;
  194. scan_features(node, pa_ftrs, tablelen,
  195. ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
  196. }
  197. #ifdef CONFIG_PPC_STD_MMU_64
  198. static void __init check_cpu_slb_size(unsigned long node)
  199. {
  200. const __be32 *slb_size_ptr;
  201. slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
  202. if (slb_size_ptr != NULL) {
  203. mmu_slb_size = be32_to_cpup(slb_size_ptr);
  204. return;
  205. }
  206. slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
  207. if (slb_size_ptr != NULL) {
  208. mmu_slb_size = be32_to_cpup(slb_size_ptr);
  209. }
  210. }
  211. #else
  212. #define check_cpu_slb_size(node) do { } while(0)
  213. #endif
  214. static struct feature_property {
  215. const char *name;
  216. u32 min_value;
  217. unsigned long cpu_feature;
  218. unsigned long cpu_user_ftr;
  219. } feature_properties[] __initdata = {
  220. #ifdef CONFIG_ALTIVEC
  221. {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  222. {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  223. #endif /* CONFIG_ALTIVEC */
  224. #ifdef CONFIG_VSX
  225. /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
  226. {"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
  227. #endif /* CONFIG_VSX */
  228. #ifdef CONFIG_PPC64
  229. {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
  230. {"ibm,purr", 1, CPU_FTR_PURR, 0},
  231. {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
  232. #endif /* CONFIG_PPC64 */
  233. };
  234. #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
  235. static inline void identical_pvr_fixup(unsigned long node)
  236. {
  237. unsigned int pvr;
  238. const char *model = of_get_flat_dt_prop(node, "model", NULL);
  239. /*
  240. * Since 440GR(x)/440EP(x) processors have the same pvr,
  241. * we check the node path and set bit 28 in the cur_cpu_spec
  242. * pvr for EP(x) processor version. This bit is always 0 in
  243. * the "real" pvr. Then we call identify_cpu again with
  244. * the new logical pvr to enable FPU support.
  245. */
  246. if (model && strstr(model, "440EP")) {
  247. pvr = cur_cpu_spec->pvr_value | 0x8;
  248. identify_cpu(0, pvr);
  249. DBG("Using logical pvr %x for %s\n", pvr, model);
  250. }
  251. }
  252. #else
  253. #define identical_pvr_fixup(node) do { } while(0)
  254. #endif
  255. static void __init check_cpu_feature_properties(unsigned long node)
  256. {
  257. unsigned long i;
  258. struct feature_property *fp = feature_properties;
  259. const __be32 *prop;
  260. for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
  261. prop = of_get_flat_dt_prop(node, fp->name, NULL);
  262. if (prop && be32_to_cpup(prop) >= fp->min_value) {
  263. cur_cpu_spec->cpu_features |= fp->cpu_feature;
  264. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
  265. }
  266. }
  267. }
  268. static int __init early_init_dt_scan_cpus(unsigned long node,
  269. const char *uname, int depth,
  270. void *data)
  271. {
  272. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  273. const __be32 *prop;
  274. const __be32 *intserv;
  275. int i, nthreads;
  276. int len;
  277. int found = -1;
  278. int found_thread = 0;
  279. /* We are scanning "cpu" nodes only */
  280. if (type == NULL || strcmp(type, "cpu") != 0)
  281. return 0;
  282. /* Get physical cpuid */
  283. intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
  284. if (!intserv)
  285. intserv = of_get_flat_dt_prop(node, "reg", &len);
  286. nthreads = len / sizeof(int);
  287. /*
  288. * Now see if any of these threads match our boot cpu.
  289. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  290. */
  291. for (i = 0; i < nthreads; i++) {
  292. /*
  293. * version 2 of the kexec param format adds the phys cpuid of
  294. * booted proc.
  295. */
  296. if (fdt_version(initial_boot_params) >= 2) {
  297. if (be32_to_cpu(intserv[i]) ==
  298. fdt_boot_cpuid_phys(initial_boot_params)) {
  299. found = boot_cpu_count;
  300. found_thread = i;
  301. }
  302. } else {
  303. /*
  304. * Check if it's the boot-cpu, set it's hw index now,
  305. * unfortunately this format did not support booting
  306. * off secondary threads.
  307. */
  308. if (of_get_flat_dt_prop(node,
  309. "linux,boot-cpu", NULL) != NULL)
  310. found = boot_cpu_count;
  311. }
  312. #ifdef CONFIG_SMP
  313. /* logical cpu id is always 0 on UP kernels */
  314. boot_cpu_count++;
  315. #endif
  316. }
  317. /* Not the boot CPU */
  318. if (found < 0)
  319. return 0;
  320. DBG("boot cpu: logical %d physical %d\n", found,
  321. be32_to_cpu(intserv[found_thread]));
  322. boot_cpuid = found;
  323. set_hard_smp_processor_id(found, be32_to_cpu(intserv[found_thread]));
  324. /*
  325. * PAPR defines "logical" PVR values for cpus that
  326. * meet various levels of the architecture:
  327. * 0x0f000001 Architecture version 2.04
  328. * 0x0f000002 Architecture version 2.05
  329. * If the cpu-version property in the cpu node contains
  330. * such a value, we call identify_cpu again with the
  331. * logical PVR value in order to use the cpu feature
  332. * bits appropriate for the architecture level.
  333. *
  334. * A POWER6 partition in "POWER6 architected" mode
  335. * uses the 0x0f000002 PVR value; in POWER5+ mode
  336. * it uses 0x0f000001.
  337. */
  338. prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
  339. if (prop && (be32_to_cpup(prop) & 0xff000000) == 0x0f000000)
  340. identify_cpu(0, be32_to_cpup(prop));
  341. identical_pvr_fixup(node);
  342. check_cpu_feature_properties(node);
  343. check_cpu_pa_features(node);
  344. check_cpu_slb_size(node);
  345. #ifdef CONFIG_PPC64
  346. if (nthreads > 1)
  347. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  348. else
  349. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  350. #endif
  351. return 0;
  352. }
  353. static int __init early_init_dt_scan_chosen_ppc(unsigned long node,
  354. const char *uname,
  355. int depth, void *data)
  356. {
  357. const unsigned long *lprop; /* All these set by kernel, so no need to convert endian */
  358. /* Use common scan routine to determine if this is the chosen node */
  359. if (early_init_dt_scan_chosen(node, uname, depth, data) == 0)
  360. return 0;
  361. #ifdef CONFIG_PPC64
  362. /* check if iommu is forced on or off */
  363. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  364. iommu_is_off = 1;
  365. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  366. iommu_force_on = 1;
  367. #endif
  368. /* mem=x on the command line is the preferred mechanism */
  369. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  370. if (lprop)
  371. memory_limit = *lprop;
  372. #ifdef CONFIG_PPC64
  373. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  374. if (lprop)
  375. tce_alloc_start = *lprop;
  376. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  377. if (lprop)
  378. tce_alloc_end = *lprop;
  379. #endif
  380. #ifdef CONFIG_KEXEC
  381. lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  382. if (lprop)
  383. crashk_res.start = *lprop;
  384. lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  385. if (lprop)
  386. crashk_res.end = crashk_res.start + *lprop - 1;
  387. #endif
  388. /* break now */
  389. return 1;
  390. }
  391. #ifdef CONFIG_PPC_PSERIES
  392. /*
  393. * Interpret the ibm,dynamic-memory property in the
  394. * /ibm,dynamic-reconfiguration-memory node.
  395. * This contains a list of memory blocks along with NUMA affinity
  396. * information.
  397. */
  398. static int __init early_init_dt_scan_drconf_memory(unsigned long node)
  399. {
  400. const __be32 *dm, *ls, *usm;
  401. int l;
  402. unsigned long n, flags;
  403. u64 base, size, memblock_size;
  404. unsigned int is_kexec_kdump = 0, rngs;
  405. ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
  406. if (ls == NULL || l < dt_root_size_cells * sizeof(__be32))
  407. return 0;
  408. memblock_size = dt_mem_next_cell(dt_root_size_cells, &ls);
  409. dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
  410. if (dm == NULL || l < sizeof(__be32))
  411. return 0;
  412. n = of_read_number(dm++, 1); /* number of entries */
  413. if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(__be32))
  414. return 0;
  415. /* check if this is a kexec/kdump kernel. */
  416. usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
  417. &l);
  418. if (usm != NULL)
  419. is_kexec_kdump = 1;
  420. for (; n != 0; --n) {
  421. base = dt_mem_next_cell(dt_root_addr_cells, &dm);
  422. flags = of_read_number(&dm[3], 1);
  423. /* skip DRC index, pad, assoc. list index, flags */
  424. dm += 4;
  425. /* skip this block if the reserved bit is set in flags (0x80)
  426. or if the block is not assigned to this partition (0x8) */
  427. if ((flags & 0x80) || !(flags & 0x8))
  428. continue;
  429. size = memblock_size;
  430. rngs = 1;
  431. if (is_kexec_kdump) {
  432. /*
  433. * For each memblock in ibm,dynamic-memory, a corresponding
  434. * entry in linux,drconf-usable-memory property contains
  435. * a counter 'p' followed by 'p' (base, size) duple.
  436. * Now read the counter from
  437. * linux,drconf-usable-memory property
  438. */
  439. rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
  440. if (!rngs) /* there are no (base, size) duple */
  441. continue;
  442. }
  443. do {
  444. if (is_kexec_kdump) {
  445. base = dt_mem_next_cell(dt_root_addr_cells,
  446. &usm);
  447. size = dt_mem_next_cell(dt_root_size_cells,
  448. &usm);
  449. }
  450. if (iommu_is_off) {
  451. if (base >= 0x80000000ul)
  452. continue;
  453. if ((base + size) > 0x80000000ul)
  454. size = 0x80000000ul - base;
  455. }
  456. memblock_add(base, size);
  457. } while (--rngs);
  458. }
  459. memblock_dump_all();
  460. return 0;
  461. }
  462. #else
  463. #define early_init_dt_scan_drconf_memory(node) 0
  464. #endif /* CONFIG_PPC_PSERIES */
  465. static int __init early_init_dt_scan_memory_ppc(unsigned long node,
  466. const char *uname,
  467. int depth, void *data)
  468. {
  469. if (depth == 1 &&
  470. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  471. return early_init_dt_scan_drconf_memory(node);
  472. return early_init_dt_scan_memory(node, uname, depth, data);
  473. }
  474. /*
  475. * For a relocatable kernel, we need to get the memstart_addr first,
  476. * then use it to calculate the virtual kernel start address. This has
  477. * to happen at a very early stage (before machine_init). In this case,
  478. * we just want to get the memstart_address and would not like to mess the
  479. * memblock at this stage. So introduce a variable to skip the memblock_add()
  480. * for this reason.
  481. */
  482. #ifdef CONFIG_RELOCATABLE
  483. static int add_mem_to_memblock = 1;
  484. #else
  485. #define add_mem_to_memblock 1
  486. #endif
  487. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  488. {
  489. #ifdef CONFIG_PPC64
  490. if (iommu_is_off) {
  491. if (base >= 0x80000000ul)
  492. return;
  493. if ((base + size) > 0x80000000ul)
  494. size = 0x80000000ul - base;
  495. }
  496. #endif
  497. /* Keep track of the beginning of memory -and- the size of
  498. * the very first block in the device-tree as it represents
  499. * the RMA on ppc64 server
  500. */
  501. if (base < memstart_addr) {
  502. memstart_addr = base;
  503. first_memblock_size = size;
  504. }
  505. /* Add the chunk to the MEMBLOCK list */
  506. if (add_mem_to_memblock)
  507. memblock_add(base, size);
  508. }
  509. static void __init early_reserve_mem_dt(void)
  510. {
  511. unsigned long i, dt_root;
  512. int len;
  513. const __be32 *prop;
  514. early_init_fdt_scan_reserved_mem();
  515. dt_root = of_get_flat_dt_root();
  516. prop = of_get_flat_dt_prop(dt_root, "reserved-ranges", &len);
  517. if (!prop)
  518. return;
  519. DBG("Found new-style reserved-ranges\n");
  520. /* Each reserved range is an (address,size) pair, 2 cells each,
  521. * totalling 4 cells per range. */
  522. for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
  523. u64 base, size;
  524. base = of_read_number(prop + (i * 4) + 0, 2);
  525. size = of_read_number(prop + (i * 4) + 2, 2);
  526. if (size) {
  527. DBG("reserving: %llx -> %llx\n", base, size);
  528. memblock_reserve(base, size);
  529. }
  530. }
  531. }
  532. static void __init early_reserve_mem(void)
  533. {
  534. __be64 *reserve_map;
  535. reserve_map = (__be64 *)(((unsigned long)initial_boot_params) +
  536. fdt_off_mem_rsvmap(initial_boot_params));
  537. /* Look for the new "reserved-regions" property in the DT */
  538. early_reserve_mem_dt();
  539. #ifdef CONFIG_BLK_DEV_INITRD
  540. /* Then reserve the initrd, if any */
  541. if (initrd_start && (initrd_end > initrd_start)) {
  542. memblock_reserve(_ALIGN_DOWN(__pa(initrd_start), PAGE_SIZE),
  543. _ALIGN_UP(initrd_end, PAGE_SIZE) -
  544. _ALIGN_DOWN(initrd_start, PAGE_SIZE));
  545. }
  546. #endif /* CONFIG_BLK_DEV_INITRD */
  547. #ifdef CONFIG_PPC32
  548. /*
  549. * Handle the case where we might be booting from an old kexec
  550. * image that setup the mem_rsvmap as pairs of 32-bit values
  551. */
  552. if (be64_to_cpup(reserve_map) > 0xffffffffull) {
  553. u32 base_32, size_32;
  554. __be32 *reserve_map_32 = (__be32 *)reserve_map;
  555. DBG("Found old 32-bit reserve map\n");
  556. while (1) {
  557. base_32 = be32_to_cpup(reserve_map_32++);
  558. size_32 = be32_to_cpup(reserve_map_32++);
  559. if (size_32 == 0)
  560. break;
  561. DBG("reserving: %x -> %x\n", base_32, size_32);
  562. memblock_reserve(base_32, size_32);
  563. }
  564. return;
  565. }
  566. #endif
  567. }
  568. void __init early_init_devtree(void *params)
  569. {
  570. phys_addr_t limit;
  571. DBG(" -> early_init_devtree(%p)\n", params);
  572. /* Too early to BUG_ON(), do it by hand */
  573. if (!early_init_dt_verify(params))
  574. panic("BUG: Failed verifying flat device tree, bad version?");
  575. #ifdef CONFIG_PPC_RTAS
  576. /* Some machines might need RTAS info for debugging, grab it now. */
  577. of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
  578. #endif
  579. #ifdef CONFIG_PPC_POWERNV
  580. /* Some machines might need OPAL info for debugging, grab it now. */
  581. of_scan_flat_dt(early_init_dt_scan_opal, NULL);
  582. #endif
  583. #ifdef CONFIG_FA_DUMP
  584. /* scan tree to see if dump is active during last boot */
  585. of_scan_flat_dt(early_init_dt_scan_fw_dump, NULL);
  586. #endif
  587. /* Retrieve various informations from the /chosen node of the
  588. * device-tree, including the platform type, initrd location and
  589. * size, TCE reserve, and more ...
  590. */
  591. of_scan_flat_dt(early_init_dt_scan_chosen_ppc, boot_command_line);
  592. /* Scan memory nodes and rebuild MEMBLOCKs */
  593. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  594. of_scan_flat_dt(early_init_dt_scan_memory_ppc, NULL);
  595. parse_early_param();
  596. /* make sure we've parsed cmdline for mem= before this */
  597. if (memory_limit)
  598. first_memblock_size = min_t(u64, first_memblock_size, memory_limit);
  599. setup_initial_memory_limit(memstart_addr, first_memblock_size);
  600. /* Reserve MEMBLOCK regions used by kernel, initrd, dt, etc... */
  601. memblock_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  602. /* If relocatable, reserve first 32k for interrupt vectors etc. */
  603. if (PHYSICAL_START > MEMORY_START)
  604. memblock_reserve(MEMORY_START, 0x8000);
  605. reserve_kdump_trampoline();
  606. #ifdef CONFIG_FA_DUMP
  607. /*
  608. * If we fail to reserve memory for firmware-assisted dump then
  609. * fallback to kexec based kdump.
  610. */
  611. if (fadump_reserve_mem() == 0)
  612. #endif
  613. reserve_crashkernel();
  614. early_reserve_mem();
  615. /* Ensure that total memory size is page-aligned. */
  616. limit = ALIGN(memory_limit ?: memblock_phys_mem_size(), PAGE_SIZE);
  617. memblock_enforce_memory_limit(limit);
  618. memblock_allow_resize();
  619. memblock_dump_all();
  620. DBG("Phys. mem: %llx\n", memblock_phys_mem_size());
  621. /* We may need to relocate the flat tree, do it now.
  622. * FIXME .. and the initrd too? */
  623. move_device_tree();
  624. allocate_pacas();
  625. DBG("Scanning CPUs ...\n");
  626. /* Retrieve CPU related informations from the flat tree
  627. * (altivec support, boot CPU ID, ...)
  628. */
  629. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  630. if (boot_cpuid < 0) {
  631. printk("Failed to identify boot CPU !\n");
  632. BUG();
  633. }
  634. #if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
  635. /* We'll later wait for secondaries to check in; there are
  636. * NCPUS-1 non-boot CPUs :-)
  637. */
  638. spinning_secondaries = boot_cpu_count - 1;
  639. #endif
  640. #ifdef CONFIG_PPC_POWERNV
  641. /* Scan and build the list of machine check recoverable ranges */
  642. of_scan_flat_dt(early_init_dt_scan_recoverable_ranges, NULL);
  643. #endif
  644. DBG(" <- early_init_devtree()\n");
  645. }
  646. #ifdef CONFIG_RELOCATABLE
  647. /*
  648. * This function run before early_init_devtree, so we have to init
  649. * initial_boot_params.
  650. */
  651. void __init early_get_first_memblock_info(void *params, phys_addr_t *size)
  652. {
  653. /* Setup flat device-tree pointer */
  654. initial_boot_params = params;
  655. /*
  656. * Scan the memory nodes and set add_mem_to_memblock to 0 to avoid
  657. * mess the memblock.
  658. */
  659. add_mem_to_memblock = 0;
  660. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  661. of_scan_flat_dt(early_init_dt_scan_memory_ppc, NULL);
  662. add_mem_to_memblock = 1;
  663. if (size)
  664. *size = first_memblock_size;
  665. }
  666. #endif
  667. /*******
  668. *
  669. * New implementation of the OF "find" APIs, return a refcounted
  670. * object, call of_node_put() when done. The device tree and list
  671. * are protected by a rw_lock.
  672. *
  673. * Note that property management will need some locking as well,
  674. * this isn't dealt with yet.
  675. *
  676. *******/
  677. /**
  678. * of_get_ibm_chip_id - Returns the IBM "chip-id" of a device
  679. * @np: device node of the device
  680. *
  681. * This looks for a property "ibm,chip-id" in the node or any
  682. * of its parents and returns its content, or -1 if it cannot
  683. * be found.
  684. */
  685. int of_get_ibm_chip_id(struct device_node *np)
  686. {
  687. of_node_get(np);
  688. while(np) {
  689. struct device_node *old = np;
  690. const __be32 *prop;
  691. prop = of_get_property(np, "ibm,chip-id", NULL);
  692. if (prop) {
  693. of_node_put(np);
  694. return be32_to_cpup(prop);
  695. }
  696. np = of_get_parent(np);
  697. of_node_put(old);
  698. }
  699. return -1;
  700. }
  701. /**
  702. * cpu_to_chip_id - Return the cpus chip-id
  703. * @cpu: The logical cpu number.
  704. *
  705. * Return the value of the ibm,chip-id property corresponding to the given
  706. * logical cpu number. If the chip-id can not be found, returns -1.
  707. */
  708. int cpu_to_chip_id(int cpu)
  709. {
  710. struct device_node *np;
  711. np = of_get_cpu_node(cpu, NULL);
  712. if (!np)
  713. return -1;
  714. of_node_put(np);
  715. return of_get_ibm_chip_id(np);
  716. }
  717. EXPORT_SYMBOL(cpu_to_chip_id);
  718. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  719. {
  720. return (int)phys_id == get_hard_smp_processor_id(cpu);
  721. }