setup_percpu.c 9.4 KB

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