prom.c 39 KB

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