prom.c 24 KB

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