prom.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  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 = 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,
  361. (char *)pp->value);
  362. }
  363. }
  364. if (allnextpp) {
  365. *prev_pp = NULL;
  366. np->name = of_get_property(np, "name", NULL);
  367. np->type = of_get_property(np, "device_type", NULL);
  368. if (!np->name)
  369. np->name = "<NULL>";
  370. if (!np->type)
  371. np->type = "<NULL>";
  372. }
  373. while (tag == OF_DT_BEGIN_NODE) {
  374. mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
  375. tag = *((u32 *)(*p));
  376. }
  377. if (tag != OF_DT_END_NODE) {
  378. printk("Weird tag at end of node: %x\n", tag);
  379. return mem;
  380. }
  381. *p += 4;
  382. return mem;
  383. }
  384. static int __init early_parse_mem(char *p)
  385. {
  386. if (!p)
  387. return 1;
  388. memory_limit = PAGE_ALIGN(memparse(p, &p));
  389. DBG("memory limit = 0x%lx\n", memory_limit);
  390. return 0;
  391. }
  392. early_param("mem", early_parse_mem);
  393. /*
  394. * The device tree may be allocated below our memory limit, or inside the
  395. * crash kernel region for kdump. If so, move it out now.
  396. */
  397. static void move_device_tree(void)
  398. {
  399. unsigned long start, size;
  400. void *p;
  401. DBG("-> move_device_tree\n");
  402. start = __pa(initial_boot_params);
  403. size = initial_boot_params->totalsize;
  404. if ((memory_limit && (start + size) > memory_limit) ||
  405. overlaps_crashkernel(start, size)) {
  406. p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
  407. memcpy(p, initial_boot_params, size);
  408. initial_boot_params = (struct boot_param_header *)p;
  409. DBG("Moved device tree to 0x%p\n", p);
  410. }
  411. DBG("<- move_device_tree\n");
  412. }
  413. /**
  414. * unflattens the device-tree passed by the firmware, creating the
  415. * tree of struct device_node. It also fills the "name" and "type"
  416. * pointers of the nodes so the normal device-tree walking functions
  417. * can be used (this used to be done by finish_device_tree)
  418. */
  419. void __init unflatten_device_tree(void)
  420. {
  421. unsigned long start, mem, size;
  422. struct device_node **allnextp = &allnodes;
  423. DBG(" -> unflatten_device_tree()\n");
  424. /* First pass, scan for size */
  425. start = ((unsigned long)initial_boot_params) +
  426. initial_boot_params->off_dt_struct;
  427. size = unflatten_dt_node(0, &start, NULL, NULL, 0);
  428. size = (size | 3) + 1;
  429. DBG(" size is %lx, allocating...\n", size);
  430. /* Allocate memory for the expanded device tree */
  431. mem = lmb_alloc(size + 4, __alignof__(struct device_node));
  432. mem = (unsigned long) __va(mem);
  433. ((u32 *)mem)[size / 4] = 0xdeadbeef;
  434. DBG(" unflattening %lx...\n", mem);
  435. /* Second pass, do actual unflattening */
  436. start = ((unsigned long)initial_boot_params) +
  437. initial_boot_params->off_dt_struct;
  438. unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
  439. if (*((u32 *)start) != OF_DT_END)
  440. printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
  441. if (((u32 *)mem)[size / 4] != 0xdeadbeef)
  442. printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
  443. ((u32 *)mem)[size / 4] );
  444. *allnextp = NULL;
  445. /* Get pointer to OF "/chosen" node for use everywhere */
  446. of_chosen = of_find_node_by_path("/chosen");
  447. if (of_chosen == NULL)
  448. of_chosen = of_find_node_by_path("/chosen@0");
  449. DBG(" <- unflatten_device_tree()\n");
  450. }
  451. /*
  452. * ibm,pa-features is a per-cpu property that contains a string of
  453. * attribute descriptors, each of which has a 2 byte header plus up
  454. * to 254 bytes worth of processor attribute bits. First header
  455. * byte specifies the number of bytes following the header.
  456. * Second header byte is an "attribute-specifier" type, of which
  457. * zero is the only currently-defined value.
  458. * Implementation: Pass in the byte and bit offset for the feature
  459. * that we are interested in. The function will return -1 if the
  460. * pa-features property is missing, or a 1/0 to indicate if the feature
  461. * is supported/not supported. Note that the bit numbers are
  462. * big-endian to match the definition in PAPR.
  463. */
  464. static struct ibm_pa_feature {
  465. unsigned long cpu_features; /* CPU_FTR_xxx bit */
  466. unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
  467. unsigned char pabyte; /* byte number in ibm,pa-features */
  468. unsigned char pabit; /* bit number (big-endian) */
  469. unsigned char invert; /* if 1, pa bit set => clear feature */
  470. } ibm_pa_features[] __initdata = {
  471. {0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
  472. {0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
  473. {CPU_FTR_SLB, 0, 0, 2, 0},
  474. {CPU_FTR_CTRL, 0, 0, 3, 0},
  475. {CPU_FTR_NOEXECUTE, 0, 0, 6, 0},
  476. {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1},
  477. #if 0
  478. /* put this back once we know how to test if firmware does 64k IO */
  479. {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
  480. #endif
  481. {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
  482. };
  483. static void __init scan_features(unsigned long node, unsigned char *ftrs,
  484. unsigned long tablelen,
  485. struct ibm_pa_feature *fp,
  486. unsigned long ft_size)
  487. {
  488. unsigned long i, len, bit;
  489. /* find descriptor with type == 0 */
  490. for (;;) {
  491. if (tablelen < 3)
  492. return;
  493. len = 2 + ftrs[0];
  494. if (tablelen < len)
  495. return; /* descriptor 0 not found */
  496. if (ftrs[1] == 0)
  497. break;
  498. tablelen -= len;
  499. ftrs += len;
  500. }
  501. /* loop over bits we know about */
  502. for (i = 0; i < ft_size; ++i, ++fp) {
  503. if (fp->pabyte >= ftrs[0])
  504. continue;
  505. bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
  506. if (bit ^ fp->invert) {
  507. cur_cpu_spec->cpu_features |= fp->cpu_features;
  508. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
  509. } else {
  510. cur_cpu_spec->cpu_features &= ~fp->cpu_features;
  511. cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
  512. }
  513. }
  514. }
  515. static void __init check_cpu_pa_features(unsigned long node)
  516. {
  517. unsigned char *pa_ftrs;
  518. unsigned long tablelen;
  519. pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
  520. if (pa_ftrs == NULL)
  521. return;
  522. scan_features(node, pa_ftrs, tablelen,
  523. ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
  524. }
  525. static struct feature_property {
  526. const char *name;
  527. u32 min_value;
  528. unsigned long cpu_feature;
  529. unsigned long cpu_user_ftr;
  530. } feature_properties[] __initdata = {
  531. #ifdef CONFIG_ALTIVEC
  532. {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  533. {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  534. #endif /* CONFIG_ALTIVEC */
  535. #ifdef CONFIG_PPC64
  536. {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
  537. {"ibm,purr", 1, CPU_FTR_PURR, 0},
  538. {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
  539. #endif /* CONFIG_PPC64 */
  540. };
  541. static void __init check_cpu_feature_properties(unsigned long node)
  542. {
  543. unsigned long i;
  544. struct feature_property *fp = feature_properties;
  545. const u32 *prop;
  546. for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
  547. prop = of_get_flat_dt_prop(node, fp->name, NULL);
  548. if (prop && *prop >= fp->min_value) {
  549. cur_cpu_spec->cpu_features |= fp->cpu_feature;
  550. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
  551. }
  552. }
  553. }
  554. static int __init early_init_dt_scan_cpus(unsigned long node,
  555. const char *uname, int depth,
  556. void *data)
  557. {
  558. static int logical_cpuid = 0;
  559. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  560. const u32 *prop;
  561. const u32 *intserv;
  562. int i, nthreads;
  563. unsigned long len;
  564. int found = 0;
  565. /* We are scanning "cpu" nodes only */
  566. if (type == NULL || strcmp(type, "cpu") != 0)
  567. return 0;
  568. /* Get physical cpuid */
  569. intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
  570. if (intserv) {
  571. nthreads = len / sizeof(int);
  572. } else {
  573. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  574. nthreads = 1;
  575. }
  576. /*
  577. * Now see if any of these threads match our boot cpu.
  578. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  579. */
  580. for (i = 0; i < nthreads; i++) {
  581. /*
  582. * version 2 of the kexec param format adds the phys cpuid of
  583. * booted proc.
  584. */
  585. if (initial_boot_params && initial_boot_params->version >= 2) {
  586. if (intserv[i] ==
  587. initial_boot_params->boot_cpuid_phys) {
  588. found = 1;
  589. break;
  590. }
  591. } else {
  592. /*
  593. * Check if it's the boot-cpu, set it's hw index now,
  594. * unfortunately this format did not support booting
  595. * off secondary threads.
  596. */
  597. if (of_get_flat_dt_prop(node,
  598. "linux,boot-cpu", NULL) != NULL) {
  599. found = 1;
  600. break;
  601. }
  602. }
  603. #ifdef CONFIG_SMP
  604. /* logical cpu id is always 0 on UP kernels */
  605. logical_cpuid++;
  606. #endif
  607. }
  608. if (found) {
  609. DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
  610. intserv[i]);
  611. boot_cpuid = logical_cpuid;
  612. set_hard_smp_processor_id(boot_cpuid, intserv[i]);
  613. /*
  614. * PAPR defines "logical" PVR values for cpus that
  615. * meet various levels of the architecture:
  616. * 0x0f000001 Architecture version 2.04
  617. * 0x0f000002 Architecture version 2.05
  618. * If the cpu-version property in the cpu node contains
  619. * such a value, we call identify_cpu again with the
  620. * logical PVR value in order to use the cpu feature
  621. * bits appropriate for the architecture level.
  622. *
  623. * A POWER6 partition in "POWER6 architected" mode
  624. * uses the 0x0f000002 PVR value; in POWER5+ mode
  625. * it uses 0x0f000001.
  626. */
  627. prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
  628. if (prop && (*prop & 0xff000000) == 0x0f000000)
  629. identify_cpu(0, *prop);
  630. }
  631. check_cpu_feature_properties(node);
  632. check_cpu_pa_features(node);
  633. #ifdef CONFIG_PPC_PSERIES
  634. if (nthreads > 1)
  635. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  636. else
  637. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  638. #endif
  639. return 0;
  640. }
  641. #ifdef CONFIG_BLK_DEV_INITRD
  642. static void __init early_init_dt_check_for_initrd(unsigned long node)
  643. {
  644. unsigned long l;
  645. u32 *prop;
  646. DBG("Looking for initrd properties... ");
  647. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
  648. if (prop) {
  649. initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
  650. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
  651. if (prop) {
  652. initrd_end = (unsigned long)
  653. __va(of_read_ulong(prop, l/4));
  654. initrd_below_start_ok = 1;
  655. } else {
  656. initrd_start = 0;
  657. }
  658. }
  659. DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start, initrd_end);
  660. }
  661. #else
  662. static inline void early_init_dt_check_for_initrd(unsigned long node)
  663. {
  664. }
  665. #endif /* CONFIG_BLK_DEV_INITRD */
  666. static int __init early_init_dt_scan_chosen(unsigned long node,
  667. const char *uname, int depth, void *data)
  668. {
  669. unsigned long *lprop;
  670. unsigned long l;
  671. char *p;
  672. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  673. if (depth != 1 ||
  674. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  675. return 0;
  676. #ifdef CONFIG_PPC64
  677. /* check if iommu is forced on or off */
  678. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  679. iommu_is_off = 1;
  680. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  681. iommu_force_on = 1;
  682. #endif
  683. /* mem=x on the command line is the preferred mechanism */
  684. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  685. if (lprop)
  686. memory_limit = *lprop;
  687. #ifdef CONFIG_PPC64
  688. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  689. if (lprop)
  690. tce_alloc_start = *lprop;
  691. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  692. if (lprop)
  693. tce_alloc_end = *lprop;
  694. #endif
  695. #ifdef CONFIG_KEXEC
  696. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  697. if (lprop)
  698. crashk_res.start = *lprop;
  699. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  700. if (lprop)
  701. crashk_res.end = crashk_res.start + *lprop - 1;
  702. #endif
  703. early_init_dt_check_for_initrd(node);
  704. /* Retreive command line */
  705. p = of_get_flat_dt_prop(node, "bootargs", &l);
  706. if (p != NULL && l > 0)
  707. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  708. #ifdef CONFIG_CMDLINE
  709. if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
  710. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  711. #endif /* CONFIG_CMDLINE */
  712. DBG("Command line is: %s\n", cmd_line);
  713. /* break now */
  714. return 1;
  715. }
  716. static int __init early_init_dt_scan_root(unsigned long node,
  717. const char *uname, int depth, void *data)
  718. {
  719. u32 *prop;
  720. if (depth != 0)
  721. return 0;
  722. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  723. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  724. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  725. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  726. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  727. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  728. /* break now */
  729. return 1;
  730. }
  731. static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
  732. {
  733. cell_t *p = *cellp;
  734. *cellp = p + s;
  735. return of_read_ulong(p, s);
  736. }
  737. #ifdef CONFIG_PPC_PSERIES
  738. /*
  739. * Interpret the ibm,dynamic-memory property in the
  740. * /ibm,dynamic-reconfiguration-memory node.
  741. * This contains a list of memory blocks along with NUMA affinity
  742. * information.
  743. */
  744. static int __init early_init_dt_scan_drconf_memory(unsigned long node)
  745. {
  746. cell_t *dm, *ls;
  747. unsigned long l, n;
  748. unsigned long base, size, lmb_size, flags;
  749. ls = (cell_t *)of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
  750. if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
  751. return 0;
  752. lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
  753. dm = (cell_t *)of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
  754. if (dm == NULL || l < sizeof(cell_t))
  755. return 0;
  756. n = *dm++; /* number of entries */
  757. if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
  758. return 0;
  759. for (; n != 0; --n) {
  760. base = dt_mem_next_cell(dt_root_addr_cells, &dm);
  761. flags = dm[3];
  762. /* skip DRC index, pad, assoc. list index, flags */
  763. dm += 4;
  764. /* skip this block if the reserved bit is set in flags (0x80)
  765. or if the block is not assigned to this partition (0x8) */
  766. if ((flags & 0x80) || !(flags & 0x8))
  767. continue;
  768. size = lmb_size;
  769. if (iommu_is_off) {
  770. if (base >= 0x80000000ul)
  771. continue;
  772. if ((base + size) > 0x80000000ul)
  773. size = 0x80000000ul - base;
  774. }
  775. lmb_add(base, size);
  776. }
  777. lmb_dump_all();
  778. return 0;
  779. }
  780. #else
  781. #define early_init_dt_scan_drconf_memory(node) 0
  782. #endif /* CONFIG_PPC_PSERIES */
  783. static int __init early_init_dt_scan_memory(unsigned long node,
  784. const char *uname, int depth, void *data)
  785. {
  786. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  787. cell_t *reg, *endp;
  788. unsigned long l;
  789. /* Look for the ibm,dynamic-reconfiguration-memory node */
  790. if (depth == 1 &&
  791. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  792. return early_init_dt_scan_drconf_memory(node);
  793. /* We are scanning "memory" nodes only */
  794. if (type == NULL) {
  795. /*
  796. * The longtrail doesn't have a device_type on the
  797. * /memory node, so look for the node called /memory@0.
  798. */
  799. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  800. return 0;
  801. } else if (strcmp(type, "memory") != 0)
  802. return 0;
  803. reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  804. if (reg == NULL)
  805. reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
  806. if (reg == NULL)
  807. return 0;
  808. endp = reg + (l / sizeof(cell_t));
  809. DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  810. uname, l, reg[0], reg[1], reg[2], reg[3]);
  811. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  812. unsigned long base, size;
  813. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  814. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  815. if (size == 0)
  816. continue;
  817. DBG(" - %lx , %lx\n", base, size);
  818. #ifdef CONFIG_PPC64
  819. if (iommu_is_off) {
  820. if (base >= 0x80000000ul)
  821. continue;
  822. if ((base + size) > 0x80000000ul)
  823. size = 0x80000000ul - base;
  824. }
  825. #endif
  826. lmb_add(base, size);
  827. }
  828. return 0;
  829. }
  830. static void __init early_reserve_mem(void)
  831. {
  832. u64 base, size;
  833. u64 *reserve_map;
  834. unsigned long self_base;
  835. unsigned long self_size;
  836. reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  837. initial_boot_params->off_mem_rsvmap);
  838. /* before we do anything, lets reserve the dt blob */
  839. self_base = __pa((unsigned long)initial_boot_params);
  840. self_size = initial_boot_params->totalsize;
  841. lmb_reserve(self_base, self_size);
  842. #ifdef CONFIG_BLK_DEV_INITRD
  843. /* then reserve the initrd, if any */
  844. if (initrd_start && (initrd_end > initrd_start))
  845. lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
  846. #endif /* CONFIG_BLK_DEV_INITRD */
  847. #ifdef CONFIG_PPC32
  848. /*
  849. * Handle the case where we might be booting from an old kexec
  850. * image that setup the mem_rsvmap as pairs of 32-bit values
  851. */
  852. if (*reserve_map > 0xffffffffull) {
  853. u32 base_32, size_32;
  854. u32 *reserve_map_32 = (u32 *)reserve_map;
  855. while (1) {
  856. base_32 = *(reserve_map_32++);
  857. size_32 = *(reserve_map_32++);
  858. if (size_32 == 0)
  859. break;
  860. /* skip if the reservation is for the blob */
  861. if (base_32 == self_base && size_32 == self_size)
  862. continue;
  863. DBG("reserving: %x -> %x\n", base_32, size_32);
  864. lmb_reserve(base_32, size_32);
  865. }
  866. return;
  867. }
  868. #endif
  869. while (1) {
  870. base = *(reserve_map++);
  871. size = *(reserve_map++);
  872. if (size == 0)
  873. break;
  874. DBG("reserving: %llx -> %llx\n", base, size);
  875. lmb_reserve(base, size);
  876. }
  877. #if 0
  878. DBG("memory reserved, lmbs :\n");
  879. lmb_dump_all();
  880. #endif
  881. }
  882. void __init early_init_devtree(void *params)
  883. {
  884. DBG(" -> early_init_devtree()\n");
  885. /* Setup flat device-tree pointer */
  886. initial_boot_params = params;
  887. #ifdef CONFIG_PPC_RTAS
  888. /* Some machines might need RTAS info for debugging, grab it now. */
  889. of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
  890. #endif
  891. /* Retrieve various informations from the /chosen node of the
  892. * device-tree, including the platform type, initrd location and
  893. * size, TCE reserve, and more ...
  894. */
  895. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  896. /* Scan memory nodes and rebuild LMBs */
  897. lmb_init();
  898. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  899. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  900. /* Save command line for /proc/cmdline and then parse parameters */
  901. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  902. parse_early_param();
  903. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  904. lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  905. reserve_kdump_trampoline();
  906. reserve_crashkernel();
  907. early_reserve_mem();
  908. lmb_enforce_memory_limit(memory_limit);
  909. lmb_analyze();
  910. DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
  911. /* We may need to relocate the flat tree, do it now.
  912. * FIXME .. and the initrd too? */
  913. move_device_tree();
  914. DBG("Scanning CPUs ...\n");
  915. /* Retreive CPU related informations from the flat tree
  916. * (altivec support, boot CPU ID, ...)
  917. */
  918. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  919. DBG(" <- early_init_devtree()\n");
  920. }
  921. #undef printk
  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, the node
  1026. * you pass will not be searched, only the next one
  1027. * will; typically, you pass what the previous call
  1028. * returned. of_node_put() will be called on it
  1029. * @name: The type string to match against
  1030. *
  1031. * Returns a node pointer with refcount incremented, use
  1032. * of_node_put() on it when done.
  1033. */
  1034. struct device_node *of_find_node_by_type(struct device_node *from,
  1035. const char *type)
  1036. {
  1037. struct device_node *np;
  1038. read_lock(&devtree_lock);
  1039. np = from ? from->allnext : allnodes;
  1040. for (; np != 0; np = np->allnext)
  1041. if (np->type != 0 && strcasecmp(np->type, type) == 0
  1042. && of_node_get(np))
  1043. break;
  1044. of_node_put(from);
  1045. read_unlock(&devtree_lock);
  1046. return np;
  1047. }
  1048. EXPORT_SYMBOL(of_find_node_by_type);
  1049. /**
  1050. * of_find_compatible_node - Find a node based on type and one of the
  1051. * tokens in its "compatible" property
  1052. * @from: The node to start searching from or NULL, the node
  1053. * you pass will not be searched, only the next one
  1054. * will; typically, you pass what the previous call
  1055. * returned. of_node_put() will be called on it
  1056. * @type: The type string to match "device_type" or NULL to ignore
  1057. * @compatible: The string to match to one of the tokens in the device
  1058. * "compatible" list.
  1059. *
  1060. * Returns a node pointer with refcount incremented, use
  1061. * of_node_put() on it when done.
  1062. */
  1063. struct device_node *of_find_compatible_node(struct device_node *from,
  1064. const char *type, const char *compatible)
  1065. {
  1066. struct device_node *np;
  1067. read_lock(&devtree_lock);
  1068. np = from ? from->allnext : allnodes;
  1069. for (; np != 0; np = np->allnext) {
  1070. if (type != NULL
  1071. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1072. continue;
  1073. if (of_device_is_compatible(np, compatible) && of_node_get(np))
  1074. break;
  1075. }
  1076. of_node_put(from);
  1077. read_unlock(&devtree_lock);
  1078. return np;
  1079. }
  1080. EXPORT_SYMBOL(of_find_compatible_node);
  1081. /**
  1082. * of_find_node_by_path - Find a node matching a full OF path
  1083. * @path: The full path to match
  1084. *
  1085. * Returns a node pointer with refcount incremented, use
  1086. * of_node_put() on it when done.
  1087. */
  1088. struct device_node *of_find_node_by_path(const char *path)
  1089. {
  1090. struct device_node *np = allnodes;
  1091. read_lock(&devtree_lock);
  1092. for (; np != 0; np = np->allnext) {
  1093. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
  1094. && of_node_get(np))
  1095. break;
  1096. }
  1097. read_unlock(&devtree_lock);
  1098. return np;
  1099. }
  1100. EXPORT_SYMBOL(of_find_node_by_path);
  1101. /**
  1102. * of_find_node_by_phandle - Find a node given a phandle
  1103. * @handle: phandle of the node to find
  1104. *
  1105. * Returns a node pointer with refcount incremented, use
  1106. * of_node_put() on it when done.
  1107. */
  1108. struct device_node *of_find_node_by_phandle(phandle handle)
  1109. {
  1110. struct device_node *np;
  1111. read_lock(&devtree_lock);
  1112. for (np = allnodes; np != 0; np = np->allnext)
  1113. if (np->linux_phandle == handle)
  1114. break;
  1115. of_node_get(np);
  1116. read_unlock(&devtree_lock);
  1117. return np;
  1118. }
  1119. EXPORT_SYMBOL(of_find_node_by_phandle);
  1120. /**
  1121. * of_find_all_nodes - Get next node in global list
  1122. * @prev: Previous node or NULL to start iteration
  1123. * of_node_put() will be called on it
  1124. *
  1125. * Returns a node pointer with refcount incremented, use
  1126. * of_node_put() on it when done.
  1127. */
  1128. struct device_node *of_find_all_nodes(struct device_node *prev)
  1129. {
  1130. struct device_node *np;
  1131. read_lock(&devtree_lock);
  1132. np = prev ? prev->allnext : allnodes;
  1133. for (; np != 0; np = np->allnext)
  1134. if (of_node_get(np))
  1135. break;
  1136. of_node_put(prev);
  1137. read_unlock(&devtree_lock);
  1138. return np;
  1139. }
  1140. EXPORT_SYMBOL(of_find_all_nodes);
  1141. /**
  1142. * of_get_parent - Get a node's parent if any
  1143. * @node: Node to get parent
  1144. *
  1145. * Returns a node pointer with refcount incremented, use
  1146. * of_node_put() on it when done.
  1147. */
  1148. struct device_node *of_get_parent(const struct device_node *node)
  1149. {
  1150. struct device_node *np;
  1151. if (!node)
  1152. return NULL;
  1153. read_lock(&devtree_lock);
  1154. np = of_node_get(node->parent);
  1155. read_unlock(&devtree_lock);
  1156. return np;
  1157. }
  1158. EXPORT_SYMBOL(of_get_parent);
  1159. /**
  1160. * of_get_next_child - Iterate a node childs
  1161. * @node: parent node
  1162. * @prev: previous child of the parent node, or NULL to get first
  1163. *
  1164. * Returns a node pointer with refcount incremented, use
  1165. * of_node_put() on it when done.
  1166. */
  1167. struct device_node *of_get_next_child(const struct device_node *node,
  1168. struct device_node *prev)
  1169. {
  1170. struct device_node *next;
  1171. read_lock(&devtree_lock);
  1172. next = prev ? prev->sibling : node->child;
  1173. for (; next != 0; next = next->sibling)
  1174. if (of_node_get(next))
  1175. break;
  1176. of_node_put(prev);
  1177. read_unlock(&devtree_lock);
  1178. return next;
  1179. }
  1180. EXPORT_SYMBOL(of_get_next_child);
  1181. /**
  1182. * of_node_get - Increment refcount of a node
  1183. * @node: Node to inc refcount, NULL is supported to
  1184. * simplify writing of callers
  1185. *
  1186. * Returns node.
  1187. */
  1188. struct device_node *of_node_get(struct device_node *node)
  1189. {
  1190. if (node)
  1191. kref_get(&node->kref);
  1192. return node;
  1193. }
  1194. EXPORT_SYMBOL(of_node_get);
  1195. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1196. {
  1197. return container_of(kref, struct device_node, kref);
  1198. }
  1199. /**
  1200. * of_node_release - release a dynamically allocated node
  1201. * @kref: kref element of the node to be released
  1202. *
  1203. * In of_node_put() this function is passed to kref_put()
  1204. * as the destructor.
  1205. */
  1206. static void of_node_release(struct kref *kref)
  1207. {
  1208. struct device_node *node = kref_to_device_node(kref);
  1209. struct property *prop = node->properties;
  1210. if (!OF_IS_DYNAMIC(node))
  1211. return;
  1212. while (prop) {
  1213. struct property *next = prop->next;
  1214. kfree(prop->name);
  1215. kfree(prop->value);
  1216. kfree(prop);
  1217. prop = next;
  1218. if (!prop) {
  1219. prop = node->deadprops;
  1220. node->deadprops = NULL;
  1221. }
  1222. }
  1223. kfree(node->full_name);
  1224. kfree(node->data);
  1225. kfree(node);
  1226. }
  1227. /**
  1228. * of_node_put - Decrement refcount of a node
  1229. * @node: Node to dec refcount, NULL is supported to
  1230. * simplify writing of callers
  1231. *
  1232. */
  1233. void of_node_put(struct device_node *node)
  1234. {
  1235. if (node)
  1236. kref_put(&node->kref, of_node_release);
  1237. }
  1238. EXPORT_SYMBOL(of_node_put);
  1239. /*
  1240. * Plug a device node into the tree and global list.
  1241. */
  1242. void of_attach_node(struct device_node *np)
  1243. {
  1244. write_lock(&devtree_lock);
  1245. np->sibling = np->parent->child;
  1246. np->allnext = allnodes;
  1247. np->parent->child = np;
  1248. allnodes = np;
  1249. write_unlock(&devtree_lock);
  1250. }
  1251. /*
  1252. * "Unplug" a node from the device tree. The caller must hold
  1253. * a reference to the node. The memory associated with the node
  1254. * is not freed until its refcount goes to zero.
  1255. */
  1256. void of_detach_node(const struct device_node *np)
  1257. {
  1258. struct device_node *parent;
  1259. write_lock(&devtree_lock);
  1260. parent = np->parent;
  1261. if (allnodes == np)
  1262. allnodes = np->allnext;
  1263. else {
  1264. struct device_node *prev;
  1265. for (prev = allnodes;
  1266. prev->allnext != np;
  1267. prev = prev->allnext)
  1268. ;
  1269. prev->allnext = np->allnext;
  1270. }
  1271. if (parent->child == np)
  1272. parent->child = np->sibling;
  1273. else {
  1274. struct device_node *prevsib;
  1275. for (prevsib = np->parent->child;
  1276. prevsib->sibling != np;
  1277. prevsib = prevsib->sibling)
  1278. ;
  1279. prevsib->sibling = np->sibling;
  1280. }
  1281. write_unlock(&devtree_lock);
  1282. }
  1283. #ifdef CONFIG_PPC_PSERIES
  1284. /*
  1285. * Fix up the uninitialized fields in a new device node:
  1286. * name, type and pci-specific fields
  1287. */
  1288. static int of_finish_dynamic_node(struct device_node *node)
  1289. {
  1290. struct device_node *parent = of_get_parent(node);
  1291. int err = 0;
  1292. const phandle *ibm_phandle;
  1293. node->name = of_get_property(node, "name", NULL);
  1294. node->type = of_get_property(node, "device_type", NULL);
  1295. if (!node->name)
  1296. node->name = "<NULL>";
  1297. if (!node->type)
  1298. node->type = "<NULL>";
  1299. if (!parent) {
  1300. err = -ENODEV;
  1301. goto out;
  1302. }
  1303. /* We don't support that function on PowerMac, at least
  1304. * not yet
  1305. */
  1306. if (machine_is(powermac))
  1307. return -ENODEV;
  1308. /* fix up new node's linux_phandle field */
  1309. if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
  1310. node->linux_phandle = *ibm_phandle;
  1311. out:
  1312. of_node_put(parent);
  1313. return err;
  1314. }
  1315. static int prom_reconfig_notifier(struct notifier_block *nb,
  1316. unsigned long action, void *node)
  1317. {
  1318. int err;
  1319. switch (action) {
  1320. case PSERIES_RECONFIG_ADD:
  1321. err = of_finish_dynamic_node(node);
  1322. if (err < 0) {
  1323. printk(KERN_ERR "finish_node returned %d\n", err);
  1324. err = NOTIFY_BAD;
  1325. }
  1326. break;
  1327. default:
  1328. err = NOTIFY_DONE;
  1329. break;
  1330. }
  1331. return err;
  1332. }
  1333. static struct notifier_block prom_reconfig_nb = {
  1334. .notifier_call = prom_reconfig_notifier,
  1335. .priority = 10, /* This one needs to run first */
  1336. };
  1337. static int __init prom_reconfig_setup(void)
  1338. {
  1339. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1340. }
  1341. __initcall(prom_reconfig_setup);
  1342. #endif
  1343. struct property *of_find_property(const struct device_node *np,
  1344. const char *name,
  1345. int *lenp)
  1346. {
  1347. struct property *pp;
  1348. read_lock(&devtree_lock);
  1349. for (pp = np->properties; pp != 0; pp = pp->next)
  1350. if (strcmp(pp->name, name) == 0) {
  1351. if (lenp != 0)
  1352. *lenp = pp->length;
  1353. break;
  1354. }
  1355. read_unlock(&devtree_lock);
  1356. return pp;
  1357. }
  1358. EXPORT_SYMBOL(of_find_property);
  1359. /*
  1360. * Find a property with a given name for a given node
  1361. * and return the value.
  1362. */
  1363. const void *of_get_property(const struct device_node *np, const char *name,
  1364. int *lenp)
  1365. {
  1366. struct property *pp = of_find_property(np,name,lenp);
  1367. return pp ? pp->value : NULL;
  1368. }
  1369. EXPORT_SYMBOL(of_get_property);
  1370. /*
  1371. * Add a property to a node
  1372. */
  1373. int prom_add_property(struct device_node* np, struct property* prop)
  1374. {
  1375. struct property **next;
  1376. prop->next = NULL;
  1377. write_lock(&devtree_lock);
  1378. next = &np->properties;
  1379. while (*next) {
  1380. if (strcmp(prop->name, (*next)->name) == 0) {
  1381. /* duplicate ! don't insert it */
  1382. write_unlock(&devtree_lock);
  1383. return -1;
  1384. }
  1385. next = &(*next)->next;
  1386. }
  1387. *next = prop;
  1388. write_unlock(&devtree_lock);
  1389. #ifdef CONFIG_PROC_DEVICETREE
  1390. /* try to add to proc as well if it was initialized */
  1391. if (np->pde)
  1392. proc_device_tree_add_prop(np->pde, prop);
  1393. #endif /* CONFIG_PROC_DEVICETREE */
  1394. return 0;
  1395. }
  1396. /*
  1397. * Remove a property from a node. Note that we don't actually
  1398. * remove it, since we have given out who-knows-how-many pointers
  1399. * to the data using get-property. Instead we just move the property
  1400. * to the "dead properties" list, so it won't be found any more.
  1401. */
  1402. int prom_remove_property(struct device_node *np, struct property *prop)
  1403. {
  1404. struct property **next;
  1405. int found = 0;
  1406. write_lock(&devtree_lock);
  1407. next = &np->properties;
  1408. while (*next) {
  1409. if (*next == prop) {
  1410. /* found the node */
  1411. *next = prop->next;
  1412. prop->next = np->deadprops;
  1413. np->deadprops = prop;
  1414. found = 1;
  1415. break;
  1416. }
  1417. next = &(*next)->next;
  1418. }
  1419. write_unlock(&devtree_lock);
  1420. if (!found)
  1421. return -ENODEV;
  1422. #ifdef CONFIG_PROC_DEVICETREE
  1423. /* try to remove the proc node as well */
  1424. if (np->pde)
  1425. proc_device_tree_remove_prop(np->pde, prop);
  1426. #endif /* CONFIG_PROC_DEVICETREE */
  1427. return 0;
  1428. }
  1429. /*
  1430. * Update a property in a node. Note that we don't actually
  1431. * remove it, since we have given out who-knows-how-many pointers
  1432. * to the data using get-property. Instead we just move the property
  1433. * to the "dead properties" list, and add the new property to the
  1434. * property list
  1435. */
  1436. int prom_update_property(struct device_node *np,
  1437. struct property *newprop,
  1438. struct property *oldprop)
  1439. {
  1440. struct property **next;
  1441. int found = 0;
  1442. write_lock(&devtree_lock);
  1443. next = &np->properties;
  1444. while (*next) {
  1445. if (*next == oldprop) {
  1446. /* found the node */
  1447. newprop->next = oldprop->next;
  1448. *next = newprop;
  1449. oldprop->next = np->deadprops;
  1450. np->deadprops = oldprop;
  1451. found = 1;
  1452. break;
  1453. }
  1454. next = &(*next)->next;
  1455. }
  1456. write_unlock(&devtree_lock);
  1457. if (!found)
  1458. return -ENODEV;
  1459. #ifdef CONFIG_PROC_DEVICETREE
  1460. /* try to add to proc as well if it was initialized */
  1461. if (np->pde)
  1462. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1463. #endif /* CONFIG_PROC_DEVICETREE */
  1464. return 0;
  1465. }
  1466. /* Find the device node for a given logical cpu number, also returns the cpu
  1467. * local thread number (index in ibm,interrupt-server#s) if relevant and
  1468. * asked for (non NULL)
  1469. */
  1470. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  1471. {
  1472. int hardid;
  1473. struct device_node *np;
  1474. hardid = get_hard_smp_processor_id(cpu);
  1475. for_each_node_by_type(np, "cpu") {
  1476. const u32 *intserv;
  1477. unsigned int plen, t;
  1478. /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
  1479. * fallback to "reg" property and assume no threads
  1480. */
  1481. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
  1482. &plen);
  1483. if (intserv == NULL) {
  1484. const u32 *reg = of_get_property(np, "reg", NULL);
  1485. if (reg == NULL)
  1486. continue;
  1487. if (*reg == hardid) {
  1488. if (thread)
  1489. *thread = 0;
  1490. return np;
  1491. }
  1492. } else {
  1493. plen /= sizeof(u32);
  1494. for (t = 0; t < plen; t++) {
  1495. if (hardid == intserv[t]) {
  1496. if (thread)
  1497. *thread = t;
  1498. return np;
  1499. }
  1500. }
  1501. }
  1502. }
  1503. return NULL;
  1504. }
  1505. EXPORT_SYMBOL(of_get_cpu_node);
  1506. #ifdef DEBUG
  1507. static struct debugfs_blob_wrapper flat_dt_blob;
  1508. static int __init export_flat_device_tree(void)
  1509. {
  1510. struct dentry *d;
  1511. d = debugfs_create_dir("powerpc", NULL);
  1512. if (!d)
  1513. return 1;
  1514. flat_dt_blob.data = initial_boot_params;
  1515. flat_dt_blob.size = initial_boot_params->totalsize;
  1516. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  1517. d, &flat_dt_blob);
  1518. if (!d)
  1519. return 1;
  1520. return 0;
  1521. }
  1522. __initcall(export_flat_device_tree);
  1523. #endif