setup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 <asm/smp.h>
  8. #include <asm/percpu.h>
  9. #include <asm/sections.h>
  10. #include <asm/processor.h>
  11. #include <asm/setup.h>
  12. #include <asm/topology.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. unsigned int max_physical_apicid;
  22. EXPORT_SYMBOL(boot_cpu_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_HAVE_CPUMASK_OF_CPU_MAP
  71. cpumask_t *cpumask_of_cpu_map __read_mostly;
  72. EXPORT_SYMBOL(cpumask_of_cpu_map);
  73. /* requires nr_cpu_ids to be initialized */
  74. static void __init setup_cpumask_of_cpu(void)
  75. {
  76. int i;
  77. /* alloc_bootmem zeroes memory */
  78. cpumask_of_cpu_map = alloc_bootmem_low(sizeof(cpumask_t) * nr_cpu_ids);
  79. for (i = 0; i < nr_cpu_ids; i++)
  80. cpu_set(i, cpumask_of_cpu_map[i]);
  81. }
  82. #else
  83. static inline void setup_cpumask_of_cpu(void) { }
  84. #endif
  85. #ifdef CONFIG_X86_32
  86. /*
  87. * Great future not-so-futuristic plan: make i386 and x86_64 do it
  88. * the same way
  89. */
  90. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  91. EXPORT_SYMBOL(__per_cpu_offset);
  92. static inline void setup_cpu_pda_map(void) { }
  93. #elif !defined(CONFIG_SMP)
  94. static inline void setup_cpu_pda_map(void) { }
  95. #else /* CONFIG_SMP && CONFIG_X86_64 */
  96. /*
  97. * Allocate cpu_pda pointer table and array via alloc_bootmem.
  98. */
  99. static void __init setup_cpu_pda_map(void)
  100. {
  101. char *pda;
  102. struct x8664_pda **new_cpu_pda;
  103. unsigned long size;
  104. int cpu;
  105. size = roundup(sizeof(struct x8664_pda), cache_line_size());
  106. /* allocate cpu_pda array and pointer table */
  107. {
  108. unsigned long tsize = nr_cpu_ids * sizeof(void *);
  109. unsigned long asize = size * (nr_cpu_ids - 1);
  110. tsize = roundup(tsize, cache_line_size());
  111. new_cpu_pda = alloc_bootmem(tsize + asize);
  112. pda = (char *)new_cpu_pda + tsize;
  113. }
  114. /* initialize pointer table to static pda's */
  115. for_each_possible_cpu(cpu) {
  116. if (cpu == 0) {
  117. /* leave boot cpu pda in place */
  118. new_cpu_pda[0] = cpu_pda(0);
  119. continue;
  120. }
  121. new_cpu_pda[cpu] = (struct x8664_pda *)pda;
  122. new_cpu_pda[cpu]->in_bootmem = 1;
  123. pda += size;
  124. }
  125. /* point to new pointer table */
  126. _cpu_pda = new_cpu_pda;
  127. }
  128. #endif
  129. /*
  130. * Great future plan:
  131. * Declare PDA itself and support (irqstack,tss,pgd) as per cpu data.
  132. * Always point %gs to its beginning
  133. */
  134. void __init setup_per_cpu_areas(void)
  135. {
  136. ssize_t size = PERCPU_ENOUGH_ROOM;
  137. char *ptr;
  138. int cpu;
  139. #ifdef CONFIG_HOTPLUG_CPU
  140. prefill_possible_map();
  141. #else
  142. nr_cpu_ids = num_processors;
  143. #endif
  144. /* Setup cpu_pda map */
  145. setup_cpu_pda_map();
  146. /* Copy section for each CPU (we discard the original) */
  147. size = PERCPU_ENOUGH_ROOM;
  148. printk(KERN_INFO "PERCPU: Allocating %zd bytes of per cpu data\n",
  149. size);
  150. for_each_possible_cpu(cpu) {
  151. #ifndef CONFIG_NEED_MULTIPLE_NODES
  152. ptr = alloc_bootmem_pages(size);
  153. #else
  154. int node = early_cpu_to_node(cpu);
  155. if (!node_online(node) || !NODE_DATA(node)) {
  156. ptr = alloc_bootmem_pages(size);
  157. printk(KERN_INFO
  158. "cpu %d has no node %d or node-local memory\n",
  159. cpu, node);
  160. }
  161. else
  162. ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
  163. #endif
  164. per_cpu_offset(cpu) = ptr - __per_cpu_start;
  165. memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
  166. }
  167. printk(KERN_DEBUG "NR_CPUS: %d, nr_cpu_ids: %d, nr_node_ids %d\n",
  168. NR_CPUS, nr_cpu_ids, nr_node_ids);
  169. /* Setup percpu data maps */
  170. setup_per_cpu_maps();
  171. /* Setup node to cpumask map */
  172. setup_node_to_cpumask_map();
  173. /* Setup cpumask_of_cpu map */
  174. setup_cpumask_of_cpu();
  175. }
  176. #endif
  177. void __init parse_setup_data(void)
  178. {
  179. struct setup_data *data;
  180. u64 pa_data;
  181. if (boot_params.hdr.version < 0x0209)
  182. return;
  183. pa_data = boot_params.hdr.setup_data;
  184. while (pa_data) {
  185. data = early_ioremap(pa_data, PAGE_SIZE);
  186. switch (data->type) {
  187. case SETUP_E820_EXT:
  188. parse_e820_ext(data, pa_data);
  189. break;
  190. default:
  191. break;
  192. }
  193. #ifndef CONFIG_DEBUG_BOOT_PARAMS
  194. free_early(pa_data, pa_data+sizeof(*data)+data->len);
  195. #endif
  196. pa_data = data->next;
  197. early_iounmap(data, PAGE_SIZE);
  198. }
  199. }
  200. #ifdef X86_64_NUMA
  201. /*
  202. * Allocate node_to_cpumask_map based on number of available nodes
  203. * Requires node_possible_map to be valid.
  204. *
  205. * Note: node_to_cpumask() is not valid until after this is done.
  206. */
  207. static void __init setup_node_to_cpumask_map(void)
  208. {
  209. unsigned int node, num = 0;
  210. cpumask_t *map;
  211. /* setup nr_node_ids if not done yet */
  212. if (nr_node_ids == MAX_NUMNODES) {
  213. for_each_node_mask(node, node_possible_map)
  214. num = node;
  215. nr_node_ids = num + 1;
  216. }
  217. /* allocate the map */
  218. map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
  219. Dprintk(KERN_DEBUG "Node to cpumask map at %p for %d nodes\n",
  220. map, nr_node_ids);
  221. /* node_to_cpumask() will now work */
  222. node_to_cpumask_map = map;
  223. }
  224. void __cpuinit numa_set_node(int cpu, int node)
  225. {
  226. int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
  227. if (cpu_pda(cpu) && node != NUMA_NO_NODE)
  228. cpu_pda(cpu)->nodenumber = node;
  229. if (cpu_to_node_map)
  230. cpu_to_node_map[cpu] = node;
  231. else if (per_cpu_offset(cpu))
  232. per_cpu(x86_cpu_to_node_map, cpu) = node;
  233. else
  234. Dprintk(KERN_INFO "Setting node for non-present cpu %d\n", cpu);
  235. }
  236. void __cpuinit numa_clear_node(int cpu)
  237. {
  238. numa_set_node(cpu, NUMA_NO_NODE);
  239. }
  240. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  241. void __cpuinit numa_add_cpu(int cpu)
  242. {
  243. cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  244. }
  245. void __cpuinit numa_remove_cpu(int cpu)
  246. {
  247. cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]);
  248. }
  249. #else /* CONFIG_DEBUG_PER_CPU_MAPS */
  250. /*
  251. * --------- debug versions of the numa functions ---------
  252. */
  253. static void __cpuinit numa_set_cpumask(int cpu, int enable)
  254. {
  255. int node = cpu_to_node(cpu);
  256. cpumask_t *mask;
  257. char buf[64];
  258. if (node_to_cpumask_map == NULL) {
  259. printk(KERN_ERR "node_to_cpumask_map NULL\n");
  260. dump_stack();
  261. return;
  262. }
  263. mask = &node_to_cpumask_map[node];
  264. if (enable)
  265. cpu_set(cpu, *mask);
  266. else
  267. cpu_clear(cpu, *mask);
  268. cpulist_scnprintf(buf, sizeof(buf), *mask);
  269. printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
  270. enable? "numa_add_cpu":"numa_remove_cpu", cpu, node, buf);
  271. }
  272. void __cpuinit numa_add_cpu(int cpu)
  273. {
  274. numa_set_cpumask(cpu, 1);
  275. }
  276. void __cpuinit numa_remove_cpu(int cpu)
  277. {
  278. numa_set_cpumask(cpu, 0);
  279. }
  280. int cpu_to_node(int cpu)
  281. {
  282. if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
  283. printk(KERN_WARNING
  284. "cpu_to_node(%d): usage too early!\n", cpu);
  285. dump_stack();
  286. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  287. }
  288. return per_cpu(x86_cpu_to_node_map, cpu);
  289. }
  290. EXPORT_SYMBOL(cpu_to_node);
  291. /*
  292. * Same function as cpu_to_node() but used if called before the
  293. * per_cpu areas are setup.
  294. */
  295. int early_cpu_to_node(int cpu)
  296. {
  297. if (early_per_cpu_ptr(x86_cpu_to_node_map))
  298. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  299. if (!per_cpu_offset(cpu)) {
  300. printk(KERN_WARNING
  301. "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
  302. dump_stack();
  303. return NUMA_NO_NODE;
  304. }
  305. return per_cpu(x86_cpu_to_node_map, cpu);
  306. }
  307. /*
  308. * Returns a pointer to the bitmask of CPUs on Node 'node'.
  309. */
  310. cpumask_t *_node_to_cpumask_ptr(int node)
  311. {
  312. if (node_to_cpumask_map == NULL) {
  313. printk(KERN_WARNING
  314. "_node_to_cpumask_ptr(%d): no node_to_cpumask_map!\n",
  315. node);
  316. dump_stack();
  317. return &cpu_online_map;
  318. }
  319. BUG_ON(node >= nr_node_ids);
  320. return &node_to_cpumask_map[node];
  321. }
  322. EXPORT_SYMBOL(_node_to_cpumask_ptr);
  323. /*
  324. * Returns a bitmask of CPUs on Node 'node'.
  325. */
  326. cpumask_t node_to_cpumask(int node)
  327. {
  328. if (node_to_cpumask_map == NULL) {
  329. printk(KERN_WARNING
  330. "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
  331. dump_stack();
  332. return cpu_online_map;
  333. }
  334. BUG_ON(node >= nr_node_ids);
  335. return node_to_cpumask_map[node];
  336. }
  337. EXPORT_SYMBOL(node_to_cpumask);
  338. /*
  339. * --------- end of debug versions of the numa functions ---------
  340. */
  341. #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
  342. #endif /* X86_64_NUMA */
  343. /*
  344. * --------- Crashkernel reservation ------------------------------
  345. */
  346. static inline unsigned long long get_total_mem(void)
  347. {
  348. unsigned long long total;
  349. total = max_low_pfn - min_low_pfn;
  350. #ifdef CONFIG_HIGHMEM
  351. total += highend_pfn - highstart_pfn;
  352. #endif
  353. return total << PAGE_SHIFT;
  354. }
  355. #ifdef CONFIG_KEXEC
  356. void __init reserve_crashkernel(void)
  357. {
  358. unsigned long long total_mem;
  359. unsigned long long crash_size, crash_base;
  360. int ret;
  361. total_mem = get_total_mem();
  362. ret = parse_crashkernel(boot_command_line, total_mem,
  363. &crash_size, &crash_base);
  364. if (ret == 0 && crash_size > 0) {
  365. if (crash_base <= 0) {
  366. printk(KERN_INFO "crashkernel reservation failed - "
  367. "you have to specify a base address\n");
  368. return;
  369. }
  370. if (reserve_bootmem_generic(crash_base, crash_size,
  371. BOOTMEM_EXCLUSIVE) < 0) {
  372. printk(KERN_INFO "crashkernel reservation failed - "
  373. "memory is in use\n");
  374. return;
  375. }
  376. printk(KERN_INFO "Reserving %ldMB of memory at %ldMB "
  377. "for crashkernel (System RAM: %ldMB)\n",
  378. (unsigned long)(crash_size >> 20),
  379. (unsigned long)(crash_base >> 20),
  380. (unsigned long)(total_mem >> 20));
  381. crashk_res.start = crash_base;
  382. crashk_res.end = crash_base + crash_size - 1;
  383. insert_resource(&iomem_resource, &crashk_res);
  384. }
  385. }
  386. #else
  387. void __init reserve_crashkernel(void)
  388. {}
  389. #endif