setup.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/init.h>
  4. #include <linux/bootmem.h>
  5. #include <linux/percpu.h>
  6. #include <asm/smp.h>
  7. #include <asm/percpu.h>
  8. #include <asm/sections.h>
  9. #include <asm/processor.h>
  10. #include <asm/setup.h>
  11. #include <asm/topology.h>
  12. #include <asm/mpspec.h>
  13. #include <asm/apicdef.h>
  14. #ifdef CONFIG_X86_LOCAL_APIC
  15. unsigned int num_processors;
  16. unsigned disabled_cpus __cpuinitdata;
  17. /* Processor that is doing the boot up */
  18. unsigned int boot_cpu_physical_apicid = -1U;
  19. EXPORT_SYMBOL(boot_cpu_physical_apicid);
  20. /* Bitmask of physically existing CPUs */
  21. physid_mask_t phys_cpu_present_map;
  22. #endif
  23. /* map cpu index to physical APIC ID */
  24. DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
  25. DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
  26. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
  27. EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
  28. #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
  29. #define X86_64_NUMA 1
  30. /* map cpu index to node index */
  31. DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
  32. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
  33. #endif
  34. #if defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) && defined(CONFIG_X86_SMP)
  35. /*
  36. * Copy data used in early init routines from the initial arrays to the
  37. * per cpu data areas. These arrays then become expendable and the
  38. * *_early_ptr's are zeroed indicating that the static arrays are gone.
  39. */
  40. static void __init setup_per_cpu_maps(void)
  41. {
  42. int cpu;
  43. for_each_possible_cpu(cpu) {
  44. per_cpu(x86_cpu_to_apicid, cpu) =
  45. early_per_cpu_map(x86_cpu_to_apicid, cpu);
  46. per_cpu(x86_bios_cpu_apicid, cpu) =
  47. early_per_cpu_map(x86_bios_cpu_apicid, cpu);
  48. #ifdef X86_64_NUMA
  49. per_cpu(x86_cpu_to_node_map, cpu) =
  50. early_per_cpu_map(x86_cpu_to_node_map, cpu);
  51. #endif
  52. }
  53. /* indicate the early static arrays will soon be gone */
  54. early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
  55. early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
  56. #ifdef X86_64_NUMA
  57. early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
  58. #endif
  59. }
  60. #ifdef CONFIG_HAVE_CPUMASK_OF_CPU_MAP
  61. cpumask_t *cpumask_of_cpu_map __read_mostly;
  62. EXPORT_SYMBOL(cpumask_of_cpu_map);
  63. /* requires nr_cpu_ids to be initialized */
  64. static void __init setup_cpumask_of_cpu(void)
  65. {
  66. int i;
  67. /* alloc_bootmem zeroes memory */
  68. cpumask_of_cpu_map = alloc_bootmem_low(sizeof(cpumask_t) * nr_cpu_ids);
  69. for (i = 0; i < nr_cpu_ids; i++)
  70. cpu_set(i, cpumask_of_cpu_map[i]);
  71. }
  72. #else
  73. static inline void setup_cpumask_of_cpu(void) { }
  74. #endif
  75. #ifdef CONFIG_X86_32
  76. /*
  77. * Great future not-so-futuristic plan: make i386 and x86_64 do it
  78. * the same way
  79. */
  80. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  81. EXPORT_SYMBOL(__per_cpu_offset);
  82. #endif
  83. /*
  84. * Great future plan:
  85. * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
  86. * Always point %gs to its beginning
  87. */
  88. void __init setup_per_cpu_areas(void)
  89. {
  90. int i, highest_cpu = 0;
  91. unsigned long size;
  92. #ifdef CONFIG_HOTPLUG_CPU
  93. prefill_possible_map();
  94. #endif
  95. /* Copy section for each CPU (we discard the original) */
  96. size = PERCPU_ENOUGH_ROOM;
  97. printk(KERN_INFO "PERCPU: Allocating %lu bytes of per cpu data\n",
  98. size);
  99. for_each_possible_cpu(i) {
  100. char *ptr;
  101. #ifndef CONFIG_NEED_MULTIPLE_NODES
  102. ptr = alloc_bootmem_pages(size);
  103. #else
  104. int node = early_cpu_to_node(i);
  105. if (!node_online(node) || !NODE_DATA(node)) {
  106. ptr = alloc_bootmem_pages(size);
  107. printk(KERN_INFO
  108. "cpu %d has no node %d or node-local memory\n",
  109. i, node);
  110. }
  111. else
  112. ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
  113. #endif
  114. if (!ptr)
  115. panic("Cannot allocate cpu data for CPU %d\n", i);
  116. #ifdef CONFIG_X86_64
  117. cpu_pda(i)->data_offset = ptr - __per_cpu_start;
  118. #else
  119. __per_cpu_offset[i] = ptr - __per_cpu_start;
  120. #endif
  121. memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
  122. highest_cpu = i;
  123. }
  124. nr_cpu_ids = highest_cpu + 1;
  125. printk(KERN_DEBUG "NR_CPUS: %d, nr_cpu_ids: %d\n", NR_CPUS, nr_cpu_ids);
  126. /* Setup percpu data maps */
  127. setup_per_cpu_maps();
  128. /* Setup cpumask_of_cpu map */
  129. setup_cpumask_of_cpu();
  130. }
  131. #endif
  132. #ifdef X86_64_NUMA
  133. void __cpuinit numa_set_node(int cpu, int node)
  134. {
  135. int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
  136. if (node != NUMA_NO_NODE)
  137. cpu_pda(cpu)->nodenumber = node;
  138. if (cpu_to_node_map)
  139. cpu_to_node_map[cpu] = node;
  140. else if (per_cpu_offset(cpu))
  141. per_cpu(x86_cpu_to_node_map, cpu) = node;
  142. else
  143. Dprintk(KERN_INFO "Setting node for non-present cpu %d\n", cpu);
  144. }
  145. void __cpuinit numa_clear_node(int cpu)
  146. {
  147. numa_set_node(cpu, NUMA_NO_NODE);
  148. }
  149. void __cpuinit numa_add_cpu(int cpu)
  150. {
  151. cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  152. }
  153. void __cpuinit numa_remove_cpu(int cpu)
  154. {
  155. cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]);
  156. }
  157. #endif /* CONFIG_NUMA */
  158. #if defined(CONFIG_DEBUG_PER_CPU_MAPS) && defined(CONFIG_X86_64)
  159. int cpu_to_node(int cpu)
  160. {
  161. if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
  162. printk(KERN_WARNING
  163. "cpu_to_node(%d): usage too early!\n", cpu);
  164. dump_stack();
  165. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  166. }
  167. return per_cpu(x86_cpu_to_node_map, cpu);
  168. }
  169. EXPORT_SYMBOL(cpu_to_node);
  170. int early_cpu_to_node(int cpu)
  171. {
  172. if (early_per_cpu_ptr(x86_cpu_to_node_map))
  173. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  174. if (!per_cpu_offset(cpu)) {
  175. printk(KERN_WARNING
  176. "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
  177. dump_stack();
  178. return NUMA_NO_NODE;
  179. }
  180. return per_cpu(x86_cpu_to_node_map, cpu);
  181. }
  182. #endif