prom.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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 u64 __init dt_mem_next_cell(int s, cell_t **cellp)
  763. {
  764. cell_t *p = *cellp;
  765. *cellp = p + s;
  766. return of_read_number(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, flags;
  779. u64 base, size, lmb_size;
  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. u64 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(" - %llx , %llx\n", (unsigned long long)base,
  849. (unsigned long long)size);
  850. #ifdef CONFIG_PPC64
  851. if (iommu_is_off) {
  852. if (base >= 0x80000000ul)
  853. continue;
  854. if ((base + size) > 0x80000000ul)
  855. size = 0x80000000ul - base;
  856. }
  857. #endif
  858. lmb_add(base, size);
  859. }
  860. return 0;
  861. }
  862. static void __init early_reserve_mem(void)
  863. {
  864. u64 base, size;
  865. u64 *reserve_map;
  866. unsigned long self_base;
  867. unsigned long self_size;
  868. reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  869. initial_boot_params->off_mem_rsvmap);
  870. /* before we do anything, lets reserve the dt blob */
  871. self_base = __pa((unsigned long)initial_boot_params);
  872. self_size = initial_boot_params->totalsize;
  873. lmb_reserve(self_base, self_size);
  874. #ifdef CONFIG_BLK_DEV_INITRD
  875. /* then reserve the initrd, if any */
  876. if (initrd_start && (initrd_end > initrd_start))
  877. lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
  878. #endif /* CONFIG_BLK_DEV_INITRD */
  879. #ifdef CONFIG_PPC32
  880. /*
  881. * Handle the case where we might be booting from an old kexec
  882. * image that setup the mem_rsvmap as pairs of 32-bit values
  883. */
  884. if (*reserve_map > 0xffffffffull) {
  885. u32 base_32, size_32;
  886. u32 *reserve_map_32 = (u32 *)reserve_map;
  887. while (1) {
  888. base_32 = *(reserve_map_32++);
  889. size_32 = *(reserve_map_32++);
  890. if (size_32 == 0)
  891. break;
  892. /* skip if the reservation is for the blob */
  893. if (base_32 == self_base && size_32 == self_size)
  894. continue;
  895. DBG("reserving: %x -> %x\n", base_32, size_32);
  896. lmb_reserve(base_32, size_32);
  897. }
  898. return;
  899. }
  900. #endif
  901. while (1) {
  902. base = *(reserve_map++);
  903. size = *(reserve_map++);
  904. if (size == 0)
  905. break;
  906. DBG("reserving: %llx -> %llx\n", base, size);
  907. lmb_reserve(base, size);
  908. }
  909. #if 0
  910. DBG("memory reserved, lmbs :\n");
  911. lmb_dump_all();
  912. #endif
  913. }
  914. void __init early_init_devtree(void *params)
  915. {
  916. DBG(" -> early_init_devtree(%p)\n", params);
  917. /* Setup flat device-tree pointer */
  918. initial_boot_params = params;
  919. #ifdef CONFIG_PPC_RTAS
  920. /* Some machines might need RTAS info for debugging, grab it now. */
  921. of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
  922. #endif
  923. /* Retrieve various informations from the /chosen node of the
  924. * device-tree, including the platform type, initrd location and
  925. * size, TCE reserve, and more ...
  926. */
  927. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  928. /* Scan memory nodes and rebuild LMBs */
  929. lmb_init();
  930. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  931. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  932. /* Save command line for /proc/cmdline and then parse parameters */
  933. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  934. parse_early_param();
  935. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  936. lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  937. reserve_kdump_trampoline();
  938. reserve_crashkernel();
  939. early_reserve_mem();
  940. lmb_enforce_memory_limit(memory_limit);
  941. lmb_analyze();
  942. DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
  943. /* We may need to relocate the flat tree, do it now.
  944. * FIXME .. and the initrd too? */
  945. move_device_tree();
  946. DBG("Scanning CPUs ...\n");
  947. /* Retreive CPU related informations from the flat tree
  948. * (altivec support, boot CPU ID, ...)
  949. */
  950. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  951. DBG(" <- early_init_devtree()\n");
  952. }
  953. /**
  954. * Indicates whether the root node has a given value in its
  955. * compatible property.
  956. */
  957. int machine_is_compatible(const char *compat)
  958. {
  959. struct device_node *root;
  960. int rc = 0;
  961. root = of_find_node_by_path("/");
  962. if (root) {
  963. rc = of_device_is_compatible(root, compat);
  964. of_node_put(root);
  965. }
  966. return rc;
  967. }
  968. EXPORT_SYMBOL(machine_is_compatible);
  969. /*******
  970. *
  971. * New implementation of the OF "find" APIs, return a refcounted
  972. * object, call of_node_put() when done. The device tree and list
  973. * are protected by a rw_lock.
  974. *
  975. * Note that property management will need some locking as well,
  976. * this isn't dealt with yet.
  977. *
  978. *******/
  979. /**
  980. * of_find_node_by_phandle - Find a node given a phandle
  981. * @handle: phandle of the node to find
  982. *
  983. * Returns a node pointer with refcount incremented, use
  984. * of_node_put() on it when done.
  985. */
  986. struct device_node *of_find_node_by_phandle(phandle handle)
  987. {
  988. struct device_node *np;
  989. read_lock(&devtree_lock);
  990. for (np = allnodes; np != 0; np = np->allnext)
  991. if (np->linux_phandle == handle)
  992. break;
  993. of_node_get(np);
  994. read_unlock(&devtree_lock);
  995. return np;
  996. }
  997. EXPORT_SYMBOL(of_find_node_by_phandle);
  998. /**
  999. * of_find_all_nodes - Get next node in global list
  1000. * @prev: Previous node or NULL to start iteration
  1001. * of_node_put() will be called on it
  1002. *
  1003. * Returns a node pointer with refcount incremented, use
  1004. * of_node_put() on it when done.
  1005. */
  1006. struct device_node *of_find_all_nodes(struct device_node *prev)
  1007. {
  1008. struct device_node *np;
  1009. read_lock(&devtree_lock);
  1010. np = prev ? prev->allnext : allnodes;
  1011. for (; np != 0; np = np->allnext)
  1012. if (of_node_get(np))
  1013. break;
  1014. of_node_put(prev);
  1015. read_unlock(&devtree_lock);
  1016. return np;
  1017. }
  1018. EXPORT_SYMBOL(of_find_all_nodes);
  1019. /**
  1020. * of_node_get - Increment refcount of a node
  1021. * @node: Node to inc refcount, NULL is supported to
  1022. * simplify writing of callers
  1023. *
  1024. * Returns node.
  1025. */
  1026. struct device_node *of_node_get(struct device_node *node)
  1027. {
  1028. if (node)
  1029. kref_get(&node->kref);
  1030. return node;
  1031. }
  1032. EXPORT_SYMBOL(of_node_get);
  1033. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1034. {
  1035. return container_of(kref, struct device_node, kref);
  1036. }
  1037. /**
  1038. * of_node_release - release a dynamically allocated node
  1039. * @kref: kref element of the node to be released
  1040. *
  1041. * In of_node_put() this function is passed to kref_put()
  1042. * as the destructor.
  1043. */
  1044. static void of_node_release(struct kref *kref)
  1045. {
  1046. struct device_node *node = kref_to_device_node(kref);
  1047. struct property *prop = node->properties;
  1048. /* We should never be releasing nodes that haven't been detached. */
  1049. if (!of_node_check_flag(node, OF_DETACHED)) {
  1050. printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
  1051. dump_stack();
  1052. kref_init(&node->kref);
  1053. return;
  1054. }
  1055. if (!of_node_check_flag(node, OF_DYNAMIC))
  1056. return;
  1057. while (prop) {
  1058. struct property *next = prop->next;
  1059. kfree(prop->name);
  1060. kfree(prop->value);
  1061. kfree(prop);
  1062. prop = next;
  1063. if (!prop) {
  1064. prop = node->deadprops;
  1065. node->deadprops = NULL;
  1066. }
  1067. }
  1068. kfree(node->full_name);
  1069. kfree(node->data);
  1070. kfree(node);
  1071. }
  1072. /**
  1073. * of_node_put - Decrement refcount of a node
  1074. * @node: Node to dec refcount, NULL is supported to
  1075. * simplify writing of callers
  1076. *
  1077. */
  1078. void of_node_put(struct device_node *node)
  1079. {
  1080. if (node)
  1081. kref_put(&node->kref, of_node_release);
  1082. }
  1083. EXPORT_SYMBOL(of_node_put);
  1084. /*
  1085. * Plug a device node into the tree and global list.
  1086. */
  1087. void of_attach_node(struct device_node *np)
  1088. {
  1089. write_lock(&devtree_lock);
  1090. np->sibling = np->parent->child;
  1091. np->allnext = allnodes;
  1092. np->parent->child = np;
  1093. allnodes = np;
  1094. write_unlock(&devtree_lock);
  1095. }
  1096. /*
  1097. * "Unplug" a node from the device tree. The caller must hold
  1098. * a reference to the node. The memory associated with the node
  1099. * is not freed until its refcount goes to zero.
  1100. */
  1101. void of_detach_node(struct device_node *np)
  1102. {
  1103. struct device_node *parent;
  1104. write_lock(&devtree_lock);
  1105. parent = np->parent;
  1106. if (!parent)
  1107. goto out_unlock;
  1108. if (allnodes == np)
  1109. allnodes = np->allnext;
  1110. else {
  1111. struct device_node *prev;
  1112. for (prev = allnodes;
  1113. prev->allnext != np;
  1114. prev = prev->allnext)
  1115. ;
  1116. prev->allnext = np->allnext;
  1117. }
  1118. if (parent->child == np)
  1119. parent->child = np->sibling;
  1120. else {
  1121. struct device_node *prevsib;
  1122. for (prevsib = np->parent->child;
  1123. prevsib->sibling != np;
  1124. prevsib = prevsib->sibling)
  1125. ;
  1126. prevsib->sibling = np->sibling;
  1127. }
  1128. of_node_set_flag(np, OF_DETACHED);
  1129. out_unlock:
  1130. write_unlock(&devtree_lock);
  1131. }
  1132. #ifdef CONFIG_PPC_PSERIES
  1133. /*
  1134. * Fix up the uninitialized fields in a new device node:
  1135. * name, type and pci-specific fields
  1136. */
  1137. static int of_finish_dynamic_node(struct device_node *node)
  1138. {
  1139. struct device_node *parent = of_get_parent(node);
  1140. int err = 0;
  1141. const phandle *ibm_phandle;
  1142. node->name = of_get_property(node, "name", NULL);
  1143. node->type = of_get_property(node, "device_type", NULL);
  1144. if (!node->name)
  1145. node->name = "<NULL>";
  1146. if (!node->type)
  1147. node->type = "<NULL>";
  1148. if (!parent) {
  1149. err = -ENODEV;
  1150. goto out;
  1151. }
  1152. /* We don't support that function on PowerMac, at least
  1153. * not yet
  1154. */
  1155. if (machine_is(powermac))
  1156. return -ENODEV;
  1157. /* fix up new node's linux_phandle field */
  1158. if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
  1159. node->linux_phandle = *ibm_phandle;
  1160. out:
  1161. of_node_put(parent);
  1162. return err;
  1163. }
  1164. static int prom_reconfig_notifier(struct notifier_block *nb,
  1165. unsigned long action, void *node)
  1166. {
  1167. int err;
  1168. switch (action) {
  1169. case PSERIES_RECONFIG_ADD:
  1170. err = of_finish_dynamic_node(node);
  1171. if (err < 0) {
  1172. printk(KERN_ERR "finish_node returned %d\n", err);
  1173. err = NOTIFY_BAD;
  1174. }
  1175. break;
  1176. default:
  1177. err = NOTIFY_DONE;
  1178. break;
  1179. }
  1180. return err;
  1181. }
  1182. static struct notifier_block prom_reconfig_nb = {
  1183. .notifier_call = prom_reconfig_notifier,
  1184. .priority = 10, /* This one needs to run first */
  1185. };
  1186. static int __init prom_reconfig_setup(void)
  1187. {
  1188. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1189. }
  1190. __initcall(prom_reconfig_setup);
  1191. #endif
  1192. /*
  1193. * Add a property to a node
  1194. */
  1195. int prom_add_property(struct device_node* np, struct property* prop)
  1196. {
  1197. struct property **next;
  1198. prop->next = NULL;
  1199. write_lock(&devtree_lock);
  1200. next = &np->properties;
  1201. while (*next) {
  1202. if (strcmp(prop->name, (*next)->name) == 0) {
  1203. /* duplicate ! don't insert it */
  1204. write_unlock(&devtree_lock);
  1205. return -1;
  1206. }
  1207. next = &(*next)->next;
  1208. }
  1209. *next = prop;
  1210. write_unlock(&devtree_lock);
  1211. #ifdef CONFIG_PROC_DEVICETREE
  1212. /* try to add to proc as well if it was initialized */
  1213. if (np->pde)
  1214. proc_device_tree_add_prop(np->pde, prop);
  1215. #endif /* CONFIG_PROC_DEVICETREE */
  1216. return 0;
  1217. }
  1218. /*
  1219. * Remove a property from a node. Note that we don't actually
  1220. * remove it, since we have given out who-knows-how-many pointers
  1221. * to the data using get-property. Instead we just move the property
  1222. * to the "dead properties" list, so it won't be found any more.
  1223. */
  1224. int prom_remove_property(struct device_node *np, struct property *prop)
  1225. {
  1226. struct property **next;
  1227. int found = 0;
  1228. write_lock(&devtree_lock);
  1229. next = &np->properties;
  1230. while (*next) {
  1231. if (*next == prop) {
  1232. /* found the node */
  1233. *next = prop->next;
  1234. prop->next = np->deadprops;
  1235. np->deadprops = prop;
  1236. found = 1;
  1237. break;
  1238. }
  1239. next = &(*next)->next;
  1240. }
  1241. write_unlock(&devtree_lock);
  1242. if (!found)
  1243. return -ENODEV;
  1244. #ifdef CONFIG_PROC_DEVICETREE
  1245. /* try to remove the proc node as well */
  1246. if (np->pde)
  1247. proc_device_tree_remove_prop(np->pde, prop);
  1248. #endif /* CONFIG_PROC_DEVICETREE */
  1249. return 0;
  1250. }
  1251. /*
  1252. * Update a property in a node. Note that we don't actually
  1253. * remove it, since we have given out who-knows-how-many pointers
  1254. * to the data using get-property. Instead we just move the property
  1255. * to the "dead properties" list, and add the new property to the
  1256. * property list
  1257. */
  1258. int prom_update_property(struct device_node *np,
  1259. struct property *newprop,
  1260. struct property *oldprop)
  1261. {
  1262. struct property **next;
  1263. int found = 0;
  1264. write_lock(&devtree_lock);
  1265. next = &np->properties;
  1266. while (*next) {
  1267. if (*next == oldprop) {
  1268. /* found the node */
  1269. newprop->next = oldprop->next;
  1270. *next = newprop;
  1271. oldprop->next = np->deadprops;
  1272. np->deadprops = oldprop;
  1273. found = 1;
  1274. break;
  1275. }
  1276. next = &(*next)->next;
  1277. }
  1278. write_unlock(&devtree_lock);
  1279. if (!found)
  1280. return -ENODEV;
  1281. #ifdef CONFIG_PROC_DEVICETREE
  1282. /* try to add to proc as well if it was initialized */
  1283. if (np->pde)
  1284. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1285. #endif /* CONFIG_PROC_DEVICETREE */
  1286. return 0;
  1287. }
  1288. /* Find the device node for a given logical cpu number, also returns the cpu
  1289. * local thread number (index in ibm,interrupt-server#s) if relevant and
  1290. * asked for (non NULL)
  1291. */
  1292. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  1293. {
  1294. int hardid;
  1295. struct device_node *np;
  1296. hardid = get_hard_smp_processor_id(cpu);
  1297. for_each_node_by_type(np, "cpu") {
  1298. const u32 *intserv;
  1299. unsigned int plen, t;
  1300. /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
  1301. * fallback to "reg" property and assume no threads
  1302. */
  1303. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
  1304. &plen);
  1305. if (intserv == NULL) {
  1306. const u32 *reg = of_get_property(np, "reg", NULL);
  1307. if (reg == NULL)
  1308. continue;
  1309. if (*reg == hardid) {
  1310. if (thread)
  1311. *thread = 0;
  1312. return np;
  1313. }
  1314. } else {
  1315. plen /= sizeof(u32);
  1316. for (t = 0; t < plen; t++) {
  1317. if (hardid == intserv[t]) {
  1318. if (thread)
  1319. *thread = t;
  1320. return np;
  1321. }
  1322. }
  1323. }
  1324. }
  1325. return NULL;
  1326. }
  1327. EXPORT_SYMBOL(of_get_cpu_node);
  1328. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  1329. static struct debugfs_blob_wrapper flat_dt_blob;
  1330. static int __init export_flat_device_tree(void)
  1331. {
  1332. struct dentry *d;
  1333. flat_dt_blob.data = initial_boot_params;
  1334. flat_dt_blob.size = initial_boot_params->totalsize;
  1335. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  1336. powerpc_debugfs_root, &flat_dt_blob);
  1337. if (!d)
  1338. return 1;
  1339. return 0;
  1340. }
  1341. __initcall(export_flat_device_tree);
  1342. #endif