prom.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  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. static struct device_node *allnodes = NULL;
  71. /* use when traversing tree through the allnext, child, sibling,
  72. * or parent members of struct device_node.
  73. */
  74. static DEFINE_RWLOCK(devtree_lock);
  75. /* export that to outside world */
  76. struct device_node *of_chosen;
  77. static inline char *find_flat_dt_string(u32 offset)
  78. {
  79. return ((char *)initial_boot_params) +
  80. initial_boot_params->off_dt_strings + offset;
  81. }
  82. /**
  83. * This function is used to scan the flattened device-tree, it is
  84. * used to extract the memory informations at boot before we can
  85. * unflatten the tree
  86. */
  87. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  88. const char *uname, int depth,
  89. void *data),
  90. void *data)
  91. {
  92. unsigned long p = ((unsigned long)initial_boot_params) +
  93. initial_boot_params->off_dt_struct;
  94. int rc = 0;
  95. int depth = -1;
  96. do {
  97. u32 tag = *((u32 *)p);
  98. char *pathp;
  99. p += 4;
  100. if (tag == OF_DT_END_NODE) {
  101. depth --;
  102. continue;
  103. }
  104. if (tag == OF_DT_NOP)
  105. continue;
  106. if (tag == OF_DT_END)
  107. break;
  108. if (tag == OF_DT_PROP) {
  109. u32 sz = *((u32 *)p);
  110. p += 8;
  111. if (initial_boot_params->version < 0x10)
  112. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  113. p += sz;
  114. p = _ALIGN(p, 4);
  115. continue;
  116. }
  117. if (tag != OF_DT_BEGIN_NODE) {
  118. printk(KERN_WARNING "Invalid tag %x scanning flattened"
  119. " device tree !\n", tag);
  120. return -EINVAL;
  121. }
  122. depth++;
  123. pathp = (char *)p;
  124. p = _ALIGN(p + strlen(pathp) + 1, 4);
  125. if ((*pathp) == '/') {
  126. char *lp, *np;
  127. for (lp = NULL, np = pathp; *np; np++)
  128. if ((*np) == '/')
  129. lp = np+1;
  130. if (lp != NULL)
  131. pathp = lp;
  132. }
  133. rc = it(p, pathp, depth, data);
  134. if (rc != 0)
  135. break;
  136. } while(1);
  137. return rc;
  138. }
  139. unsigned long __init of_get_flat_dt_root(void)
  140. {
  141. unsigned long p = ((unsigned long)initial_boot_params) +
  142. initial_boot_params->off_dt_struct;
  143. while(*((u32 *)p) == OF_DT_NOP)
  144. p += 4;
  145. BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE);
  146. p += 4;
  147. return _ALIGN(p + strlen((char *)p) + 1, 4);
  148. }
  149. /**
  150. * This function can be used within scan_flattened_dt callback to get
  151. * access to properties
  152. */
  153. void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
  154. unsigned long *size)
  155. {
  156. unsigned long p = node;
  157. do {
  158. u32 tag = *((u32 *)p);
  159. u32 sz, noff;
  160. const char *nstr;
  161. p += 4;
  162. if (tag == OF_DT_NOP)
  163. continue;
  164. if (tag != OF_DT_PROP)
  165. return NULL;
  166. sz = *((u32 *)p);
  167. noff = *((u32 *)(p + 4));
  168. p += 8;
  169. if (initial_boot_params->version < 0x10)
  170. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  171. nstr = find_flat_dt_string(noff);
  172. if (nstr == NULL) {
  173. printk(KERN_WARNING "Can't find property index"
  174. " name !\n");
  175. return NULL;
  176. }
  177. if (strcmp(name, nstr) == 0) {
  178. if (size)
  179. *size = sz;
  180. return (void *)p;
  181. }
  182. p += sz;
  183. p = _ALIGN(p, 4);
  184. } while(1);
  185. }
  186. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  187. {
  188. const char* cp;
  189. unsigned long cplen, l;
  190. cp = of_get_flat_dt_prop(node, "compatible", &cplen);
  191. if (cp == NULL)
  192. return 0;
  193. while (cplen > 0) {
  194. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  195. return 1;
  196. l = strlen(cp) + 1;
  197. cp += l;
  198. cplen -= l;
  199. }
  200. return 0;
  201. }
  202. static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
  203. unsigned long align)
  204. {
  205. void *res;
  206. *mem = _ALIGN(*mem, align);
  207. res = (void *)*mem;
  208. *mem += size;
  209. return res;
  210. }
  211. static unsigned long __init unflatten_dt_node(unsigned long mem,
  212. unsigned long *p,
  213. struct device_node *dad,
  214. struct device_node ***allnextpp,
  215. unsigned long fpsize)
  216. {
  217. struct device_node *np;
  218. struct property *pp, **prev_pp = NULL;
  219. char *pathp;
  220. u32 tag;
  221. unsigned int l, allocl;
  222. int has_name = 0;
  223. int new_format = 0;
  224. tag = *((u32 *)(*p));
  225. if (tag != OF_DT_BEGIN_NODE) {
  226. printk("Weird tag at start of node: %x\n", tag);
  227. return mem;
  228. }
  229. *p += 4;
  230. pathp = (char *)*p;
  231. l = allocl = strlen(pathp) + 1;
  232. *p = _ALIGN(*p + l, 4);
  233. /* version 0x10 has a more compact unit name here instead of the full
  234. * path. we accumulate the full path size using "fpsize", we'll rebuild
  235. * it later. We detect this because the first character of the name is
  236. * not '/'.
  237. */
  238. if ((*pathp) != '/') {
  239. new_format = 1;
  240. if (fpsize == 0) {
  241. /* root node: special case. fpsize accounts for path
  242. * plus terminating zero. root node only has '/', so
  243. * fpsize should be 2, but we want to avoid the first
  244. * level nodes to have two '/' so we use fpsize 1 here
  245. */
  246. fpsize = 1;
  247. allocl = 2;
  248. } else {
  249. /* account for '/' and path size minus terminal 0
  250. * already in 'l'
  251. */
  252. fpsize += l;
  253. allocl = fpsize;
  254. }
  255. }
  256. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  257. __alignof__(struct device_node));
  258. if (allnextpp) {
  259. memset(np, 0, sizeof(*np));
  260. np->full_name = ((char*)np) + sizeof(struct device_node);
  261. if (new_format) {
  262. char *p = np->full_name;
  263. /* rebuild full path for new format */
  264. if (dad && dad->parent) {
  265. strcpy(p, dad->full_name);
  266. #ifdef DEBUG
  267. if ((strlen(p) + l + 1) != allocl) {
  268. DBG("%s: p: %d, l: %d, a: %d\n",
  269. pathp, (int)strlen(p), l, allocl);
  270. }
  271. #endif
  272. p += strlen(p);
  273. }
  274. *(p++) = '/';
  275. memcpy(p, pathp, l);
  276. } else
  277. memcpy(np->full_name, pathp, l);
  278. prev_pp = &np->properties;
  279. **allnextpp = np;
  280. *allnextpp = &np->allnext;
  281. if (dad != NULL) {
  282. np->parent = dad;
  283. /* we temporarily use the next field as `last_child'*/
  284. if (dad->next == 0)
  285. dad->child = np;
  286. else
  287. dad->next->sibling = np;
  288. dad->next = np;
  289. }
  290. kref_init(&np->kref);
  291. }
  292. while(1) {
  293. u32 sz, noff;
  294. char *pname;
  295. tag = *((u32 *)(*p));
  296. if (tag == OF_DT_NOP) {
  297. *p += 4;
  298. continue;
  299. }
  300. if (tag != OF_DT_PROP)
  301. break;
  302. *p += 4;
  303. sz = *((u32 *)(*p));
  304. noff = *((u32 *)((*p) + 4));
  305. *p += 8;
  306. if (initial_boot_params->version < 0x10)
  307. *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
  308. pname = find_flat_dt_string(noff);
  309. if (pname == NULL) {
  310. printk("Can't find property name in list !\n");
  311. break;
  312. }
  313. if (strcmp(pname, "name") == 0)
  314. has_name = 1;
  315. l = strlen(pname) + 1;
  316. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  317. __alignof__(struct property));
  318. if (allnextpp) {
  319. if (strcmp(pname, "linux,phandle") == 0) {
  320. np->node = *((u32 *)*p);
  321. if (np->linux_phandle == 0)
  322. np->linux_phandle = np->node;
  323. }
  324. if (strcmp(pname, "ibm,phandle") == 0)
  325. np->linux_phandle = *((u32 *)*p);
  326. pp->name = pname;
  327. pp->length = sz;
  328. pp->value = (void *)*p;
  329. *prev_pp = pp;
  330. prev_pp = &pp->next;
  331. }
  332. *p = _ALIGN((*p) + sz, 4);
  333. }
  334. /* with version 0x10 we may not have the name property, recreate
  335. * it here from the unit name if absent
  336. */
  337. if (!has_name) {
  338. char *p = pathp, *ps = pathp, *pa = NULL;
  339. int sz;
  340. while (*p) {
  341. if ((*p) == '@')
  342. pa = p;
  343. if ((*p) == '/')
  344. ps = p + 1;
  345. p++;
  346. }
  347. if (pa < ps)
  348. pa = p;
  349. sz = (pa - ps) + 1;
  350. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  351. __alignof__(struct property));
  352. if (allnextpp) {
  353. pp->name = "name";
  354. pp->length = sz;
  355. pp->value = pp + 1;
  356. *prev_pp = pp;
  357. prev_pp = &pp->next;
  358. memcpy(pp->value, ps, sz - 1);
  359. ((char *)pp->value)[sz - 1] = 0;
  360. DBG("fixed up name for %s -> %s\n", pathp,
  361. (char *)pp->value);
  362. }
  363. }
  364. if (allnextpp) {
  365. *prev_pp = NULL;
  366. np->name = of_get_property(np, "name", NULL);
  367. np->type = of_get_property(np, "device_type", NULL);
  368. if (!np->name)
  369. np->name = "<NULL>";
  370. if (!np->type)
  371. np->type = "<NULL>";
  372. }
  373. while (tag == OF_DT_BEGIN_NODE) {
  374. mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
  375. tag = *((u32 *)(*p));
  376. }
  377. if (tag != OF_DT_END_NODE) {
  378. printk("Weird tag at end of node: %x\n", tag);
  379. return mem;
  380. }
  381. *p += 4;
  382. return mem;
  383. }
  384. static int __init early_parse_mem(char *p)
  385. {
  386. if (!p)
  387. return 1;
  388. memory_limit = PAGE_ALIGN(memparse(p, &p));
  389. DBG("memory limit = 0x%lx\n", memory_limit);
  390. return 0;
  391. }
  392. early_param("mem", early_parse_mem);
  393. /*
  394. * The device tree may be allocated below our memory limit, or inside the
  395. * crash kernel region for kdump. If so, move it out now.
  396. */
  397. static void 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. #if 0
  478. /* put this back once we know how to test if firmware does 64k IO */
  479. {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
  480. #endif
  481. {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
  482. };
  483. static void __init scan_features(unsigned long node, unsigned char *ftrs,
  484. unsigned long tablelen,
  485. struct ibm_pa_feature *fp,
  486. unsigned long ft_size)
  487. {
  488. unsigned long i, len, bit;
  489. /* find descriptor with type == 0 */
  490. for (;;) {
  491. if (tablelen < 3)
  492. return;
  493. len = 2 + ftrs[0];
  494. if (tablelen < len)
  495. return; /* descriptor 0 not found */
  496. if (ftrs[1] == 0)
  497. break;
  498. tablelen -= len;
  499. ftrs += len;
  500. }
  501. /* loop over bits we know about */
  502. for (i = 0; i < ft_size; ++i, ++fp) {
  503. if (fp->pabyte >= ftrs[0])
  504. continue;
  505. bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
  506. if (bit ^ fp->invert) {
  507. cur_cpu_spec->cpu_features |= fp->cpu_features;
  508. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
  509. } else {
  510. cur_cpu_spec->cpu_features &= ~fp->cpu_features;
  511. cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
  512. }
  513. }
  514. }
  515. static void __init check_cpu_pa_features(unsigned long node)
  516. {
  517. unsigned char *pa_ftrs;
  518. unsigned long tablelen;
  519. pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
  520. if (pa_ftrs == NULL)
  521. return;
  522. scan_features(node, pa_ftrs, tablelen,
  523. ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
  524. }
  525. static struct feature_property {
  526. const char *name;
  527. u32 min_value;
  528. unsigned long cpu_feature;
  529. unsigned long cpu_user_ftr;
  530. } feature_properties[] __initdata = {
  531. #ifdef CONFIG_ALTIVEC
  532. {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  533. {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  534. #endif /* CONFIG_ALTIVEC */
  535. #ifdef CONFIG_PPC64
  536. {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
  537. {"ibm,purr", 1, CPU_FTR_PURR, 0},
  538. {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
  539. #endif /* CONFIG_PPC64 */
  540. };
  541. static void __init check_cpu_feature_properties(unsigned long node)
  542. {
  543. unsigned long i;
  544. struct feature_property *fp = feature_properties;
  545. const u32 *prop;
  546. for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
  547. prop = of_get_flat_dt_prop(node, fp->name, NULL);
  548. if (prop && *prop >= fp->min_value) {
  549. cur_cpu_spec->cpu_features |= fp->cpu_feature;
  550. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
  551. }
  552. }
  553. }
  554. static int __init early_init_dt_scan_cpus(unsigned long node,
  555. const char *uname, int depth,
  556. void *data)
  557. {
  558. static int logical_cpuid = 0;
  559. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  560. const u32 *prop;
  561. const u32 *intserv;
  562. int i, nthreads;
  563. unsigned long len;
  564. int found = 0;
  565. /* We are scanning "cpu" nodes only */
  566. if (type == NULL || strcmp(type, "cpu") != 0)
  567. return 0;
  568. /* Get physical cpuid */
  569. intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
  570. if (intserv) {
  571. nthreads = len / sizeof(int);
  572. } else {
  573. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  574. nthreads = 1;
  575. }
  576. /*
  577. * Now see if any of these threads match our boot cpu.
  578. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  579. */
  580. for (i = 0; i < nthreads; i++) {
  581. /*
  582. * version 2 of the kexec param format adds the phys cpuid of
  583. * booted proc.
  584. */
  585. if (initial_boot_params && initial_boot_params->version >= 2) {
  586. if (intserv[i] ==
  587. initial_boot_params->boot_cpuid_phys) {
  588. found = 1;
  589. break;
  590. }
  591. } else {
  592. /*
  593. * Check if it's the boot-cpu, set it's hw index now,
  594. * unfortunately this format did not support booting
  595. * off secondary threads.
  596. */
  597. if (of_get_flat_dt_prop(node,
  598. "linux,boot-cpu", NULL) != NULL) {
  599. found = 1;
  600. break;
  601. }
  602. }
  603. #ifdef CONFIG_SMP
  604. /* logical cpu id is always 0 on UP kernels */
  605. logical_cpuid++;
  606. #endif
  607. }
  608. if (found) {
  609. DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
  610. intserv[i]);
  611. boot_cpuid = logical_cpuid;
  612. set_hard_smp_processor_id(boot_cpuid, intserv[i]);
  613. /*
  614. * PAPR defines "logical" PVR values for cpus that
  615. * meet various levels of the architecture:
  616. * 0x0f000001 Architecture version 2.04
  617. * 0x0f000002 Architecture version 2.05
  618. * If the cpu-version property in the cpu node contains
  619. * such a value, we call identify_cpu again with the
  620. * logical PVR value in order to use the cpu feature
  621. * bits appropriate for the architecture level.
  622. *
  623. * A POWER6 partition in "POWER6 architected" mode
  624. * uses the 0x0f000002 PVR value; in POWER5+ mode
  625. * it uses 0x0f000001.
  626. */
  627. prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
  628. if (prop && (*prop & 0xff000000) == 0x0f000000)
  629. identify_cpu(0, *prop);
  630. }
  631. check_cpu_feature_properties(node);
  632. check_cpu_pa_features(node);
  633. #ifdef CONFIG_PPC_PSERIES
  634. if (nthreads > 1)
  635. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  636. else
  637. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  638. #endif
  639. return 0;
  640. }
  641. static int __init early_init_dt_scan_chosen(unsigned long node,
  642. const char *uname, int depth, void *data)
  643. {
  644. unsigned long *lprop;
  645. u32 *prop;
  646. unsigned long l;
  647. char *p;
  648. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  649. if (depth != 1 ||
  650. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  651. return 0;
  652. #ifdef CONFIG_PPC64
  653. /* check if iommu is forced on or off */
  654. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  655. iommu_is_off = 1;
  656. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  657. iommu_force_on = 1;
  658. #endif
  659. /* mem=x on the command line is the preferred mechanism */
  660. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  661. if (lprop)
  662. memory_limit = *lprop;
  663. #ifdef CONFIG_PPC64
  664. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  665. if (lprop)
  666. tce_alloc_start = *lprop;
  667. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  668. if (lprop)
  669. tce_alloc_end = *lprop;
  670. #endif
  671. #ifdef CONFIG_KEXEC
  672. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  673. if (lprop)
  674. crashk_res.start = *lprop;
  675. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  676. if (lprop)
  677. crashk_res.end = crashk_res.start + *lprop - 1;
  678. #endif
  679. #ifdef CONFIG_BLK_DEV_INITRD
  680. DBG("Looking for initrd properties... ");
  681. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
  682. if (prop) {
  683. initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
  684. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
  685. if (prop) {
  686. initrd_end = (unsigned long)__va(of_read_ulong(prop, l/4));
  687. initrd_below_start_ok = 1;
  688. } else {
  689. initrd_start = 0;
  690. }
  691. }
  692. DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start, initrd_end);
  693. #endif /* CONFIG_BLK_DEV_INITRD */
  694. /* Retreive command line */
  695. p = of_get_flat_dt_prop(node, "bootargs", &l);
  696. if (p != NULL && l > 0)
  697. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  698. #ifdef CONFIG_CMDLINE
  699. if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
  700. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  701. #endif /* CONFIG_CMDLINE */
  702. DBG("Command line is: %s\n", cmd_line);
  703. /* break now */
  704. return 1;
  705. }
  706. static int __init early_init_dt_scan_root(unsigned long node,
  707. const char *uname, int depth, void *data)
  708. {
  709. u32 *prop;
  710. if (depth != 0)
  711. return 0;
  712. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  713. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  714. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  715. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  716. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  717. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  718. /* break now */
  719. return 1;
  720. }
  721. static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
  722. {
  723. cell_t *p = *cellp;
  724. *cellp = p + s;
  725. return of_read_ulong(p, s);
  726. }
  727. #ifdef CONFIG_PPC_PSERIES
  728. /*
  729. * Interpret the ibm,dynamic-memory property in the
  730. * /ibm,dynamic-reconfiguration-memory node.
  731. * This contains a list of memory blocks along with NUMA affinity
  732. * information.
  733. */
  734. static int __init early_init_dt_scan_drconf_memory(unsigned long node)
  735. {
  736. cell_t *dm, *ls;
  737. unsigned long l, n;
  738. unsigned long base, size, lmb_size, flags;
  739. ls = (cell_t *)of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
  740. if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
  741. return 0;
  742. lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
  743. dm = (cell_t *)of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
  744. if (dm == NULL || l < sizeof(cell_t))
  745. return 0;
  746. n = *dm++; /* number of entries */
  747. if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
  748. return 0;
  749. for (; n != 0; --n) {
  750. base = dt_mem_next_cell(dt_root_addr_cells, &dm);
  751. flags = dm[3];
  752. /* skip DRC index, pad, assoc. list index, flags */
  753. dm += 4;
  754. /* skip this block if the reserved bit is set in flags (0x80)
  755. or if the block is not assigned to this partition (0x8) */
  756. if ((flags & 0x80) || !(flags & 0x8))
  757. continue;
  758. size = lmb_size;
  759. if (iommu_is_off) {
  760. if (base >= 0x80000000ul)
  761. continue;
  762. if ((base + size) > 0x80000000ul)
  763. size = 0x80000000ul - base;
  764. }
  765. lmb_add(base, size);
  766. }
  767. lmb_dump_all();
  768. return 0;
  769. }
  770. #else
  771. #define early_init_dt_scan_drconf_memory(node) 0
  772. #endif /* CONFIG_PPC_PSERIES */
  773. static int __init early_init_dt_scan_memory(unsigned long node,
  774. const char *uname, int depth, void *data)
  775. {
  776. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  777. cell_t *reg, *endp;
  778. unsigned long l;
  779. /* Look for the ibm,dynamic-reconfiguration-memory node */
  780. if (depth == 1 &&
  781. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  782. return early_init_dt_scan_drconf_memory(node);
  783. /* We are scanning "memory" nodes only */
  784. if (type == NULL) {
  785. /*
  786. * The longtrail doesn't have a device_type on the
  787. * /memory node, so look for the node called /memory@0.
  788. */
  789. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  790. return 0;
  791. } else if (strcmp(type, "memory") != 0)
  792. return 0;
  793. reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  794. if (reg == NULL)
  795. reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
  796. if (reg == NULL)
  797. return 0;
  798. endp = reg + (l / sizeof(cell_t));
  799. DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  800. uname, l, reg[0], reg[1], reg[2], reg[3]);
  801. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  802. unsigned long base, size;
  803. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  804. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  805. if (size == 0)
  806. continue;
  807. DBG(" - %lx , %lx\n", base, size);
  808. #ifdef CONFIG_PPC64
  809. if (iommu_is_off) {
  810. if (base >= 0x80000000ul)
  811. continue;
  812. if ((base + size) > 0x80000000ul)
  813. size = 0x80000000ul - base;
  814. }
  815. #endif
  816. lmb_add(base, size);
  817. }
  818. return 0;
  819. }
  820. static void __init early_reserve_mem(void)
  821. {
  822. u64 base, size;
  823. u64 *reserve_map;
  824. unsigned long self_base;
  825. unsigned long self_size;
  826. reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  827. initial_boot_params->off_mem_rsvmap);
  828. /* before we do anything, lets reserve the dt blob */
  829. self_base = __pa((unsigned long)initial_boot_params);
  830. self_size = initial_boot_params->totalsize;
  831. lmb_reserve(self_base, self_size);
  832. #ifdef CONFIG_BLK_DEV_INITRD
  833. /* then reserve the initrd, if any */
  834. if (initrd_start && (initrd_end > initrd_start))
  835. lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
  836. #endif /* CONFIG_BLK_DEV_INITRD */
  837. #ifdef CONFIG_PPC32
  838. /*
  839. * Handle the case where we might be booting from an old kexec
  840. * image that setup the mem_rsvmap as pairs of 32-bit values
  841. */
  842. if (*reserve_map > 0xffffffffull) {
  843. u32 base_32, size_32;
  844. u32 *reserve_map_32 = (u32 *)reserve_map;
  845. while (1) {
  846. base_32 = *(reserve_map_32++);
  847. size_32 = *(reserve_map_32++);
  848. if (size_32 == 0)
  849. break;
  850. /* skip if the reservation is for the blob */
  851. if (base_32 == self_base && size_32 == self_size)
  852. continue;
  853. DBG("reserving: %x -> %x\n", base_32, size_32);
  854. lmb_reserve(base_32, size_32);
  855. }
  856. return;
  857. }
  858. #endif
  859. while (1) {
  860. base = *(reserve_map++);
  861. size = *(reserve_map++);
  862. if (size == 0)
  863. break;
  864. DBG("reserving: %llx -> %llx\n", base, size);
  865. lmb_reserve(base, size);
  866. }
  867. #if 0
  868. DBG("memory reserved, lmbs :\n");
  869. lmb_dump_all();
  870. #endif
  871. }
  872. void __init early_init_devtree(void *params)
  873. {
  874. DBG(" -> early_init_devtree()\n");
  875. /* Setup flat device-tree pointer */
  876. initial_boot_params = params;
  877. #ifdef CONFIG_PPC_RTAS
  878. /* Some machines might need RTAS info for debugging, grab it now. */
  879. of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
  880. #endif
  881. /* Retrieve various informations from the /chosen node of the
  882. * device-tree, including the platform type, initrd location and
  883. * size, TCE reserve, and more ...
  884. */
  885. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  886. /* Scan memory nodes and rebuild LMBs */
  887. lmb_init();
  888. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  889. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  890. /* Save command line for /proc/cmdline and then parse parameters */
  891. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  892. parse_early_param();
  893. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  894. lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  895. reserve_kdump_trampoline();
  896. reserve_crashkernel();
  897. early_reserve_mem();
  898. lmb_enforce_memory_limit(memory_limit);
  899. lmb_analyze();
  900. DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
  901. /* We may need to relocate the flat tree, do it now.
  902. * FIXME .. and the initrd too? */
  903. move_device_tree();
  904. DBG("Scanning CPUs ...\n");
  905. /* Retreive CPU related informations from the flat tree
  906. * (altivec support, boot CPU ID, ...)
  907. */
  908. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  909. DBG(" <- early_init_devtree()\n");
  910. }
  911. #undef printk
  912. int of_n_addr_cells(struct device_node* np)
  913. {
  914. const int *ip;
  915. do {
  916. if (np->parent)
  917. np = np->parent;
  918. ip = of_get_property(np, "#address-cells", NULL);
  919. if (ip != NULL)
  920. return *ip;
  921. } while (np->parent);
  922. /* No #address-cells property for the root node, default to 1 */
  923. return 1;
  924. }
  925. EXPORT_SYMBOL(of_n_addr_cells);
  926. int of_n_size_cells(struct device_node* np)
  927. {
  928. const int* ip;
  929. do {
  930. if (np->parent)
  931. np = np->parent;
  932. ip = of_get_property(np, "#size-cells", NULL);
  933. if (ip != NULL)
  934. return *ip;
  935. } while (np->parent);
  936. /* No #size-cells property for the root node, default to 1 */
  937. return 1;
  938. }
  939. EXPORT_SYMBOL(of_n_size_cells);
  940. /**
  941. * Construct and return a list of the device_nodes with a given name.
  942. */
  943. struct device_node *find_devices(const char *name)
  944. {
  945. struct device_node *head, **prevp, *np;
  946. prevp = &head;
  947. for (np = allnodes; np != 0; np = np->allnext) {
  948. if (np->name != 0 && strcasecmp(np->name, name) == 0) {
  949. *prevp = np;
  950. prevp = &np->next;
  951. }
  952. }
  953. *prevp = NULL;
  954. return head;
  955. }
  956. EXPORT_SYMBOL(find_devices);
  957. /** Checks if the given "compat" string matches one of the strings in
  958. * the device's "compatible" property
  959. */
  960. int of_device_is_compatible(const struct device_node *device,
  961. const char *compat)
  962. {
  963. const char* cp;
  964. int cplen, l;
  965. cp = of_get_property(device, "compatible", &cplen);
  966. if (cp == NULL)
  967. return 0;
  968. while (cplen > 0) {
  969. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  970. return 1;
  971. l = strlen(cp) + 1;
  972. cp += l;
  973. cplen -= l;
  974. }
  975. return 0;
  976. }
  977. EXPORT_SYMBOL(of_device_is_compatible);
  978. /**
  979. * Indicates whether the root node has a given value in its
  980. * compatible property.
  981. */
  982. int machine_is_compatible(const char *compat)
  983. {
  984. struct device_node *root;
  985. int rc = 0;
  986. root = of_find_node_by_path("/");
  987. if (root) {
  988. rc = of_device_is_compatible(root, compat);
  989. of_node_put(root);
  990. }
  991. return rc;
  992. }
  993. EXPORT_SYMBOL(machine_is_compatible);
  994. /*******
  995. *
  996. * New implementation of the OF "find" APIs, return a refcounted
  997. * object, call of_node_put() when done. The device tree and list
  998. * are protected by a rw_lock.
  999. *
  1000. * Note that property management will need some locking as well,
  1001. * this isn't dealt with yet.
  1002. *
  1003. *******/
  1004. /**
  1005. * of_find_node_by_name - Find a node by its "name" property
  1006. * @from: The node to start searching from or NULL, the node
  1007. * you pass will not be searched, only the next one
  1008. * will; typically, you pass what the previous call
  1009. * returned. of_node_put() will be called on it
  1010. * @name: The name string to match against
  1011. *
  1012. * Returns a node pointer with refcount incremented, use
  1013. * of_node_put() on it when done.
  1014. */
  1015. struct device_node *of_find_node_by_name(struct device_node *from,
  1016. const char *name)
  1017. {
  1018. struct device_node *np;
  1019. read_lock(&devtree_lock);
  1020. np = from ? from->allnext : allnodes;
  1021. for (; np != NULL; np = np->allnext)
  1022. if (np->name != NULL && strcasecmp(np->name, name) == 0
  1023. && of_node_get(np))
  1024. break;
  1025. of_node_put(from);
  1026. read_unlock(&devtree_lock);
  1027. return np;
  1028. }
  1029. EXPORT_SYMBOL(of_find_node_by_name);
  1030. /**
  1031. * of_find_node_by_type - Find a node by its "device_type" property
  1032. * @from: The node to start searching from or NULL, the node
  1033. * you pass will not be searched, only the next one
  1034. * will; typically, you pass what the previous call
  1035. * returned. of_node_put() will be called on it
  1036. * @name: The type string to match against
  1037. *
  1038. * Returns a node pointer with refcount incremented, use
  1039. * of_node_put() on it when done.
  1040. */
  1041. struct device_node *of_find_node_by_type(struct device_node *from,
  1042. const char *type)
  1043. {
  1044. struct device_node *np;
  1045. read_lock(&devtree_lock);
  1046. np = from ? from->allnext : allnodes;
  1047. for (; np != 0; np = np->allnext)
  1048. if (np->type != 0 && strcasecmp(np->type, type) == 0
  1049. && of_node_get(np))
  1050. break;
  1051. of_node_put(from);
  1052. read_unlock(&devtree_lock);
  1053. return np;
  1054. }
  1055. EXPORT_SYMBOL(of_find_node_by_type);
  1056. /**
  1057. * of_find_compatible_node - Find a node based on type and one of the
  1058. * tokens in its "compatible" property
  1059. * @from: The node to start searching from or NULL, the node
  1060. * you pass will not be searched, only the next one
  1061. * will; typically, you pass what the previous call
  1062. * returned. of_node_put() will be called on it
  1063. * @type: The type string to match "device_type" or NULL to ignore
  1064. * @compatible: The string to match to one of the tokens in the device
  1065. * "compatible" list.
  1066. *
  1067. * Returns a node pointer with refcount incremented, use
  1068. * of_node_put() on it when done.
  1069. */
  1070. struct device_node *of_find_compatible_node(struct device_node *from,
  1071. const char *type, const char *compatible)
  1072. {
  1073. struct device_node *np;
  1074. read_lock(&devtree_lock);
  1075. np = from ? from->allnext : allnodes;
  1076. for (; np != 0; np = np->allnext) {
  1077. if (type != NULL
  1078. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1079. continue;
  1080. if (of_device_is_compatible(np, compatible) && of_node_get(np))
  1081. break;
  1082. }
  1083. of_node_put(from);
  1084. read_unlock(&devtree_lock);
  1085. return np;
  1086. }
  1087. EXPORT_SYMBOL(of_find_compatible_node);
  1088. /**
  1089. * of_find_node_by_path - Find a node matching a full OF path
  1090. * @path: The full path to match
  1091. *
  1092. * Returns a node pointer with refcount incremented, use
  1093. * of_node_put() on it when done.
  1094. */
  1095. struct device_node *of_find_node_by_path(const char *path)
  1096. {
  1097. struct device_node *np = allnodes;
  1098. read_lock(&devtree_lock);
  1099. for (; np != 0; np = np->allnext) {
  1100. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
  1101. && of_node_get(np))
  1102. break;
  1103. }
  1104. read_unlock(&devtree_lock);
  1105. return np;
  1106. }
  1107. EXPORT_SYMBOL(of_find_node_by_path);
  1108. /**
  1109. * of_find_node_by_phandle - Find a node given a phandle
  1110. * @handle: phandle of the node to find
  1111. *
  1112. * Returns a node pointer with refcount incremented, use
  1113. * of_node_put() on it when done.
  1114. */
  1115. struct device_node *of_find_node_by_phandle(phandle handle)
  1116. {
  1117. struct device_node *np;
  1118. read_lock(&devtree_lock);
  1119. for (np = allnodes; np != 0; np = np->allnext)
  1120. if (np->linux_phandle == handle)
  1121. break;
  1122. of_node_get(np);
  1123. read_unlock(&devtree_lock);
  1124. return np;
  1125. }
  1126. EXPORT_SYMBOL(of_find_node_by_phandle);
  1127. /**
  1128. * of_find_all_nodes - Get next node in global list
  1129. * @prev: Previous node or NULL to start iteration
  1130. * of_node_put() will be called on it
  1131. *
  1132. * Returns a node pointer with refcount incremented, use
  1133. * of_node_put() on it when done.
  1134. */
  1135. struct device_node *of_find_all_nodes(struct device_node *prev)
  1136. {
  1137. struct device_node *np;
  1138. read_lock(&devtree_lock);
  1139. np = prev ? prev->allnext : allnodes;
  1140. for (; np != 0; np = np->allnext)
  1141. if (of_node_get(np))
  1142. break;
  1143. of_node_put(prev);
  1144. read_unlock(&devtree_lock);
  1145. return np;
  1146. }
  1147. EXPORT_SYMBOL(of_find_all_nodes);
  1148. /**
  1149. * of_get_parent - Get a node's parent if any
  1150. * @node: Node to get parent
  1151. *
  1152. * Returns a node pointer with refcount incremented, use
  1153. * of_node_put() on it when done.
  1154. */
  1155. struct device_node *of_get_parent(const struct device_node *node)
  1156. {
  1157. struct device_node *np;
  1158. if (!node)
  1159. return NULL;
  1160. read_lock(&devtree_lock);
  1161. np = of_node_get(node->parent);
  1162. read_unlock(&devtree_lock);
  1163. return np;
  1164. }
  1165. EXPORT_SYMBOL(of_get_parent);
  1166. /**
  1167. * of_get_next_child - Iterate a node childs
  1168. * @node: parent node
  1169. * @prev: previous child of the parent node, or NULL to get first
  1170. *
  1171. * Returns a node pointer with refcount incremented, use
  1172. * of_node_put() on it when done.
  1173. */
  1174. struct device_node *of_get_next_child(const struct device_node *node,
  1175. struct device_node *prev)
  1176. {
  1177. struct device_node *next;
  1178. read_lock(&devtree_lock);
  1179. next = prev ? prev->sibling : node->child;
  1180. for (; next != 0; next = next->sibling)
  1181. if (of_node_get(next))
  1182. break;
  1183. of_node_put(prev);
  1184. read_unlock(&devtree_lock);
  1185. return next;
  1186. }
  1187. EXPORT_SYMBOL(of_get_next_child);
  1188. /**
  1189. * of_node_get - Increment refcount of a node
  1190. * @node: Node to inc refcount, NULL is supported to
  1191. * simplify writing of callers
  1192. *
  1193. * Returns node.
  1194. */
  1195. struct device_node *of_node_get(struct device_node *node)
  1196. {
  1197. if (node)
  1198. kref_get(&node->kref);
  1199. return node;
  1200. }
  1201. EXPORT_SYMBOL(of_node_get);
  1202. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1203. {
  1204. return container_of(kref, struct device_node, kref);
  1205. }
  1206. /**
  1207. * of_node_release - release a dynamically allocated node
  1208. * @kref: kref element of the node to be released
  1209. *
  1210. * In of_node_put() this function is passed to kref_put()
  1211. * as the destructor.
  1212. */
  1213. static void of_node_release(struct kref *kref)
  1214. {
  1215. struct device_node *node = kref_to_device_node(kref);
  1216. struct property *prop = node->properties;
  1217. if (!OF_IS_DYNAMIC(node))
  1218. return;
  1219. while (prop) {
  1220. struct property *next = prop->next;
  1221. kfree(prop->name);
  1222. kfree(prop->value);
  1223. kfree(prop);
  1224. prop = next;
  1225. if (!prop) {
  1226. prop = node->deadprops;
  1227. node->deadprops = NULL;
  1228. }
  1229. }
  1230. kfree(node->full_name);
  1231. kfree(node->data);
  1232. kfree(node);
  1233. }
  1234. /**
  1235. * of_node_put - Decrement refcount of a node
  1236. * @node: Node to dec refcount, NULL is supported to
  1237. * simplify writing of callers
  1238. *
  1239. */
  1240. void of_node_put(struct device_node *node)
  1241. {
  1242. if (node)
  1243. kref_put(&node->kref, of_node_release);
  1244. }
  1245. EXPORT_SYMBOL(of_node_put);
  1246. /*
  1247. * Plug a device node into the tree and global list.
  1248. */
  1249. void of_attach_node(struct device_node *np)
  1250. {
  1251. write_lock(&devtree_lock);
  1252. np->sibling = np->parent->child;
  1253. np->allnext = allnodes;
  1254. np->parent->child = np;
  1255. allnodes = np;
  1256. write_unlock(&devtree_lock);
  1257. }
  1258. /*
  1259. * "Unplug" a node from the device tree. The caller must hold
  1260. * a reference to the node. The memory associated with the node
  1261. * is not freed until its refcount goes to zero.
  1262. */
  1263. void of_detach_node(const struct device_node *np)
  1264. {
  1265. struct device_node *parent;
  1266. write_lock(&devtree_lock);
  1267. parent = np->parent;
  1268. if (allnodes == np)
  1269. allnodes = np->allnext;
  1270. else {
  1271. struct device_node *prev;
  1272. for (prev = allnodes;
  1273. prev->allnext != np;
  1274. prev = prev->allnext)
  1275. ;
  1276. prev->allnext = np->allnext;
  1277. }
  1278. if (parent->child == np)
  1279. parent->child = np->sibling;
  1280. else {
  1281. struct device_node *prevsib;
  1282. for (prevsib = np->parent->child;
  1283. prevsib->sibling != np;
  1284. prevsib = prevsib->sibling)
  1285. ;
  1286. prevsib->sibling = np->sibling;
  1287. }
  1288. write_unlock(&devtree_lock);
  1289. }
  1290. #ifdef CONFIG_PPC_PSERIES
  1291. /*
  1292. * Fix up the uninitialized fields in a new device node:
  1293. * name, type and pci-specific fields
  1294. */
  1295. static int of_finish_dynamic_node(struct device_node *node)
  1296. {
  1297. struct device_node *parent = of_get_parent(node);
  1298. int err = 0;
  1299. const phandle *ibm_phandle;
  1300. node->name = of_get_property(node, "name", NULL);
  1301. node->type = of_get_property(node, "device_type", NULL);
  1302. if (!parent) {
  1303. err = -ENODEV;
  1304. goto out;
  1305. }
  1306. /* We don't support that function on PowerMac, at least
  1307. * not yet
  1308. */
  1309. if (machine_is(powermac))
  1310. return -ENODEV;
  1311. /* fix up new node's linux_phandle field */
  1312. if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
  1313. node->linux_phandle = *ibm_phandle;
  1314. out:
  1315. of_node_put(parent);
  1316. return err;
  1317. }
  1318. static int prom_reconfig_notifier(struct notifier_block *nb,
  1319. unsigned long action, void *node)
  1320. {
  1321. int err;
  1322. switch (action) {
  1323. case PSERIES_RECONFIG_ADD:
  1324. err = of_finish_dynamic_node(node);
  1325. if (err < 0) {
  1326. printk(KERN_ERR "finish_node returned %d\n", err);
  1327. err = NOTIFY_BAD;
  1328. }
  1329. break;
  1330. default:
  1331. err = NOTIFY_DONE;
  1332. break;
  1333. }
  1334. return err;
  1335. }
  1336. static struct notifier_block prom_reconfig_nb = {
  1337. .notifier_call = prom_reconfig_notifier,
  1338. .priority = 10, /* This one needs to run first */
  1339. };
  1340. static int __init prom_reconfig_setup(void)
  1341. {
  1342. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1343. }
  1344. __initcall(prom_reconfig_setup);
  1345. #endif
  1346. struct property *of_find_property(const struct device_node *np,
  1347. const char *name,
  1348. int *lenp)
  1349. {
  1350. struct property *pp;
  1351. read_lock(&devtree_lock);
  1352. for (pp = np->properties; pp != 0; pp = pp->next)
  1353. if (strcmp(pp->name, name) == 0) {
  1354. if (lenp != 0)
  1355. *lenp = pp->length;
  1356. break;
  1357. }
  1358. read_unlock(&devtree_lock);
  1359. return pp;
  1360. }
  1361. EXPORT_SYMBOL(of_find_property);
  1362. /*
  1363. * Find a property with a given name for a given node
  1364. * and return the value.
  1365. */
  1366. const void *of_get_property(const struct device_node *np, const char *name,
  1367. int *lenp)
  1368. {
  1369. struct property *pp = of_find_property(np,name,lenp);
  1370. return pp ? pp->value : NULL;
  1371. }
  1372. EXPORT_SYMBOL(of_get_property);
  1373. /*
  1374. * Add a property to a node
  1375. */
  1376. int prom_add_property(struct device_node* np, struct property* prop)
  1377. {
  1378. struct property **next;
  1379. prop->next = NULL;
  1380. write_lock(&devtree_lock);
  1381. next = &np->properties;
  1382. while (*next) {
  1383. if (strcmp(prop->name, (*next)->name) == 0) {
  1384. /* duplicate ! don't insert it */
  1385. write_unlock(&devtree_lock);
  1386. return -1;
  1387. }
  1388. next = &(*next)->next;
  1389. }
  1390. *next = prop;
  1391. write_unlock(&devtree_lock);
  1392. #ifdef CONFIG_PROC_DEVICETREE
  1393. /* try to add to proc as well if it was initialized */
  1394. if (np->pde)
  1395. proc_device_tree_add_prop(np->pde, prop);
  1396. #endif /* CONFIG_PROC_DEVICETREE */
  1397. return 0;
  1398. }
  1399. /*
  1400. * Remove a property from a node. Note that we don't actually
  1401. * remove it, since we have given out who-knows-how-many pointers
  1402. * to the data using get-property. Instead we just move the property
  1403. * to the "dead properties" list, so it won't be found any more.
  1404. */
  1405. int prom_remove_property(struct device_node *np, struct property *prop)
  1406. {
  1407. struct property **next;
  1408. int found = 0;
  1409. write_lock(&devtree_lock);
  1410. next = &np->properties;
  1411. while (*next) {
  1412. if (*next == prop) {
  1413. /* found the node */
  1414. *next = prop->next;
  1415. prop->next = np->deadprops;
  1416. np->deadprops = prop;
  1417. found = 1;
  1418. break;
  1419. }
  1420. next = &(*next)->next;
  1421. }
  1422. write_unlock(&devtree_lock);
  1423. if (!found)
  1424. return -ENODEV;
  1425. #ifdef CONFIG_PROC_DEVICETREE
  1426. /* try to remove the proc node as well */
  1427. if (np->pde)
  1428. proc_device_tree_remove_prop(np->pde, prop);
  1429. #endif /* CONFIG_PROC_DEVICETREE */
  1430. return 0;
  1431. }
  1432. /*
  1433. * Update a property in a node. Note that we don't actually
  1434. * remove it, since we have given out who-knows-how-many pointers
  1435. * to the data using get-property. Instead we just move the property
  1436. * to the "dead properties" list, and add the new property to the
  1437. * property list
  1438. */
  1439. int prom_update_property(struct device_node *np,
  1440. struct property *newprop,
  1441. struct property *oldprop)
  1442. {
  1443. struct property **next;
  1444. int found = 0;
  1445. write_lock(&devtree_lock);
  1446. next = &np->properties;
  1447. while (*next) {
  1448. if (*next == oldprop) {
  1449. /* found the node */
  1450. newprop->next = oldprop->next;
  1451. *next = newprop;
  1452. oldprop->next = np->deadprops;
  1453. np->deadprops = oldprop;
  1454. found = 1;
  1455. break;
  1456. }
  1457. next = &(*next)->next;
  1458. }
  1459. write_unlock(&devtree_lock);
  1460. if (!found)
  1461. return -ENODEV;
  1462. #ifdef CONFIG_PROC_DEVICETREE
  1463. /* try to add to proc as well if it was initialized */
  1464. if (np->pde)
  1465. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1466. #endif /* CONFIG_PROC_DEVICETREE */
  1467. return 0;
  1468. }
  1469. /* Find the device node for a given logical cpu number, also returns the cpu
  1470. * local thread number (index in ibm,interrupt-server#s) if relevant and
  1471. * asked for (non NULL)
  1472. */
  1473. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  1474. {
  1475. int hardid;
  1476. struct device_node *np;
  1477. hardid = get_hard_smp_processor_id(cpu);
  1478. for_each_node_by_type(np, "cpu") {
  1479. const u32 *intserv;
  1480. unsigned int plen, t;
  1481. /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
  1482. * fallback to "reg" property and assume no threads
  1483. */
  1484. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
  1485. &plen);
  1486. if (intserv == NULL) {
  1487. const u32 *reg = of_get_property(np, "reg", NULL);
  1488. if (reg == NULL)
  1489. continue;
  1490. if (*reg == hardid) {
  1491. if (thread)
  1492. *thread = 0;
  1493. return np;
  1494. }
  1495. } else {
  1496. plen /= sizeof(u32);
  1497. for (t = 0; t < plen; t++) {
  1498. if (hardid == intserv[t]) {
  1499. if (thread)
  1500. *thread = t;
  1501. return np;
  1502. }
  1503. }
  1504. }
  1505. }
  1506. return NULL;
  1507. }
  1508. EXPORT_SYMBOL(of_get_cpu_node);
  1509. #ifdef DEBUG
  1510. static struct debugfs_blob_wrapper flat_dt_blob;
  1511. static int __init export_flat_device_tree(void)
  1512. {
  1513. struct dentry *d;
  1514. d = debugfs_create_dir("powerpc", NULL);
  1515. if (!d)
  1516. return 1;
  1517. flat_dt_blob.data = initial_boot_params;
  1518. flat_dt_blob.size = initial_boot_params->totalsize;
  1519. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  1520. d, &flat_dt_blob);
  1521. if (!d)
  1522. return 1;
  1523. return 0;
  1524. }
  1525. __initcall(export_flat_device_tree);
  1526. #endif