prom.c 27 KB

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