devtree.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * linux/arch/arm/kernel/devtree.c
  3. *
  4. * Copyright (C) 2009 Canonical Ltd. <jeremy.kerr@canonical.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/export.h>
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/memblock.h>
  16. #include <linux/of.h>
  17. #include <linux/of_fdt.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_platform.h>
  20. #include <asm/cputype.h>
  21. #include <asm/setup.h>
  22. #include <asm/page.h>
  23. #include <asm/smp_plat.h>
  24. #include <asm/mach/arch.h>
  25. #include <asm/mach-types.h>
  26. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  27. {
  28. arm_add_memory(base, size);
  29. }
  30. void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
  31. {
  32. return alloc_bootmem_align(size, align);
  33. }
  34. void __init arm_dt_memblock_reserve(void)
  35. {
  36. u64 *reserve_map, base, size;
  37. if (!initial_boot_params)
  38. return;
  39. /* Reserve the dtb region */
  40. memblock_reserve(virt_to_phys(initial_boot_params),
  41. be32_to_cpu(initial_boot_params->totalsize));
  42. /*
  43. * Process the reserve map. This will probably overlap the initrd
  44. * and dtb locations which are already reserved, but overlaping
  45. * doesn't hurt anything
  46. */
  47. reserve_map = ((void*)initial_boot_params) +
  48. be32_to_cpu(initial_boot_params->off_mem_rsvmap);
  49. while (1) {
  50. base = be64_to_cpup(reserve_map++);
  51. size = be64_to_cpup(reserve_map++);
  52. if (!size)
  53. break;
  54. memblock_reserve(base, size);
  55. }
  56. }
  57. /*
  58. * arm_dt_init_cpu_maps - Function retrieves cpu nodes from the device tree
  59. * and builds the cpu logical map array containing MPIDR values related to
  60. * logical cpus
  61. *
  62. * Updates the cpu possible mask with the number of parsed cpu nodes
  63. */
  64. void __init arm_dt_init_cpu_maps(void)
  65. {
  66. /*
  67. * Temp logical map is initialized with UINT_MAX values that are
  68. * considered invalid logical map entries since the logical map must
  69. * contain a list of MPIDR[23:0] values where MPIDR[31:24] must
  70. * read as 0.
  71. */
  72. struct device_node *cpu, *cpus;
  73. u32 i, j, cpuidx = 1;
  74. u32 mpidr = is_smp() ? read_cpuid_mpidr() & MPIDR_HWID_BITMASK : 0;
  75. u32 tmp_map[NR_CPUS] = { [0 ... NR_CPUS-1] = UINT_MAX };
  76. bool bootcpu_valid = false;
  77. cpus = of_find_node_by_path("/cpus");
  78. if (!cpus)
  79. return;
  80. for_each_child_of_node(cpus, cpu) {
  81. u32 hwid;
  82. pr_debug(" * %s...\n", cpu->full_name);
  83. /*
  84. * A device tree containing CPU nodes with missing "reg"
  85. * properties is considered invalid to build the
  86. * cpu_logical_map.
  87. */
  88. if (of_property_read_u32(cpu, "reg", &hwid)) {
  89. pr_debug(" * %s missing reg property\n",
  90. cpu->full_name);
  91. return;
  92. }
  93. /*
  94. * 8 MSBs must be set to 0 in the DT since the reg property
  95. * defines the MPIDR[23:0].
  96. */
  97. if (hwid & ~MPIDR_HWID_BITMASK)
  98. return;
  99. /*
  100. * Duplicate MPIDRs are a recipe for disaster.
  101. * Scan all initialized entries and check for
  102. * duplicates. If any is found just bail out.
  103. * temp values were initialized to UINT_MAX
  104. * to avoid matching valid MPIDR[23:0] values.
  105. */
  106. for (j = 0; j < cpuidx; j++)
  107. if (WARN(tmp_map[j] == hwid, "Duplicate /cpu reg "
  108. "properties in the DT\n"))
  109. return;
  110. /*
  111. * Build a stashed array of MPIDR values. Numbering scheme
  112. * requires that if detected the boot CPU must be assigned
  113. * logical id 0. Other CPUs get sequential indexes starting
  114. * from 1. If a CPU node with a reg property matching the
  115. * boot CPU MPIDR is detected, this is recorded so that the
  116. * logical map built from DT is validated and can be used
  117. * to override the map created in smp_setup_processor_id().
  118. */
  119. if (hwid == mpidr) {
  120. i = 0;
  121. bootcpu_valid = true;
  122. } else {
  123. i = cpuidx++;
  124. }
  125. if (WARN(cpuidx > nr_cpu_ids, "DT /cpu %u nodes greater than "
  126. "max cores %u, capping them\n",
  127. cpuidx, nr_cpu_ids)) {
  128. cpuidx = nr_cpu_ids;
  129. break;
  130. }
  131. tmp_map[i] = hwid;
  132. }
  133. if (WARN(!bootcpu_valid, "DT missing boot CPU MPIDR[23:0], "
  134. "fall back to default cpu_logical_map\n"))
  135. return;
  136. /*
  137. * Since the boot CPU node contains proper data, and all nodes have
  138. * a reg property, the DT CPU list can be considered valid and the
  139. * logical map created in smp_setup_processor_id() can be overridden
  140. */
  141. for (i = 0; i < cpuidx; i++) {
  142. set_cpu_possible(i, true);
  143. cpu_logical_map(i) = tmp_map[i];
  144. pr_debug("cpu logical map 0x%x\n", cpu_logical_map(i));
  145. }
  146. }
  147. /**
  148. * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
  149. * @dt_phys: physical address of dt blob
  150. *
  151. * If a dtb was passed to the kernel in r2, then use it to choose the
  152. * correct machine_desc and to setup the system.
  153. */
  154. struct machine_desc * __init setup_machine_fdt(unsigned int dt_phys)
  155. {
  156. struct boot_param_header *devtree;
  157. struct machine_desc *mdesc, *mdesc_best = NULL;
  158. unsigned int score, mdesc_score = ~1;
  159. unsigned long dt_root;
  160. const char *model;
  161. if (!dt_phys)
  162. return NULL;
  163. devtree = phys_to_virt(dt_phys);
  164. /* check device tree validity */
  165. if (be32_to_cpu(devtree->magic) != OF_DT_HEADER)
  166. return NULL;
  167. /* Search the mdescs for the 'best' compatible value match */
  168. initial_boot_params = devtree;
  169. dt_root = of_get_flat_dt_root();
  170. for_each_machine_desc(mdesc) {
  171. score = of_flat_dt_match(dt_root, mdesc->dt_compat);
  172. if (score > 0 && score < mdesc_score) {
  173. mdesc_best = mdesc;
  174. mdesc_score = score;
  175. }
  176. }
  177. if (!mdesc_best) {
  178. const char *prop;
  179. long size;
  180. early_print("\nError: unrecognized/unsupported "
  181. "device tree compatible list:\n[ ");
  182. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  183. while (size > 0) {
  184. early_print("'%s' ", prop);
  185. size -= strlen(prop) + 1;
  186. prop += strlen(prop) + 1;
  187. }
  188. early_print("]\n\n");
  189. dump_machine_table(); /* does not return */
  190. }
  191. model = of_get_flat_dt_prop(dt_root, "model", NULL);
  192. if (!model)
  193. model = of_get_flat_dt_prop(dt_root, "compatible", NULL);
  194. if (!model)
  195. model = "<unknown>";
  196. pr_info("Machine: %s, model: %s\n", mdesc_best->name, model);
  197. /* Retrieve various information from the /chosen node */
  198. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  199. /* Initialize {size,address}-cells info */
  200. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  201. /* Setup memory, calling early_init_dt_add_memory_arch */
  202. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  203. /* Change machine number to match the mdesc we're using */
  204. __machine_arch_type = mdesc_best->nr;
  205. return mdesc_best;
  206. }