devtree.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/kernel/devtree.c
  4. *
  5. * Copyright (C) 2009 Canonical Ltd. <jeremy.kerr@canonical.com>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/export.h>
  9. #include <linux/errno.h>
  10. #include <linux/types.h>
  11. #include <linux/memblock.h>
  12. #include <linux/of.h>
  13. #include <linux/of_fdt.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/smp.h>
  17. #include <asm/cputype.h>
  18. #include <asm/setup.h>
  19. #include <asm/page.h>
  20. #include <asm/prom.h>
  21. #include <asm/smp_plat.h>
  22. #include <asm/mach/arch.h>
  23. #include <asm/mach-types.h>
  24. #ifdef CONFIG_SMP
  25. extern struct of_cpu_method __cpu_method_of_table[];
  26. static const struct of_cpu_method __cpu_method_of_table_sentinel
  27. __used __section("__cpu_method_of_table_end");
  28. static int __init set_smp_ops_by_method(struct device_node *node)
  29. {
  30. const char *method;
  31. struct of_cpu_method *m = __cpu_method_of_table;
  32. if (of_property_read_string(node, "enable-method", &method))
  33. return 0;
  34. for (; m->method; m++)
  35. if (!strcmp(m->method, method)) {
  36. smp_set_ops(m->ops);
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. #else
  42. static inline int set_smp_ops_by_method(struct device_node *node)
  43. {
  44. return 1;
  45. }
  46. #endif
  47. /*
  48. * arm_dt_init_cpu_maps - Function retrieves cpu nodes from the device tree
  49. * and builds the cpu logical map array containing MPIDR values related to
  50. * logical cpus
  51. *
  52. * Updates the cpu possible mask with the number of parsed cpu nodes
  53. */
  54. void __init arm_dt_init_cpu_maps(void)
  55. {
  56. /*
  57. * Temp logical map is initialized with UINT_MAX values that are
  58. * considered invalid logical map entries since the logical map must
  59. * contain a list of MPIDR[23:0] values where MPIDR[31:24] must
  60. * read as 0.
  61. */
  62. struct device_node *cpu, *cpus;
  63. int found_method = 0;
  64. u32 i, j, cpuidx = 1;
  65. u32 mpidr = is_smp() ? read_cpuid_mpidr() & MPIDR_HWID_BITMASK : 0;
  66. u32 tmp_map[NR_CPUS] = { [0 ... NR_CPUS-1] = MPIDR_INVALID };
  67. bool bootcpu_valid = false;
  68. cpus = of_find_node_by_path("/cpus");
  69. if (!cpus)
  70. return;
  71. for_each_of_cpu_node(cpu) {
  72. const __be32 *cell;
  73. int prop_bytes;
  74. u32 hwid;
  75. pr_debug(" * %pOF...\n", cpu);
  76. /*
  77. * A device tree containing CPU nodes with missing "reg"
  78. * properties is considered invalid to build the
  79. * cpu_logical_map.
  80. */
  81. cell = of_get_property(cpu, "reg", &prop_bytes);
  82. if (!cell || prop_bytes < sizeof(*cell)) {
  83. pr_debug(" * %pOF missing reg property\n", cpu);
  84. of_node_put(cpu);
  85. return;
  86. }
  87. /*
  88. * Bits n:24 must be set to 0 in the DT since the reg property
  89. * defines the MPIDR[23:0].
  90. */
  91. do {
  92. hwid = be32_to_cpu(*cell++);
  93. prop_bytes -= sizeof(*cell);
  94. } while (!hwid && prop_bytes > 0);
  95. if (prop_bytes || (hwid & ~MPIDR_HWID_BITMASK)) {
  96. of_node_put(cpu);
  97. return;
  98. }
  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,
  108. "Duplicate /cpu reg properties in the DT\n")) {
  109. of_node_put(cpu);
  110. return;
  111. }
  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. of_node_put(cpu);
  132. break;
  133. }
  134. tmp_map[i] = hwid;
  135. if (!found_method)
  136. found_method = set_smp_ops_by_method(cpu);
  137. }
  138. /*
  139. * Fallback to an enable-method in the cpus node if nothing found in
  140. * a cpu node.
  141. */
  142. if (!found_method)
  143. set_smp_ops_by_method(cpus);
  144. if (!bootcpu_valid) {
  145. pr_warn("DT missing boot CPU MPIDR[23:0], fall back to default cpu_logical_map\n");
  146. return;
  147. }
  148. /*
  149. * Since the boot CPU node contains proper data, and all nodes have
  150. * a reg property, the DT CPU list can be considered valid and the
  151. * logical map created in smp_setup_processor_id() can be overridden
  152. */
  153. for (i = 0; i < cpuidx; i++) {
  154. set_cpu_possible(i, true);
  155. cpu_logical_map(i) = tmp_map[i];
  156. pr_debug("cpu logical map 0x%x\n", cpu_logical_map(i));
  157. }
  158. }
  159. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  160. {
  161. return phys_id == cpu_logical_map(cpu);
  162. }
  163. static const void * __init arch_get_next_mach(const char *const **match)
  164. {
  165. static const struct machine_desc *mdesc = __arch_info_begin;
  166. const struct machine_desc *m = mdesc;
  167. if (m >= __arch_info_end)
  168. return NULL;
  169. mdesc++;
  170. *match = m->dt_compat;
  171. return m;
  172. }
  173. /**
  174. * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
  175. * @dt_virt: virtual address of dt blob
  176. *
  177. * If a dtb was passed to the kernel in r2, then use it to choose the
  178. * correct machine_desc and to setup the system.
  179. */
  180. const struct machine_desc * __init setup_machine_fdt(void *dt_virt)
  181. {
  182. const struct machine_desc *mdesc, *mdesc_best = NULL;
  183. #if defined(CONFIG_ARCH_MULTIPLATFORM) || defined(CONFIG_ARM_SINGLE_ARMV7M)
  184. DT_MACHINE_START(GENERIC_DT, "Generic DT based system")
  185. .l2c_aux_val = 0x0,
  186. .l2c_aux_mask = ~0x0,
  187. MACHINE_END
  188. mdesc_best = &__mach_desc_GENERIC_DT;
  189. #endif
  190. if (!dt_virt || !early_init_dt_verify(dt_virt))
  191. return NULL;
  192. mdesc = of_flat_dt_match_machine(mdesc_best, arch_get_next_mach);
  193. if (!mdesc) {
  194. const char *prop;
  195. int size;
  196. unsigned long dt_root;
  197. early_print("\nError: unrecognized/unsupported "
  198. "device tree compatible list:\n[ ");
  199. dt_root = of_get_flat_dt_root();
  200. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  201. while (size > 0) {
  202. early_print("'%s' ", prop);
  203. size -= strlen(prop) + 1;
  204. prop += strlen(prop) + 1;
  205. }
  206. early_print("]\n\n");
  207. dump_machine_table(); /* does not return */
  208. }
  209. /* We really don't want to do this, but sometimes firmware provides buggy data */
  210. if (mdesc->dt_fixup)
  211. mdesc->dt_fixup();
  212. early_init_dt_scan_nodes();
  213. /* Change machine number to match the mdesc we're using */
  214. __machine_arch_type = mdesc->nr;
  215. return mdesc;
  216. }