devtree.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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] = MPIDR_INVALID };
  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. if (of_node_cmp(cpu->type, "cpu"))
  83. continue;
  84. pr_debug(" * %s...\n", cpu->full_name);
  85. /*
  86. * A device tree containing CPU nodes with missing "reg"
  87. * properties is considered invalid to build the
  88. * cpu_logical_map.
  89. */
  90. if (of_property_read_u32(cpu, "reg", &hwid)) {
  91. pr_debug(" * %s missing reg property\n",
  92. cpu->full_name);
  93. return;
  94. }
  95. /*
  96. * 8 MSBs must be set to 0 in the DT since the reg property
  97. * defines the MPIDR[23:0].
  98. */
  99. if (hwid & ~MPIDR_HWID_BITMASK)
  100. return;
  101. /*
  102. * Duplicate MPIDRs are a recipe for disaster.
  103. * Scan all initialized entries and check for
  104. * duplicates. If any is found just bail out.
  105. * temp values were initialized to UINT_MAX
  106. * to avoid matching valid MPIDR[23:0] values.
  107. */
  108. for (j = 0; j < cpuidx; j++)
  109. if (WARN(tmp_map[j] == hwid, "Duplicate /cpu reg "
  110. "properties in the DT\n"))
  111. return;
  112. /*
  113. * Build a stashed array of MPIDR values. Numbering scheme
  114. * requires that if detected the boot CPU must be assigned
  115. * logical id 0. Other CPUs get sequential indexes starting
  116. * from 1. If a CPU node with a reg property matching the
  117. * boot CPU MPIDR is detected, this is recorded so that the
  118. * logical map built from DT is validated and can be used
  119. * to override the map created in smp_setup_processor_id().
  120. */
  121. if (hwid == mpidr) {
  122. i = 0;
  123. bootcpu_valid = true;
  124. } else {
  125. i = cpuidx++;
  126. }
  127. if (WARN(cpuidx > nr_cpu_ids, "DT /cpu %u nodes greater than "
  128. "max cores %u, capping them\n",
  129. cpuidx, nr_cpu_ids)) {
  130. cpuidx = nr_cpu_ids;
  131. break;
  132. }
  133. tmp_map[i] = hwid;
  134. }
  135. if (!bootcpu_valid) {
  136. pr_warn("DT missing boot CPU MPIDR[23:0], fall back to default cpu_logical_map\n");
  137. return;
  138. }
  139. /*
  140. * Since the boot CPU node contains proper data, and all nodes have
  141. * a reg property, the DT CPU list can be considered valid and the
  142. * logical map created in smp_setup_processor_id() can be overridden
  143. */
  144. for (i = 0; i < cpuidx; i++) {
  145. set_cpu_possible(i, true);
  146. cpu_logical_map(i) = tmp_map[i];
  147. pr_debug("cpu logical map 0x%x\n", cpu_logical_map(i));
  148. }
  149. }
  150. /**
  151. * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
  152. * @dt_phys: physical address of dt blob
  153. *
  154. * If a dtb was passed to the kernel in r2, then use it to choose the
  155. * correct machine_desc and to setup the system.
  156. */
  157. struct machine_desc * __init setup_machine_fdt(unsigned int dt_phys)
  158. {
  159. struct boot_param_header *devtree;
  160. struct machine_desc *mdesc, *mdesc_best = NULL;
  161. unsigned int score, mdesc_score = ~1;
  162. unsigned long dt_root;
  163. const char *model;
  164. #ifdef CONFIG_ARCH_MULTIPLATFORM
  165. DT_MACHINE_START(GENERIC_DT, "Generic DT based system")
  166. MACHINE_END
  167. mdesc_best = (struct machine_desc *)&__mach_desc_GENERIC_DT;
  168. #endif
  169. if (!dt_phys)
  170. return NULL;
  171. devtree = phys_to_virt(dt_phys);
  172. /* check device tree validity */
  173. if (be32_to_cpu(devtree->magic) != OF_DT_HEADER)
  174. return NULL;
  175. /* Search the mdescs for the 'best' compatible value match */
  176. initial_boot_params = devtree;
  177. dt_root = of_get_flat_dt_root();
  178. for_each_machine_desc(mdesc) {
  179. score = of_flat_dt_match(dt_root, mdesc->dt_compat);
  180. if (score > 0 && score < mdesc_score) {
  181. mdesc_best = mdesc;
  182. mdesc_score = score;
  183. }
  184. }
  185. if (!mdesc_best) {
  186. const char *prop;
  187. long size;
  188. early_print("\nError: unrecognized/unsupported "
  189. "device tree compatible list:\n[ ");
  190. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  191. while (size > 0) {
  192. early_print("'%s' ", prop);
  193. size -= strlen(prop) + 1;
  194. prop += strlen(prop) + 1;
  195. }
  196. early_print("]\n\n");
  197. dump_machine_table(); /* does not return */
  198. }
  199. model = of_get_flat_dt_prop(dt_root, "model", NULL);
  200. if (!model)
  201. model = of_get_flat_dt_prop(dt_root, "compatible", NULL);
  202. if (!model)
  203. model = "<unknown>";
  204. pr_info("Machine: %s, model: %s\n", mdesc_best->name, model);
  205. /* Retrieve various information from the /chosen node */
  206. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  207. /* Initialize {size,address}-cells info */
  208. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  209. /* Setup memory, calling early_init_dt_add_memory_arch */
  210. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  211. /* Change machine number to match the mdesc we're using */
  212. __machine_arch_type = mdesc_best->nr;
  213. return mdesc_best;
  214. }