devtree.c 6.9 KB

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