devtree.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/smp.h>
  16. #include <asm/cputype.h>
  17. #include <asm/setup.h>
  18. #include <asm/page.h>
  19. #include <asm/prom.h>
  20. #include <asm/smp_plat.h>
  21. #include <asm/mach/arch.h>
  22. #include <asm/mach-types.h>
  23. #ifdef CONFIG_SMP
  24. extern struct of_cpu_method __cpu_method_of_table[];
  25. static const struct of_cpu_method __cpu_method_of_table_sentinel
  26. __used __section("__cpu_method_of_table_end");
  27. static int __init set_smp_ops_by_method(struct device_node *node)
  28. {
  29. const char *method;
  30. struct of_cpu_method *m = __cpu_method_of_table;
  31. if (of_property_read_string(node, "enable-method", &method))
  32. return 0;
  33. for (; m->method; m++)
  34. if (!strcmp(m->method, method)) {
  35. smp_set_ops(m->ops);
  36. return 1;
  37. }
  38. return 0;
  39. }
  40. #else
  41. static inline int set_smp_ops_by_method(struct device_node *node)
  42. {
  43. return 1;
  44. }
  45. #endif
  46. /*
  47. * arm_dt_init_cpu_maps - Function retrieves cpu nodes from the device tree
  48. * and builds the cpu logical map array containing MPIDR values related to
  49. * logical cpus
  50. *
  51. * Updates the cpu possible mask with the number of parsed cpu nodes
  52. */
  53. void __init arm_dt_init_cpu_maps(void)
  54. {
  55. /*
  56. * Temp logical map is initialized with UINT_MAX values that are
  57. * considered invalid logical map entries since the logical map must
  58. * contain a list of MPIDR[23:0] values where MPIDR[31:24] must
  59. * read as 0.
  60. */
  61. struct device_node *cpu, *cpus;
  62. int found_method = 0;
  63. u32 i, j, cpuidx = 1;
  64. u32 mpidr = is_smp() ? read_cpuid_mpidr() & MPIDR_HWID_BITMASK : 0;
  65. u32 tmp_map[NR_CPUS] = { [0 ... NR_CPUS-1] = MPIDR_INVALID };
  66. bool bootcpu_valid = false;
  67. cpus = of_find_node_by_path("/cpus");
  68. if (!cpus)
  69. return;
  70. for_each_of_cpu_node(cpu) {
  71. u32 hwid = of_get_cpu_hwid(cpu, 0);
  72. pr_debug(" * %pOF...\n", cpu);
  73. /*
  74. * Bits n:24 must be set to 0 in the DT since the reg property
  75. * defines the MPIDR[23:0].
  76. */
  77. if (hwid & ~MPIDR_HWID_BITMASK) {
  78. of_node_put(cpu);
  79. return;
  80. }
  81. /*
  82. * Duplicate MPIDRs are a recipe for disaster.
  83. * Scan all initialized entries and check for
  84. * duplicates. If any is found just bail out.
  85. * temp values were initialized to UINT_MAX
  86. * to avoid matching valid MPIDR[23:0] values.
  87. */
  88. for (j = 0; j < cpuidx; j++)
  89. if (WARN(tmp_map[j] == hwid,
  90. "Duplicate /cpu reg properties in the DT\n")) {
  91. of_node_put(cpu);
  92. return;
  93. }
  94. /*
  95. * Build a stashed array of MPIDR values. Numbering scheme
  96. * requires that if detected the boot CPU must be assigned
  97. * logical id 0. Other CPUs get sequential indexes starting
  98. * from 1. If a CPU node with a reg property matching the
  99. * boot CPU MPIDR is detected, this is recorded so that the
  100. * logical map built from DT is validated and can be used
  101. * to override the map created in smp_setup_processor_id().
  102. */
  103. if (hwid == mpidr) {
  104. i = 0;
  105. bootcpu_valid = true;
  106. } else {
  107. i = cpuidx++;
  108. }
  109. if (WARN(cpuidx > nr_cpu_ids, "DT /cpu %u nodes greater than "
  110. "max cores %u, capping them\n",
  111. cpuidx, nr_cpu_ids)) {
  112. cpuidx = nr_cpu_ids;
  113. of_node_put(cpu);
  114. break;
  115. }
  116. tmp_map[i] = hwid;
  117. if (!found_method)
  118. found_method = set_smp_ops_by_method(cpu);
  119. }
  120. /*
  121. * Fallback to an enable-method in the cpus node if nothing found in
  122. * a cpu node.
  123. */
  124. if (!found_method)
  125. set_smp_ops_by_method(cpus);
  126. if (!bootcpu_valid) {
  127. pr_warn("DT missing boot CPU MPIDR[23:0], fall back to default cpu_logical_map\n");
  128. return;
  129. }
  130. /*
  131. * Since the boot CPU node contains proper data, and all nodes have
  132. * a reg property, the DT CPU list can be considered valid and the
  133. * logical map created in smp_setup_processor_id() can be overridden
  134. */
  135. for (i = 0; i < cpuidx; i++) {
  136. set_cpu_possible(i, true);
  137. cpu_logical_map(i) = tmp_map[i];
  138. pr_debug("cpu logical map 0x%x\n", cpu_logical_map(i));
  139. }
  140. }
  141. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  142. {
  143. return phys_id == cpu_logical_map(cpu);
  144. }
  145. static const void * __init arch_get_next_mach(const char *const **match)
  146. {
  147. static const struct machine_desc *mdesc = __arch_info_begin;
  148. const struct machine_desc *m = mdesc;
  149. if (m >= __arch_info_end)
  150. return NULL;
  151. mdesc++;
  152. *match = m->dt_compat;
  153. return m;
  154. }
  155. /**
  156. * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
  157. * @dt_virt: virtual address of dt blob
  158. *
  159. * If a dtb was passed to the kernel in r2, then use it to choose the
  160. * correct machine_desc and to setup the system.
  161. */
  162. const struct machine_desc * __init setup_machine_fdt(void *dt_virt)
  163. {
  164. const struct machine_desc *mdesc, *mdesc_best = NULL;
  165. DT_MACHINE_START(GENERIC_DT, "Generic DT based system")
  166. .l2c_aux_val = 0x0,
  167. .l2c_aux_mask = ~0x0,
  168. MACHINE_END
  169. mdesc_best = &__mach_desc_GENERIC_DT;
  170. if (!dt_virt || !early_init_dt_verify(dt_virt, __pa(dt_virt)))
  171. return NULL;
  172. mdesc = of_flat_dt_match_machine(mdesc_best, arch_get_next_mach);
  173. if (!mdesc) {
  174. const char *prop;
  175. int size;
  176. unsigned long dt_root;
  177. early_print("\nError: unrecognized/unsupported "
  178. "device tree compatible list:\n[ ");
  179. dt_root = of_get_flat_dt_root();
  180. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  181. while (size > 0) {
  182. early_print("'%s' ", prop);
  183. size -= strlen(prop) + 1;
  184. prop += strlen(prop) + 1;
  185. }
  186. early_print("]\n\n");
  187. dump_machine_table(); /* does not return */
  188. }
  189. /* We really don't want to do this, but sometimes firmware provides buggy data */
  190. if (mdesc->dt_fixup)
  191. mdesc->dt_fixup();
  192. early_init_dt_scan_nodes();
  193. /* Change machine number to match the mdesc we're using */
  194. __machine_arch_type = mdesc->nr;
  195. return mdesc;
  196. }