prom.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  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. static struct feature_property {
  522. const char *name;
  523. u32 min_value;
  524. unsigned long cpu_feature;
  525. unsigned long cpu_user_ftr;
  526. } feature_properties[] __initdata = {
  527. #ifdef CONFIG_ALTIVEC
  528. {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  529. {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  530. #endif /* CONFIG_ALTIVEC */
  531. #ifdef CONFIG_PPC64
  532. {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
  533. {"ibm,purr", 1, CPU_FTR_PURR, 0},
  534. {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
  535. #endif /* CONFIG_PPC64 */
  536. };
  537. static void __init check_cpu_feature_properties(unsigned long node)
  538. {
  539. unsigned long i;
  540. struct feature_property *fp = feature_properties;
  541. const u32 *prop;
  542. for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
  543. prop = of_get_flat_dt_prop(node, fp->name, NULL);
  544. if (prop && *prop >= fp->min_value) {
  545. cur_cpu_spec->cpu_features |= fp->cpu_feature;
  546. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
  547. }
  548. }
  549. }
  550. static int __init early_init_dt_scan_cpus(unsigned long node,
  551. const char *uname, int depth,
  552. void *data)
  553. {
  554. static int logical_cpuid = 0;
  555. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  556. const u32 *prop;
  557. const u32 *intserv;
  558. int i, nthreads;
  559. unsigned long len;
  560. int found = 0;
  561. /* We are scanning "cpu" nodes only */
  562. if (type == NULL || strcmp(type, "cpu") != 0)
  563. return 0;
  564. /* Get physical cpuid */
  565. intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
  566. if (intserv) {
  567. nthreads = len / sizeof(int);
  568. } else {
  569. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  570. nthreads = 1;
  571. }
  572. /*
  573. * Now see if any of these threads match our boot cpu.
  574. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  575. */
  576. for (i = 0; i < nthreads; i++) {
  577. /*
  578. * version 2 of the kexec param format adds the phys cpuid of
  579. * booted proc.
  580. */
  581. if (initial_boot_params && initial_boot_params->version >= 2) {
  582. if (intserv[i] ==
  583. initial_boot_params->boot_cpuid_phys) {
  584. found = 1;
  585. break;
  586. }
  587. } else {
  588. /*
  589. * Check if it's the boot-cpu, set it's hw index now,
  590. * unfortunately this format did not support booting
  591. * off secondary threads.
  592. */
  593. if (of_get_flat_dt_prop(node,
  594. "linux,boot-cpu", NULL) != NULL) {
  595. found = 1;
  596. break;
  597. }
  598. }
  599. #ifdef CONFIG_SMP
  600. /* logical cpu id is always 0 on UP kernels */
  601. logical_cpuid++;
  602. #endif
  603. }
  604. if (found) {
  605. DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
  606. intserv[i]);
  607. boot_cpuid = logical_cpuid;
  608. set_hard_smp_processor_id(boot_cpuid, intserv[i]);
  609. /*
  610. * PAPR defines "logical" PVR values for cpus that
  611. * meet various levels of the architecture:
  612. * 0x0f000001 Architecture version 2.04
  613. * 0x0f000002 Architecture version 2.05
  614. * If the cpu-version property in the cpu node contains
  615. * such a value, we call identify_cpu again with the
  616. * logical PVR value in order to use the cpu feature
  617. * bits appropriate for the architecture level.
  618. *
  619. * A POWER6 partition in "POWER6 architected" mode
  620. * uses the 0x0f000002 PVR value; in POWER5+ mode
  621. * it uses 0x0f000001.
  622. */
  623. prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
  624. if (prop && (*prop & 0xff000000) == 0x0f000000)
  625. identify_cpu(0, *prop);
  626. }
  627. check_cpu_feature_properties(node);
  628. check_cpu_pa_features(node);
  629. #ifdef CONFIG_PPC_PSERIES
  630. if (nthreads > 1)
  631. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  632. else
  633. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  634. #endif
  635. return 0;
  636. }
  637. #ifdef CONFIG_BLK_DEV_INITRD
  638. static void __init early_init_dt_check_for_initrd(unsigned long node)
  639. {
  640. unsigned long l;
  641. u32 *prop;
  642. DBG("Looking for initrd properties... ");
  643. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
  644. if (prop) {
  645. initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
  646. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
  647. if (prop) {
  648. initrd_end = (unsigned long)
  649. __va(of_read_ulong(prop, l/4));
  650. initrd_below_start_ok = 1;
  651. } else {
  652. initrd_start = 0;
  653. }
  654. }
  655. DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start, initrd_end);
  656. }
  657. #else
  658. static inline void early_init_dt_check_for_initrd(unsigned long node)
  659. {
  660. }
  661. #endif /* CONFIG_BLK_DEV_INITRD */
  662. static int __init early_init_dt_scan_chosen(unsigned long node,
  663. const char *uname, int depth, void *data)
  664. {
  665. unsigned long *lprop;
  666. unsigned long l;
  667. char *p;
  668. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  669. if (depth != 1 ||
  670. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  671. return 0;
  672. #ifdef CONFIG_PPC64
  673. /* check if iommu is forced on or off */
  674. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  675. iommu_is_off = 1;
  676. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  677. iommu_force_on = 1;
  678. #endif
  679. /* mem=x on the command line is the preferred mechanism */
  680. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  681. if (lprop)
  682. memory_limit = *lprop;
  683. #ifdef CONFIG_PPC64
  684. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  685. if (lprop)
  686. tce_alloc_start = *lprop;
  687. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  688. if (lprop)
  689. tce_alloc_end = *lprop;
  690. #endif
  691. #ifdef CONFIG_KEXEC
  692. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  693. if (lprop)
  694. crashk_res.start = *lprop;
  695. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  696. if (lprop)
  697. crashk_res.end = crashk_res.start + *lprop - 1;
  698. #endif
  699. early_init_dt_check_for_initrd(node);
  700. /* Retreive command line */
  701. p = of_get_flat_dt_prop(node, "bootargs", &l);
  702. if (p != NULL && l > 0)
  703. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  704. #ifdef CONFIG_CMDLINE
  705. if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
  706. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  707. #endif /* CONFIG_CMDLINE */
  708. DBG("Command line is: %s\n", cmd_line);
  709. /* break now */
  710. return 1;
  711. }
  712. static int __init early_init_dt_scan_root(unsigned long node,
  713. const char *uname, int depth, void *data)
  714. {
  715. u32 *prop;
  716. if (depth != 0)
  717. return 0;
  718. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  719. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  720. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  721. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  722. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  723. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  724. /* break now */
  725. return 1;
  726. }
  727. static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
  728. {
  729. cell_t *p = *cellp;
  730. *cellp = p + s;
  731. return of_read_ulong(p, s);
  732. }
  733. #ifdef CONFIG_PPC_PSERIES
  734. /*
  735. * Interpret the ibm,dynamic-memory property in the
  736. * /ibm,dynamic-reconfiguration-memory node.
  737. * This contains a list of memory blocks along with NUMA affinity
  738. * information.
  739. */
  740. static int __init early_init_dt_scan_drconf_memory(unsigned long node)
  741. {
  742. cell_t *dm, *ls;
  743. unsigned long l, n;
  744. unsigned long base, size, lmb_size, flags;
  745. ls = (cell_t *)of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
  746. if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
  747. return 0;
  748. lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
  749. dm = (cell_t *)of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
  750. if (dm == NULL || l < sizeof(cell_t))
  751. return 0;
  752. n = *dm++; /* number of entries */
  753. if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
  754. return 0;
  755. for (; n != 0; --n) {
  756. base = dt_mem_next_cell(dt_root_addr_cells, &dm);
  757. flags = dm[3];
  758. /* skip DRC index, pad, assoc. list index, flags */
  759. dm += 4;
  760. /* skip this block if the reserved bit is set in flags (0x80)
  761. or if the block is not assigned to this partition (0x8) */
  762. if ((flags & 0x80) || !(flags & 0x8))
  763. continue;
  764. size = lmb_size;
  765. if (iommu_is_off) {
  766. if (base >= 0x80000000ul)
  767. continue;
  768. if ((base + size) > 0x80000000ul)
  769. size = 0x80000000ul - base;
  770. }
  771. lmb_add(base, size);
  772. }
  773. lmb_dump_all();
  774. return 0;
  775. }
  776. #else
  777. #define early_init_dt_scan_drconf_memory(node) 0
  778. #endif /* CONFIG_PPC_PSERIES */
  779. static int __init early_init_dt_scan_memory(unsigned long node,
  780. const char *uname, int depth, void *data)
  781. {
  782. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  783. cell_t *reg, *endp;
  784. unsigned long l;
  785. /* Look for the ibm,dynamic-reconfiguration-memory node */
  786. if (depth == 1 &&
  787. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  788. return early_init_dt_scan_drconf_memory(node);
  789. /* We are scanning "memory" nodes only */
  790. if (type == NULL) {
  791. /*
  792. * The longtrail doesn't have a device_type on the
  793. * /memory node, so look for the node called /memory@0.
  794. */
  795. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  796. return 0;
  797. } else if (strcmp(type, "memory") != 0)
  798. return 0;
  799. reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  800. if (reg == NULL)
  801. reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
  802. if (reg == NULL)
  803. return 0;
  804. endp = reg + (l / sizeof(cell_t));
  805. DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  806. uname, l, reg[0], reg[1], reg[2], reg[3]);
  807. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  808. unsigned long base, size;
  809. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  810. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  811. if (size == 0)
  812. continue;
  813. DBG(" - %lx , %lx\n", base, size);
  814. #ifdef CONFIG_PPC64
  815. if (iommu_is_off) {
  816. if (base >= 0x80000000ul)
  817. continue;
  818. if ((base + size) > 0x80000000ul)
  819. size = 0x80000000ul - base;
  820. }
  821. #endif
  822. lmb_add(base, size);
  823. }
  824. return 0;
  825. }
  826. static void __init early_reserve_mem(void)
  827. {
  828. u64 base, size;
  829. u64 *reserve_map;
  830. unsigned long self_base;
  831. unsigned long self_size;
  832. reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  833. initial_boot_params->off_mem_rsvmap);
  834. /* before we do anything, lets reserve the dt blob */
  835. self_base = __pa((unsigned long)initial_boot_params);
  836. self_size = initial_boot_params->totalsize;
  837. lmb_reserve(self_base, self_size);
  838. #ifdef CONFIG_BLK_DEV_INITRD
  839. /* then reserve the initrd, if any */
  840. if (initrd_start && (initrd_end > initrd_start))
  841. lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
  842. #endif /* CONFIG_BLK_DEV_INITRD */
  843. #ifdef CONFIG_PPC32
  844. /*
  845. * Handle the case where we might be booting from an old kexec
  846. * image that setup the mem_rsvmap as pairs of 32-bit values
  847. */
  848. if (*reserve_map > 0xffffffffull) {
  849. u32 base_32, size_32;
  850. u32 *reserve_map_32 = (u32 *)reserve_map;
  851. while (1) {
  852. base_32 = *(reserve_map_32++);
  853. size_32 = *(reserve_map_32++);
  854. if (size_32 == 0)
  855. break;
  856. /* skip if the reservation is for the blob */
  857. if (base_32 == self_base && size_32 == self_size)
  858. continue;
  859. DBG("reserving: %x -> %x\n", base_32, size_32);
  860. lmb_reserve(base_32, size_32);
  861. }
  862. return;
  863. }
  864. #endif
  865. while (1) {
  866. base = *(reserve_map++);
  867. size = *(reserve_map++);
  868. if (size == 0)
  869. break;
  870. DBG("reserving: %llx -> %llx\n", base, size);
  871. lmb_reserve(base, size);
  872. }
  873. #if 0
  874. DBG("memory reserved, lmbs :\n");
  875. lmb_dump_all();
  876. #endif
  877. }
  878. void __init early_init_devtree(void *params)
  879. {
  880. DBG(" -> early_init_devtree(%p)\n", params);
  881. /* Setup flat device-tree pointer */
  882. initial_boot_params = params;
  883. #ifdef CONFIG_PPC_RTAS
  884. /* Some machines might need RTAS info for debugging, grab it now. */
  885. of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
  886. #endif
  887. /* Retrieve various informations from the /chosen node of the
  888. * device-tree, including the platform type, initrd location and
  889. * size, TCE reserve, and more ...
  890. */
  891. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  892. /* Scan memory nodes and rebuild LMBs */
  893. lmb_init();
  894. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  895. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  896. /* Save command line for /proc/cmdline and then parse parameters */
  897. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  898. parse_early_param();
  899. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  900. lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  901. reserve_kdump_trampoline();
  902. reserve_crashkernel();
  903. early_reserve_mem();
  904. lmb_enforce_memory_limit(memory_limit);
  905. lmb_analyze();
  906. DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
  907. /* We may need to relocate the flat tree, do it now.
  908. * FIXME .. and the initrd too? */
  909. move_device_tree();
  910. DBG("Scanning CPUs ...\n");
  911. /* Retreive CPU related informations from the flat tree
  912. * (altivec support, boot CPU ID, ...)
  913. */
  914. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  915. DBG(" <- early_init_devtree()\n");
  916. }
  917. /**
  918. * Indicates whether the root node has a given value in its
  919. * compatible property.
  920. */
  921. int machine_is_compatible(const char *compat)
  922. {
  923. struct device_node *root;
  924. int rc = 0;
  925. root = of_find_node_by_path("/");
  926. if (root) {
  927. rc = of_device_is_compatible(root, compat);
  928. of_node_put(root);
  929. }
  930. return rc;
  931. }
  932. EXPORT_SYMBOL(machine_is_compatible);
  933. /*******
  934. *
  935. * New implementation of the OF "find" APIs, return a refcounted
  936. * object, call of_node_put() when done. The device tree and list
  937. * are protected by a rw_lock.
  938. *
  939. * Note that property management will need some locking as well,
  940. * this isn't dealt with yet.
  941. *
  942. *******/
  943. /**
  944. * of_find_node_by_phandle - Find a node given a phandle
  945. * @handle: phandle of the node to find
  946. *
  947. * Returns a node pointer with refcount incremented, use
  948. * of_node_put() on it when done.
  949. */
  950. struct device_node *of_find_node_by_phandle(phandle handle)
  951. {
  952. struct device_node *np;
  953. read_lock(&devtree_lock);
  954. for (np = allnodes; np != 0; np = np->allnext)
  955. if (np->linux_phandle == handle)
  956. break;
  957. of_node_get(np);
  958. read_unlock(&devtree_lock);
  959. return np;
  960. }
  961. EXPORT_SYMBOL(of_find_node_by_phandle);
  962. /**
  963. * of_find_all_nodes - Get next node in global list
  964. * @prev: Previous node or NULL to start iteration
  965. * of_node_put() will be called on it
  966. *
  967. * Returns a node pointer with refcount incremented, use
  968. * of_node_put() on it when done.
  969. */
  970. struct device_node *of_find_all_nodes(struct device_node *prev)
  971. {
  972. struct device_node *np;
  973. read_lock(&devtree_lock);
  974. np = prev ? prev->allnext : allnodes;
  975. for (; np != 0; np = np->allnext)
  976. if (of_node_get(np))
  977. break;
  978. of_node_put(prev);
  979. read_unlock(&devtree_lock);
  980. return np;
  981. }
  982. EXPORT_SYMBOL(of_find_all_nodes);
  983. /**
  984. * of_node_get - Increment refcount of a node
  985. * @node: Node to inc refcount, NULL is supported to
  986. * simplify writing of callers
  987. *
  988. * Returns node.
  989. */
  990. struct device_node *of_node_get(struct device_node *node)
  991. {
  992. if (node)
  993. kref_get(&node->kref);
  994. return node;
  995. }
  996. EXPORT_SYMBOL(of_node_get);
  997. static inline struct device_node * kref_to_device_node(struct kref *kref)
  998. {
  999. return container_of(kref, struct device_node, kref);
  1000. }
  1001. /**
  1002. * of_node_release - release a dynamically allocated node
  1003. * @kref: kref element of the node to be released
  1004. *
  1005. * In of_node_put() this function is passed to kref_put()
  1006. * as the destructor.
  1007. */
  1008. static void of_node_release(struct kref *kref)
  1009. {
  1010. struct device_node *node = kref_to_device_node(kref);
  1011. struct property *prop = node->properties;
  1012. /* We should never be releasing nodes that haven't been detached. */
  1013. if (!of_node_check_flag(node, OF_DETACHED)) {
  1014. printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
  1015. dump_stack();
  1016. kref_init(&node->kref);
  1017. return;
  1018. }
  1019. if (!of_node_check_flag(node, OF_DYNAMIC))
  1020. return;
  1021. while (prop) {
  1022. struct property *next = prop->next;
  1023. kfree(prop->name);
  1024. kfree(prop->value);
  1025. kfree(prop);
  1026. prop = next;
  1027. if (!prop) {
  1028. prop = node->deadprops;
  1029. node->deadprops = NULL;
  1030. }
  1031. }
  1032. kfree(node->full_name);
  1033. kfree(node->data);
  1034. kfree(node);
  1035. }
  1036. /**
  1037. * of_node_put - Decrement refcount of a node
  1038. * @node: Node to dec refcount, NULL is supported to
  1039. * simplify writing of callers
  1040. *
  1041. */
  1042. void of_node_put(struct device_node *node)
  1043. {
  1044. if (node)
  1045. kref_put(&node->kref, of_node_release);
  1046. }
  1047. EXPORT_SYMBOL(of_node_put);
  1048. /*
  1049. * Plug a device node into the tree and global list.
  1050. */
  1051. void of_attach_node(struct device_node *np)
  1052. {
  1053. write_lock(&devtree_lock);
  1054. np->sibling = np->parent->child;
  1055. np->allnext = allnodes;
  1056. np->parent->child = np;
  1057. allnodes = np;
  1058. write_unlock(&devtree_lock);
  1059. }
  1060. /*
  1061. * "Unplug" a node from the device tree. The caller must hold
  1062. * a reference to the node. The memory associated with the node
  1063. * is not freed until its refcount goes to zero.
  1064. */
  1065. void of_detach_node(struct device_node *np)
  1066. {
  1067. struct device_node *parent;
  1068. write_lock(&devtree_lock);
  1069. parent = np->parent;
  1070. if (!parent)
  1071. goto out_unlock;
  1072. if (allnodes == np)
  1073. allnodes = np->allnext;
  1074. else {
  1075. struct device_node *prev;
  1076. for (prev = allnodes;
  1077. prev->allnext != np;
  1078. prev = prev->allnext)
  1079. ;
  1080. prev->allnext = np->allnext;
  1081. }
  1082. if (parent->child == np)
  1083. parent->child = np->sibling;
  1084. else {
  1085. struct device_node *prevsib;
  1086. for (prevsib = np->parent->child;
  1087. prevsib->sibling != np;
  1088. prevsib = prevsib->sibling)
  1089. ;
  1090. prevsib->sibling = np->sibling;
  1091. }
  1092. of_node_set_flag(np, OF_DETACHED);
  1093. out_unlock:
  1094. write_unlock(&devtree_lock);
  1095. }
  1096. #ifdef CONFIG_PPC_PSERIES
  1097. /*
  1098. * Fix up the uninitialized fields in a new device node:
  1099. * name, type and pci-specific fields
  1100. */
  1101. static int of_finish_dynamic_node(struct device_node *node)
  1102. {
  1103. struct device_node *parent = of_get_parent(node);
  1104. int err = 0;
  1105. const phandle *ibm_phandle;
  1106. node->name = of_get_property(node, "name", NULL);
  1107. node->type = of_get_property(node, "device_type", NULL);
  1108. if (!node->name)
  1109. node->name = "<NULL>";
  1110. if (!node->type)
  1111. node->type = "<NULL>";
  1112. if (!parent) {
  1113. err = -ENODEV;
  1114. goto out;
  1115. }
  1116. /* We don't support that function on PowerMac, at least
  1117. * not yet
  1118. */
  1119. if (machine_is(powermac))
  1120. return -ENODEV;
  1121. /* fix up new node's linux_phandle field */
  1122. if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
  1123. node->linux_phandle = *ibm_phandle;
  1124. out:
  1125. of_node_put(parent);
  1126. return err;
  1127. }
  1128. static int prom_reconfig_notifier(struct notifier_block *nb,
  1129. unsigned long action, void *node)
  1130. {
  1131. int err;
  1132. switch (action) {
  1133. case PSERIES_RECONFIG_ADD:
  1134. err = of_finish_dynamic_node(node);
  1135. if (err < 0) {
  1136. printk(KERN_ERR "finish_node returned %d\n", err);
  1137. err = NOTIFY_BAD;
  1138. }
  1139. break;
  1140. default:
  1141. err = NOTIFY_DONE;
  1142. break;
  1143. }
  1144. return err;
  1145. }
  1146. static struct notifier_block prom_reconfig_nb = {
  1147. .notifier_call = prom_reconfig_notifier,
  1148. .priority = 10, /* This one needs to run first */
  1149. };
  1150. static int __init prom_reconfig_setup(void)
  1151. {
  1152. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1153. }
  1154. __initcall(prom_reconfig_setup);
  1155. #endif
  1156. /*
  1157. * Add a property to a node
  1158. */
  1159. int prom_add_property(struct device_node* np, struct property* prop)
  1160. {
  1161. struct property **next;
  1162. prop->next = NULL;
  1163. write_lock(&devtree_lock);
  1164. next = &np->properties;
  1165. while (*next) {
  1166. if (strcmp(prop->name, (*next)->name) == 0) {
  1167. /* duplicate ! don't insert it */
  1168. write_unlock(&devtree_lock);
  1169. return -1;
  1170. }
  1171. next = &(*next)->next;
  1172. }
  1173. *next = prop;
  1174. write_unlock(&devtree_lock);
  1175. #ifdef CONFIG_PROC_DEVICETREE
  1176. /* try to add to proc as well if it was initialized */
  1177. if (np->pde)
  1178. proc_device_tree_add_prop(np->pde, prop);
  1179. #endif /* CONFIG_PROC_DEVICETREE */
  1180. return 0;
  1181. }
  1182. /*
  1183. * Remove a property from a node. Note that we don't actually
  1184. * remove it, since we have given out who-knows-how-many pointers
  1185. * to the data using get-property. Instead we just move the property
  1186. * to the "dead properties" list, so it won't be found any more.
  1187. */
  1188. int prom_remove_property(struct device_node *np, struct property *prop)
  1189. {
  1190. struct property **next;
  1191. int found = 0;
  1192. write_lock(&devtree_lock);
  1193. next = &np->properties;
  1194. while (*next) {
  1195. if (*next == prop) {
  1196. /* found the node */
  1197. *next = prop->next;
  1198. prop->next = np->deadprops;
  1199. np->deadprops = prop;
  1200. found = 1;
  1201. break;
  1202. }
  1203. next = &(*next)->next;
  1204. }
  1205. write_unlock(&devtree_lock);
  1206. if (!found)
  1207. return -ENODEV;
  1208. #ifdef CONFIG_PROC_DEVICETREE
  1209. /* try to remove the proc node as well */
  1210. if (np->pde)
  1211. proc_device_tree_remove_prop(np->pde, prop);
  1212. #endif /* CONFIG_PROC_DEVICETREE */
  1213. return 0;
  1214. }
  1215. /*
  1216. * Update a property in a node. Note that we don't actually
  1217. * remove it, since we have given out who-knows-how-many pointers
  1218. * to the data using get-property. Instead we just move the property
  1219. * to the "dead properties" list, and add the new property to the
  1220. * property list
  1221. */
  1222. int prom_update_property(struct device_node *np,
  1223. struct property *newprop,
  1224. struct property *oldprop)
  1225. {
  1226. struct property **next;
  1227. int found = 0;
  1228. write_lock(&devtree_lock);
  1229. next = &np->properties;
  1230. while (*next) {
  1231. if (*next == oldprop) {
  1232. /* found the node */
  1233. newprop->next = oldprop->next;
  1234. *next = newprop;
  1235. oldprop->next = np->deadprops;
  1236. np->deadprops = oldprop;
  1237. found = 1;
  1238. break;
  1239. }
  1240. next = &(*next)->next;
  1241. }
  1242. write_unlock(&devtree_lock);
  1243. if (!found)
  1244. return -ENODEV;
  1245. #ifdef CONFIG_PROC_DEVICETREE
  1246. /* try to add to proc as well if it was initialized */
  1247. if (np->pde)
  1248. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1249. #endif /* CONFIG_PROC_DEVICETREE */
  1250. return 0;
  1251. }
  1252. /* Find the device node for a given logical cpu number, also returns the cpu
  1253. * local thread number (index in ibm,interrupt-server#s) if relevant and
  1254. * asked for (non NULL)
  1255. */
  1256. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  1257. {
  1258. int hardid;
  1259. struct device_node *np;
  1260. hardid = get_hard_smp_processor_id(cpu);
  1261. for_each_node_by_type(np, "cpu") {
  1262. const u32 *intserv;
  1263. unsigned int plen, t;
  1264. /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
  1265. * fallback to "reg" property and assume no threads
  1266. */
  1267. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
  1268. &plen);
  1269. if (intserv == NULL) {
  1270. const u32 *reg = of_get_property(np, "reg", NULL);
  1271. if (reg == NULL)
  1272. continue;
  1273. if (*reg == hardid) {
  1274. if (thread)
  1275. *thread = 0;
  1276. return np;
  1277. }
  1278. } else {
  1279. plen /= sizeof(u32);
  1280. for (t = 0; t < plen; t++) {
  1281. if (hardid == intserv[t]) {
  1282. if (thread)
  1283. *thread = t;
  1284. return np;
  1285. }
  1286. }
  1287. }
  1288. }
  1289. return NULL;
  1290. }
  1291. EXPORT_SYMBOL(of_get_cpu_node);
  1292. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  1293. static struct debugfs_blob_wrapper flat_dt_blob;
  1294. static int __init export_flat_device_tree(void)
  1295. {
  1296. struct dentry *d;
  1297. flat_dt_blob.data = initial_boot_params;
  1298. flat_dt_blob.size = initial_boot_params->totalsize;
  1299. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  1300. powerpc_debugfs_root, &flat_dt_blob);
  1301. if (!d)
  1302. return 1;
  1303. return 0;
  1304. }
  1305. __initcall(export_flat_device_tree);
  1306. #endif