prom.c 36 KB

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