numa_32.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Written by: Patricia Gaughen <gone@us.ibm.com>, IBM Corporation
  3. * August 2002: added remote node KVA remap - Martin J. Bligh
  4. *
  5. * Copyright (C) 2002, IBM Corp.
  6. *
  7. * All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  17. * NON INFRINGEMENT. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/bootmem.h>
  25. #include <linux/memblock.h>
  26. #include <linux/module.h>
  27. #include "numa_internal.h"
  28. #ifdef CONFIG_DISCONTIGMEM
  29. /*
  30. * 4) physnode_map - the mapping between a pfn and owning node
  31. * physnode_map keeps track of the physical memory layout of a generic
  32. * numa node on a 64Mb break (each element of the array will
  33. * represent 64Mb of memory and will be marked by the node id. so,
  34. * if the first gig is on node 0, and the second gig is on node 1
  35. * physnode_map will contain:
  36. *
  37. * physnode_map[0-15] = 0;
  38. * physnode_map[16-31] = 1;
  39. * physnode_map[32- ] = -1;
  40. */
  41. s8 physnode_map[MAX_ELEMENTS] __read_mostly = { [0 ... (MAX_ELEMENTS - 1)] = -1};
  42. EXPORT_SYMBOL(physnode_map);
  43. void memory_present(int nid, unsigned long start, unsigned long end)
  44. {
  45. unsigned long pfn;
  46. printk(KERN_INFO "Node: %d, start_pfn: %lx, end_pfn: %lx\n",
  47. nid, start, end);
  48. printk(KERN_DEBUG " Setting physnode_map array to node %d for pfns:\n", nid);
  49. printk(KERN_DEBUG " ");
  50. for (pfn = start; pfn < end; pfn += PAGES_PER_ELEMENT) {
  51. physnode_map[pfn / PAGES_PER_ELEMENT] = nid;
  52. printk(KERN_CONT "%lx ", pfn);
  53. }
  54. printk(KERN_CONT "\n");
  55. }
  56. unsigned long node_memmap_size_bytes(int nid, unsigned long start_pfn,
  57. unsigned long end_pfn)
  58. {
  59. unsigned long nr_pages = end_pfn - start_pfn;
  60. if (!nr_pages)
  61. return 0;
  62. return (nr_pages + 1) * sizeof(struct page);
  63. }
  64. #endif
  65. extern unsigned long highend_pfn, highstart_pfn;
  66. #define LARGE_PAGE_BYTES (PTRS_PER_PTE * PAGE_SIZE)
  67. static void *node_remap_start_vaddr[MAX_NUMNODES];
  68. void set_pmd_pfn(unsigned long vaddr, unsigned long pfn, pgprot_t flags);
  69. /*
  70. * Remap memory allocator
  71. */
  72. static unsigned long node_remap_start_pfn[MAX_NUMNODES];
  73. static void *node_remap_end_vaddr[MAX_NUMNODES];
  74. static void *node_remap_alloc_vaddr[MAX_NUMNODES];
  75. /**
  76. * alloc_remap - Allocate remapped memory
  77. * @nid: NUMA node to allocate memory from
  78. * @size: The size of allocation
  79. *
  80. * Allocate @size bytes from the remap area of NUMA node @nid. The
  81. * size of the remap area is predetermined by init_alloc_remap() and
  82. * only the callers considered there should call this function. For
  83. * more info, please read the comment on top of init_alloc_remap().
  84. *
  85. * The caller must be ready to handle allocation failure from this
  86. * function and fall back to regular memory allocator in such cases.
  87. *
  88. * CONTEXT:
  89. * Single CPU early boot context.
  90. *
  91. * RETURNS:
  92. * Pointer to the allocated memory on success, %NULL on failure.
  93. */
  94. void *alloc_remap(int nid, unsigned long size)
  95. {
  96. void *allocation = node_remap_alloc_vaddr[nid];
  97. size = ALIGN(size, L1_CACHE_BYTES);
  98. if (!allocation || (allocation + size) > node_remap_end_vaddr[nid])
  99. return NULL;
  100. node_remap_alloc_vaddr[nid] += size;
  101. memset(allocation, 0, size);
  102. return allocation;
  103. }
  104. #ifdef CONFIG_HIBERNATION
  105. /**
  106. * resume_map_numa_kva - add KVA mapping to the temporary page tables created
  107. * during resume from hibernation
  108. * @pgd_base - temporary resume page directory
  109. */
  110. void resume_map_numa_kva(pgd_t *pgd_base)
  111. {
  112. int node;
  113. for_each_online_node(node) {
  114. unsigned long start_va, start_pfn, nr_pages, pfn;
  115. start_va = (unsigned long)node_remap_start_vaddr[node];
  116. start_pfn = node_remap_start_pfn[node];
  117. nr_pages = (node_remap_end_vaddr[node] -
  118. node_remap_start_vaddr[node]) >> PAGE_SHIFT;
  119. printk(KERN_DEBUG "%s: node %d\n", __func__, node);
  120. for (pfn = 0; pfn < nr_pages; pfn += PTRS_PER_PTE) {
  121. unsigned long vaddr = start_va + (pfn << PAGE_SHIFT);
  122. pgd_t *pgd = pgd_base + pgd_index(vaddr);
  123. pud_t *pud = pud_offset(pgd, vaddr);
  124. pmd_t *pmd = pmd_offset(pud, vaddr);
  125. set_pmd(pmd, pfn_pmd(start_pfn + pfn,
  126. PAGE_KERNEL_LARGE_EXEC));
  127. printk(KERN_DEBUG "%s: %08lx -> pfn %08lx\n",
  128. __func__, vaddr, start_pfn + pfn);
  129. }
  130. }
  131. }
  132. #endif
  133. /**
  134. * init_alloc_remap - Initialize remap allocator for a NUMA node
  135. * @nid: NUMA node to initizlie remap allocator for
  136. *
  137. * NUMA nodes may end up without any lowmem. As allocating pgdat and
  138. * memmap on a different node with lowmem is inefficient, a special
  139. * remap allocator is implemented which can be used by alloc_remap().
  140. *
  141. * For each node, the amount of memory which will be necessary for
  142. * pgdat and memmap is calculated and two memory areas of the size are
  143. * allocated - one in the node and the other in lowmem; then, the area
  144. * in the node is remapped to the lowmem area.
  145. *
  146. * As pgdat and memmap must be allocated in lowmem anyway, this
  147. * doesn't waste lowmem address space; however, the actual lowmem
  148. * which gets remapped over is wasted. The amount shouldn't be
  149. * problematic on machines this feature will be used.
  150. *
  151. * Initialization failure isn't fatal. alloc_remap() is used
  152. * opportunistically and the callers will fall back to other memory
  153. * allocation mechanisms on failure.
  154. */
  155. void __init init_alloc_remap(int nid, u64 start, u64 end)
  156. {
  157. unsigned long start_pfn = start >> PAGE_SHIFT;
  158. unsigned long end_pfn = end >> PAGE_SHIFT;
  159. unsigned long size, pfn;
  160. u64 node_pa, remap_pa;
  161. void *remap_va;
  162. /*
  163. * The acpi/srat node info can show hot-add memroy zones where
  164. * memory could be added but not currently present.
  165. */
  166. printk(KERN_DEBUG "node %d pfn: [%lx - %lx]\n",
  167. nid, start_pfn, end_pfn);
  168. /* calculate the necessary space aligned to large page size */
  169. size = node_memmap_size_bytes(nid, start_pfn, end_pfn);
  170. size += ALIGN(sizeof(pg_data_t), PAGE_SIZE);
  171. size = ALIGN(size, LARGE_PAGE_BYTES);
  172. /* allocate node memory and the lowmem remap area */
  173. node_pa = memblock_find_in_range(start, end, size, LARGE_PAGE_BYTES);
  174. if (node_pa == MEMBLOCK_ERROR) {
  175. pr_warning("remap_alloc: failed to allocate %lu bytes for node %d\n",
  176. size, nid);
  177. return;
  178. }
  179. memblock_x86_reserve_range(node_pa, node_pa + size, "KVA RAM");
  180. remap_pa = memblock_find_in_range(min_low_pfn << PAGE_SHIFT,
  181. max_low_pfn << PAGE_SHIFT,
  182. size, LARGE_PAGE_BYTES);
  183. if (remap_pa == MEMBLOCK_ERROR) {
  184. pr_warning("remap_alloc: failed to allocate %lu bytes remap area for node %d\n",
  185. size, nid);
  186. memblock_x86_free_range(node_pa, node_pa + size);
  187. return;
  188. }
  189. memblock_x86_reserve_range(remap_pa, remap_pa + size, "KVA PG");
  190. remap_va = phys_to_virt(remap_pa);
  191. /* perform actual remap */
  192. for (pfn = 0; pfn < size >> PAGE_SHIFT; pfn += PTRS_PER_PTE)
  193. set_pmd_pfn((unsigned long)remap_va + (pfn << PAGE_SHIFT),
  194. (node_pa >> PAGE_SHIFT) + pfn,
  195. PAGE_KERNEL_LARGE);
  196. /* initialize remap allocator parameters */
  197. node_remap_start_pfn[nid] = node_pa >> PAGE_SHIFT;
  198. node_remap_start_vaddr[nid] = remap_va;
  199. node_remap_end_vaddr[nid] = remap_va + size;
  200. node_remap_alloc_vaddr[nid] = remap_va;
  201. printk(KERN_DEBUG "remap_alloc: node %d [%08llx-%08llx) -> [%p-%p)\n",
  202. nid, node_pa, node_pa + size, remap_va, remap_va + size);
  203. }
  204. void __init initmem_init(void)
  205. {
  206. x86_numa_init();
  207. #ifdef CONFIG_HIGHMEM
  208. highstart_pfn = highend_pfn = max_pfn;
  209. if (max_pfn > max_low_pfn)
  210. highstart_pfn = max_low_pfn;
  211. printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
  212. pages_to_mb(highend_pfn - highstart_pfn));
  213. num_physpages = highend_pfn;
  214. high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
  215. #else
  216. num_physpages = max_low_pfn;
  217. high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
  218. #endif
  219. printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
  220. pages_to_mb(max_low_pfn));
  221. printk(KERN_DEBUG "max_low_pfn = %lx, highstart_pfn = %lx\n",
  222. max_low_pfn, highstart_pfn);
  223. printk(KERN_DEBUG "Low memory ends at vaddr %08lx\n",
  224. (ulong) pfn_to_kaddr(max_low_pfn));
  225. printk(KERN_DEBUG "High memory starts at vaddr %08lx\n",
  226. (ulong) pfn_to_kaddr(highstart_pfn));
  227. setup_bootmem_allocator();
  228. }