prom.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. #define early_init_dt_scan_drconf_memory(node) 0
  48. static int __init early_init_dt_scan_cpus(unsigned long node,
  49. const char *uname, int depth,
  50. void *data)
  51. {
  52. static int logical_cpuid;
  53. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  54. const u32 *intserv;
  55. int i, nthreads;
  56. int found = 0;
  57. /* We are scanning "cpu" nodes only */
  58. if (type == NULL || strcmp(type, "cpu") != 0)
  59. return 0;
  60. /* Get physical cpuid */
  61. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  62. nthreads = 1;
  63. /*
  64. * Now see if any of these threads match our boot cpu.
  65. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  66. */
  67. for (i = 0; i < nthreads; i++) {
  68. /*
  69. * version 2 of the kexec param format adds the phys cpuid of
  70. * booted proc.
  71. */
  72. if (initial_boot_params && initial_boot_params->version >= 2) {
  73. if (intserv[i] ==
  74. initial_boot_params->boot_cpuid_phys) {
  75. found = 1;
  76. break;
  77. }
  78. } else {
  79. /*
  80. * Check if it's the boot-cpu, set it's hw index now,
  81. * unfortunately this format did not support booting
  82. * off secondary threads.
  83. */
  84. if (of_get_flat_dt_prop(node,
  85. "linux,boot-cpu", NULL) != NULL) {
  86. found = 1;
  87. break;
  88. }
  89. }
  90. #ifdef CONFIG_SMP
  91. /* logical cpu id is always 0 on UP kernels */
  92. logical_cpuid++;
  93. #endif
  94. }
  95. if (found) {
  96. pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid,
  97. intserv[i]);
  98. boot_cpuid = logical_cpuid;
  99. }
  100. return 0;
  101. }
  102. #ifdef CONFIG_BLK_DEV_INITRD
  103. static void __init early_init_dt_check_for_initrd(unsigned long node)
  104. {
  105. unsigned long l;
  106. u32 *prop;
  107. pr_debug("Looking for initrd properties... ");
  108. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
  109. if (prop) {
  110. initrd_start = (unsigned long)
  111. __va((u32)of_read_ulong(prop, l/4));
  112. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
  113. if (prop) {
  114. initrd_end = (unsigned long)
  115. __va((u32)of_read_ulong(prop, 1/4));
  116. initrd_below_start_ok = 1;
  117. } else {
  118. initrd_start = 0;
  119. }
  120. }
  121. pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
  122. initrd_start, initrd_end);
  123. }
  124. #else
  125. static inline void early_init_dt_check_for_initrd(unsigned long node)
  126. {
  127. }
  128. #endif /* CONFIG_BLK_DEV_INITRD */
  129. static int __init early_init_dt_scan_chosen(unsigned long node,
  130. const char *uname, int depth, void *data)
  131. {
  132. unsigned long l;
  133. char *p;
  134. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  135. if (depth != 1 ||
  136. (strcmp(uname, "chosen") != 0 &&
  137. strcmp(uname, "chosen@0") != 0))
  138. return 0;
  139. #ifdef CONFIG_KEXEC
  140. lprop = (u64 *)of_get_flat_dt_prop(node,
  141. "linux,crashkernel-base", NULL);
  142. if (lprop)
  143. crashk_res.start = *lprop;
  144. lprop = (u64 *)of_get_flat_dt_prop(node,
  145. "linux,crashkernel-size", NULL);
  146. if (lprop)
  147. crashk_res.end = crashk_res.start + *lprop - 1;
  148. #endif
  149. early_init_dt_check_for_initrd(node);
  150. /* Retreive command line */
  151. p = of_get_flat_dt_prop(node, "bootargs", &l);
  152. if (p != NULL && l > 0)
  153. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  154. #ifdef CONFIG_CMDLINE
  155. #ifndef CONFIG_CMDLINE_FORCE
  156. if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
  157. #endif
  158. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  159. #endif /* CONFIG_CMDLINE */
  160. pr_debug("Command line is: %s\n", cmd_line);
  161. /* break now */
  162. return 1;
  163. }
  164. static int __init early_init_dt_scan_root(unsigned long node,
  165. const char *uname, int depth, void *data)
  166. {
  167. u32 *prop;
  168. if (depth != 0)
  169. return 0;
  170. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  171. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  172. pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
  173. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  174. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  175. pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  176. /* break now */
  177. return 1;
  178. }
  179. static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
  180. {
  181. cell_t *p = *cellp;
  182. *cellp = p + s;
  183. return of_read_number(p, s);
  184. }
  185. static int __init early_init_dt_scan_memory(unsigned long node,
  186. const char *uname, int depth, void *data)
  187. {
  188. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  189. cell_t *reg, *endp;
  190. unsigned long l;
  191. /* Look for the ibm,dynamic-reconfiguration-memory node */
  192. /* if (depth == 1 &&
  193. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  194. return early_init_dt_scan_drconf_memory(node);
  195. */
  196. /* We are scanning "memory" nodes only */
  197. if (type == NULL) {
  198. /*
  199. * The longtrail doesn't have a device_type on the
  200. * /memory node, so look for the node called /memory@0.
  201. */
  202. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  203. return 0;
  204. } else if (strcmp(type, "memory") != 0)
  205. return 0;
  206. reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  207. if (reg == NULL)
  208. reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
  209. if (reg == NULL)
  210. return 0;
  211. endp = reg + (l / sizeof(cell_t));
  212. pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  213. uname, l, reg[0], reg[1], reg[2], reg[3]);
  214. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  215. u64 base, size;
  216. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  217. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  218. if (size == 0)
  219. continue;
  220. pr_debug(" - %llx , %llx\n", (unsigned long long)base,
  221. (unsigned long long)size);
  222. lmb_add(base, size);
  223. }
  224. return 0;
  225. }
  226. #ifdef CONFIG_PHYP_DUMP
  227. /**
  228. * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
  229. *
  230. * Function to find the largest size we need to reserve
  231. * during early boot process.
  232. *
  233. * It either looks for boot param and returns that OR
  234. * returns larger of 256 or 5% rounded down to multiples of 256MB.
  235. *
  236. */
  237. static inline unsigned long phyp_dump_calculate_reserve_size(void)
  238. {
  239. unsigned long tmp;
  240. if (phyp_dump_info->reserve_bootvar)
  241. return phyp_dump_info->reserve_bootvar;
  242. /* divide by 20 to get 5% of value */
  243. tmp = lmb_end_of_DRAM();
  244. do_div(tmp, 20);
  245. /* round it down in multiples of 256 */
  246. tmp = tmp & ~0x0FFFFFFFUL;
  247. return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
  248. }
  249. /**
  250. * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
  251. *
  252. * This routine may reserve memory regions in the kernel only
  253. * if the system is supported and a dump was taken in last
  254. * boot instance or if the hardware is supported and the
  255. * scratch area needs to be setup. In other instances it returns
  256. * without reserving anything. The memory in case of dump being
  257. * active is freed when the dump is collected (by userland tools).
  258. */
  259. static void __init phyp_dump_reserve_mem(void)
  260. {
  261. unsigned long base, size;
  262. unsigned long variable_reserve_size;
  263. if (!phyp_dump_info->phyp_dump_configured) {
  264. printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
  265. return;
  266. }
  267. if (!phyp_dump_info->phyp_dump_at_boot) {
  268. printk(KERN_INFO "Phyp-dump disabled at boot time\n");
  269. return;
  270. }
  271. variable_reserve_size = phyp_dump_calculate_reserve_size();
  272. if (phyp_dump_info->phyp_dump_is_active) {
  273. /* Reserve *everything* above RMR.Area freed by userland tools*/
  274. base = variable_reserve_size;
  275. size = lmb_end_of_DRAM() - base;
  276. /* XXX crashed_ram_end is wrong, since it may be beyond
  277. * the memory_limit, it will need to be adjusted. */
  278. lmb_reserve(base, size);
  279. phyp_dump_info->init_reserve_start = base;
  280. phyp_dump_info->init_reserve_size = size;
  281. } else {
  282. size = phyp_dump_info->cpu_state_size +
  283. phyp_dump_info->hpte_region_size +
  284. variable_reserve_size;
  285. base = lmb_end_of_DRAM() - size;
  286. lmb_reserve(base, size);
  287. phyp_dump_info->init_reserve_start = base;
  288. phyp_dump_info->init_reserve_size = size;
  289. }
  290. }
  291. #else
  292. static inline void __init phyp_dump_reserve_mem(void) {}
  293. #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
  294. #ifdef CONFIG_EARLY_PRINTK
  295. /* MS this is Microblaze specifig function */
  296. static int __init early_init_dt_scan_serial(unsigned long node,
  297. const char *uname, int depth, void *data)
  298. {
  299. unsigned long l;
  300. char *p;
  301. int *addr;
  302. pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  303. /* find all serial nodes */
  304. if (strncmp(uname, "serial", 6) != 0)
  305. return 0;
  306. early_init_dt_check_for_initrd(node);
  307. /* find compatible node with uartlite */
  308. p = of_get_flat_dt_prop(node, "compatible", &l);
  309. if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
  310. (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
  311. return 0;
  312. addr = of_get_flat_dt_prop(node, "reg", &l);
  313. return *addr; /* return address */
  314. }
  315. /* this function is looking for early uartlite console - Microblaze specific */
  316. int __init early_uartlite_console(void)
  317. {
  318. return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
  319. }
  320. #endif
  321. void __init early_init_devtree(void *params)
  322. {
  323. pr_debug(" -> early_init_devtree(%p)\n", params);
  324. /* Setup flat device-tree pointer */
  325. initial_boot_params = params;
  326. #ifdef CONFIG_PHYP_DUMP
  327. /* scan tree to see if dump occured during last boot */
  328. of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
  329. #endif
  330. /* Retrieve various informations from the /chosen node of the
  331. * device-tree, including the platform type, initrd location and
  332. * size, TCE reserve, and more ...
  333. */
  334. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  335. /* Scan memory nodes and rebuild LMBs */
  336. lmb_init();
  337. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  338. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  339. /* Save command line for /proc/cmdline and then parse parameters */
  340. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  341. parse_early_param();
  342. lmb_analyze();
  343. pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
  344. pr_debug("Scanning CPUs ...\n");
  345. /* Retreive CPU related informations from the flat tree
  346. * (altivec support, boot CPU ID, ...)
  347. */
  348. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  349. pr_debug(" <- early_init_devtree()\n");
  350. }
  351. /**
  352. * Indicates whether the root node has a given value in its
  353. * compatible property.
  354. */
  355. int machine_is_compatible(const char *compat)
  356. {
  357. struct device_node *root;
  358. int rc = 0;
  359. root = of_find_node_by_path("/");
  360. if (root) {
  361. rc = of_device_is_compatible(root, compat);
  362. of_node_put(root);
  363. }
  364. return rc;
  365. }
  366. EXPORT_SYMBOL(machine_is_compatible);
  367. /*******
  368. *
  369. * New implementation of the OF "find" APIs, return a refcounted
  370. * object, call of_node_put() when done. The device tree and list
  371. * are protected by a rw_lock.
  372. *
  373. * Note that property management will need some locking as well,
  374. * this isn't dealt with yet.
  375. *
  376. *******/
  377. /**
  378. * of_find_node_by_phandle - Find a node given a phandle
  379. * @handle: phandle of the node to find
  380. *
  381. * Returns a node pointer with refcount incremented, use
  382. * of_node_put() on it when done.
  383. */
  384. struct device_node *of_find_node_by_phandle(phandle handle)
  385. {
  386. struct device_node *np;
  387. read_lock(&devtree_lock);
  388. for (np = allnodes; np != NULL; np = np->allnext)
  389. if (np->linux_phandle == handle)
  390. break;
  391. of_node_get(np);
  392. read_unlock(&devtree_lock);
  393. return np;
  394. }
  395. EXPORT_SYMBOL(of_find_node_by_phandle);
  396. /**
  397. * of_node_get - Increment refcount of a node
  398. * @node: Node to inc refcount, NULL is supported to
  399. * simplify writing of callers
  400. *
  401. * Returns node.
  402. */
  403. struct device_node *of_node_get(struct device_node *node)
  404. {
  405. if (node)
  406. kref_get(&node->kref);
  407. return node;
  408. }
  409. EXPORT_SYMBOL(of_node_get);
  410. static inline struct device_node *kref_to_device_node(struct kref *kref)
  411. {
  412. return container_of(kref, struct device_node, kref);
  413. }
  414. /**
  415. * of_node_release - release a dynamically allocated node
  416. * @kref: kref element of the node to be released
  417. *
  418. * In of_node_put() this function is passed to kref_put()
  419. * as the destructor.
  420. */
  421. static void of_node_release(struct kref *kref)
  422. {
  423. struct device_node *node = kref_to_device_node(kref);
  424. struct property *prop = node->properties;
  425. /* We should never be releasing nodes that haven't been detached. */
  426. if (!of_node_check_flag(node, OF_DETACHED)) {
  427. printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
  428. node->full_name);
  429. dump_stack();
  430. kref_init(&node->kref);
  431. return;
  432. }
  433. if (!of_node_check_flag(node, OF_DYNAMIC))
  434. return;
  435. while (prop) {
  436. struct property *next = prop->next;
  437. kfree(prop->name);
  438. kfree(prop->value);
  439. kfree(prop);
  440. prop = next;
  441. if (!prop) {
  442. prop = node->deadprops;
  443. node->deadprops = NULL;
  444. }
  445. }
  446. kfree(node->full_name);
  447. kfree(node->data);
  448. kfree(node);
  449. }
  450. /**
  451. * of_node_put - Decrement refcount of a node
  452. * @node: Node to dec refcount, NULL is supported to
  453. * simplify writing of callers
  454. *
  455. */
  456. void of_node_put(struct device_node *node)
  457. {
  458. if (node)
  459. kref_put(&node->kref, of_node_release);
  460. }
  461. EXPORT_SYMBOL(of_node_put);
  462. /*
  463. * Plug a device node into the tree and global list.
  464. */
  465. void of_attach_node(struct device_node *np)
  466. {
  467. unsigned long flags;
  468. write_lock_irqsave(&devtree_lock, flags);
  469. np->sibling = np->parent->child;
  470. np->allnext = allnodes;
  471. np->parent->child = np;
  472. allnodes = np;
  473. write_unlock_irqrestore(&devtree_lock, flags);
  474. }
  475. /*
  476. * "Unplug" a node from the device tree. The caller must hold
  477. * a reference to the node. The memory associated with the node
  478. * is not freed until its refcount goes to zero.
  479. */
  480. void of_detach_node(struct device_node *np)
  481. {
  482. struct device_node *parent;
  483. unsigned long flags;
  484. write_lock_irqsave(&devtree_lock, flags);
  485. parent = np->parent;
  486. if (!parent)
  487. goto out_unlock;
  488. if (allnodes == np)
  489. allnodes = np->allnext;
  490. else {
  491. struct device_node *prev;
  492. for (prev = allnodes;
  493. prev->allnext != np;
  494. prev = prev->allnext)
  495. ;
  496. prev->allnext = np->allnext;
  497. }
  498. if (parent->child == np)
  499. parent->child = np->sibling;
  500. else {
  501. struct device_node *prevsib;
  502. for (prevsib = np->parent->child;
  503. prevsib->sibling != np;
  504. prevsib = prevsib->sibling)
  505. ;
  506. prevsib->sibling = np->sibling;
  507. }
  508. of_node_set_flag(np, OF_DETACHED);
  509. out_unlock:
  510. write_unlock_irqrestore(&devtree_lock, flags);
  511. }
  512. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  513. static struct debugfs_blob_wrapper flat_dt_blob;
  514. static int __init export_flat_device_tree(void)
  515. {
  516. struct dentry *d;
  517. flat_dt_blob.data = initial_boot_params;
  518. flat_dt_blob.size = initial_boot_params->totalsize;
  519. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  520. of_debugfs_root, &flat_dt_blob);
  521. if (!d)
  522. return 1;
  523. return 0;
  524. }
  525. device_initcall(export_flat_device_tree);
  526. #endif