prom.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #undef DEBUG
  16. #include <stdarg.h>
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/init.h>
  20. #include <linux/threads.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/types.h>
  23. #include <linux/pci.h>
  24. #include <linux/stringify.h>
  25. #include <linux/delay.h>
  26. #include <linux/initrd.h>
  27. #include <linux/bitops.h>
  28. #include <linux/module.h>
  29. #include <linux/kexec.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/irq.h>
  32. #include <linux/lmb.h>
  33. #include <asm/prom.h>
  34. #include <asm/rtas.h>
  35. #include <asm/page.h>
  36. #include <asm/processor.h>
  37. #include <asm/irq.h>
  38. #include <asm/io.h>
  39. #include <asm/kdump.h>
  40. #include <asm/smp.h>
  41. #include <asm/system.h>
  42. #include <asm/mmu.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/pci.h>
  45. #include <asm/iommu.h>
  46. #include <asm/btext.h>
  47. #include <asm/sections.h>
  48. #include <asm/machdep.h>
  49. #include <asm/pSeries_reconfig.h>
  50. #include <asm/pci-bridge.h>
  51. #include <asm/phyp_dump.h>
  52. #include <asm/kexec.h>
  53. #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. * move_device_tree - move tree to an unused area, if needed.
  393. *
  394. * The device tree may be allocated beyond our memory limit, or inside the
  395. * crash kernel region for kdump. If so, move it out of the way.
  396. */
  397. static void __init move_device_tree(void)
  398. {
  399. unsigned long start, size;
  400. void *p;
  401. DBG("-> move_device_tree\n");
  402. start = __pa(initial_boot_params);
  403. size = initial_boot_params->totalsize;
  404. if ((memory_limit && (start + size) > memory_limit) ||
  405. overlaps_crashkernel(start, size)) {
  406. p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
  407. memcpy(p, initial_boot_params, size);
  408. initial_boot_params = (struct boot_param_header *)p;
  409. DBG("Moved device tree to 0x%p\n", p);
  410. }
  411. DBG("<- move_device_tree\n");
  412. }
  413. /**
  414. * unflattens the device-tree passed by the firmware, creating the
  415. * tree of struct device_node. It also fills the "name" and "type"
  416. * pointers of the nodes so the normal device-tree walking functions
  417. * can be used (this used to be done by finish_device_tree)
  418. */
  419. void __init unflatten_device_tree(void)
  420. {
  421. unsigned long start, mem, size;
  422. struct device_node **allnextp = &allnodes;
  423. DBG(" -> unflatten_device_tree()\n");
  424. /* First pass, scan for size */
  425. start = ((unsigned long)initial_boot_params) +
  426. initial_boot_params->off_dt_struct;
  427. size = unflatten_dt_node(0, &start, NULL, NULL, 0);
  428. size = (size | 3) + 1;
  429. DBG(" size is %lx, allocating...\n", size);
  430. /* Allocate memory for the expanded device tree */
  431. mem = lmb_alloc(size + 4, __alignof__(struct device_node));
  432. mem = (unsigned long) __va(mem);
  433. ((u32 *)mem)[size / 4] = 0xdeadbeef;
  434. DBG(" unflattening %lx...\n", mem);
  435. /* Second pass, do actual unflattening */
  436. start = ((unsigned long)initial_boot_params) +
  437. initial_boot_params->off_dt_struct;
  438. unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
  439. if (*((u32 *)start) != OF_DT_END)
  440. printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
  441. if (((u32 *)mem)[size / 4] != 0xdeadbeef)
  442. printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
  443. ((u32 *)mem)[size / 4] );
  444. *allnextp = NULL;
  445. /* Get pointer to OF "/chosen" node for use everywhere */
  446. of_chosen = of_find_node_by_path("/chosen");
  447. if (of_chosen == NULL)
  448. of_chosen = of_find_node_by_path("/chosen@0");
  449. DBG(" <- unflatten_device_tree()\n");
  450. }
  451. /*
  452. * ibm,pa-features is a per-cpu property that contains a string of
  453. * attribute descriptors, each of which has a 2 byte header plus up
  454. * to 254 bytes worth of processor attribute bits. First header
  455. * byte specifies the number of bytes following the header.
  456. * Second header byte is an "attribute-specifier" type, of which
  457. * zero is the only currently-defined value.
  458. * Implementation: Pass in the byte and bit offset for the feature
  459. * that we are interested in. The function will return -1 if the
  460. * pa-features property is missing, or a 1/0 to indicate if the feature
  461. * is supported/not supported. Note that the bit numbers are
  462. * big-endian to match the definition in PAPR.
  463. */
  464. static struct ibm_pa_feature {
  465. unsigned long cpu_features; /* CPU_FTR_xxx bit */
  466. unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
  467. unsigned char pabyte; /* byte number in ibm,pa-features */
  468. unsigned char pabit; /* bit number (big-endian) */
  469. unsigned char invert; /* if 1, pa bit set => clear feature */
  470. } ibm_pa_features[] __initdata = {
  471. {0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
  472. {0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
  473. {CPU_FTR_SLB, 0, 0, 2, 0},
  474. {CPU_FTR_CTRL, 0, 0, 3, 0},
  475. {CPU_FTR_NOEXECUTE, 0, 0, 6, 0},
  476. {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1},
  477. {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
  478. {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
  479. };
  480. static void __init scan_features(unsigned long node, unsigned char *ftrs,
  481. unsigned long tablelen,
  482. struct ibm_pa_feature *fp,
  483. unsigned long ft_size)
  484. {
  485. unsigned long i, len, bit;
  486. /* find descriptor with type == 0 */
  487. for (;;) {
  488. if (tablelen < 3)
  489. return;
  490. len = 2 + ftrs[0];
  491. if (tablelen < len)
  492. return; /* descriptor 0 not found */
  493. if (ftrs[1] == 0)
  494. break;
  495. tablelen -= len;
  496. ftrs += len;
  497. }
  498. /* loop over bits we know about */
  499. for (i = 0; i < ft_size; ++i, ++fp) {
  500. if (fp->pabyte >= ftrs[0])
  501. continue;
  502. bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
  503. if (bit ^ fp->invert) {
  504. cur_cpu_spec->cpu_features |= fp->cpu_features;
  505. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
  506. } else {
  507. cur_cpu_spec->cpu_features &= ~fp->cpu_features;
  508. cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
  509. }
  510. }
  511. }
  512. static void __init check_cpu_pa_features(unsigned long node)
  513. {
  514. unsigned char *pa_ftrs;
  515. unsigned long tablelen;
  516. pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
  517. if (pa_ftrs == NULL)
  518. return;
  519. scan_features(node, pa_ftrs, tablelen,
  520. ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
  521. }
  522. #ifdef CONFIG_PPC64
  523. static void __init check_cpu_slb_size(unsigned long node)
  524. {
  525. u32 *slb_size_ptr;
  526. slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
  527. if (slb_size_ptr != NULL) {
  528. mmu_slb_size = *slb_size_ptr;
  529. }
  530. }
  531. #else
  532. #define check_cpu_slb_size(node) do { } while(0)
  533. #endif
  534. static struct feature_property {
  535. const char *name;
  536. u32 min_value;
  537. unsigned long cpu_feature;
  538. unsigned long cpu_user_ftr;
  539. } feature_properties[] __initdata = {
  540. #ifdef CONFIG_ALTIVEC
  541. {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  542. {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  543. #endif /* CONFIG_ALTIVEC */
  544. #ifdef CONFIG_PPC64
  545. {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
  546. {"ibm,purr", 1, CPU_FTR_PURR, 0},
  547. {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
  548. #endif /* CONFIG_PPC64 */
  549. };
  550. #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
  551. static inline void identical_pvr_fixup(unsigned long node)
  552. {
  553. unsigned int pvr;
  554. char *model = of_get_flat_dt_prop(node, "model", NULL);
  555. /*
  556. * Since 440GR(x)/440EP(x) processors have the same pvr,
  557. * we check the node path and set bit 28 in the cur_cpu_spec
  558. * pvr for EP(x) processor version. This bit is always 0 in
  559. * the "real" pvr. Then we call identify_cpu again with
  560. * the new logical pvr to enable FPU support.
  561. */
  562. if (model && strstr(model, "440EP")) {
  563. pvr = cur_cpu_spec->pvr_value | 0x8;
  564. identify_cpu(0, pvr);
  565. DBG("Using logical pvr %x for %s\n", pvr, model);
  566. }
  567. }
  568. #else
  569. #define identical_pvr_fixup(node) do { } while(0)
  570. #endif
  571. static void __init check_cpu_feature_properties(unsigned long node)
  572. {
  573. unsigned long i;
  574. struct feature_property *fp = feature_properties;
  575. const u32 *prop;
  576. for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
  577. prop = of_get_flat_dt_prop(node, fp->name, NULL);
  578. if (prop && *prop >= fp->min_value) {
  579. cur_cpu_spec->cpu_features |= fp->cpu_feature;
  580. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
  581. }
  582. }
  583. }
  584. static int __init early_init_dt_scan_cpus(unsigned long node,
  585. const char *uname, int depth,
  586. void *data)
  587. {
  588. static int logical_cpuid = 0;
  589. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  590. const u32 *prop;
  591. const u32 *intserv;
  592. int i, nthreads;
  593. unsigned long len;
  594. int found = 0;
  595. /* We are scanning "cpu" nodes only */
  596. if (type == NULL || strcmp(type, "cpu") != 0)
  597. return 0;
  598. /* Get physical cpuid */
  599. intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
  600. if (intserv) {
  601. nthreads = len / sizeof(int);
  602. } else {
  603. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  604. nthreads = 1;
  605. }
  606. /*
  607. * Now see if any of these threads match our boot cpu.
  608. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  609. */
  610. for (i = 0; i < nthreads; i++) {
  611. /*
  612. * version 2 of the kexec param format adds the phys cpuid of
  613. * booted proc.
  614. */
  615. if (initial_boot_params && initial_boot_params->version >= 2) {
  616. if (intserv[i] ==
  617. initial_boot_params->boot_cpuid_phys) {
  618. found = 1;
  619. break;
  620. }
  621. } else {
  622. /*
  623. * Check if it's the boot-cpu, set it's hw index now,
  624. * unfortunately this format did not support booting
  625. * off secondary threads.
  626. */
  627. if (of_get_flat_dt_prop(node,
  628. "linux,boot-cpu", NULL) != NULL) {
  629. found = 1;
  630. break;
  631. }
  632. }
  633. #ifdef CONFIG_SMP
  634. /* logical cpu id is always 0 on UP kernels */
  635. logical_cpuid++;
  636. #endif
  637. }
  638. if (found) {
  639. DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
  640. intserv[i]);
  641. boot_cpuid = logical_cpuid;
  642. set_hard_smp_processor_id(boot_cpuid, intserv[i]);
  643. /*
  644. * PAPR defines "logical" PVR values for cpus that
  645. * meet various levels of the architecture:
  646. * 0x0f000001 Architecture version 2.04
  647. * 0x0f000002 Architecture version 2.05
  648. * If the cpu-version property in the cpu node contains
  649. * such a value, we call identify_cpu again with the
  650. * logical PVR value in order to use the cpu feature
  651. * bits appropriate for the architecture level.
  652. *
  653. * A POWER6 partition in "POWER6 architected" mode
  654. * uses the 0x0f000002 PVR value; in POWER5+ mode
  655. * it uses 0x0f000001.
  656. */
  657. prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
  658. if (prop && (*prop & 0xff000000) == 0x0f000000)
  659. identify_cpu(0, *prop);
  660. identical_pvr_fixup(node);
  661. }
  662. check_cpu_feature_properties(node);
  663. check_cpu_pa_features(node);
  664. check_cpu_slb_size(node);
  665. #ifdef CONFIG_PPC_PSERIES
  666. if (nthreads > 1)
  667. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  668. else
  669. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  670. #endif
  671. return 0;
  672. }
  673. #ifdef CONFIG_BLK_DEV_INITRD
  674. static void __init early_init_dt_check_for_initrd(unsigned long node)
  675. {
  676. unsigned long l;
  677. u32 *prop;
  678. DBG("Looking for initrd properties... ");
  679. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
  680. if (prop) {
  681. initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
  682. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
  683. if (prop) {
  684. initrd_end = (unsigned long)
  685. __va(of_read_ulong(prop, l/4));
  686. initrd_below_start_ok = 1;
  687. } else {
  688. initrd_start = 0;
  689. }
  690. }
  691. DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start, initrd_end);
  692. }
  693. #else
  694. static inline void early_init_dt_check_for_initrd(unsigned long node)
  695. {
  696. }
  697. #endif /* CONFIG_BLK_DEV_INITRD */
  698. static int __init early_init_dt_scan_chosen(unsigned long node,
  699. const char *uname, int depth, void *data)
  700. {
  701. unsigned long *lprop;
  702. unsigned long l;
  703. char *p;
  704. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  705. if (depth != 1 ||
  706. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  707. return 0;
  708. #ifdef CONFIG_PPC64
  709. /* check if iommu is forced on or off */
  710. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  711. iommu_is_off = 1;
  712. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  713. iommu_force_on = 1;
  714. #endif
  715. /* mem=x on the command line is the preferred mechanism */
  716. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  717. if (lprop)
  718. memory_limit = *lprop;
  719. #ifdef CONFIG_PPC64
  720. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  721. if (lprop)
  722. tce_alloc_start = *lprop;
  723. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  724. if (lprop)
  725. tce_alloc_end = *lprop;
  726. #endif
  727. #ifdef CONFIG_KEXEC
  728. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  729. if (lprop)
  730. crashk_res.start = *lprop;
  731. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  732. if (lprop)
  733. crashk_res.end = crashk_res.start + *lprop - 1;
  734. #endif
  735. early_init_dt_check_for_initrd(node);
  736. /* Retreive command line */
  737. p = of_get_flat_dt_prop(node, "bootargs", &l);
  738. if (p != NULL && l > 0)
  739. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  740. #ifdef CONFIG_CMDLINE
  741. if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
  742. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  743. #endif /* CONFIG_CMDLINE */
  744. DBG("Command line is: %s\n", cmd_line);
  745. /* break now */
  746. return 1;
  747. }
  748. static int __init early_init_dt_scan_root(unsigned long node,
  749. const char *uname, int depth, void *data)
  750. {
  751. u32 *prop;
  752. if (depth != 0)
  753. return 0;
  754. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  755. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  756. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  757. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  758. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  759. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  760. /* break now */
  761. return 1;
  762. }
  763. static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
  764. {
  765. cell_t *p = *cellp;
  766. *cellp = p + s;
  767. return of_read_number(p, s);
  768. }
  769. #ifdef CONFIG_PPC_PSERIES
  770. /*
  771. * Interpret the ibm,dynamic-memory property in the
  772. * /ibm,dynamic-reconfiguration-memory node.
  773. * This contains a list of memory blocks along with NUMA affinity
  774. * information.
  775. */
  776. static int __init early_init_dt_scan_drconf_memory(unsigned long node)
  777. {
  778. cell_t *dm, *ls;
  779. unsigned long l, n, flags;
  780. u64 base, size, lmb_size;
  781. ls = (cell_t *)of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
  782. if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
  783. return 0;
  784. lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
  785. dm = (cell_t *)of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
  786. if (dm == NULL || l < sizeof(cell_t))
  787. return 0;
  788. n = *dm++; /* number of entries */
  789. if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
  790. return 0;
  791. for (; n != 0; --n) {
  792. base = dt_mem_next_cell(dt_root_addr_cells, &dm);
  793. flags = dm[3];
  794. /* skip DRC index, pad, assoc. list index, flags */
  795. dm += 4;
  796. /* skip this block if the reserved bit is set in flags (0x80)
  797. or if the block is not assigned to this partition (0x8) */
  798. if ((flags & 0x80) || !(flags & 0x8))
  799. continue;
  800. size = lmb_size;
  801. if (iommu_is_off) {
  802. if (base >= 0x80000000ul)
  803. continue;
  804. if ((base + size) > 0x80000000ul)
  805. size = 0x80000000ul - base;
  806. }
  807. lmb_add(base, size);
  808. }
  809. lmb_dump_all();
  810. return 0;
  811. }
  812. #else
  813. #define early_init_dt_scan_drconf_memory(node) 0
  814. #endif /* CONFIG_PPC_PSERIES */
  815. static int __init early_init_dt_scan_memory(unsigned long node,
  816. const char *uname, int depth, void *data)
  817. {
  818. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  819. cell_t *reg, *endp;
  820. unsigned long l;
  821. /* Look for the ibm,dynamic-reconfiguration-memory node */
  822. if (depth == 1 &&
  823. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  824. return early_init_dt_scan_drconf_memory(node);
  825. /* We are scanning "memory" nodes only */
  826. if (type == NULL) {
  827. /*
  828. * The longtrail doesn't have a device_type on the
  829. * /memory node, so look for the node called /memory@0.
  830. */
  831. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  832. return 0;
  833. } else if (strcmp(type, "memory") != 0)
  834. return 0;
  835. reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  836. if (reg == NULL)
  837. reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
  838. if (reg == NULL)
  839. return 0;
  840. endp = reg + (l / sizeof(cell_t));
  841. DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  842. uname, l, reg[0], reg[1], reg[2], reg[3]);
  843. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  844. u64 base, size;
  845. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  846. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  847. if (size == 0)
  848. continue;
  849. DBG(" - %llx , %llx\n", (unsigned long long)base,
  850. (unsigned long long)size);
  851. #ifdef CONFIG_PPC64
  852. if (iommu_is_off) {
  853. if (base >= 0x80000000ul)
  854. continue;
  855. if ((base + size) > 0x80000000ul)
  856. size = 0x80000000ul - base;
  857. }
  858. #endif
  859. lmb_add(base, size);
  860. }
  861. return 0;
  862. }
  863. static void __init early_reserve_mem(void)
  864. {
  865. u64 base, size;
  866. u64 *reserve_map;
  867. unsigned long self_base;
  868. unsigned long self_size;
  869. reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  870. initial_boot_params->off_mem_rsvmap);
  871. /* before we do anything, lets reserve the dt blob */
  872. self_base = __pa((unsigned long)initial_boot_params);
  873. self_size = initial_boot_params->totalsize;
  874. lmb_reserve(self_base, self_size);
  875. #ifdef CONFIG_BLK_DEV_INITRD
  876. /* then reserve the initrd, if any */
  877. if (initrd_start && (initrd_end > initrd_start))
  878. lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
  879. #endif /* CONFIG_BLK_DEV_INITRD */
  880. #ifdef CONFIG_PPC32
  881. /*
  882. * Handle the case where we might be booting from an old kexec
  883. * image that setup the mem_rsvmap as pairs of 32-bit values
  884. */
  885. if (*reserve_map > 0xffffffffull) {
  886. u32 base_32, size_32;
  887. u32 *reserve_map_32 = (u32 *)reserve_map;
  888. while (1) {
  889. base_32 = *(reserve_map_32++);
  890. size_32 = *(reserve_map_32++);
  891. if (size_32 == 0)
  892. break;
  893. /* skip if the reservation is for the blob */
  894. if (base_32 == self_base && size_32 == self_size)
  895. continue;
  896. DBG("reserving: %x -> %x\n", base_32, size_32);
  897. lmb_reserve(base_32, size_32);
  898. }
  899. return;
  900. }
  901. #endif
  902. while (1) {
  903. base = *(reserve_map++);
  904. size = *(reserve_map++);
  905. if (size == 0)
  906. break;
  907. DBG("reserving: %llx -> %llx\n", base, size);
  908. lmb_reserve(base, size);
  909. }
  910. #if 0
  911. DBG("memory reserved, lmbs :\n");
  912. lmb_dump_all();
  913. #endif
  914. }
  915. #ifdef CONFIG_PHYP_DUMP
  916. /**
  917. * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
  918. *
  919. * Function to find the largest size we need to reserve
  920. * during early boot process.
  921. *
  922. * It either looks for boot param and returns that OR
  923. * returns larger of 256 or 5% rounded down to multiples of 256MB.
  924. *
  925. */
  926. static inline unsigned long phyp_dump_calculate_reserve_size(void)
  927. {
  928. unsigned long tmp;
  929. if (phyp_dump_info->reserve_bootvar)
  930. return phyp_dump_info->reserve_bootvar;
  931. /* divide by 20 to get 5% of value */
  932. tmp = lmb_end_of_DRAM();
  933. do_div(tmp, 20);
  934. /* round it down in multiples of 256 */
  935. tmp = tmp & ~0x0FFFFFFFUL;
  936. return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
  937. }
  938. /**
  939. * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
  940. *
  941. * This routine may reserve memory regions in the kernel only
  942. * if the system is supported and a dump was taken in last
  943. * boot instance or if the hardware is supported and the
  944. * scratch area needs to be setup. In other instances it returns
  945. * without reserving anything. The memory in case of dump being
  946. * active is freed when the dump is collected (by userland tools).
  947. */
  948. static void __init phyp_dump_reserve_mem(void)
  949. {
  950. unsigned long base, size;
  951. unsigned long variable_reserve_size;
  952. if (!phyp_dump_info->phyp_dump_configured) {
  953. printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
  954. return;
  955. }
  956. if (!phyp_dump_info->phyp_dump_at_boot) {
  957. printk(KERN_INFO "Phyp-dump disabled at boot time\n");
  958. return;
  959. }
  960. variable_reserve_size = phyp_dump_calculate_reserve_size();
  961. if (phyp_dump_info->phyp_dump_is_active) {
  962. /* Reserve *everything* above RMR.Area freed by userland tools*/
  963. base = variable_reserve_size;
  964. size = lmb_end_of_DRAM() - base;
  965. /* XXX crashed_ram_end is wrong, since it may be beyond
  966. * the memory_limit, it will need to be adjusted. */
  967. lmb_reserve(base, size);
  968. phyp_dump_info->init_reserve_start = base;
  969. phyp_dump_info->init_reserve_size = size;
  970. } else {
  971. size = phyp_dump_info->cpu_state_size +
  972. phyp_dump_info->hpte_region_size +
  973. variable_reserve_size;
  974. base = lmb_end_of_DRAM() - size;
  975. lmb_reserve(base, size);
  976. phyp_dump_info->init_reserve_start = base;
  977. phyp_dump_info->init_reserve_size = size;
  978. }
  979. }
  980. #else
  981. static inline void __init phyp_dump_reserve_mem(void) {}
  982. #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
  983. void __init early_init_devtree(void *params)
  984. {
  985. DBG(" -> early_init_devtree(%p)\n", params);
  986. /* Setup flat device-tree pointer */
  987. initial_boot_params = params;
  988. #ifdef CONFIG_PPC_RTAS
  989. /* Some machines might need RTAS info for debugging, grab it now. */
  990. of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
  991. #endif
  992. #ifdef CONFIG_PHYP_DUMP
  993. /* scan tree to see if dump occured during last boot */
  994. of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
  995. #endif
  996. /* Retrieve various informations from the /chosen node of the
  997. * device-tree, including the platform type, initrd location and
  998. * size, TCE reserve, and more ...
  999. */
  1000. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  1001. /* Scan memory nodes and rebuild LMBs */
  1002. lmb_init();
  1003. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  1004. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  1005. /* Save command line for /proc/cmdline and then parse parameters */
  1006. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  1007. parse_early_param();
  1008. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  1009. lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  1010. reserve_kdump_trampoline();
  1011. reserve_crashkernel();
  1012. early_reserve_mem();
  1013. phyp_dump_reserve_mem();
  1014. lmb_enforce_memory_limit(memory_limit);
  1015. lmb_analyze();
  1016. DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
  1017. /* We may need to relocate the flat tree, do it now.
  1018. * FIXME .. and the initrd too? */
  1019. move_device_tree();
  1020. DBG("Scanning CPUs ...\n");
  1021. /* Retreive CPU related informations from the flat tree
  1022. * (altivec support, boot CPU ID, ...)
  1023. */
  1024. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  1025. DBG(" <- early_init_devtree()\n");
  1026. }
  1027. /**
  1028. * Indicates whether the root node has a given value in its
  1029. * compatible property.
  1030. */
  1031. int machine_is_compatible(const char *compat)
  1032. {
  1033. struct device_node *root;
  1034. int rc = 0;
  1035. root = of_find_node_by_path("/");
  1036. if (root) {
  1037. rc = of_device_is_compatible(root, compat);
  1038. of_node_put(root);
  1039. }
  1040. return rc;
  1041. }
  1042. EXPORT_SYMBOL(machine_is_compatible);
  1043. /*******
  1044. *
  1045. * New implementation of the OF "find" APIs, return a refcounted
  1046. * object, call of_node_put() when done. The device tree and list
  1047. * are protected by a rw_lock.
  1048. *
  1049. * Note that property management will need some locking as well,
  1050. * this isn't dealt with yet.
  1051. *
  1052. *******/
  1053. /**
  1054. * of_find_node_by_phandle - Find a node given a phandle
  1055. * @handle: phandle of the node to find
  1056. *
  1057. * Returns a node pointer with refcount incremented, use
  1058. * of_node_put() on it when done.
  1059. */
  1060. struct device_node *of_find_node_by_phandle(phandle handle)
  1061. {
  1062. struct device_node *np;
  1063. read_lock(&devtree_lock);
  1064. for (np = allnodes; np != 0; np = np->allnext)
  1065. if (np->linux_phandle == handle)
  1066. break;
  1067. of_node_get(np);
  1068. read_unlock(&devtree_lock);
  1069. return np;
  1070. }
  1071. EXPORT_SYMBOL(of_find_node_by_phandle);
  1072. /**
  1073. * of_find_all_nodes - Get next node in global list
  1074. * @prev: Previous node or NULL to start iteration
  1075. * of_node_put() will be called on it
  1076. *
  1077. * Returns a node pointer with refcount incremented, use
  1078. * of_node_put() on it when done.
  1079. */
  1080. struct device_node *of_find_all_nodes(struct device_node *prev)
  1081. {
  1082. struct device_node *np;
  1083. read_lock(&devtree_lock);
  1084. np = prev ? prev->allnext : allnodes;
  1085. for (; np != 0; np = np->allnext)
  1086. if (of_node_get(np))
  1087. break;
  1088. of_node_put(prev);
  1089. read_unlock(&devtree_lock);
  1090. return np;
  1091. }
  1092. EXPORT_SYMBOL(of_find_all_nodes);
  1093. /**
  1094. * of_node_get - Increment refcount of a node
  1095. * @node: Node to inc refcount, NULL is supported to
  1096. * simplify writing of callers
  1097. *
  1098. * Returns node.
  1099. */
  1100. struct device_node *of_node_get(struct device_node *node)
  1101. {
  1102. if (node)
  1103. kref_get(&node->kref);
  1104. return node;
  1105. }
  1106. EXPORT_SYMBOL(of_node_get);
  1107. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1108. {
  1109. return container_of(kref, struct device_node, kref);
  1110. }
  1111. /**
  1112. * of_node_release - release a dynamically allocated node
  1113. * @kref: kref element of the node to be released
  1114. *
  1115. * In of_node_put() this function is passed to kref_put()
  1116. * as the destructor.
  1117. */
  1118. static void of_node_release(struct kref *kref)
  1119. {
  1120. struct device_node *node = kref_to_device_node(kref);
  1121. struct property *prop = node->properties;
  1122. /* We should never be releasing nodes that haven't been detached. */
  1123. if (!of_node_check_flag(node, OF_DETACHED)) {
  1124. printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
  1125. dump_stack();
  1126. kref_init(&node->kref);
  1127. return;
  1128. }
  1129. if (!of_node_check_flag(node, OF_DYNAMIC))
  1130. return;
  1131. while (prop) {
  1132. struct property *next = prop->next;
  1133. kfree(prop->name);
  1134. kfree(prop->value);
  1135. kfree(prop);
  1136. prop = next;
  1137. if (!prop) {
  1138. prop = node->deadprops;
  1139. node->deadprops = NULL;
  1140. }
  1141. }
  1142. kfree(node->full_name);
  1143. kfree(node->data);
  1144. kfree(node);
  1145. }
  1146. /**
  1147. * of_node_put - Decrement refcount of a node
  1148. * @node: Node to dec refcount, NULL is supported to
  1149. * simplify writing of callers
  1150. *
  1151. */
  1152. void of_node_put(struct device_node *node)
  1153. {
  1154. if (node)
  1155. kref_put(&node->kref, of_node_release);
  1156. }
  1157. EXPORT_SYMBOL(of_node_put);
  1158. /*
  1159. * Plug a device node into the tree and global list.
  1160. */
  1161. void of_attach_node(struct device_node *np)
  1162. {
  1163. unsigned long flags;
  1164. write_lock_irqsave(&devtree_lock, flags);
  1165. np->sibling = np->parent->child;
  1166. np->allnext = allnodes;
  1167. np->parent->child = np;
  1168. allnodes = np;
  1169. write_unlock_irqrestore(&devtree_lock, flags);
  1170. }
  1171. /*
  1172. * "Unplug" a node from the device tree. The caller must hold
  1173. * a reference to the node. The memory associated with the node
  1174. * is not freed until its refcount goes to zero.
  1175. */
  1176. void of_detach_node(struct device_node *np)
  1177. {
  1178. struct device_node *parent;
  1179. unsigned long flags;
  1180. write_lock_irqsave(&devtree_lock, flags);
  1181. parent = np->parent;
  1182. if (!parent)
  1183. goto out_unlock;
  1184. if (allnodes == np)
  1185. allnodes = np->allnext;
  1186. else {
  1187. struct device_node *prev;
  1188. for (prev = allnodes;
  1189. prev->allnext != np;
  1190. prev = prev->allnext)
  1191. ;
  1192. prev->allnext = np->allnext;
  1193. }
  1194. if (parent->child == np)
  1195. parent->child = np->sibling;
  1196. else {
  1197. struct device_node *prevsib;
  1198. for (prevsib = np->parent->child;
  1199. prevsib->sibling != np;
  1200. prevsib = prevsib->sibling)
  1201. ;
  1202. prevsib->sibling = np->sibling;
  1203. }
  1204. of_node_set_flag(np, OF_DETACHED);
  1205. out_unlock:
  1206. write_unlock_irqrestore(&devtree_lock, flags);
  1207. }
  1208. #ifdef CONFIG_PPC_PSERIES
  1209. /*
  1210. * Fix up the uninitialized fields in a new device node:
  1211. * name, type and pci-specific fields
  1212. */
  1213. static int of_finish_dynamic_node(struct device_node *node)
  1214. {
  1215. struct device_node *parent = of_get_parent(node);
  1216. int err = 0;
  1217. const phandle *ibm_phandle;
  1218. node->name = of_get_property(node, "name", NULL);
  1219. node->type = of_get_property(node, "device_type", NULL);
  1220. if (!node->name)
  1221. node->name = "<NULL>";
  1222. if (!node->type)
  1223. node->type = "<NULL>";
  1224. if (!parent) {
  1225. err = -ENODEV;
  1226. goto out;
  1227. }
  1228. /* We don't support that function on PowerMac, at least
  1229. * not yet
  1230. */
  1231. if (machine_is(powermac))
  1232. return -ENODEV;
  1233. /* fix up new node's linux_phandle field */
  1234. if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
  1235. node->linux_phandle = *ibm_phandle;
  1236. out:
  1237. of_node_put(parent);
  1238. return err;
  1239. }
  1240. static int prom_reconfig_notifier(struct notifier_block *nb,
  1241. unsigned long action, void *node)
  1242. {
  1243. int err;
  1244. switch (action) {
  1245. case PSERIES_RECONFIG_ADD:
  1246. err = of_finish_dynamic_node(node);
  1247. if (err < 0) {
  1248. printk(KERN_ERR "finish_node returned %d\n", err);
  1249. err = NOTIFY_BAD;
  1250. }
  1251. break;
  1252. default:
  1253. err = NOTIFY_DONE;
  1254. break;
  1255. }
  1256. return err;
  1257. }
  1258. static struct notifier_block prom_reconfig_nb = {
  1259. .notifier_call = prom_reconfig_notifier,
  1260. .priority = 10, /* This one needs to run first */
  1261. };
  1262. static int __init prom_reconfig_setup(void)
  1263. {
  1264. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1265. }
  1266. __initcall(prom_reconfig_setup);
  1267. #endif
  1268. /*
  1269. * Add a property to a node
  1270. */
  1271. int prom_add_property(struct device_node* np, struct property* prop)
  1272. {
  1273. struct property **next;
  1274. unsigned long flags;
  1275. prop->next = NULL;
  1276. write_lock_irqsave(&devtree_lock, flags);
  1277. next = &np->properties;
  1278. while (*next) {
  1279. if (strcmp(prop->name, (*next)->name) == 0) {
  1280. /* duplicate ! don't insert it */
  1281. write_unlock_irqrestore(&devtree_lock, flags);
  1282. return -1;
  1283. }
  1284. next = &(*next)->next;
  1285. }
  1286. *next = prop;
  1287. write_unlock_irqrestore(&devtree_lock, flags);
  1288. #ifdef CONFIG_PROC_DEVICETREE
  1289. /* try to add to proc as well if it was initialized */
  1290. if (np->pde)
  1291. proc_device_tree_add_prop(np->pde, prop);
  1292. #endif /* CONFIG_PROC_DEVICETREE */
  1293. return 0;
  1294. }
  1295. /*
  1296. * Remove a property from a node. Note that we don't actually
  1297. * remove it, since we have given out who-knows-how-many pointers
  1298. * to the data using get-property. Instead we just move the property
  1299. * to the "dead properties" list, so it won't be found any more.
  1300. */
  1301. int prom_remove_property(struct device_node *np, struct property *prop)
  1302. {
  1303. struct property **next;
  1304. unsigned long flags;
  1305. int found = 0;
  1306. write_lock_irqsave(&devtree_lock, flags);
  1307. next = &np->properties;
  1308. while (*next) {
  1309. if (*next == prop) {
  1310. /* found the node */
  1311. *next = prop->next;
  1312. prop->next = np->deadprops;
  1313. np->deadprops = prop;
  1314. found = 1;
  1315. break;
  1316. }
  1317. next = &(*next)->next;
  1318. }
  1319. write_unlock_irqrestore(&devtree_lock, flags);
  1320. if (!found)
  1321. return -ENODEV;
  1322. #ifdef CONFIG_PROC_DEVICETREE
  1323. /* try to remove the proc node as well */
  1324. if (np->pde)
  1325. proc_device_tree_remove_prop(np->pde, prop);
  1326. #endif /* CONFIG_PROC_DEVICETREE */
  1327. return 0;
  1328. }
  1329. /*
  1330. * Update a property in a node. Note that we don't actually
  1331. * remove it, since we have given out who-knows-how-many pointers
  1332. * to the data using get-property. Instead we just move the property
  1333. * to the "dead properties" list, and add the new property to the
  1334. * property list
  1335. */
  1336. int prom_update_property(struct device_node *np,
  1337. struct property *newprop,
  1338. struct property *oldprop)
  1339. {
  1340. struct property **next;
  1341. unsigned long flags;
  1342. int found = 0;
  1343. write_lock_irqsave(&devtree_lock, flags);
  1344. next = &np->properties;
  1345. while (*next) {
  1346. if (*next == oldprop) {
  1347. /* found the node */
  1348. newprop->next = oldprop->next;
  1349. *next = newprop;
  1350. oldprop->next = np->deadprops;
  1351. np->deadprops = oldprop;
  1352. found = 1;
  1353. break;
  1354. }
  1355. next = &(*next)->next;
  1356. }
  1357. write_unlock_irqrestore(&devtree_lock, flags);
  1358. if (!found)
  1359. return -ENODEV;
  1360. #ifdef CONFIG_PROC_DEVICETREE
  1361. /* try to add to proc as well if it was initialized */
  1362. if (np->pde)
  1363. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1364. #endif /* CONFIG_PROC_DEVICETREE */
  1365. return 0;
  1366. }
  1367. /* Find the device node for a given logical cpu number, also returns the cpu
  1368. * local thread number (index in ibm,interrupt-server#s) if relevant and
  1369. * asked for (non NULL)
  1370. */
  1371. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  1372. {
  1373. int hardid;
  1374. struct device_node *np;
  1375. hardid = get_hard_smp_processor_id(cpu);
  1376. for_each_node_by_type(np, "cpu") {
  1377. const u32 *intserv;
  1378. unsigned int plen, t;
  1379. /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
  1380. * fallback to "reg" property and assume no threads
  1381. */
  1382. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
  1383. &plen);
  1384. if (intserv == NULL) {
  1385. const u32 *reg = of_get_property(np, "reg", NULL);
  1386. if (reg == NULL)
  1387. continue;
  1388. if (*reg == hardid) {
  1389. if (thread)
  1390. *thread = 0;
  1391. return np;
  1392. }
  1393. } else {
  1394. plen /= sizeof(u32);
  1395. for (t = 0; t < plen; t++) {
  1396. if (hardid == intserv[t]) {
  1397. if (thread)
  1398. *thread = t;
  1399. return np;
  1400. }
  1401. }
  1402. }
  1403. }
  1404. return NULL;
  1405. }
  1406. EXPORT_SYMBOL(of_get_cpu_node);
  1407. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  1408. static struct debugfs_blob_wrapper flat_dt_blob;
  1409. static int __init export_flat_device_tree(void)
  1410. {
  1411. struct dentry *d;
  1412. flat_dt_blob.data = initial_boot_params;
  1413. flat_dt_blob.size = initial_boot_params->totalsize;
  1414. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  1415. powerpc_debugfs_root, &flat_dt_blob);
  1416. if (!d)
  1417. return 1;
  1418. return 0;
  1419. }
  1420. __initcall(export_flat_device_tree);
  1421. #endif