prom.c 42 KB

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