setup_percpu.c 9.8 KB

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