prom.c 14 KB

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