prom.c 40 KB

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