prom.c 42 KB

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