devtree.c 6.2 KB

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