setup_percpu.c 13 KB

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