prom.c 40 KB

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