prom.c 12 KB

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