setup_percpu.c 9.9 KB

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