setup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. /* no processor from mptable or madt */
  140. if (!num_processors)
  141. num_processors = 1;
  142. #ifdef CONFIG_HOTPLUG_CPU
  143. prefill_possible_map();
  144. #else
  145. nr_cpu_ids = num_processors;
  146. #endif
  147. /* Setup cpu_pda map */
  148. setup_cpu_pda_map();
  149. /* Copy section for each CPU (we discard the original) */
  150. size = PERCPU_ENOUGH_ROOM;
  151. printk(KERN_INFO "PERCPU: Allocating %zd bytes of per cpu data\n",
  152. size);
  153. for_each_possible_cpu(cpu) {
  154. #ifndef CONFIG_NEED_MULTIPLE_NODES
  155. ptr = alloc_bootmem_pages(size);
  156. #else
  157. int node = early_cpu_to_node(cpu);
  158. if (!node_online(node) || !NODE_DATA(node)) {
  159. ptr = alloc_bootmem_pages(size);
  160. printk(KERN_INFO
  161. "cpu %d has no node %d or node-local memory\n",
  162. cpu, node);
  163. }
  164. else
  165. ptr = alloc_bootmem_pages_node(NODE_DATA(node), size);
  166. #endif
  167. per_cpu_offset(cpu) = ptr - __per_cpu_start;
  168. memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
  169. }
  170. printk(KERN_DEBUG "NR_CPUS: %d, nr_cpu_ids: %d, nr_node_ids %d\n",
  171. NR_CPUS, nr_cpu_ids, nr_node_ids);
  172. /* Setup percpu data maps */
  173. setup_per_cpu_maps();
  174. /* Setup node to cpumask map */
  175. setup_node_to_cpumask_map();
  176. /* Setup cpumask_of_cpu map */
  177. setup_cpumask_of_cpu();
  178. }
  179. #endif
  180. void __init parse_setup_data(void)
  181. {
  182. struct setup_data *data;
  183. u64 pa_data;
  184. if (boot_params.hdr.version < 0x0209)
  185. return;
  186. pa_data = boot_params.hdr.setup_data;
  187. while (pa_data) {
  188. data = early_ioremap(pa_data, PAGE_SIZE);
  189. switch (data->type) {
  190. case SETUP_E820_EXT:
  191. parse_e820_ext(data, pa_data);
  192. break;
  193. default:
  194. break;
  195. }
  196. #ifndef CONFIG_DEBUG_BOOT_PARAMS
  197. free_early(pa_data, pa_data+sizeof(*data)+data->len);
  198. #endif
  199. pa_data = data->next;
  200. early_iounmap(data, PAGE_SIZE);
  201. }
  202. }
  203. #ifdef X86_64_NUMA
  204. /*
  205. * Allocate node_to_cpumask_map based on number of available nodes
  206. * Requires node_possible_map to be valid.
  207. *
  208. * Note: node_to_cpumask() is not valid until after this is done.
  209. */
  210. static void __init setup_node_to_cpumask_map(void)
  211. {
  212. unsigned int node, num = 0;
  213. cpumask_t *map;
  214. /* setup nr_node_ids if not done yet */
  215. if (nr_node_ids == MAX_NUMNODES) {
  216. for_each_node_mask(node, node_possible_map)
  217. num = node;
  218. nr_node_ids = num + 1;
  219. }
  220. /* allocate the map */
  221. map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
  222. Dprintk(KERN_DEBUG "Node to cpumask map at %p for %d nodes\n",
  223. map, nr_node_ids);
  224. /* node_to_cpumask() will now work */
  225. node_to_cpumask_map = map;
  226. }
  227. void __cpuinit numa_set_node(int cpu, int node)
  228. {
  229. int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
  230. if (cpu_pda(cpu) && node != NUMA_NO_NODE)
  231. cpu_pda(cpu)->nodenumber = node;
  232. if (cpu_to_node_map)
  233. cpu_to_node_map[cpu] = node;
  234. else if (per_cpu_offset(cpu))
  235. per_cpu(x86_cpu_to_node_map, cpu) = node;
  236. else
  237. Dprintk(KERN_INFO "Setting node for non-present cpu %d\n", cpu);
  238. }
  239. void __cpuinit numa_clear_node(int cpu)
  240. {
  241. numa_set_node(cpu, NUMA_NO_NODE);
  242. }
  243. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  244. void __cpuinit numa_add_cpu(int cpu)
  245. {
  246. cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  247. }
  248. void __cpuinit numa_remove_cpu(int cpu)
  249. {
  250. cpu_clear(cpu, node_to_cpumask_map[cpu_to_node(cpu)]);
  251. }
  252. #else /* CONFIG_DEBUG_PER_CPU_MAPS */
  253. /*
  254. * --------- debug versions of the numa functions ---------
  255. */
  256. static void __cpuinit numa_set_cpumask(int cpu, int enable)
  257. {
  258. int node = cpu_to_node(cpu);
  259. cpumask_t *mask;
  260. char buf[64];
  261. if (node_to_cpumask_map == NULL) {
  262. printk(KERN_ERR "node_to_cpumask_map NULL\n");
  263. dump_stack();
  264. return;
  265. }
  266. mask = &node_to_cpumask_map[node];
  267. if (enable)
  268. cpu_set(cpu, *mask);
  269. else
  270. cpu_clear(cpu, *mask);
  271. cpulist_scnprintf(buf, sizeof(buf), *mask);
  272. printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
  273. enable? "numa_add_cpu":"numa_remove_cpu", cpu, node, buf);
  274. }
  275. void __cpuinit numa_add_cpu(int cpu)
  276. {
  277. numa_set_cpumask(cpu, 1);
  278. }
  279. void __cpuinit numa_remove_cpu(int cpu)
  280. {
  281. numa_set_cpumask(cpu, 0);
  282. }
  283. int cpu_to_node(int cpu)
  284. {
  285. if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
  286. printk(KERN_WARNING
  287. "cpu_to_node(%d): usage too early!\n", cpu);
  288. dump_stack();
  289. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  290. }
  291. return per_cpu(x86_cpu_to_node_map, cpu);
  292. }
  293. EXPORT_SYMBOL(cpu_to_node);
  294. /*
  295. * Same function as cpu_to_node() but used if called before the
  296. * per_cpu areas are setup.
  297. */
  298. int early_cpu_to_node(int cpu)
  299. {
  300. if (early_per_cpu_ptr(x86_cpu_to_node_map))
  301. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  302. if (!per_cpu_offset(cpu)) {
  303. printk(KERN_WARNING
  304. "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
  305. dump_stack();
  306. return NUMA_NO_NODE;
  307. }
  308. return per_cpu(x86_cpu_to_node_map, cpu);
  309. }
  310. /*
  311. * Returns a pointer to the bitmask of CPUs on Node 'node'.
  312. */
  313. cpumask_t *_node_to_cpumask_ptr(int node)
  314. {
  315. if (node_to_cpumask_map == NULL) {
  316. printk(KERN_WARNING
  317. "_node_to_cpumask_ptr(%d): no node_to_cpumask_map!\n",
  318. node);
  319. dump_stack();
  320. return &cpu_online_map;
  321. }
  322. BUG_ON(node >= nr_node_ids);
  323. return &node_to_cpumask_map[node];
  324. }
  325. EXPORT_SYMBOL(_node_to_cpumask_ptr);
  326. /*
  327. * Returns a bitmask of CPUs on Node 'node'.
  328. */
  329. cpumask_t node_to_cpumask(int node)
  330. {
  331. if (node_to_cpumask_map == NULL) {
  332. printk(KERN_WARNING
  333. "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
  334. dump_stack();
  335. return cpu_online_map;
  336. }
  337. BUG_ON(node >= nr_node_ids);
  338. return node_to_cpumask_map[node];
  339. }
  340. EXPORT_SYMBOL(node_to_cpumask);
  341. /*
  342. * --------- end of debug versions of the numa functions ---------
  343. */
  344. #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
  345. #endif /* X86_64_NUMA */
  346. /*
  347. * --------- Crashkernel reservation ------------------------------
  348. */
  349. static inline unsigned long long get_total_mem(void)
  350. {
  351. unsigned long long total;
  352. total = max_low_pfn - min_low_pfn;
  353. #ifdef CONFIG_HIGHMEM
  354. total += highend_pfn - highstart_pfn;
  355. #endif
  356. return total << PAGE_SHIFT;
  357. }
  358. #ifdef CONFIG_KEXEC
  359. void __init reserve_crashkernel(void)
  360. {
  361. unsigned long long total_mem;
  362. unsigned long long crash_size, crash_base;
  363. int ret;
  364. total_mem = get_total_mem();
  365. ret = parse_crashkernel(boot_command_line, total_mem,
  366. &crash_size, &crash_base);
  367. if (ret == 0 && crash_size > 0) {
  368. if (crash_base <= 0) {
  369. printk(KERN_INFO "crashkernel reservation failed - "
  370. "you have to specify a base address\n");
  371. return;
  372. }
  373. if (reserve_bootmem_generic(crash_base, crash_size,
  374. BOOTMEM_EXCLUSIVE) < 0) {
  375. printk(KERN_INFO "crashkernel reservation failed - "
  376. "memory is in use\n");
  377. return;
  378. }
  379. printk(KERN_INFO "Reserving %ldMB of memory at %ldMB "
  380. "for crashkernel (System RAM: %ldMB)\n",
  381. (unsigned long)(crash_size >> 20),
  382. (unsigned long)(crash_base >> 20),
  383. (unsigned long)(total_mem >> 20));
  384. crashk_res.start = crash_base;
  385. crashk_res.end = crash_base + crash_size - 1;
  386. insert_resource(&iomem_resource, &crashk_res);
  387. }
  388. }
  389. #else
  390. void __init reserve_crashkernel(void)
  391. {}
  392. #endif
  393. static struct resource standard_io_resources[] = {
  394. { .name = "dma1", .start = 0x00, .end = 0x1f,
  395. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  396. { .name = "pic1", .start = 0x20, .end = 0x21,
  397. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  398. { .name = "timer0", .start = 0x40, .end = 0x43,
  399. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  400. { .name = "timer1", .start = 0x50, .end = 0x53,
  401. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  402. { .name = "keyboard", .start = 0x60, .end = 0x60,
  403. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  404. { .name = "keyboard", .start = 0x64, .end = 0x64,
  405. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  406. { .name = "dma page reg", .start = 0x80, .end = 0x8f,
  407. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  408. { .name = "pic2", .start = 0xa0, .end = 0xa1,
  409. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  410. { .name = "dma2", .start = 0xc0, .end = 0xdf,
  411. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  412. { .name = "fpu", .start = 0xf0, .end = 0xff,
  413. .flags = IORESOURCE_BUSY | IORESOURCE_IO }
  414. };
  415. void __init reserve_standard_io_resources(void)
  416. {
  417. int i;
  418. /* request I/O space for devices used on all i[345]86 PCs */
  419. for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
  420. request_resource(&ioport_resource, &standard_io_resources[i]);
  421. }