prom.c 40 KB

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