contig.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1998-2003 Hewlett-Packard Co
  7. * David Mosberger-Tang <davidm@hpl.hp.com>
  8. * Stephane Eranian <eranian@hpl.hp.com>
  9. * Copyright (C) 2000, Rohit Seth <rohit.seth@intel.com>
  10. * Copyright (C) 1999 VA Linux Systems
  11. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  12. * Copyright (C) 2003 Silicon Graphics, Inc. All rights reserved.
  13. *
  14. * Routines used by ia64 machines with contiguous (or virtually contiguous)
  15. * memory.
  16. */
  17. #include <linux/bootmem.h>
  18. #include <linux/efi.h>
  19. #include <linux/memblock.h>
  20. #include <linux/mm.h>
  21. #include <linux/nmi.h>
  22. #include <linux/swap.h>
  23. #include <asm/meminit.h>
  24. #include <asm/pgalloc.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/sections.h>
  27. #include <asm/mca.h>
  28. #ifdef CONFIG_VIRTUAL_MEM_MAP
  29. static unsigned long max_gap;
  30. #endif
  31. /* physical address where the bootmem map is located */
  32. unsigned long bootmap_start;
  33. /**
  34. * find_bootmap_location - callback to find a memory area for the bootmap
  35. * @start: start of region
  36. * @end: end of region
  37. * @arg: unused callback data
  38. *
  39. * Find a place to put the bootmap and return its starting address in
  40. * bootmap_start. This address must be page-aligned.
  41. */
  42. static int __init
  43. find_bootmap_location (u64 start, u64 end, void *arg)
  44. {
  45. u64 needed = *(unsigned long *)arg;
  46. u64 range_start, range_end, free_start;
  47. int i;
  48. #if IGNORE_PFN0
  49. if (start == PAGE_OFFSET) {
  50. start += PAGE_SIZE;
  51. if (start >= end)
  52. return 0;
  53. }
  54. #endif
  55. free_start = PAGE_OFFSET;
  56. for (i = 0; i < num_rsvd_regions; i++) {
  57. range_start = max(start, free_start);
  58. range_end = min(end, rsvd_region[i].start & PAGE_MASK);
  59. free_start = PAGE_ALIGN(rsvd_region[i].end);
  60. if (range_end <= range_start)
  61. continue; /* skip over empty range */
  62. if (range_end - range_start >= needed) {
  63. bootmap_start = __pa(range_start);
  64. return -1; /* done */
  65. }
  66. /* nothing more available in this segment */
  67. if (range_end == end)
  68. return 0;
  69. }
  70. return 0;
  71. }
  72. #ifdef CONFIG_SMP
  73. static void *cpu_data;
  74. /**
  75. * per_cpu_init - setup per-cpu variables
  76. *
  77. * Allocate and setup per-cpu data areas.
  78. */
  79. void *per_cpu_init(void)
  80. {
  81. static bool first_time = true;
  82. void *cpu0_data = __cpu0_per_cpu;
  83. unsigned int cpu;
  84. if (!first_time)
  85. goto skip;
  86. first_time = false;
  87. /*
  88. * get_free_pages() cannot be used before cpu_init() done.
  89. * BSP allocates PERCPU_PAGE_SIZE bytes for all possible CPUs
  90. * to avoid that AP calls get_zeroed_page().
  91. */
  92. for_each_possible_cpu(cpu) {
  93. void *src = cpu == 0 ? cpu0_data : __phys_per_cpu_start;
  94. memcpy(cpu_data, src, __per_cpu_end - __per_cpu_start);
  95. __per_cpu_offset[cpu] = (char *)cpu_data - __per_cpu_start;
  96. per_cpu(local_per_cpu_offset, cpu) = __per_cpu_offset[cpu];
  97. /*
  98. * percpu area for cpu0 is moved from the __init area
  99. * which is setup by head.S and used till this point.
  100. * Update ar.k3. This move is ensures that percpu
  101. * area for cpu0 is on the correct node and its
  102. * virtual address isn't insanely far from other
  103. * percpu areas which is important for congruent
  104. * percpu allocator.
  105. */
  106. if (cpu == 0)
  107. ia64_set_kr(IA64_KR_PER_CPU_DATA, __pa(cpu_data) -
  108. (unsigned long)__per_cpu_start);
  109. cpu_data += PERCPU_PAGE_SIZE;
  110. }
  111. skip:
  112. return __per_cpu_start + __per_cpu_offset[smp_processor_id()];
  113. }
  114. static inline void
  115. alloc_per_cpu_data(void)
  116. {
  117. cpu_data = __alloc_bootmem(PERCPU_PAGE_SIZE * num_possible_cpus(),
  118. PERCPU_PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
  119. }
  120. /**
  121. * setup_per_cpu_areas - setup percpu areas
  122. *
  123. * Arch code has already allocated and initialized percpu areas. All
  124. * this function has to do is to teach the determined layout to the
  125. * dynamic percpu allocator, which happens to be more complex than
  126. * creating whole new ones using helpers.
  127. */
  128. void __init
  129. setup_per_cpu_areas(void)
  130. {
  131. struct pcpu_alloc_info *ai;
  132. struct pcpu_group_info *gi;
  133. unsigned int cpu;
  134. ssize_t static_size, reserved_size, dyn_size;
  135. int rc;
  136. ai = pcpu_alloc_alloc_info(1, num_possible_cpus());
  137. if (!ai)
  138. panic("failed to allocate pcpu_alloc_info");
  139. gi = &ai->groups[0];
  140. /* units are assigned consecutively to possible cpus */
  141. for_each_possible_cpu(cpu)
  142. gi->cpu_map[gi->nr_units++] = cpu;
  143. /* set parameters */
  144. static_size = __per_cpu_end - __per_cpu_start;
  145. reserved_size = PERCPU_MODULE_RESERVE;
  146. dyn_size = PERCPU_PAGE_SIZE - static_size - reserved_size;
  147. if (dyn_size < 0)
  148. panic("percpu area overflow static=%zd reserved=%zd\n",
  149. static_size, reserved_size);
  150. ai->static_size = static_size;
  151. ai->reserved_size = reserved_size;
  152. ai->dyn_size = dyn_size;
  153. ai->unit_size = PERCPU_PAGE_SIZE;
  154. ai->atom_size = PAGE_SIZE;
  155. ai->alloc_size = PERCPU_PAGE_SIZE;
  156. rc = pcpu_setup_first_chunk(ai, __per_cpu_start + __per_cpu_offset[0]);
  157. if (rc)
  158. panic("failed to setup percpu area (err=%d)", rc);
  159. pcpu_free_alloc_info(ai);
  160. }
  161. #else
  162. #define alloc_per_cpu_data() do { } while (0)
  163. #endif /* CONFIG_SMP */
  164. /**
  165. * find_memory - setup memory map
  166. *
  167. * Walk the EFI memory map and find usable memory for the system, taking
  168. * into account reserved areas.
  169. */
  170. void __init
  171. find_memory (void)
  172. {
  173. unsigned long bootmap_size;
  174. reserve_memory();
  175. /* first find highest page frame number */
  176. min_low_pfn = ~0UL;
  177. max_low_pfn = 0;
  178. efi_memmap_walk(find_max_min_low_pfn, NULL);
  179. max_pfn = max_low_pfn;
  180. /* how many bytes to cover all the pages */
  181. bootmap_size = bootmem_bootmap_pages(max_pfn) << PAGE_SHIFT;
  182. /* look for a location to hold the bootmap */
  183. bootmap_start = ~0UL;
  184. efi_memmap_walk(find_bootmap_location, &bootmap_size);
  185. if (bootmap_start == ~0UL)
  186. panic("Cannot find %ld bytes for bootmap\n", bootmap_size);
  187. bootmap_size = init_bootmem_node(NODE_DATA(0),
  188. (bootmap_start >> PAGE_SHIFT), 0, max_pfn);
  189. /* Free all available memory, then mark bootmem-map as being in use. */
  190. efi_memmap_walk(filter_rsvd_memory, free_bootmem);
  191. reserve_bootmem(bootmap_start, bootmap_size, BOOTMEM_DEFAULT);
  192. find_initrd();
  193. alloc_per_cpu_data();
  194. }
  195. /*
  196. * Set up the page tables.
  197. */
  198. void __init
  199. paging_init (void)
  200. {
  201. unsigned long max_dma;
  202. unsigned long max_zone_pfns[MAX_NR_ZONES];
  203. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  204. #ifdef CONFIG_ZONE_DMA
  205. max_dma = virt_to_phys((void *) MAX_DMA_ADDRESS) >> PAGE_SHIFT;
  206. max_zone_pfns[ZONE_DMA] = max_dma;
  207. #endif
  208. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  209. #ifdef CONFIG_VIRTUAL_MEM_MAP
  210. efi_memmap_walk(filter_memory, register_active_ranges);
  211. efi_memmap_walk(find_largest_hole, (u64 *)&max_gap);
  212. if (max_gap < LARGE_GAP) {
  213. vmem_map = (struct page *) 0;
  214. free_area_init_nodes(max_zone_pfns);
  215. } else {
  216. unsigned long map_size;
  217. /* allocate virtual_mem_map */
  218. map_size = PAGE_ALIGN(ALIGN(max_low_pfn, MAX_ORDER_NR_PAGES) *
  219. sizeof(struct page));
  220. VMALLOC_END -= map_size;
  221. vmem_map = (struct page *) VMALLOC_END;
  222. efi_memmap_walk(create_mem_map_page_table, NULL);
  223. /*
  224. * alloc_node_mem_map makes an adjustment for mem_map
  225. * which isn't compatible with vmem_map.
  226. */
  227. NODE_DATA(0)->node_mem_map = vmem_map +
  228. find_min_pfn_with_active_regions();
  229. free_area_init_nodes(max_zone_pfns);
  230. printk("Virtual mem_map starts at 0x%p\n", mem_map);
  231. }
  232. #else /* !CONFIG_VIRTUAL_MEM_MAP */
  233. memblock_add_node(0, PFN_PHYS(max_low_pfn), 0);
  234. free_area_init_nodes(max_zone_pfns);
  235. #endif /* !CONFIG_VIRTUAL_MEM_MAP */
  236. zero_page_memmap_ptr = virt_to_page(ia64_imva(empty_zero_page));
  237. }