devtree.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/kernel/devtree.c
  4. *
  5. * Copyright (C) 2009 Canonical Ltd. <[email protected]>
  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. u32 hwid = of_get_cpu_hwid(cpu, 0);
  73. pr_debug(" * %pOF...\n", cpu);
  74. /*
  75. * Bits n:24 must be set to 0 in the DT since the reg property
  76. * defines the MPIDR[23:0].
  77. */
  78. if (hwid & ~MPIDR_HWID_BITMASK) {
  79. of_node_put(cpu);
  80. return;
  81. }
  82. /*
  83. * Duplicate MPIDRs are a recipe for disaster.
  84. * Scan all initialized entries and check for
  85. * duplicates. If any is found just bail out.
  86. * temp values were initialized to UINT_MAX
  87. * to avoid matching valid MPIDR[23:0] values.
  88. */
  89. for (j = 0; j < cpuidx; j++)
  90. if (WARN(tmp_map[j] == hwid,
  91. "Duplicate /cpu reg properties in the DT\n")) {
  92. of_node_put(cpu);
  93. return;
  94. }
  95. /*
  96. * Build a stashed array of MPIDR values. Numbering scheme
  97. * requires that if detected the boot CPU must be assigned
  98. * logical id 0. Other CPUs get sequential indexes starting
  99. * from 1. If a CPU node with a reg property matching the
  100. * boot CPU MPIDR is detected, this is recorded so that the
  101. * logical map built from DT is validated and can be used
  102. * to override the map created in smp_setup_processor_id().
  103. */
  104. if (hwid == mpidr) {
  105. i = 0;
  106. bootcpu_valid = true;
  107. } else {
  108. i = cpuidx++;
  109. }
  110. if (WARN(cpuidx > nr_cpu_ids, "DT /cpu %u nodes greater than "
  111. "max cores %u, capping them\n",
  112. cpuidx, nr_cpu_ids)) {
  113. cpuidx = nr_cpu_ids;
  114. of_node_put(cpu);
  115. break;
  116. }
  117. tmp_map[i] = hwid;
  118. if (!found_method)
  119. found_method = set_smp_ops_by_method(cpu);
  120. }
  121. /*
  122. * Fallback to an enable-method in the cpus node if nothing found in
  123. * a cpu node.
  124. */
  125. if (!found_method)
  126. set_smp_ops_by_method(cpus);
  127. if (!bootcpu_valid) {
  128. pr_warn("DT missing boot CPU MPIDR[23:0], fall back to default cpu_logical_map\n");
  129. return;
  130. }
  131. /*
  132. * Since the boot CPU node contains proper data, and all nodes have
  133. * a reg property, the DT CPU list can be considered valid and the
  134. * logical map created in smp_setup_processor_id() can be overridden
  135. */
  136. for (i = 0; i < cpuidx; i++) {
  137. set_cpu_possible(i, true);
  138. cpu_logical_map(i) = tmp_map[i];
  139. pr_debug("cpu logical map 0x%x\n", cpu_logical_map(i));
  140. }
  141. }
  142. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  143. {
  144. return phys_id == cpu_logical_map(cpu);
  145. }
  146. static const void * __init arch_get_next_mach(const char *const **match)
  147. {
  148. static const struct machine_desc *mdesc = __arch_info_begin;
  149. const struct machine_desc *m = mdesc;
  150. if (m >= __arch_info_end)
  151. return NULL;
  152. mdesc++;
  153. *match = m->dt_compat;
  154. return m;
  155. }
  156. /**
  157. * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
  158. * @dt_virt: virtual address of dt blob
  159. *
  160. * If a dtb was passed to the kernel in r2, then use it to choose the
  161. * correct machine_desc and to setup the system.
  162. */
  163. const struct machine_desc * __init setup_machine_fdt(void *dt_virt)
  164. {
  165. const struct machine_desc *mdesc, *mdesc_best = NULL;
  166. DT_MACHINE_START(GENERIC_DT, "Generic DT based system")
  167. .l2c_aux_val = 0x0,
  168. .l2c_aux_mask = ~0x0,
  169. MACHINE_END
  170. mdesc_best = &__mach_desc_GENERIC_DT;
  171. if (!dt_virt || !early_init_dt_verify(dt_virt))
  172. return NULL;
  173. mdesc = of_flat_dt_match_machine(mdesc_best, arch_get_next_mach);
  174. if (!mdesc) {
  175. const char *prop;
  176. int size;
  177. unsigned long dt_root;
  178. early_print("\nError: unrecognized/unsupported "
  179. "device tree compatible list:\n[ ");
  180. dt_root = of_get_flat_dt_root();
  181. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  182. while (size > 0) {
  183. early_print("'%s' ", prop);
  184. size -= strlen(prop) + 1;
  185. prop += strlen(prop) + 1;
  186. }
  187. early_print("]\n\n");
  188. dump_machine_table(); /* does not return */
  189. }
  190. /* We really don't want to do this, but sometimes firmware provides buggy data */
  191. if (mdesc->dt_fixup)
  192. mdesc->dt_fixup();
  193. early_init_dt_scan_nodes();
  194. /* Change machine number to match the mdesc we're using */
  195. __machine_arch_type = mdesc->nr;
  196. return mdesc;
  197. }