prom.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. {0, MMU_FTR_SLB, 0, 0, 2, 0},
  142. {CPU_FTR_CTRL, 0, 0, 0, 3, 0},
  143. {CPU_FTR_NOEXECUTE, 0, 0, 0, 6, 0},
  144. {CPU_FTR_NODSISRALIGN, 0, 0, 1, 1, 1},
  145. {0, MMU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
  146. {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
  147. };
  148. static void __init scan_features(unsigned long node, const unsigned char *ftrs,
  149. unsigned long tablelen,
  150. struct ibm_pa_feature *fp,
  151. unsigned long ft_size)
  152. {
  153. unsigned long i, len, bit;
  154. /* find descriptor with type == 0 */
  155. for (;;) {
  156. if (tablelen < 3)
  157. return;
  158. len = 2 + ftrs[0];
  159. if (tablelen < len)
  160. return; /* descriptor 0 not found */
  161. if (ftrs[1] == 0)
  162. break;
  163. tablelen -= len;
  164. ftrs += len;
  165. }
  166. /* loop over bits we know about */
  167. for (i = 0; i < ft_size; ++i, ++fp) {
  168. if (fp->pabyte >= ftrs[0])
  169. continue;
  170. bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
  171. if (bit ^ fp->invert) {
  172. cur_cpu_spec->cpu_features |= fp->cpu_features;
  173. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
  174. cur_cpu_spec->mmu_features |= fp->mmu_features;
  175. } else {
  176. cur_cpu_spec->cpu_features &= ~fp->cpu_features;
  177. cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
  178. cur_cpu_spec->mmu_features &= ~fp->mmu_features;
  179. }
  180. }
  181. }
  182. static void __init check_cpu_pa_features(unsigned long node)
  183. {
  184. const unsigned char *pa_ftrs;
  185. int tablelen;
  186. pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
  187. if (pa_ftrs == NULL)
  188. return;
  189. scan_features(node, pa_ftrs, tablelen,
  190. ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
  191. }
  192. #ifdef CONFIG_PPC_STD_MMU_64
  193. static void __init check_cpu_slb_size(unsigned long node)
  194. {
  195. const __be32 *slb_size_ptr;
  196. slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
  197. if (slb_size_ptr != NULL) {
  198. mmu_slb_size = be32_to_cpup(slb_size_ptr);
  199. return;
  200. }
  201. slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
  202. if (slb_size_ptr != NULL) {
  203. mmu_slb_size = be32_to_cpup(slb_size_ptr);
  204. }
  205. }
  206. #else
  207. #define check_cpu_slb_size(node) do { } while(0)
  208. #endif
  209. static struct feature_property {
  210. const char *name;
  211. u32 min_value;
  212. unsigned long cpu_feature;
  213. unsigned long cpu_user_ftr;
  214. } feature_properties[] __initdata = {
  215. #ifdef CONFIG_ALTIVEC
  216. {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  217. {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  218. #endif /* CONFIG_ALTIVEC */
  219. #ifdef CONFIG_VSX
  220. /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
  221. {"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
  222. #endif /* CONFIG_VSX */
  223. #ifdef CONFIG_PPC64
  224. {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
  225. {"ibm,purr", 1, CPU_FTR_PURR, 0},
  226. {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
  227. #endif /* CONFIG_PPC64 */
  228. };
  229. #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
  230. static inline void identical_pvr_fixup(unsigned long node)
  231. {
  232. unsigned int pvr;
  233. const char *model = of_get_flat_dt_prop(node, "model", NULL);
  234. /*
  235. * Since 440GR(x)/440EP(x) processors have the same pvr,
  236. * we check the node path and set bit 28 in the cur_cpu_spec
  237. * pvr for EP(x) processor version. This bit is always 0 in
  238. * the "real" pvr. Then we call identify_cpu again with
  239. * the new logical pvr to enable FPU support.
  240. */
  241. if (model && strstr(model, "440EP")) {
  242. pvr = cur_cpu_spec->pvr_value | 0x8;
  243. identify_cpu(0, pvr);
  244. DBG("Using logical pvr %x for %s\n", pvr, model);
  245. }
  246. }
  247. #else
  248. #define identical_pvr_fixup(node) do { } while(0)
  249. #endif
  250. static void __init check_cpu_feature_properties(unsigned long node)
  251. {
  252. unsigned long i;
  253. struct feature_property *fp = feature_properties;
  254. const __be32 *prop;
  255. for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
  256. prop = of_get_flat_dt_prop(node, fp->name, NULL);
  257. if (prop && be32_to_cpup(prop) >= fp->min_value) {
  258. cur_cpu_spec->cpu_features |= fp->cpu_feature;
  259. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
  260. }
  261. }
  262. }
  263. static int __init early_init_dt_scan_cpus(unsigned long node,
  264. const char *uname, int depth,
  265. void *data)
  266. {
  267. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  268. const __be32 *prop;
  269. const __be32 *intserv;
  270. int i, nthreads;
  271. int len;
  272. int found = -1;
  273. int found_thread = 0;
  274. /* We are scanning "cpu" nodes only */
  275. if (type == NULL || strcmp(type, "cpu") != 0)
  276. return 0;
  277. /* Get physical cpuid */
  278. intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
  279. if (intserv) {
  280. nthreads = len / sizeof(int);
  281. } else {
  282. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  283. nthreads = 1;
  284. }
  285. /*
  286. * Now see if any of these threads match our boot cpu.
  287. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  288. */
  289. for (i = 0; i < nthreads; i++) {
  290. /*
  291. * version 2 of the kexec param format adds the phys cpuid of
  292. * booted proc.
  293. */
  294. if (fdt_version(initial_boot_params) >= 2) {
  295. if (be32_to_cpu(intserv[i]) ==
  296. fdt_boot_cpuid_phys(initial_boot_params)) {
  297. found = boot_cpu_count;
  298. found_thread = i;
  299. }
  300. } else {
  301. /*
  302. * Check if it's the boot-cpu, set it's hw index now,
  303. * unfortunately this format did not support booting
  304. * off secondary threads.
  305. */
  306. if (of_get_flat_dt_prop(node,
  307. "linux,boot-cpu", NULL) != NULL)
  308. found = boot_cpu_count;
  309. }
  310. #ifdef CONFIG_SMP
  311. /* logical cpu id is always 0 on UP kernels */
  312. boot_cpu_count++;
  313. #endif
  314. }
  315. /* Not the boot CPU */
  316. if (found < 0)
  317. return 0;
  318. DBG("boot cpu: logical %d physical %d\n", found,
  319. be32_to_cpu(intserv[found_thread]));
  320. boot_cpuid = found;
  321. set_hard_smp_processor_id(found, be32_to_cpu(intserv[found_thread]));
  322. /*
  323. * PAPR defines "logical" PVR values for cpus that
  324. * meet various levels of the architecture:
  325. * 0x0f000001 Architecture version 2.04
  326. * 0x0f000002 Architecture version 2.05
  327. * If the cpu-version property in the cpu node contains
  328. * such a value, we call identify_cpu again with the
  329. * logical PVR value in order to use the cpu feature
  330. * bits appropriate for the architecture level.
  331. *
  332. * A POWER6 partition in "POWER6 architected" mode
  333. * uses the 0x0f000002 PVR value; in POWER5+ mode
  334. * it uses 0x0f000001.
  335. */
  336. prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
  337. if (prop && (be32_to_cpup(prop) & 0xff000000) == 0x0f000000)
  338. identify_cpu(0, be32_to_cpup(prop));
  339. identical_pvr_fixup(node);
  340. check_cpu_feature_properties(node);
  341. check_cpu_pa_features(node);
  342. check_cpu_slb_size(node);
  343. #ifdef CONFIG_PPC64
  344. if (nthreads > 1)
  345. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  346. else
  347. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  348. #endif
  349. return 0;
  350. }
  351. int __init early_init_dt_scan_chosen_ppc(unsigned long node, const char *uname,
  352. int depth, void *data)
  353. {
  354. const unsigned long *lprop; /* All these set by kernel, so no need to convert endian */
  355. /* Use common scan routine to determine if this is the chosen node */
  356. if (early_init_dt_scan_chosen(node, uname, depth, data) == 0)
  357. return 0;
  358. #ifdef CONFIG_PPC64
  359. /* check if iommu is forced on or off */
  360. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  361. iommu_is_off = 1;
  362. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  363. iommu_force_on = 1;
  364. #endif
  365. /* mem=x on the command line is the preferred mechanism */
  366. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  367. if (lprop)
  368. memory_limit = *lprop;
  369. #ifdef CONFIG_PPC64
  370. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  371. if (lprop)
  372. tce_alloc_start = *lprop;
  373. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  374. if (lprop)
  375. tce_alloc_end = *lprop;
  376. #endif
  377. #ifdef CONFIG_KEXEC
  378. lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  379. if (lprop)
  380. crashk_res.start = *lprop;
  381. lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  382. if (lprop)
  383. crashk_res.end = crashk_res.start + *lprop - 1;
  384. #endif
  385. /* break now */
  386. return 1;
  387. }
  388. #ifdef CONFIG_PPC_PSERIES
  389. /*
  390. * Interpret the ibm,dynamic-memory property in the
  391. * /ibm,dynamic-reconfiguration-memory node.
  392. * This contains a list of memory blocks along with NUMA affinity
  393. * information.
  394. */
  395. static int __init early_init_dt_scan_drconf_memory(unsigned long node)
  396. {
  397. const __be32 *dm, *ls, *usm;
  398. int l;
  399. unsigned long n, flags;
  400. u64 base, size, memblock_size;
  401. unsigned int is_kexec_kdump = 0, rngs;
  402. ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
  403. if (ls == NULL || l < dt_root_size_cells * sizeof(__be32))
  404. return 0;
  405. memblock_size = dt_mem_next_cell(dt_root_size_cells, &ls);
  406. dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
  407. if (dm == NULL || l < sizeof(__be32))
  408. return 0;
  409. n = of_read_number(dm++, 1); /* number of entries */
  410. if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(__be32))
  411. return 0;
  412. /* check if this is a kexec/kdump kernel. */
  413. usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
  414. &l);
  415. if (usm != NULL)
  416. is_kexec_kdump = 1;
  417. for (; n != 0; --n) {
  418. base = dt_mem_next_cell(dt_root_addr_cells, &dm);
  419. flags = of_read_number(&dm[3], 1);
  420. /* skip DRC index, pad, assoc. list index, flags */
  421. dm += 4;
  422. /* skip this block if the reserved bit is set in flags (0x80)
  423. or if the block is not assigned to this partition (0x8) */
  424. if ((flags & 0x80) || !(flags & 0x8))
  425. continue;
  426. size = memblock_size;
  427. rngs = 1;
  428. if (is_kexec_kdump) {
  429. /*
  430. * For each memblock in ibm,dynamic-memory, a corresponding
  431. * entry in linux,drconf-usable-memory property contains
  432. * a counter 'p' followed by 'p' (base, size) duple.
  433. * Now read the counter from
  434. * linux,drconf-usable-memory property
  435. */
  436. rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
  437. if (!rngs) /* there are no (base, size) duple */
  438. continue;
  439. }
  440. do {
  441. if (is_kexec_kdump) {
  442. base = dt_mem_next_cell(dt_root_addr_cells,
  443. &usm);
  444. size = dt_mem_next_cell(dt_root_size_cells,
  445. &usm);
  446. }
  447. if (iommu_is_off) {
  448. if (base >= 0x80000000ul)
  449. continue;
  450. if ((base + size) > 0x80000000ul)
  451. size = 0x80000000ul - base;
  452. }
  453. memblock_add(base, size);
  454. } while (--rngs);
  455. }
  456. memblock_dump_all();
  457. return 0;
  458. }
  459. #else
  460. #define early_init_dt_scan_drconf_memory(node) 0
  461. #endif /* CONFIG_PPC_PSERIES */
  462. static int __init early_init_dt_scan_memory_ppc(unsigned long node,
  463. const char *uname,
  464. int depth, void *data)
  465. {
  466. if (depth == 1 &&
  467. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  468. return early_init_dt_scan_drconf_memory(node);
  469. return early_init_dt_scan_memory(node, uname, depth, data);
  470. }
  471. /*
  472. * For a relocatable kernel, we need to get the memstart_addr first,
  473. * then use it to calculate the virtual kernel start address. This has
  474. * to happen at a very early stage (before machine_init). In this case,
  475. * we just want to get the memstart_address and would not like to mess the
  476. * memblock at this stage. So introduce a variable to skip the memblock_add()
  477. * for this reason.
  478. */
  479. #ifdef CONFIG_RELOCATABLE
  480. static int add_mem_to_memblock = 1;
  481. #else
  482. #define add_mem_to_memblock 1
  483. #endif
  484. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  485. {
  486. #ifdef CONFIG_PPC64
  487. if (iommu_is_off) {
  488. if (base >= 0x80000000ul)
  489. return;
  490. if ((base + size) > 0x80000000ul)
  491. size = 0x80000000ul - base;
  492. }
  493. #endif
  494. /* Keep track of the beginning of memory -and- the size of
  495. * the very first block in the device-tree as it represents
  496. * the RMA on ppc64 server
  497. */
  498. if (base < memstart_addr) {
  499. memstart_addr = base;
  500. first_memblock_size = size;
  501. }
  502. /* Add the chunk to the MEMBLOCK list */
  503. if (add_mem_to_memblock)
  504. memblock_add(base, size);
  505. }
  506. static void __init early_reserve_mem_dt(void)
  507. {
  508. unsigned long i, dt_root;
  509. int len;
  510. const __be32 *prop;
  511. early_init_fdt_scan_reserved_mem();
  512. dt_root = of_get_flat_dt_root();
  513. prop = of_get_flat_dt_prop(dt_root, "reserved-ranges", &len);
  514. if (!prop)
  515. return;
  516. DBG("Found new-style reserved-ranges\n");
  517. /* Each reserved range is an (address,size) pair, 2 cells each,
  518. * totalling 4 cells per range. */
  519. for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
  520. u64 base, size;
  521. base = of_read_number(prop + (i * 4) + 0, 2);
  522. size = of_read_number(prop + (i * 4) + 2, 2);
  523. if (size) {
  524. DBG("reserving: %llx -> %llx\n", base, size);
  525. memblock_reserve(base, size);
  526. }
  527. }
  528. }
  529. static void __init early_reserve_mem(void)
  530. {
  531. __be64 *reserve_map;
  532. reserve_map = (__be64 *)(((unsigned long)initial_boot_params) +
  533. fdt_off_mem_rsvmap(initial_boot_params));
  534. /* Look for the new "reserved-regions" property in the DT */
  535. early_reserve_mem_dt();
  536. #ifdef CONFIG_BLK_DEV_INITRD
  537. /* Then reserve the initrd, if any */
  538. if (initrd_start && (initrd_end > initrd_start)) {
  539. memblock_reserve(_ALIGN_DOWN(__pa(initrd_start), PAGE_SIZE),
  540. _ALIGN_UP(initrd_end, PAGE_SIZE) -
  541. _ALIGN_DOWN(initrd_start, PAGE_SIZE));
  542. }
  543. #endif /* CONFIG_BLK_DEV_INITRD */
  544. #ifdef CONFIG_PPC32
  545. /*
  546. * Handle the case where we might be booting from an old kexec
  547. * image that setup the mem_rsvmap as pairs of 32-bit values
  548. */
  549. if (be64_to_cpup(reserve_map) > 0xffffffffull) {
  550. u32 base_32, size_32;
  551. __be32 *reserve_map_32 = (__be32 *)reserve_map;
  552. DBG("Found old 32-bit reserve map\n");
  553. while (1) {
  554. base_32 = be32_to_cpup(reserve_map_32++);
  555. size_32 = be32_to_cpup(reserve_map_32++);
  556. if (size_32 == 0)
  557. break;
  558. DBG("reserving: %x -> %x\n", base_32, size_32);
  559. memblock_reserve(base_32, size_32);
  560. }
  561. return;
  562. }
  563. #endif
  564. }
  565. void __init early_init_devtree(void *params)
  566. {
  567. phys_addr_t limit;
  568. DBG(" -> early_init_devtree(%p)\n", params);
  569. /* Setup flat device-tree pointer */
  570. initial_boot_params = params;
  571. #ifdef CONFIG_PPC_RTAS
  572. /* Some machines might need RTAS info for debugging, grab it now. */
  573. of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
  574. #endif
  575. #ifdef CONFIG_PPC_POWERNV
  576. /* Some machines might need OPAL info for debugging, grab it now. */
  577. of_scan_flat_dt(early_init_dt_scan_opal, NULL);
  578. #endif
  579. #ifdef CONFIG_FA_DUMP
  580. /* scan tree to see if dump is active during last boot */
  581. of_scan_flat_dt(early_init_dt_scan_fw_dump, NULL);
  582. #endif
  583. /* Retrieve various informations from the /chosen node of the
  584. * device-tree, including the platform type, initrd location and
  585. * size, TCE reserve, and more ...
  586. */
  587. of_scan_flat_dt(early_init_dt_scan_chosen_ppc, cmd_line);
  588. /* Scan memory nodes and rebuild MEMBLOCKs */
  589. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  590. of_scan_flat_dt(early_init_dt_scan_memory_ppc, NULL);
  591. /* Save command line for /proc/cmdline and then parse parameters */
  592. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  593. parse_early_param();
  594. /* make sure we've parsed cmdline for mem= before this */
  595. if (memory_limit)
  596. first_memblock_size = min_t(u64, first_memblock_size, memory_limit);
  597. setup_initial_memory_limit(memstart_addr, first_memblock_size);
  598. /* Reserve MEMBLOCK regions used by kernel, initrd, dt, etc... */
  599. memblock_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  600. /* If relocatable, reserve first 32k for interrupt vectors etc. */
  601. if (PHYSICAL_START > MEMORY_START)
  602. memblock_reserve(MEMORY_START, 0x8000);
  603. reserve_kdump_trampoline();
  604. #ifdef CONFIG_FA_DUMP
  605. /*
  606. * If we fail to reserve memory for firmware-assisted dump then
  607. * fallback to kexec based kdump.
  608. */
  609. if (fadump_reserve_mem() == 0)
  610. #endif
  611. reserve_crashkernel();
  612. early_reserve_mem();
  613. /*
  614. * Ensure that total memory size is page-aligned, because otherwise
  615. * mark_bootmem() gets upset.
  616. */
  617. limit = ALIGN(memory_limit ?: memblock_phys_mem_size(), PAGE_SIZE);
  618. memblock_enforce_memory_limit(limit);
  619. memblock_allow_resize();
  620. memblock_dump_all();
  621. DBG("Phys. mem: %llx\n", memblock_phys_mem_size());
  622. /* We may need to relocate the flat tree, do it now.
  623. * FIXME .. and the initrd too? */
  624. move_device_tree();
  625. allocate_pacas();
  626. DBG("Scanning CPUs ...\n");
  627. /* Retrieve CPU related informations from the flat tree
  628. * (altivec support, boot CPU ID, ...)
  629. */
  630. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  631. if (boot_cpuid < 0) {
  632. printk("Failed to indentify boot CPU !\n");
  633. BUG();
  634. }
  635. #if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
  636. /* We'll later wait for secondaries to check in; there are
  637. * NCPUS-1 non-boot CPUs :-)
  638. */
  639. spinning_secondaries = boot_cpu_count - 1;
  640. #endif
  641. #ifdef CONFIG_PPC_POWERNV
  642. /* Scan and build the list of machine check recoverable ranges */
  643. of_scan_flat_dt(early_init_dt_scan_recoverable_ranges, NULL);
  644. #endif
  645. DBG(" <- early_init_devtree()\n");
  646. }
  647. #ifdef CONFIG_RELOCATABLE
  648. /*
  649. * This function run before early_init_devtree, so we have to init
  650. * initial_boot_params.
  651. */
  652. void __init early_get_first_memblock_info(void *params, phys_addr_t *size)
  653. {
  654. /* Setup flat device-tree pointer */
  655. initial_boot_params = params;
  656. /*
  657. * Scan the memory nodes and set add_mem_to_memblock to 0 to avoid
  658. * mess the memblock.
  659. */
  660. add_mem_to_memblock = 0;
  661. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  662. of_scan_flat_dt(early_init_dt_scan_memory_ppc, NULL);
  663. add_mem_to_memblock = 1;
  664. if (size)
  665. *size = first_memblock_size;
  666. }
  667. #endif
  668. /*******
  669. *
  670. * New implementation of the OF "find" APIs, return a refcounted
  671. * object, call of_node_put() when done. The device tree and list
  672. * are protected by a rw_lock.
  673. *
  674. * Note that property management will need some locking as well,
  675. * this isn't dealt with yet.
  676. *
  677. *******/
  678. /**
  679. * of_get_ibm_chip_id - Returns the IBM "chip-id" of a device
  680. * @np: device node of the device
  681. *
  682. * This looks for a property "ibm,chip-id" in the node or any
  683. * of its parents and returns its content, or -1 if it cannot
  684. * be found.
  685. */
  686. int of_get_ibm_chip_id(struct device_node *np)
  687. {
  688. of_node_get(np);
  689. while(np) {
  690. struct device_node *old = np;
  691. const __be32 *prop;
  692. prop = of_get_property(np, "ibm,chip-id", NULL);
  693. if (prop) {
  694. of_node_put(np);
  695. return be32_to_cpup(prop);
  696. }
  697. np = of_get_parent(np);
  698. of_node_put(old);
  699. }
  700. return -1;
  701. }
  702. /**
  703. * cpu_to_chip_id - Return the cpus chip-id
  704. * @cpu: The logical cpu number.
  705. *
  706. * Return the value of the ibm,chip-id property corresponding to the given
  707. * logical cpu number. If the chip-id can not be found, returns -1.
  708. */
  709. int cpu_to_chip_id(int cpu)
  710. {
  711. struct device_node *np;
  712. np = of_get_cpu_node(cpu, NULL);
  713. if (!np)
  714. return -1;
  715. of_node_put(np);
  716. return of_get_ibm_chip_id(np);
  717. }
  718. EXPORT_SYMBOL(cpu_to_chip_id);
  719. #ifdef CONFIG_PPC_PSERIES
  720. /*
  721. * Fix up the uninitialized fields in a new device node:
  722. * name, type and pci-specific fields
  723. */
  724. static int of_finish_dynamic_node(struct device_node *node)
  725. {
  726. struct device_node *parent = of_get_parent(node);
  727. int err = 0;
  728. const phandle *ibm_phandle;
  729. node->name = of_get_property(node, "name", NULL);
  730. node->type = of_get_property(node, "device_type", NULL);
  731. if (!node->name)
  732. node->name = "<NULL>";
  733. if (!node->type)
  734. node->type = "<NULL>";
  735. if (!parent) {
  736. err = -ENODEV;
  737. goto out;
  738. }
  739. /* We don't support that function on PowerMac, at least
  740. * not yet
  741. */
  742. if (machine_is(powermac))
  743. return -ENODEV;
  744. /* fix up new node's phandle field */
  745. if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
  746. node->phandle = *ibm_phandle;
  747. out:
  748. of_node_put(parent);
  749. return err;
  750. }
  751. static int prom_reconfig_notifier(struct notifier_block *nb,
  752. unsigned long action, void *node)
  753. {
  754. int err;
  755. switch (action) {
  756. case OF_RECONFIG_ATTACH_NODE:
  757. err = of_finish_dynamic_node(node);
  758. if (err < 0)
  759. printk(KERN_ERR "finish_node returned %d\n", err);
  760. break;
  761. default:
  762. err = 0;
  763. break;
  764. }
  765. return notifier_from_errno(err);
  766. }
  767. static struct notifier_block prom_reconfig_nb = {
  768. .notifier_call = prom_reconfig_notifier,
  769. .priority = 10, /* This one needs to run first */
  770. };
  771. static int __init prom_reconfig_setup(void)
  772. {
  773. return of_reconfig_notifier_register(&prom_reconfig_nb);
  774. }
  775. __initcall(prom_reconfig_setup);
  776. #endif
  777. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  778. {
  779. return (int)phys_id == get_hard_smp_processor_id(cpu);
  780. }