setup_percpu.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 <asm/smp.h>
  9. #include <asm/percpu.h>
  10. #include <asm/sections.h>
  11. #include <asm/processor.h>
  12. #include <asm/setup.h>
  13. #include <asm/topology.h>
  14. #include <asm/mpspec.h>
  15. #include <asm/apicdef.h>
  16. #include <asm/highmem.h>
  17. #ifdef CONFIG_X86_LOCAL_APIC
  18. unsigned int num_processors;
  19. unsigned disabled_cpus __cpuinitdata;
  20. /* Processor that is doing the boot up */
  21. unsigned int boot_cpu_physical_apicid = -1U;
  22. unsigned int max_physical_apicid;
  23. EXPORT_SYMBOL(boot_cpu_physical_apicid);
  24. /* Bitmask of physically existing CPUs */
  25. physid_mask_t phys_cpu_present_map;
  26. #endif
  27. /* map cpu index to physical APIC ID */
  28. DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
  29. DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
  30. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
  31. EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
  32. #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
  33. #define X86_64_NUMA 1
  34. /* map cpu index to node index */
  35. DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
  36. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
  37. /* which logical CPUs are on which nodes */
  38. cpumask_t *node_to_cpumask_map;
  39. EXPORT_SYMBOL(node_to_cpumask_map);
  40. /* setup node_to_cpumask_map */
  41. static void __init setup_node_to_cpumask_map(void);
  42. #else
  43. static inline void setup_node_to_cpumask_map(void) { }
  44. #endif
  45. #if defined(CONFIG_HAVE_SETUP_PER_CPU_AREA) && defined(CONFIG_X86_SMP)
  46. /*
  47. * Copy data used in early init routines from the initial arrays to the
  48. * per cpu data areas. These arrays then become expendable and the
  49. * *_early_ptr's are zeroed indicating that the static arrays are gone.
  50. */
  51. static void __init setup_per_cpu_maps(void)
  52. {
  53. int cpu;
  54. for_each_possible_cpu(cpu) {
  55. per_cpu(x86_cpu_to_apicid, cpu) =
  56. early_per_cpu_map(x86_cpu_to_apicid, cpu);
  57. per_cpu(x86_bios_cpu_apicid, cpu) =
  58. early_per_cpu_map(x86_bios_cpu_apicid, cpu);
  59. #ifdef X86_64_NUMA
  60. per_cpu(x86_cpu_to_node_map, cpu) =
  61. early_per_cpu_map(x86_cpu_to_node_map, cpu);
  62. #endif
  63. }
  64. /* indicate the early static arrays will soon be gone */
  65. early_per_cpu_ptr(x86_cpu_to_apicid) = NULL;
  66. early_per_cpu_ptr(x86_bios_cpu_apicid) = NULL;
  67. #ifdef X86_64_NUMA
  68. early_per_cpu_ptr(x86_cpu_to_node_map) = NULL;
  69. #endif
  70. }
  71. #ifdef CONFIG_X86_32
  72. /*
  73. * Great future not-so-futuristic plan: make i386 and x86_64 do it
  74. * the same way
  75. */
  76. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  77. EXPORT_SYMBOL(__per_cpu_offset);
  78. static inline void setup_cpu_pda_map(void) { }
  79. #elif !defined(CONFIG_SMP)
  80. static inline void setup_cpu_pda_map(void) { }
  81. #else /* CONFIG_SMP && CONFIG_X86_64 */
  82. /*
  83. * Allocate cpu_pda pointer table and array via alloc_bootmem.
  84. */
  85. static void __init setup_cpu_pda_map(void)
  86. {
  87. char *pda;
  88. struct x8664_pda **new_cpu_pda;
  89. unsigned long size;
  90. int cpu;
  91. size = roundup(sizeof(struct x8664_pda), cache_line_size());
  92. /* allocate cpu_pda array and pointer table */
  93. {
  94. unsigned long tsize = nr_cpu_ids * sizeof(void *);
  95. unsigned long asize = size * (nr_cpu_ids - 1);
  96. tsize = roundup(tsize, cache_line_size());
  97. new_cpu_pda = alloc_bootmem(tsize + asize);
  98. pda = (char *)new_cpu_pda + tsize;
  99. }
  100. /* initialize pointer table to static pda's */
  101. for_each_possible_cpu(cpu) {
  102. if (cpu == 0) {
  103. /* leave boot cpu pda in place */
  104. new_cpu_pda[0] = cpu_pda(0);
  105. continue;
  106. }
  107. new_cpu_pda[cpu] = (struct x8664_pda *)pda;
  108. new_cpu_pda[cpu]->in_bootmem = 1;
  109. pda += size;
  110. }
  111. /* point to new pointer table */
  112. _cpu_pda = new_cpu_pda;
  113. }
  114. #endif /* CONFIG_SMP && CONFIG_X86_64 */
  115. #ifdef CONFIG_X86_64
  116. /* correctly size the local cpu masks */
  117. static void setup_cpu_local_masks(void)
  118. {
  119. alloc_bootmem_cpumask_var(&cpu_initialized_mask);
  120. alloc_bootmem_cpumask_var(&cpu_callin_mask);
  121. alloc_bootmem_cpumask_var(&cpu_callout_mask);
  122. alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
  123. }
  124. #else /* CONFIG_X86_32 */
  125. static inline void setup_cpu_local_masks(void)
  126. {
  127. }
  128. #endif /* CONFIG_X86_32 */
  129. /*
  130. * Great future plan:
  131. * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
  132. * Always point %gs to its beginning
  133. */
  134. void __init setup_per_cpu_areas(void)
  135. {
  136. ssize_t size, old_size;
  137. char *ptr;
  138. int cpu;
  139. unsigned long align = 1;
  140. /* Setup cpu_pda map */
  141. setup_cpu_pda_map();
  142. /* Copy section for each CPU (we discard the original) */
  143. old_size = PERCPU_ENOUGH_ROOM;
  144. align = max_t(unsigned long, PAGE_SIZE, align);
  145. size = roundup(old_size, align);
  146. pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
  147. NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
  148. pr_info("PERCPU: Allocating %zd bytes of per cpu data\n", size);
  149. for_each_possible_cpu(cpu) {
  150. #ifndef CONFIG_NEED_MULTIPLE_NODES
  151. ptr = __alloc_bootmem(size, align,
  152. __pa(MAX_DMA_ADDRESS));
  153. #else
  154. int node = early_cpu_to_node(cpu);
  155. if (!node_online(node) || !NODE_DATA(node)) {
  156. ptr = __alloc_bootmem(size, align,
  157. __pa(MAX_DMA_ADDRESS));
  158. pr_info("cpu %d has no node %d or node-local memory\n",
  159. cpu, node);
  160. pr_debug("per cpu data for cpu%d at %016lx\n",
  161. cpu, __pa(ptr));
  162. } else {
  163. ptr = __alloc_bootmem_node(NODE_DATA(node), size, align,
  164. __pa(MAX_DMA_ADDRESS));
  165. pr_debug("per cpu data for cpu%d on node%d at %016lx\n",
  166. cpu, node, __pa(ptr));
  167. }
  168. #endif
  169. per_cpu_offset(cpu) = ptr - __per_cpu_start;
  170. memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
  171. }
  172. /* Setup percpu data maps */
  173. setup_per_cpu_maps();
  174. /* Setup node to cpumask map */
  175. setup_node_to_cpumask_map();
  176. /* Setup cpu initialized, callin, callout masks */
  177. setup_cpu_local_masks();
  178. }
  179. #endif
  180. #ifdef X86_64_NUMA
  181. /*
  182. * Allocate node_to_cpumask_map based on number of available nodes
  183. * Requires node_possible_map to be valid.
  184. *
  185. * Note: node_to_cpumask() is not valid until after this is done.
  186. */
  187. static void __init setup_node_to_cpumask_map(void)
  188. {
  189. unsigned int node, num = 0;
  190. cpumask_t *map;
  191. /* setup nr_node_ids if not done yet */
  192. if (nr_node_ids == MAX_NUMNODES) {
  193. for_each_node_mask(node, node_possible_map)
  194. num = node;
  195. nr_node_ids = num + 1;
  196. }
  197. /* allocate the map */
  198. map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
  199. pr_debug("Node to cpumask map at %p for %d nodes\n",
  200. map, nr_node_ids);
  201. /* node_to_cpumask() will now work */
  202. node_to_cpumask_map = map;
  203. }
  204. void __cpuinit numa_set_node(int cpu, int node)
  205. {
  206. int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
  207. if (cpu_pda(cpu) && node != NUMA_NO_NODE)
  208. cpu_pda(cpu)->nodenumber = node;
  209. if (cpu_to_node_map)
  210. cpu_to_node_map[cpu] = node;
  211. else if (per_cpu_offset(cpu))
  212. per_cpu(x86_cpu_to_node_map, cpu) = node;
  213. else
  214. pr_debug("Setting node for non-present cpu %d\n", cpu);
  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[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 = 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 */