prom.c 37 KB

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