prom.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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. #include <stdarg.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <linux/threads.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/types.h>
  22. #include <linux/pci.h>
  23. #include <linux/stringify.h>
  24. #include <linux/delay.h>
  25. #include <linux/initrd.h>
  26. #include <linux/bitops.h>
  27. #include <linux/module.h>
  28. #include <linux/kexec.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/irq.h>
  31. #include <linux/lmb.h>
  32. #include <asm/prom.h>
  33. #include <asm/page.h>
  34. #include <asm/processor.h>
  35. #include <asm/irq.h>
  36. #include <linux/io.h>
  37. #include <asm/system.h>
  38. #include <asm/mmu.h>
  39. #include <asm/pgtable.h>
  40. #include <asm/sections.h>
  41. #include <asm/pci-bridge.h>
  42. static int __initdata dt_root_addr_cells;
  43. static int __initdata dt_root_size_cells;
  44. typedef u32 cell_t;
  45. static struct boot_param_header *initial_boot_params;
  46. /* export that to outside world */
  47. struct device_node *of_chosen;
  48. static inline char *find_flat_dt_string(u32 offset)
  49. {
  50. return ((char *)initial_boot_params) +
  51. initial_boot_params->off_dt_strings + offset;
  52. }
  53. /**
  54. * This function is used to scan the flattened device-tree, it is
  55. * used to extract the memory informations at boot before we can
  56. * unflatten the tree
  57. */
  58. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  59. const char *uname, int depth,
  60. void *data),
  61. void *data)
  62. {
  63. unsigned long p = ((unsigned long)initial_boot_params) +
  64. initial_boot_params->off_dt_struct;
  65. int rc = 0;
  66. int depth = -1;
  67. do {
  68. u32 tag = *((u32 *)p);
  69. char *pathp;
  70. p += 4;
  71. if (tag == OF_DT_END_NODE) {
  72. depth--;
  73. continue;
  74. }
  75. if (tag == OF_DT_NOP)
  76. continue;
  77. if (tag == OF_DT_END)
  78. break;
  79. if (tag == OF_DT_PROP) {
  80. u32 sz = *((u32 *)p);
  81. p += 8;
  82. if (initial_boot_params->version < 0x10)
  83. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  84. p += sz;
  85. p = _ALIGN(p, 4);
  86. continue;
  87. }
  88. if (tag != OF_DT_BEGIN_NODE) {
  89. printk(KERN_WARNING "Invalid tag %x scanning flattened"
  90. " device tree !\n", tag);
  91. return -EINVAL;
  92. }
  93. depth++;
  94. pathp = (char *)p;
  95. p = _ALIGN(p + strlen(pathp) + 1, 4);
  96. if ((*pathp) == '/') {
  97. char *lp, *np;
  98. for (lp = NULL, np = pathp; *np; np++)
  99. if ((*np) == '/')
  100. lp = np+1;
  101. if (lp != NULL)
  102. pathp = lp;
  103. }
  104. rc = it(p, pathp, depth, data);
  105. if (rc != 0)
  106. break;
  107. } while (1);
  108. return rc;
  109. }
  110. unsigned long __init of_get_flat_dt_root(void)
  111. {
  112. unsigned long p = ((unsigned long)initial_boot_params) +
  113. initial_boot_params->off_dt_struct;
  114. while (*((u32 *)p) == OF_DT_NOP)
  115. p += 4;
  116. BUG_ON(*((u32 *)p) != OF_DT_BEGIN_NODE);
  117. p += 4;
  118. return _ALIGN(p + strlen((char *)p) + 1, 4);
  119. }
  120. /**
  121. * This function can be used within scan_flattened_dt callback to get
  122. * access to properties
  123. */
  124. void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
  125. unsigned long *size)
  126. {
  127. unsigned long p = node;
  128. do {
  129. u32 tag = *((u32 *)p);
  130. u32 sz, noff;
  131. const char *nstr;
  132. p += 4;
  133. if (tag == OF_DT_NOP)
  134. continue;
  135. if (tag != OF_DT_PROP)
  136. return NULL;
  137. sz = *((u32 *)p);
  138. noff = *((u32 *)(p + 4));
  139. p += 8;
  140. if (initial_boot_params->version < 0x10)
  141. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  142. nstr = find_flat_dt_string(noff);
  143. if (nstr == NULL) {
  144. printk(KERN_WARNING "Can't find property index"
  145. " name !\n");
  146. return NULL;
  147. }
  148. if (strcmp(name, nstr) == 0) {
  149. if (size)
  150. *size = sz;
  151. return (void *)p;
  152. }
  153. p += sz;
  154. p = _ALIGN(p, 4);
  155. } while (1);
  156. }
  157. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  158. {
  159. const char *cp;
  160. unsigned long cplen, l;
  161. cp = of_get_flat_dt_prop(node, "compatible", &cplen);
  162. if (cp == NULL)
  163. return 0;
  164. while (cplen > 0) {
  165. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  166. return 1;
  167. l = strlen(cp) + 1;
  168. cp += l;
  169. cplen -= l;
  170. }
  171. return 0;
  172. }
  173. static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
  174. unsigned long align)
  175. {
  176. void *res;
  177. *mem = _ALIGN(*mem, align);
  178. res = (void *)*mem;
  179. *mem += size;
  180. return res;
  181. }
  182. static unsigned long __init unflatten_dt_node(unsigned long mem,
  183. unsigned long *p,
  184. struct device_node *dad,
  185. struct device_node ***allnextpp,
  186. unsigned long fpsize)
  187. {
  188. struct device_node *np;
  189. struct property *pp, **prev_pp = NULL;
  190. char *pathp;
  191. u32 tag;
  192. unsigned int l, allocl;
  193. int has_name = 0;
  194. int new_format = 0;
  195. tag = *((u32 *)(*p));
  196. if (tag != OF_DT_BEGIN_NODE) {
  197. printk("Weird tag at start of node: %x\n", tag);
  198. return mem;
  199. }
  200. *p += 4;
  201. pathp = (char *)*p;
  202. l = allocl = strlen(pathp) + 1;
  203. *p = _ALIGN(*p + l, 4);
  204. /* version 0x10 has a more compact unit name here instead of the full
  205. * path. we accumulate the full path size using "fpsize", we'll rebuild
  206. * it later. We detect this because the first character of the name is
  207. * not '/'.
  208. */
  209. if ((*pathp) != '/') {
  210. new_format = 1;
  211. if (fpsize == 0) {
  212. /* root node: special case. fpsize accounts for path
  213. * plus terminating zero. root node only has '/', so
  214. * fpsize should be 2, but we want to avoid the first
  215. * level nodes to have two '/' so we use fpsize 1 here
  216. */
  217. fpsize = 1;
  218. allocl = 2;
  219. } else {
  220. /* account for '/' and path size minus terminal 0
  221. * already in 'l'
  222. */
  223. fpsize += l;
  224. allocl = fpsize;
  225. }
  226. }
  227. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  228. __alignof__(struct device_node));
  229. if (allnextpp) {
  230. memset(np, 0, sizeof(*np));
  231. np->full_name = ((char *)np) + sizeof(struct device_node);
  232. if (new_format) {
  233. char *p2 = np->full_name;
  234. /* rebuild full path for new format */
  235. if (dad && dad->parent) {
  236. strcpy(p2, dad->full_name);
  237. #ifdef DEBUG
  238. if ((strlen(p2) + l + 1) != allocl) {
  239. pr_debug("%s: p: %d, l: %d, a: %d\n",
  240. pathp, (int)strlen(p2),
  241. l, allocl);
  242. }
  243. #endif
  244. p2 += strlen(p2);
  245. }
  246. *(p2++) = '/';
  247. memcpy(p2, pathp, l);
  248. } else
  249. memcpy(np->full_name, pathp, l);
  250. prev_pp = &np->properties;
  251. **allnextpp = np;
  252. *allnextpp = &np->allnext;
  253. if (dad != NULL) {
  254. np->parent = dad;
  255. /* we temporarily use the next field as `last_child'*/
  256. if (dad->next == NULL)
  257. dad->child = np;
  258. else
  259. dad->next->sibling = np;
  260. dad->next = np;
  261. }
  262. kref_init(&np->kref);
  263. }
  264. while (1) {
  265. u32 sz, noff;
  266. char *pname;
  267. tag = *((u32 *)(*p));
  268. if (tag == OF_DT_NOP) {
  269. *p += 4;
  270. continue;
  271. }
  272. if (tag != OF_DT_PROP)
  273. break;
  274. *p += 4;
  275. sz = *((u32 *)(*p));
  276. noff = *((u32 *)((*p) + 4));
  277. *p += 8;
  278. if (initial_boot_params->version < 0x10)
  279. *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
  280. pname = find_flat_dt_string(noff);
  281. if (pname == NULL) {
  282. printk(KERN_INFO
  283. "Can't find property name in list !\n");
  284. break;
  285. }
  286. if (strcmp(pname, "name") == 0)
  287. has_name = 1;
  288. l = strlen(pname) + 1;
  289. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  290. __alignof__(struct property));
  291. if (allnextpp) {
  292. if (strcmp(pname, "linux,phandle") == 0) {
  293. np->node = *((u32 *)*p);
  294. if (np->linux_phandle == 0)
  295. np->linux_phandle = np->node;
  296. }
  297. if (strcmp(pname, "ibm,phandle") == 0)
  298. np->linux_phandle = *((u32 *)*p);
  299. pp->name = pname;
  300. pp->length = sz;
  301. pp->value = (void *)*p;
  302. *prev_pp = pp;
  303. prev_pp = &pp->next;
  304. }
  305. *p = _ALIGN((*p) + sz, 4);
  306. }
  307. /* with version 0x10 we may not have the name property, recreate
  308. * it here from the unit name if absent
  309. */
  310. if (!has_name) {
  311. char *p1 = pathp, *ps = pathp, *pa = NULL;
  312. int sz;
  313. while (*p1) {
  314. if ((*p1) == '@')
  315. pa = p1;
  316. if ((*p1) == '/')
  317. ps = p1 + 1;
  318. p1++;
  319. }
  320. if (pa < ps)
  321. pa = p1;
  322. sz = (pa - ps) + 1;
  323. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  324. __alignof__(struct property));
  325. if (allnextpp) {
  326. pp->name = "name";
  327. pp->length = sz;
  328. pp->value = pp + 1;
  329. *prev_pp = pp;
  330. prev_pp = &pp->next;
  331. memcpy(pp->value, ps, sz - 1);
  332. ((char *)pp->value)[sz - 1] = 0;
  333. pr_debug("fixed up name for %s -> %s\n", pathp,
  334. (char *)pp->value);
  335. }
  336. }
  337. if (allnextpp) {
  338. *prev_pp = NULL;
  339. np->name = of_get_property(np, "name", NULL);
  340. np->type = of_get_property(np, "device_type", NULL);
  341. if (!np->name)
  342. np->name = "<NULL>";
  343. if (!np->type)
  344. np->type = "<NULL>";
  345. }
  346. while (tag == OF_DT_BEGIN_NODE) {
  347. mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
  348. tag = *((u32 *)(*p));
  349. }
  350. if (tag != OF_DT_END_NODE) {
  351. printk(KERN_INFO "Weird tag at end of node: %x\n", tag);
  352. return mem;
  353. }
  354. *p += 4;
  355. return mem;
  356. }
  357. /**
  358. * unflattens the device-tree passed by the firmware, creating the
  359. * tree of struct device_node. It also fills the "name" and "type"
  360. * pointers of the nodes so the normal device-tree walking functions
  361. * can be used (this used to be done by finish_device_tree)
  362. */
  363. void __init unflatten_device_tree(void)
  364. {
  365. unsigned long start, mem, size;
  366. struct device_node **allnextp = &allnodes;
  367. pr_debug(" -> unflatten_device_tree()\n");
  368. /* First pass, scan for size */
  369. start = ((unsigned long)initial_boot_params) +
  370. initial_boot_params->off_dt_struct;
  371. size = unflatten_dt_node(0, &start, NULL, NULL, 0);
  372. size = (size | 3) + 1;
  373. pr_debug(" size is %lx, allocating...\n", size);
  374. /* Allocate memory for the expanded device tree */
  375. mem = lmb_alloc(size + 4, __alignof__(struct device_node));
  376. mem = (unsigned long) __va(mem);
  377. ((u32 *)mem)[size / 4] = 0xdeadbeef;
  378. pr_debug(" unflattening %lx...\n", mem);
  379. /* Second pass, do actual unflattening */
  380. start = ((unsigned long)initial_boot_params) +
  381. initial_boot_params->off_dt_struct;
  382. unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
  383. if (*((u32 *)start) != OF_DT_END)
  384. printk(KERN_WARNING "Weird tag at end of tree: %08x\n",
  385. *((u32 *)start));
  386. if (((u32 *)mem)[size / 4] != 0xdeadbeef)
  387. printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
  388. ((u32 *)mem)[size / 4]);
  389. *allnextp = NULL;
  390. /* Get pointer to OF "/chosen" node for use everywhere */
  391. of_chosen = of_find_node_by_path("/chosen");
  392. if (of_chosen == NULL)
  393. of_chosen = of_find_node_by_path("/chosen@0");
  394. pr_debug(" <- unflatten_device_tree()\n");
  395. }
  396. #define early_init_dt_scan_drconf_memory(node) 0
  397. static int __init early_init_dt_scan_cpus(unsigned long node,
  398. const char *uname, int depth,
  399. void *data)
  400. {
  401. static int logical_cpuid;
  402. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  403. const u32 *intserv;
  404. int i, nthreads;
  405. int found = 0;
  406. /* We are scanning "cpu" nodes only */
  407. if (type == NULL || strcmp(type, "cpu") != 0)
  408. return 0;
  409. /* Get physical cpuid */
  410. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  411. nthreads = 1;
  412. /*
  413. * Now see if any of these threads match our boot cpu.
  414. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  415. */
  416. for (i = 0; i < nthreads; i++) {
  417. /*
  418. * version 2 of the kexec param format adds the phys cpuid of
  419. * booted proc.
  420. */
  421. if (initial_boot_params && initial_boot_params->version >= 2) {
  422. if (intserv[i] ==
  423. initial_boot_params->boot_cpuid_phys) {
  424. found = 1;
  425. break;
  426. }
  427. } else {
  428. /*
  429. * Check if it's the boot-cpu, set it's hw index now,
  430. * unfortunately this format did not support booting
  431. * off secondary threads.
  432. */
  433. if (of_get_flat_dt_prop(node,
  434. "linux,boot-cpu", NULL) != NULL) {
  435. found = 1;
  436. break;
  437. }
  438. }
  439. #ifdef CONFIG_SMP
  440. /* logical cpu id is always 0 on UP kernels */
  441. logical_cpuid++;
  442. #endif
  443. }
  444. if (found) {
  445. pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid,
  446. intserv[i]);
  447. boot_cpuid = logical_cpuid;
  448. }
  449. return 0;
  450. }
  451. #ifdef CONFIG_BLK_DEV_INITRD
  452. static void __init early_init_dt_check_for_initrd(unsigned long node)
  453. {
  454. unsigned long l;
  455. u32 *prop;
  456. pr_debug("Looking for initrd properties... ");
  457. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
  458. if (prop) {
  459. initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
  460. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
  461. if (prop) {
  462. initrd_end = (unsigned long)
  463. __va(of_read_ulong(prop, l/4));
  464. initrd_below_start_ok = 1;
  465. } else {
  466. initrd_start = 0;
  467. }
  468. }
  469. pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
  470. initrd_start, initrd_end);
  471. }
  472. #else
  473. static inline void early_init_dt_check_for_initrd(unsigned long node)
  474. {
  475. }
  476. #endif /* CONFIG_BLK_DEV_INITRD */
  477. static int __init early_init_dt_scan_chosen(unsigned long node,
  478. const char *uname, int depth, void *data)
  479. {
  480. unsigned long l;
  481. char *p;
  482. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  483. if (depth != 1 ||
  484. (strcmp(uname, "chosen") != 0 &&
  485. strcmp(uname, "chosen@0") != 0))
  486. return 0;
  487. #ifdef CONFIG_KEXEC
  488. lprop = (u64 *)of_get_flat_dt_prop(node,
  489. "linux,crashkernel-base", NULL);
  490. if (lprop)
  491. crashk_res.start = *lprop;
  492. lprop = (u64 *)of_get_flat_dt_prop(node,
  493. "linux,crashkernel-size", NULL);
  494. if (lprop)
  495. crashk_res.end = crashk_res.start + *lprop - 1;
  496. #endif
  497. early_init_dt_check_for_initrd(node);
  498. /* Retreive command line */
  499. p = of_get_flat_dt_prop(node, "bootargs", &l);
  500. if (p != NULL && l > 0)
  501. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  502. #ifdef CONFIG_CMDLINE
  503. #ifndef CONFIG_CMDLINE_FORCE
  504. if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
  505. #endif
  506. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  507. #endif /* CONFIG_CMDLINE */
  508. pr_debug("Command line is: %s\n", cmd_line);
  509. /* break now */
  510. return 1;
  511. }
  512. static int __init early_init_dt_scan_root(unsigned long node,
  513. const char *uname, int depth, void *data)
  514. {
  515. u32 *prop;
  516. if (depth != 0)
  517. return 0;
  518. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  519. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  520. pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
  521. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  522. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  523. pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  524. /* break now */
  525. return 1;
  526. }
  527. static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
  528. {
  529. cell_t *p = *cellp;
  530. *cellp = p + s;
  531. return of_read_number(p, s);
  532. }
  533. static int __init early_init_dt_scan_memory(unsigned long node,
  534. const char *uname, int depth, void *data)
  535. {
  536. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  537. cell_t *reg, *endp;
  538. unsigned long l;
  539. /* Look for the ibm,dynamic-reconfiguration-memory node */
  540. /* if (depth == 1 &&
  541. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  542. return early_init_dt_scan_drconf_memory(node);
  543. */
  544. /* We are scanning "memory" nodes only */
  545. if (type == NULL) {
  546. /*
  547. * The longtrail doesn't have a device_type on the
  548. * /memory node, so look for the node called /memory@0.
  549. */
  550. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  551. return 0;
  552. } else if (strcmp(type, "memory") != 0)
  553. return 0;
  554. reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  555. if (reg == NULL)
  556. reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
  557. if (reg == NULL)
  558. return 0;
  559. endp = reg + (l / sizeof(cell_t));
  560. pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  561. uname, l, reg[0], reg[1], reg[2], reg[3]);
  562. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  563. u64 base, size;
  564. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  565. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  566. if (size == 0)
  567. continue;
  568. pr_debug(" - %llx , %llx\n", (unsigned long long)base,
  569. (unsigned long long)size);
  570. lmb_add(base, size);
  571. }
  572. return 0;
  573. }
  574. #ifdef CONFIG_PHYP_DUMP
  575. /**
  576. * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
  577. *
  578. * Function to find the largest size we need to reserve
  579. * during early boot process.
  580. *
  581. * It either looks for boot param and returns that OR
  582. * returns larger of 256 or 5% rounded down to multiples of 256MB.
  583. *
  584. */
  585. static inline unsigned long phyp_dump_calculate_reserve_size(void)
  586. {
  587. unsigned long tmp;
  588. if (phyp_dump_info->reserve_bootvar)
  589. return phyp_dump_info->reserve_bootvar;
  590. /* divide by 20 to get 5% of value */
  591. tmp = lmb_end_of_DRAM();
  592. do_div(tmp, 20);
  593. /* round it down in multiples of 256 */
  594. tmp = tmp & ~0x0FFFFFFFUL;
  595. return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
  596. }
  597. /**
  598. * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
  599. *
  600. * This routine may reserve memory regions in the kernel only
  601. * if the system is supported and a dump was taken in last
  602. * boot instance or if the hardware is supported and the
  603. * scratch area needs to be setup. In other instances it returns
  604. * without reserving anything. The memory in case of dump being
  605. * active is freed when the dump is collected (by userland tools).
  606. */
  607. static void __init phyp_dump_reserve_mem(void)
  608. {
  609. unsigned long base, size;
  610. unsigned long variable_reserve_size;
  611. if (!phyp_dump_info->phyp_dump_configured) {
  612. printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
  613. return;
  614. }
  615. if (!phyp_dump_info->phyp_dump_at_boot) {
  616. printk(KERN_INFO "Phyp-dump disabled at boot time\n");
  617. return;
  618. }
  619. variable_reserve_size = phyp_dump_calculate_reserve_size();
  620. if (phyp_dump_info->phyp_dump_is_active) {
  621. /* Reserve *everything* above RMR.Area freed by userland tools*/
  622. base = variable_reserve_size;
  623. size = lmb_end_of_DRAM() - base;
  624. /* XXX crashed_ram_end is wrong, since it may be beyond
  625. * the memory_limit, it will need to be adjusted. */
  626. lmb_reserve(base, size);
  627. phyp_dump_info->init_reserve_start = base;
  628. phyp_dump_info->init_reserve_size = size;
  629. } else {
  630. size = phyp_dump_info->cpu_state_size +
  631. phyp_dump_info->hpte_region_size +
  632. variable_reserve_size;
  633. base = lmb_end_of_DRAM() - size;
  634. lmb_reserve(base, size);
  635. phyp_dump_info->init_reserve_start = base;
  636. phyp_dump_info->init_reserve_size = size;
  637. }
  638. }
  639. #else
  640. static inline void __init phyp_dump_reserve_mem(void) {}
  641. #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
  642. #ifdef CONFIG_EARLY_PRINTK
  643. /* MS this is Microblaze specifig function */
  644. static int __init early_init_dt_scan_serial(unsigned long node,
  645. const char *uname, int depth, void *data)
  646. {
  647. unsigned long l;
  648. char *p;
  649. int *addr;
  650. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  651. /* find all serial nodes */
  652. if (strncmp(uname, "serial", 6) != 0)
  653. return 0;
  654. early_init_dt_check_for_initrd(node);
  655. /* find compatible node with uartlite */
  656. p = of_get_flat_dt_prop(node, "compatible", &l);
  657. if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
  658. (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
  659. return 0;
  660. addr = of_get_flat_dt_prop(node, "reg", &l);
  661. return *addr; /* return address */
  662. }
  663. /* this function is looking for early uartlite console - Microblaze specific */
  664. int __init early_uartlite_console(void)
  665. {
  666. return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
  667. }
  668. #endif
  669. void __init early_init_devtree(void *params)
  670. {
  671. pr_debug(" -> early_init_devtree(%p)\n", params);
  672. /* Setup flat device-tree pointer */
  673. initial_boot_params = params;
  674. #ifdef CONFIG_PHYP_DUMP
  675. /* scan tree to see if dump occured during last boot */
  676. of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
  677. #endif
  678. /* Retrieve various informations from the /chosen node of the
  679. * device-tree, including the platform type, initrd location and
  680. * size, TCE reserve, and more ...
  681. */
  682. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  683. /* Scan memory nodes and rebuild LMBs */
  684. lmb_init();
  685. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  686. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  687. /* Save command line for /proc/cmdline and then parse parameters */
  688. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  689. parse_early_param();
  690. lmb_analyze();
  691. pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
  692. pr_debug("Scanning CPUs ...\n");
  693. /* Retreive CPU related informations from the flat tree
  694. * (altivec support, boot CPU ID, ...)
  695. */
  696. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  697. pr_debug(" <- early_init_devtree()\n");
  698. }
  699. /**
  700. * Indicates whether the root node has a given value in its
  701. * compatible property.
  702. */
  703. int machine_is_compatible(const char *compat)
  704. {
  705. struct device_node *root;
  706. int rc = 0;
  707. root = of_find_node_by_path("/");
  708. if (root) {
  709. rc = of_device_is_compatible(root, compat);
  710. of_node_put(root);
  711. }
  712. return rc;
  713. }
  714. EXPORT_SYMBOL(machine_is_compatible);
  715. /*******
  716. *
  717. * New implementation of the OF "find" APIs, return a refcounted
  718. * object, call of_node_put() when done. The device tree and list
  719. * are protected by a rw_lock.
  720. *
  721. * Note that property management will need some locking as well,
  722. * this isn't dealt with yet.
  723. *
  724. *******/
  725. /**
  726. * of_find_node_by_phandle - Find a node given a phandle
  727. * @handle: phandle of the node to find
  728. *
  729. * Returns a node pointer with refcount incremented, use
  730. * of_node_put() on it when done.
  731. */
  732. struct device_node *of_find_node_by_phandle(phandle handle)
  733. {
  734. struct device_node *np;
  735. read_lock(&devtree_lock);
  736. for (np = allnodes; np != NULL; np = np->allnext)
  737. if (np->linux_phandle == handle)
  738. break;
  739. of_node_get(np);
  740. read_unlock(&devtree_lock);
  741. return np;
  742. }
  743. EXPORT_SYMBOL(of_find_node_by_phandle);
  744. /**
  745. * of_find_all_nodes - Get next node in global list
  746. * @prev: Previous node or NULL to start iteration
  747. * of_node_put() will be called on it
  748. *
  749. * Returns a node pointer with refcount incremented, use
  750. * of_node_put() on it when done.
  751. */
  752. struct device_node *of_find_all_nodes(struct device_node *prev)
  753. {
  754. struct device_node *np;
  755. read_lock(&devtree_lock);
  756. np = prev ? prev->allnext : allnodes;
  757. for (; np != NULL; np = np->allnext)
  758. if (of_node_get(np))
  759. break;
  760. of_node_put(prev);
  761. read_unlock(&devtree_lock);
  762. return np;
  763. }
  764. EXPORT_SYMBOL(of_find_all_nodes);
  765. /**
  766. * of_node_get - Increment refcount of a node
  767. * @node: Node to inc refcount, NULL is supported to
  768. * simplify writing of callers
  769. *
  770. * Returns node.
  771. */
  772. struct device_node *of_node_get(struct device_node *node)
  773. {
  774. if (node)
  775. kref_get(&node->kref);
  776. return node;
  777. }
  778. EXPORT_SYMBOL(of_node_get);
  779. static inline struct device_node *kref_to_device_node(struct kref *kref)
  780. {
  781. return container_of(kref, struct device_node, kref);
  782. }
  783. /**
  784. * of_node_release - release a dynamically allocated node
  785. * @kref: kref element of the node to be released
  786. *
  787. * In of_node_put() this function is passed to kref_put()
  788. * as the destructor.
  789. */
  790. static void of_node_release(struct kref *kref)
  791. {
  792. struct device_node *node = kref_to_device_node(kref);
  793. struct property *prop = node->properties;
  794. /* We should never be releasing nodes that haven't been detached. */
  795. if (!of_node_check_flag(node, OF_DETACHED)) {
  796. printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
  797. node->full_name);
  798. dump_stack();
  799. kref_init(&node->kref);
  800. return;
  801. }
  802. if (!of_node_check_flag(node, OF_DYNAMIC))
  803. return;
  804. while (prop) {
  805. struct property *next = prop->next;
  806. kfree(prop->name);
  807. kfree(prop->value);
  808. kfree(prop);
  809. prop = next;
  810. if (!prop) {
  811. prop = node->deadprops;
  812. node->deadprops = NULL;
  813. }
  814. }
  815. kfree(node->full_name);
  816. kfree(node->data);
  817. kfree(node);
  818. }
  819. /**
  820. * of_node_put - Decrement refcount of a node
  821. * @node: Node to dec refcount, NULL is supported to
  822. * simplify writing of callers
  823. *
  824. */
  825. void of_node_put(struct device_node *node)
  826. {
  827. if (node)
  828. kref_put(&node->kref, of_node_release);
  829. }
  830. EXPORT_SYMBOL(of_node_put);
  831. /*
  832. * Plug a device node into the tree and global list.
  833. */
  834. void of_attach_node(struct device_node *np)
  835. {
  836. unsigned long flags;
  837. write_lock_irqsave(&devtree_lock, flags);
  838. np->sibling = np->parent->child;
  839. np->allnext = allnodes;
  840. np->parent->child = np;
  841. allnodes = np;
  842. write_unlock_irqrestore(&devtree_lock, flags);
  843. }
  844. /*
  845. * "Unplug" a node from the device tree. The caller must hold
  846. * a reference to the node. The memory associated with the node
  847. * is not freed until its refcount goes to zero.
  848. */
  849. void of_detach_node(struct device_node *np)
  850. {
  851. struct device_node *parent;
  852. unsigned long flags;
  853. write_lock_irqsave(&devtree_lock, flags);
  854. parent = np->parent;
  855. if (!parent)
  856. goto out_unlock;
  857. if (allnodes == np)
  858. allnodes = np->allnext;
  859. else {
  860. struct device_node *prev;
  861. for (prev = allnodes;
  862. prev->allnext != np;
  863. prev = prev->allnext)
  864. ;
  865. prev->allnext = np->allnext;
  866. }
  867. if (parent->child == np)
  868. parent->child = np->sibling;
  869. else {
  870. struct device_node *prevsib;
  871. for (prevsib = np->parent->child;
  872. prevsib->sibling != np;
  873. prevsib = prevsib->sibling)
  874. ;
  875. prevsib->sibling = np->sibling;
  876. }
  877. of_node_set_flag(np, OF_DETACHED);
  878. out_unlock:
  879. write_unlock_irqrestore(&devtree_lock, flags);
  880. }
  881. /*
  882. * Add a property to a node
  883. */
  884. int prom_add_property(struct device_node *np, struct property *prop)
  885. {
  886. struct property **next;
  887. unsigned long flags;
  888. prop->next = NULL;
  889. write_lock_irqsave(&devtree_lock, flags);
  890. next = &np->properties;
  891. while (*next) {
  892. if (strcmp(prop->name, (*next)->name) == 0) {
  893. /* duplicate ! don't insert it */
  894. write_unlock_irqrestore(&devtree_lock, flags);
  895. return -1;
  896. }
  897. next = &(*next)->next;
  898. }
  899. *next = prop;
  900. write_unlock_irqrestore(&devtree_lock, flags);
  901. #ifdef CONFIG_PROC_DEVICETREE
  902. /* try to add to proc as well if it was initialized */
  903. if (np->pde)
  904. proc_device_tree_add_prop(np->pde, prop);
  905. #endif /* CONFIG_PROC_DEVICETREE */
  906. return 0;
  907. }
  908. /*
  909. * Remove a property from a node. Note that we don't actually
  910. * remove it, since we have given out who-knows-how-many pointers
  911. * to the data using get-property. Instead we just move the property
  912. * to the "dead properties" list, so it won't be found any more.
  913. */
  914. int prom_remove_property(struct device_node *np, struct property *prop)
  915. {
  916. struct property **next;
  917. unsigned long flags;
  918. int found = 0;
  919. write_lock_irqsave(&devtree_lock, flags);
  920. next = &np->properties;
  921. while (*next) {
  922. if (*next == prop) {
  923. /* found the node */
  924. *next = prop->next;
  925. prop->next = np->deadprops;
  926. np->deadprops = prop;
  927. found = 1;
  928. break;
  929. }
  930. next = &(*next)->next;
  931. }
  932. write_unlock_irqrestore(&devtree_lock, flags);
  933. if (!found)
  934. return -ENODEV;
  935. #ifdef CONFIG_PROC_DEVICETREE
  936. /* try to remove the proc node as well */
  937. if (np->pde)
  938. proc_device_tree_remove_prop(np->pde, prop);
  939. #endif /* CONFIG_PROC_DEVICETREE */
  940. return 0;
  941. }
  942. /*
  943. * Update a property in a node. Note that we don't actually
  944. * remove it, since we have given out who-knows-how-many pointers
  945. * to the data using get-property. Instead we just move the property
  946. * to the "dead properties" list, and add the new property to the
  947. * property list
  948. */
  949. int prom_update_property(struct device_node *np,
  950. struct property *newprop,
  951. struct property *oldprop)
  952. {
  953. struct property **next;
  954. unsigned long flags;
  955. int found = 0;
  956. write_lock_irqsave(&devtree_lock, flags);
  957. next = &np->properties;
  958. while (*next) {
  959. if (*next == oldprop) {
  960. /* found the node */
  961. newprop->next = oldprop->next;
  962. *next = newprop;
  963. oldprop->next = np->deadprops;
  964. np->deadprops = oldprop;
  965. found = 1;
  966. break;
  967. }
  968. next = &(*next)->next;
  969. }
  970. write_unlock_irqrestore(&devtree_lock, flags);
  971. if (!found)
  972. return -ENODEV;
  973. #ifdef CONFIG_PROC_DEVICETREE
  974. /* try to add to proc as well if it was initialized */
  975. if (np->pde)
  976. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  977. #endif /* CONFIG_PROC_DEVICETREE */
  978. return 0;
  979. }
  980. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  981. static struct debugfs_blob_wrapper flat_dt_blob;
  982. static int __init export_flat_device_tree(void)
  983. {
  984. struct dentry *d;
  985. flat_dt_blob.data = initial_boot_params;
  986. flat_dt_blob.size = initial_boot_params->totalsize;
  987. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  988. of_debugfs_root, &flat_dt_blob);
  989. if (!d)
  990. return 1;
  991. return 0;
  992. }
  993. device_initcall(export_flat_device_tree);
  994. #endif