prom.c 14 KB

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