prom.c 44 KB

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