prom.c 39 KB

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