devtree.c 6.6 KB

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