devtree.c 6.6 KB

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