prom.c 39 KB

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