prom.c 29 KB

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