devtree.c 6.5 KB

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