setup_percpu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. struct x8664_pda **new_cpu_pda;
  102. unsigned long size;
  103. int cpu;
  104. size = roundup(sizeof(struct x8664_pda), cache_line_size());
  105. /* allocate cpu_pda array and pointer table */
  106. {
  107. unsigned long tsize = nr_cpu_ids * sizeof(void *);
  108. unsigned long asize = size * (nr_cpu_ids - 1);
  109. tsize = roundup(tsize, cache_line_size());
  110. new_cpu_pda = alloc_bootmem(tsize + asize);
  111. pda = (char *)new_cpu_pda + tsize;
  112. }
  113. /* initialize pointer table to static pda's */
  114. for_each_possible_cpu(cpu) {
  115. if (cpu == 0) {
  116. /* leave boot cpu pda in place */
  117. new_cpu_pda[0] = cpu_pda(0);
  118. continue;
  119. }
  120. new_cpu_pda[cpu] = (struct x8664_pda *)pda;
  121. new_cpu_pda[cpu]->in_bootmem = 1;
  122. pda += size;
  123. }
  124. /* point to new pointer table */
  125. _cpu_pda = new_cpu_pda;
  126. }
  127. #endif /* CONFIG_SMP && CONFIG_X86_64 */
  128. #ifdef CONFIG_X86_64
  129. /* correctly size the local cpu masks */
  130. static void setup_cpu_local_masks(void)
  131. {
  132. alloc_bootmem_cpumask_var(&cpu_initialized_mask);
  133. alloc_bootmem_cpumask_var(&cpu_callin_mask);
  134. alloc_bootmem_cpumask_var(&cpu_callout_mask);
  135. alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
  136. }
  137. #else /* CONFIG_X86_32 */
  138. static inline void setup_cpu_local_masks(void)
  139. {
  140. }
  141. #endif /* CONFIG_X86_32 */
  142. /*
  143. * Great future plan:
  144. * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
  145. * Always point %gs to its beginning
  146. */
  147. void __init setup_per_cpu_areas(void)
  148. {
  149. ssize_t size, old_size;
  150. char *ptr;
  151. int cpu;
  152. unsigned long align = 1;
  153. /* Setup cpu_pda map */
  154. setup_cpu_pda_map();
  155. /* Copy section for each CPU (we discard the original) */
  156. old_size = PERCPU_ENOUGH_ROOM;
  157. align = max_t(unsigned long, PAGE_SIZE, align);
  158. size = roundup(old_size, align);
  159. pr_info("NR_CPUS:%d nr_cpumask_bits:%d nr_cpu_ids:%d nr_node_ids:%d\n",
  160. NR_CPUS, nr_cpumask_bits, nr_cpu_ids, nr_node_ids);
  161. pr_info("PERCPU: Allocating %zd bytes of per cpu data\n", size);
  162. for_each_possible_cpu(cpu) {
  163. #ifndef CONFIG_NEED_MULTIPLE_NODES
  164. ptr = __alloc_bootmem(size, align,
  165. __pa(MAX_DMA_ADDRESS));
  166. #else
  167. int node = early_cpu_to_node(cpu);
  168. if (!node_online(node) || !NODE_DATA(node)) {
  169. ptr = __alloc_bootmem(size, align,
  170. __pa(MAX_DMA_ADDRESS));
  171. pr_info("cpu %d has no node %d or node-local memory\n",
  172. cpu, node);
  173. pr_debug("per cpu data for cpu%d at %016lx\n",
  174. cpu, __pa(ptr));
  175. } else {
  176. ptr = __alloc_bootmem_node(NODE_DATA(node), size, align,
  177. __pa(MAX_DMA_ADDRESS));
  178. pr_debug("per cpu data for cpu%d on node%d at %016lx\n",
  179. cpu, node, __pa(ptr));
  180. }
  181. #endif
  182. per_cpu_offset(cpu) = ptr - __per_cpu_start;
  183. memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
  184. DBG("PERCPU: cpu %4d %p\n", cpu, ptr);
  185. }
  186. /* Setup percpu data maps */
  187. setup_per_cpu_maps();
  188. /* Setup node to cpumask map */
  189. setup_node_to_cpumask_map();
  190. /* Setup cpu initialized, callin, callout masks */
  191. setup_cpu_local_masks();
  192. }
  193. #endif
  194. #ifdef X86_64_NUMA
  195. /*
  196. * Allocate node_to_cpumask_map based on number of available nodes
  197. * Requires node_possible_map to be valid.
  198. *
  199. * Note: node_to_cpumask() is not valid until after this is done.
  200. * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
  201. */
  202. static void __init setup_node_to_cpumask_map(void)
  203. {
  204. unsigned int node, num = 0;
  205. cpumask_t *map;
  206. /* setup nr_node_ids if not done yet */
  207. if (nr_node_ids == MAX_NUMNODES) {
  208. for_each_node_mask(node, node_possible_map)
  209. num = node;
  210. nr_node_ids = num + 1;
  211. }
  212. /* allocate the map */
  213. map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
  214. DBG("node_to_cpumask_map at %p for %d nodes\n", map, nr_node_ids);
  215. pr_debug("Node to cpumask map at %p for %d nodes\n",
  216. map, nr_node_ids);
  217. /* node_to_cpumask() will now work */
  218. node_to_cpumask_map = map;
  219. }
  220. void __cpuinit numa_set_node(int cpu, int node)
  221. {
  222. int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
  223. /* early setting, no percpu area yet */
  224. if (cpu_to_node_map) {
  225. cpu_to_node_map[cpu] = node;
  226. return;
  227. }
  228. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  229. if (cpu >= nr_cpu_ids || !per_cpu_offset(cpu)) {
  230. printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
  231. dump_stack();
  232. return;
  233. }
  234. #endif
  235. per_cpu(x86_cpu_to_node_map, cpu) = node;
  236. if (node != NUMA_NO_NODE)
  237. cpu_pda(cpu)->nodenumber = node;
  238. }
  239. void __cpuinit numa_clear_node(int cpu)
  240. {
  241. numa_set_node(cpu, NUMA_NO_NODE);
  242. }
  243. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  244. void __cpuinit numa_add_cpu(int cpu)
  245. {
  246. cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  247. }
  248. void __cpuinit numa_remove_cpu(int cpu)
  249. {
  250. cpu_clear(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  251. }
  252. #else /* CONFIG_DEBUG_PER_CPU_MAPS */
  253. /*
  254. * --------- debug versions of the numa functions ---------
  255. */
  256. static void __cpuinit numa_set_cpumask(int cpu, int enable)
  257. {
  258. int node = early_cpu_to_node(cpu);
  259. cpumask_t *mask;
  260. char buf[64];
  261. if (node_to_cpumask_map == NULL) {
  262. printk(KERN_ERR "node_to_cpumask_map NULL\n");
  263. dump_stack();
  264. return;
  265. }
  266. mask = &node_to_cpumask_map[node];
  267. if (enable)
  268. cpu_set(cpu, *mask);
  269. else
  270. cpu_clear(cpu, *mask);
  271. cpulist_scnprintf(buf, sizeof(buf), mask);
  272. printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
  273. enable ? "numa_add_cpu" : "numa_remove_cpu", cpu, node, buf);
  274. }
  275. void __cpuinit numa_add_cpu(int cpu)
  276. {
  277. numa_set_cpumask(cpu, 1);
  278. }
  279. void __cpuinit numa_remove_cpu(int cpu)
  280. {
  281. numa_set_cpumask(cpu, 0);
  282. }
  283. int cpu_to_node(int cpu)
  284. {
  285. if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
  286. printk(KERN_WARNING
  287. "cpu_to_node(%d): usage too early!\n", cpu);
  288. dump_stack();
  289. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  290. }
  291. return per_cpu(x86_cpu_to_node_map, cpu);
  292. }
  293. EXPORT_SYMBOL(cpu_to_node);
  294. /*
  295. * Same function as cpu_to_node() but used if called before the
  296. * per_cpu areas are setup.
  297. */
  298. int early_cpu_to_node(int cpu)
  299. {
  300. if (early_per_cpu_ptr(x86_cpu_to_node_map))
  301. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  302. if (!per_cpu_offset(cpu)) {
  303. printk(KERN_WARNING
  304. "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
  305. dump_stack();
  306. return NUMA_NO_NODE;
  307. }
  308. return per_cpu(x86_cpu_to_node_map, cpu);
  309. }
  310. /* empty cpumask */
  311. static const cpumask_t cpu_mask_none;
  312. /*
  313. * Returns a pointer to the bitmask of CPUs on Node 'node'.
  314. */
  315. const cpumask_t *cpumask_of_node(int node)
  316. {
  317. if (node_to_cpumask_map == NULL) {
  318. printk(KERN_WARNING
  319. "cpumask_of_node(%d): no node_to_cpumask_map!\n",
  320. node);
  321. dump_stack();
  322. return (const cpumask_t *)&cpu_online_map;
  323. }
  324. if (node >= nr_node_ids) {
  325. printk(KERN_WARNING
  326. "cpumask_of_node(%d): node > nr_node_ids(%d)\n",
  327. node, nr_node_ids);
  328. dump_stack();
  329. return &cpu_mask_none;
  330. }
  331. return &node_to_cpumask_map[node];
  332. }
  333. EXPORT_SYMBOL(cpumask_of_node);
  334. /*
  335. * Returns a bitmask of CPUs on Node 'node'.
  336. *
  337. * Side note: this function creates the returned cpumask on the stack
  338. * so with a high NR_CPUS count, excessive stack space is used. The
  339. * node_to_cpumask_ptr function should be used whenever possible.
  340. */
  341. cpumask_t node_to_cpumask(int node)
  342. {
  343. if (node_to_cpumask_map == NULL) {
  344. printk(KERN_WARNING
  345. "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
  346. dump_stack();
  347. return cpu_online_map;
  348. }
  349. if (node >= nr_node_ids) {
  350. printk(KERN_WARNING
  351. "node_to_cpumask(%d): node > nr_node_ids(%d)\n",
  352. node, nr_node_ids);
  353. dump_stack();
  354. return cpu_mask_none;
  355. }
  356. return node_to_cpumask_map[node];
  357. }
  358. EXPORT_SYMBOL(node_to_cpumask);
  359. /*
  360. * --------- end of debug versions of the numa functions ---------
  361. */
  362. #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
  363. #endif /* X86_64_NUMA */