setup_percpu.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <linux/kexec.h>
  7. #include <linux/crash_dump.h>
  8. #include <linux/smp.h>
  9. #include <linux/topology.h>
  10. #include <asm/sections.h>
  11. #include <asm/processor.h>
  12. #include <asm/setup.h>
  13. #include <asm/mpspec.h>
  14. #include <asm/apicdef.h>
  15. #include <asm/highmem.h>
  16. #include <asm/proto.h>
  17. #include <asm/cpumask.h>
  18. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  19. # define DBG(x...) printk(KERN_DEBUG x)
  20. #else
  21. # define DBG(x...)
  22. #endif
  23. /*
  24. * Could be inside CONFIG_HAVE_SETUP_PER_CPU_AREA with other stuff but
  25. * voyager wants cpu_number too.
  26. */
  27. #ifdef CONFIG_SMP
  28. DEFINE_PER_CPU(int, cpu_number);
  29. EXPORT_PER_CPU_SYMBOL(cpu_number);
  30. #endif
  31. #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
  32. #ifdef CONFIG_X86_64
  33. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = {
  34. [0] = (unsigned long)__per_cpu_load,
  35. };
  36. #else
  37. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  38. #endif
  39. EXPORT_SYMBOL(__per_cpu_offset);
  40. /*
  41. * Great future plan:
  42. * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
  43. * Always point %gs to its beginning
  44. */
  45. void __init setup_per_cpu_areas(void)
  46. {
  47. ssize_t size;
  48. char *ptr;
  49. int cpu;
  50. /* Copy section for each CPU (we discard the original) */
  51. size = roundup(PERCPU_ENOUGH_ROOM, PAGE_SIZE);
  52. pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
  53. NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
  54. pr_info("PERCPU: Allocating %zd bytes of per cpu data\n", size);
  55. for_each_possible_cpu(cpu) {
  56. #ifndef CONFIG_NEED_MULTIPLE_NODES
  57. ptr = alloc_bootmem_pages(size);
  58. #else
  59. int node = early_cpu_to_node(cpu);
  60. if (!node_online(node) || !NODE_DATA(node)) {
  61. ptr = alloc_bootmem_pages(size);
  62. pr_info("cpu %d has no node %d or node-local memory\n",
  63. cpu, node);
  64. pr_debug("per cpu data for cpu%d at %016lx\n",
  65. cpu, __pa(ptr));
  66. } else {
  67. ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
  68. pr_debug("per cpu data for cpu%d on node%d at %016lx\n",
  69. cpu, node, __pa(ptr));
  70. }
  71. #endif
  72. memcpy(ptr, __per_cpu_load, __per_cpu_end - __per_cpu_start);
  73. per_cpu_offset(cpu) = ptr - __per_cpu_start;
  74. per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
  75. per_cpu(cpu_number, cpu) = cpu;
  76. /*
  77. * Copy data used in early init routines from the initial arrays to the
  78. * per cpu data areas. These arrays then become expendable and the
  79. * *_early_ptr's are zeroed indicating that the static arrays are gone.
  80. */
  81. #ifdef CONFIG_X86_LOCAL_APIC
  82. per_cpu(x86_cpu_to_apicid, cpu) =
  83. early_per_cpu_map(x86_cpu_to_apicid, cpu);
  84. per_cpu(x86_bios_cpu_apicid, cpu) =
  85. early_per_cpu_map(x86_bios_cpu_apicid, cpu);
  86. #endif
  87. #ifdef CONFIG_X86_64
  88. per_cpu(irq_stack_ptr, cpu) =
  89. per_cpu(irq_stack_union.irq_stack, cpu) + IRQ_STACK_SIZE - 64;
  90. #ifdef CONFIG_NUMA
  91. per_cpu(x86_cpu_to_node_map, cpu) =
  92. early_per_cpu_map(x86_cpu_to_node_map, cpu);
  93. #endif
  94. /*
  95. * Up to this point, CPU0 has been using .data.init
  96. * area. Reload %gs offset for CPU0.
  97. */
  98. if (cpu == 0)
  99. load_gs_base(cpu);
  100. #endif
  101. DBG("PERCPU: cpu %4d %p\n", cpu, ptr);
  102. }
  103. /* indicate the early static arrays will soon be gone */
  104. early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
  105. early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
  106. #if defined(CONFIG_X86_64) && defined(CONFIG_NUMA)
  107. early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
  108. #endif
  109. /* Setup node to cpumask map */
  110. setup_node_to_cpumask_map();
  111. /* Setup cpu initialized, callin, callout masks */
  112. setup_cpu_local_masks();
  113. }
  114. #endif