setup_percpu.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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_X86_LOCAL_APIC
  32. unsigned int num_processors;
  33. unsigned disabled_cpus __cpuinitdata;
  34. /* Processor that is doing the boot up */
  35. unsigned int boot_cpu_physical_apicid = -1U;
  36. EXPORT_SYMBOL(boot_cpu_physical_apicid);
  37. unsigned int max_physical_apicid;
  38. /* Bitmask of physically existing CPUs */
  39. physid_mask_t phys_cpu_present_map;
  40. #endif
  41. /*
  42. * Map cpu index to physical APIC ID
  43. */
  44. DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
  45. DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
  46. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
  47. EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
  48. #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
  49. #define X86_64_NUMA 1 /* (used later) */
  50. DEFINE_PER_CPU(int, node_number) = 0;
  51. EXPORT_PER_CPU_SYMBOL(node_number);
  52. /*
  53. * Map cpu index to node index
  54. */
  55. DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
  56. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
  57. /*
  58. * Which logical CPUs are on which nodes
  59. */
  60. cpumask_t *node_to_cpumask_map;
  61. EXPORT_SYMBOL(node_to_cpumask_map);
  62. /*
  63. * Setup node_to_cpumask_map
  64. */
  65. static void __init setup_node_to_cpumask_map(void);
  66. #else
  67. static inline void setup_node_to_cpumask_map(void) { }
  68. #endif
  69. #ifdef CONFIG_X86_64
  70. /* correctly size the local cpu masks */
  71. static void setup_cpu_local_masks(void)
  72. {
  73. alloc_bootmem_cpumask_var(&cpu_initialized_mask);
  74. alloc_bootmem_cpumask_var(&cpu_callin_mask);
  75. alloc_bootmem_cpumask_var(&cpu_callout_mask);
  76. alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
  77. }
  78. #else /* CONFIG_X86_32 */
  79. static inline void setup_cpu_local_masks(void)
  80. {
  81. }
  82. #endif /* CONFIG_X86_32 */
  83. #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
  84. #ifdef CONFIG_X86_64
  85. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly = {
  86. [0] = (unsigned long)__per_cpu_load,
  87. };
  88. #else
  89. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  90. #endif
  91. EXPORT_SYMBOL(__per_cpu_offset);
  92. /*
  93. * Great future plan:
  94. * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
  95. * Always point %gs to its beginning
  96. */
  97. void __init setup_per_cpu_areas(void)
  98. {
  99. ssize_t size, old_size;
  100. char *ptr;
  101. int cpu;
  102. unsigned long align = 1;
  103. /* Copy section for each CPU (we discard the original) */
  104. old_size = PERCPU_ENOUGH_ROOM;
  105. align = max_t(unsigned long, PAGE_SIZE, align);
  106. size = roundup(old_size, align);
  107. pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
  108. NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
  109. pr_info("PERCPU: Allocating %zd bytes of per cpu data\n", size);
  110. for_each_possible_cpu(cpu) {
  111. #ifndef CONFIG_NEED_MULTIPLE_NODES
  112. ptr = __alloc_bootmem(size, align,
  113. __pa(MAX_DMA_ADDRESS));
  114. #else
  115. int node = early_cpu_to_node(cpu);
  116. if (!node_online(node) || !NODE_DATA(node)) {
  117. ptr = __alloc_bootmem(size, align,
  118. __pa(MAX_DMA_ADDRESS));
  119. pr_info("cpu %d has no node %d or node-local memory\n",
  120. cpu, node);
  121. pr_debug("per cpu data for cpu%d at %016lx\n",
  122. cpu, __pa(ptr));
  123. } else {
  124. ptr = __alloc_bootmem_node(NODE_DATA(node), size, align,
  125. __pa(MAX_DMA_ADDRESS));
  126. pr_debug("per cpu data for cpu%d on node%d at %016lx\n",
  127. cpu, node, __pa(ptr));
  128. }
  129. #endif
  130. memcpy(ptr, __per_cpu_load, __per_cpu_end - __per_cpu_start);
  131. per_cpu_offset(cpu) = ptr - __per_cpu_start;
  132. per_cpu(this_cpu_off, cpu) = per_cpu_offset(cpu);
  133. per_cpu(cpu_number, cpu) = cpu;
  134. /*
  135. * Copy data used in early init routines from the initial arrays to the
  136. * per cpu data areas. These arrays then become expendable and the
  137. * *_early_ptr's are zeroed indicating that the static arrays are gone.
  138. */
  139. per_cpu(x86_cpu_to_apicid, cpu) =
  140. early_per_cpu_map(x86_cpu_to_apicid, cpu);
  141. per_cpu(x86_bios_cpu_apicid, cpu) =
  142. early_per_cpu_map(x86_bios_cpu_apicid, cpu);
  143. #ifdef X86_64_NUMA
  144. per_cpu(x86_cpu_to_node_map, cpu) =
  145. early_per_cpu_map(x86_cpu_to_node_map, cpu);
  146. #endif
  147. #ifdef CONFIG_X86_64
  148. per_cpu(irq_stack_ptr, cpu) =
  149. per_cpu(irq_stack_union.irq_stack, cpu) + IRQ_STACK_SIZE - 64;
  150. /*
  151. * Up to this point, CPU0 has been using .data.init
  152. * area. Reload %gs offset for CPU0.
  153. */
  154. if (cpu == 0)
  155. load_gs_base(cpu);
  156. #endif
  157. DBG("PERCPU: cpu %4d %p\n", cpu, ptr);
  158. }
  159. /* indicate the early static arrays will soon be gone */
  160. early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
  161. early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
  162. #ifdef X86_64_NUMA
  163. early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
  164. #endif
  165. /* Setup node to cpumask map */
  166. setup_node_to_cpumask_map();
  167. /* Setup cpu initialized, callin, callout masks */
  168. setup_cpu_local_masks();
  169. }
  170. #endif
  171. #ifdef X86_64_NUMA
  172. /*
  173. * Allocate node_to_cpumask_map based on number of available nodes
  174. * Requires node_possible_map to be valid.
  175. *
  176. * Note: node_to_cpumask() is not valid until after this is done.
  177. * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
  178. */
  179. static void __init setup_node_to_cpumask_map(void)
  180. {
  181. unsigned int node, num = 0;
  182. cpumask_t *map;
  183. /* setup nr_node_ids if not done yet */
  184. if (nr_node_ids == MAX_NUMNODES) {
  185. for_each_node_mask(node, node_possible_map)
  186. num = node;
  187. nr_node_ids = num + 1;
  188. }
  189. /* allocate the map */
  190. map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
  191. DBG("node_to_cpumask_map at %p for %d nodes\n", map, nr_node_ids);
  192. pr_debug("Node to cpumask map at %p for %d nodes\n",
  193. map, nr_node_ids);
  194. /* node_to_cpumask() will now work */
  195. node_to_cpumask_map = map;
  196. }
  197. void __cpuinit numa_set_node(int cpu, int node)
  198. {
  199. int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
  200. /* early setting, no percpu area yet */
  201. if (cpu_to_node_map) {
  202. cpu_to_node_map[cpu] = node;
  203. return;
  204. }
  205. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  206. if (cpu >= nr_cpu_ids || !per_cpu_offset(cpu)) {
  207. printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
  208. dump_stack();
  209. return;
  210. }
  211. #endif
  212. per_cpu(x86_cpu_to_node_map, cpu) = node;
  213. if (node != NUMA_NO_NODE)
  214. per_cpu(node_number, cpu) = node;
  215. }
  216. void __cpuinit numa_clear_node(int cpu)
  217. {
  218. numa_set_node(cpu, NUMA_NO_NODE);
  219. }
  220. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  221. void __cpuinit numa_add_cpu(int cpu)
  222. {
  223. cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  224. }
  225. void __cpuinit numa_remove_cpu(int cpu)
  226. {
  227. cpu_clear(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  228. }
  229. #else /* CONFIG_DEBUG_PER_CPU_MAPS */
  230. /*
  231. * --------- debug versions of the numa functions ---------
  232. */
  233. static void __cpuinit numa_set_cpumask(int cpu, int enable)
  234. {
  235. int node = early_cpu_to_node(cpu);
  236. cpumask_t *mask;
  237. char buf[64];
  238. if (node_to_cpumask_map == NULL) {
  239. printk(KERN_ERR "node_to_cpumask_map NULL\n");
  240. dump_stack();
  241. return;
  242. }
  243. mask = &node_to_cpumask_map[node];
  244. if (enable)
  245. cpu_set(cpu, *mask);
  246. else
  247. cpu_clear(cpu, *mask);
  248. cpulist_scnprintf(buf, sizeof(buf), mask);
  249. printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
  250. enable ? "numa_add_cpu" : "numa_remove_cpu", cpu, node, buf);
  251. }
  252. void __cpuinit numa_add_cpu(int cpu)
  253. {
  254. numa_set_cpumask(cpu, 1);
  255. }
  256. void __cpuinit numa_remove_cpu(int cpu)
  257. {
  258. numa_set_cpumask(cpu, 0);
  259. }
  260. int cpu_to_node(int cpu)
  261. {
  262. if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
  263. printk(KERN_WARNING
  264. "cpu_to_node(%d): usage too early!\n", cpu);
  265. dump_stack();
  266. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  267. }
  268. return per_cpu(x86_cpu_to_node_map, cpu);
  269. }
  270. EXPORT_SYMBOL(cpu_to_node);
  271. /*
  272. * Same function as cpu_to_node() but used if called before the
  273. * per_cpu areas are setup.
  274. */
  275. int early_cpu_to_node(int cpu)
  276. {
  277. if (early_per_cpu_ptr(x86_cpu_to_node_map))
  278. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  279. if (!per_cpu_offset(cpu)) {
  280. printk(KERN_WARNING
  281. "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
  282. dump_stack();
  283. return NUMA_NO_NODE;
  284. }
  285. return per_cpu(x86_cpu_to_node_map, cpu);
  286. }
  287. /* empty cpumask */
  288. static const cpumask_t cpu_mask_none;
  289. /*
  290. * Returns a pointer to the bitmask of CPUs on Node 'node'.
  291. */
  292. const cpumask_t *cpumask_of_node(int node)
  293. {
  294. if (node_to_cpumask_map == NULL) {
  295. printk(KERN_WARNING
  296. "cpumask_of_node(%d): no node_to_cpumask_map!\n",
  297. node);
  298. dump_stack();
  299. return (const cpumask_t *)&cpu_online_map;
  300. }
  301. if (node >= nr_node_ids) {
  302. printk(KERN_WARNING
  303. "cpumask_of_node(%d): node > nr_node_ids(%d)\n",
  304. node, nr_node_ids);
  305. dump_stack();
  306. return &cpu_mask_none;
  307. }
  308. return &node_to_cpumask_map[node];
  309. }
  310. EXPORT_SYMBOL(cpumask_of_node);
  311. /*
  312. * Returns a bitmask of CPUs on Node 'node'.
  313. *
  314. * Side note: this function creates the returned cpumask on the stack
  315. * so with a high NR_CPUS count, excessive stack space is used. The
  316. * node_to_cpumask_ptr function should be used whenever possible.
  317. */
  318. cpumask_t node_to_cpumask(int node)
  319. {
  320. if (node_to_cpumask_map == NULL) {
  321. printk(KERN_WARNING
  322. "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
  323. dump_stack();
  324. return cpu_online_map;
  325. }
  326. if (node >= nr_node_ids) {
  327. printk(KERN_WARNING
  328. "node_to_cpumask(%d): node > nr_node_ids(%d)\n",
  329. node, nr_node_ids);
  330. dump_stack();
  331. return cpu_mask_none;
  332. }
  333. return node_to_cpumask_map[node];
  334. }
  335. EXPORT_SYMBOL(node_to_cpumask);
  336. /*
  337. * --------- end of debug versions of the numa functions ---------
  338. */
  339. #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
  340. #endif /* X86_64_NUMA */