prom.c 24 KB

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