prom.c 40 KB

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