prom.c 41 KB

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