setup_percpu.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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_HAVE_CPUMASK_OF_CPU_MAP
  72. /*
  73. * Replace static cpumask_of_cpu_map in the initdata section,
  74. * with one that's allocated sized by the possible number of cpus.
  75. *
  76. * (requires nr_cpu_ids to be initialized)
  77. */
  78. static void __init setup_cpumask_of_cpu(void)
  79. {
  80. int i;
  81. /* alloc_bootmem zeroes memory */
  82. cpumask_of_cpu_map = alloc_bootmem_low(sizeof(cpumask_t) * nr_cpu_ids);
  83. for (i = 0; i < nr_cpu_ids; i++)
  84. cpu_set(i, cpumask_of_cpu_map[i]);
  85. }
  86. #else
  87. static inline void setup_cpumask_of_cpu(void) { }
  88. #endif
  89. #ifdef CONFIG_X86_32
  90. /*
  91. * Great future not-so-futuristic plan: make i386 and x86_64 do it
  92. * the same way
  93. */
  94. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  95. EXPORT_SYMBOL(__per_cpu_offset);
  96. static inline void setup_cpu_pda_map(void) { }
  97. #elif !defined(CONFIG_SMP)
  98. static inline void setup_cpu_pda_map(void) { }
  99. #else /* CONFIG_SMP && CONFIG_X86_64 */
  100. /*
  101. * Allocate cpu_pda pointer table and array via alloc_bootmem.
  102. */
  103. static void __init setup_cpu_pda_map(void)
  104. {
  105. char *pda;
  106. struct x8664_pda **new_cpu_pda;
  107. unsigned long size;
  108. int cpu;
  109. size = roundup(sizeof(struct x8664_pda), cache_line_size());
  110. /* allocate cpu_pda array and pointer table */
  111. {
  112. unsigned long tsize = nr_cpu_ids * sizeof(void *);
  113. unsigned long asize = size * (nr_cpu_ids - 1);
  114. tsize = roundup(tsize, cache_line_size());
  115. new_cpu_pda = alloc_bootmem(tsize + asize);
  116. pda = (char *)new_cpu_pda + tsize;
  117. }
  118. /* initialize pointer table to static pda's */
  119. for_each_possible_cpu(cpu) {
  120. if (cpu == 0) {
  121. /* leave boot cpu pda in place */
  122. new_cpu_pda[0] = cpu_pda(0);
  123. continue;
  124. }
  125. new_cpu_pda[cpu] = (struct x8664_pda *)pda;
  126. new_cpu_pda[cpu]->in_bootmem = 1;
  127. pda += size;
  128. }
  129. /* point to new pointer table */
  130. _cpu_pda = new_cpu_pda;
  131. }
  132. #endif
  133. /*
  134. * Great future plan:
  135. * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
  136. * Always point %gs to its beginning
  137. */
  138. void __init setup_per_cpu_areas(void)
  139. {
  140. ssize_t size = PERCPU_ENOUGH_ROOM;
  141. char *ptr;
  142. int cpu;
  143. /* Setup cpu_pda map */
  144. setup_cpu_pda_map();
  145. /* Copy section for each CPU (we discard the original) */
  146. size = PERCPU_ENOUGH_ROOM;
  147. printk(KERN_INFO "PERCPU: Allocating %zd bytes of per cpu data\n",
  148. size);
  149. for_each_possible_cpu(cpu) {
  150. #ifndef CONFIG_NEED_MULTIPLE_NODES
  151. ptr = alloc_bootmem_pages(size);
  152. #else
  153. int node = early_cpu_to_node(cpu);
  154. if (!node_online(node) || !NODE_DATA(node)) {
  155. ptr = alloc_bootmem_pages(size);
  156. printk(KERN_INFO
  157. "cpu %d has no node %d or node-local memory\n",
  158. cpu, node);
  159. }
  160. else
  161. ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
  162. #endif
  163. per_cpu_offset(cpu) = ptr - __per_cpu_start;
  164. memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
  165. }
  166. printk(KERN_DEBUG "NR_CPUS: %d, nr_cpu_ids: %d, nr_node_ids %d\n",
  167. NR_CPUS, nr_cpu_ids, nr_node_ids);
  168. /* Setup percpu data maps */
  169. setup_per_cpu_maps();
  170. /* Setup node to cpumask map */
  171. setup_node_to_cpumask_map();
  172. /* Setup cpumask_of_cpu map */
  173. setup_cpumask_of_cpu();
  174. }
  175. #endif
  176. #ifdef X86_64_NUMA
  177. /*
  178. * Allocate node_to_cpumask_map based on number of available nodes
  179. * Requires node_possible_map to be valid.
  180. *
  181. * Note: node_to_cpumask() is not valid until after this is done.
  182. */
  183. static void __init setup_node_to_cpumask_map(void)
  184. {
  185. unsigned int node, num = 0;
  186. cpumask_t *map;
  187. /* setup nr_node_ids if not done yet */
  188. if (nr_node_ids == MAX_NUMNODES) {
  189. for_each_node_mask(node, node_possible_map)
  190. num = node;
  191. nr_node_ids = num + 1;
  192. }
  193. /* allocate the map */
  194. map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
  195. pr_debug(KERN_DEBUG "Node to cpumask map at %p for %d nodes\n",
  196. map, nr_node_ids);
  197. /* node_to_cpumask() will now work */
  198. node_to_cpumask_map = map;
  199. }
  200. void __cpuinit numa_set_node(int cpu, int node)
  201. {
  202. int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
  203. if (cpu_pda(cpu) && node != NUMA_NO_NODE)
  204. cpu_pda(cpu)->nodenumber = node;
  205. if (cpu_to_node_map)
  206. cpu_to_node_map[cpu] = node;
  207. else if (per_cpu_offset(cpu))
  208. per_cpu(x86_cpu_to_node_map, cpu) = node;
  209. else
  210. pr_debug("Setting node for non-present cpu %d\n", cpu);
  211. }
  212. void __cpuinit numa_clear_node(int cpu)
  213. {
  214. numa_set_node(cpu, NUMA_NO_NODE);
  215. }
  216. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  217. void __cpuinit numa_add_cpu(int cpu)
  218. {
  219. cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  220. }
  221. void __cpuinit numa_remove_cpu(int cpu)
  222. {
  223. cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]);
  224. }
  225. #else /* CONFIG_DEBUG_PER_CPU_MAPS */
  226. /*
  227. * --------- debug versions of the numa functions ---------
  228. */
  229. static void __cpuinit numa_set_cpumask(int cpu, int enable)
  230. {
  231. int node = cpu_to_node(cpu);
  232. cpumask_t *mask;
  233. char buf[64];
  234. if (node_to_cpumask_map == NULL) {
  235. printk(KERN_ERR "node_to_cpumask_map NULL\n");
  236. dump_stack();
  237. return;
  238. }
  239. mask = &node_to_cpumask_map[node];
  240. if (enable)
  241. cpu_set(cpu, *mask);
  242. else
  243. cpu_clear(cpu, *mask);
  244. cpulist_scnprintf(buf, sizeof(buf), *mask);
  245. printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
  246. enable? "numa_add_cpu":"numa_remove_cpu", cpu, node, buf);
  247. }
  248. void __cpuinit numa_add_cpu(int cpu)
  249. {
  250. numa_set_cpumask(cpu, 1);
  251. }
  252. void __cpuinit numa_remove_cpu(int cpu)
  253. {
  254. numa_set_cpumask(cpu, 0);
  255. }
  256. int cpu_to_node(int cpu)
  257. {
  258. if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
  259. printk(KERN_WARNING
  260. "cpu_to_node(%d): usage too early!\n", cpu);
  261. dump_stack();
  262. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  263. }
  264. return per_cpu(x86_cpu_to_node_map, cpu);
  265. }
  266. EXPORT_SYMBOL(cpu_to_node);
  267. /*
  268. * Same function as cpu_to_node() but used if called before the
  269. * per_cpu areas are setup.
  270. */
  271. int early_cpu_to_node(int cpu)
  272. {
  273. if (early_per_cpu_ptr(x86_cpu_to_node_map))
  274. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  275. if (!per_cpu_offset(cpu)) {
  276. printk(KERN_WARNING
  277. "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
  278. dump_stack();
  279. return NUMA_NO_NODE;
  280. }
  281. return per_cpu(x86_cpu_to_node_map, cpu);
  282. }
  283. /* empty cpumask */
  284. static const cpumask_t cpu_mask_none;
  285. /*
  286. * Returns a pointer to the bitmask of CPUs on Node 'node'.
  287. */
  288. const cpumask_t *_node_to_cpumask_ptr(int node)
  289. {
  290. if (node_to_cpumask_map == NULL) {
  291. printk(KERN_WARNING
  292. "_node_to_cpumask_ptr(%d): no node_to_cpumask_map!\n",
  293. node);
  294. dump_stack();
  295. return (const cpumask_t *)&cpu_online_map;
  296. }
  297. if (node >= nr_node_ids) {
  298. printk(KERN_WARNING
  299. "_node_to_cpumask_ptr(%d): node > nr_node_ids(%d)\n",
  300. node, nr_node_ids);
  301. dump_stack();
  302. return &cpu_mask_none;
  303. }
  304. return &node_to_cpumask_map[node];
  305. }
  306. EXPORT_SYMBOL(_node_to_cpumask_ptr);
  307. /*
  308. * Returns a bitmask of CPUs on Node 'node'.
  309. *
  310. * Side note: this function creates the returned cpumask on the stack
  311. * so with a high NR_CPUS count, excessive stack space is used. The
  312. * node_to_cpumask_ptr function should be used whenever possible.
  313. */
  314. cpumask_t node_to_cpumask(int node)
  315. {
  316. if (node_to_cpumask_map == NULL) {
  317. printk(KERN_WARNING
  318. "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
  319. dump_stack();
  320. return cpu_online_map;
  321. }
  322. if (node >= nr_node_ids) {
  323. printk(KERN_WARNING
  324. "node_to_cpumask(%d): node > nr_node_ids(%d)\n",
  325. node, nr_node_ids);
  326. dump_stack();
  327. return cpu_mask_none;
  328. }
  329. return node_to_cpumask_map[node];
  330. }
  331. EXPORT_SYMBOL(node_to_cpumask);
  332. /*
  333. * --------- end of debug versions of the numa functions ---------
  334. */
  335. #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
  336. #endif /* X86_64_NUMA */