setup.c 9.4 KB

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