prom.c 14 KB

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