prom.c 34 KB

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