vmem.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Copyright IBM Corp. 2006
  3. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  4. */
  5. #include <linux/bootmem.h>
  6. #include <linux/pfn.h>
  7. #include <linux/mm.h>
  8. #include <linux/module.h>
  9. #include <linux/list.h>
  10. #include <linux/hugetlb.h>
  11. #include <linux/slab.h>
  12. #include <linux/memblock.h>
  13. #include <asm/pgalloc.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/setup.h>
  16. #include <asm/tlbflush.h>
  17. #include <asm/sections.h>
  18. static DEFINE_MUTEX(vmem_mutex);
  19. struct memory_segment {
  20. struct list_head list;
  21. unsigned long start;
  22. unsigned long size;
  23. };
  24. static LIST_HEAD(mem_segs);
  25. static void __ref *vmem_alloc_pages(unsigned int order)
  26. {
  27. if (slab_is_available())
  28. return (void *)__get_free_pages(GFP_KERNEL, order);
  29. return alloc_bootmem_pages((1 << order) * PAGE_SIZE);
  30. }
  31. static inline pud_t *vmem_pud_alloc(void)
  32. {
  33. pud_t *pud = NULL;
  34. #ifdef CONFIG_64BIT
  35. pud = vmem_alloc_pages(2);
  36. if (!pud)
  37. return NULL;
  38. clear_table((unsigned long *) pud, _REGION3_ENTRY_EMPTY, PAGE_SIZE * 4);
  39. #endif
  40. return pud;
  41. }
  42. static inline pmd_t *vmem_pmd_alloc(void)
  43. {
  44. pmd_t *pmd = NULL;
  45. #ifdef CONFIG_64BIT
  46. pmd = vmem_alloc_pages(2);
  47. if (!pmd)
  48. return NULL;
  49. clear_table((unsigned long *) pmd, _SEGMENT_ENTRY_EMPTY, PAGE_SIZE * 4);
  50. #endif
  51. return pmd;
  52. }
  53. static pte_t __ref *vmem_pte_alloc(unsigned long address)
  54. {
  55. pte_t *pte;
  56. if (slab_is_available())
  57. pte = (pte_t *) page_table_alloc(&init_mm, address);
  58. else
  59. pte = alloc_bootmem_align(PTRS_PER_PTE * sizeof(pte_t),
  60. PTRS_PER_PTE * sizeof(pte_t));
  61. if (!pte)
  62. return NULL;
  63. clear_table((unsigned long *) pte, _PAGE_INVALID,
  64. PTRS_PER_PTE * sizeof(pte_t));
  65. return pte;
  66. }
  67. /*
  68. * Add a physical memory range to the 1:1 mapping.
  69. */
  70. static int vmem_add_mem(unsigned long start, unsigned long size, int ro)
  71. {
  72. unsigned long end = start + size;
  73. unsigned long address = start;
  74. pgd_t *pg_dir;
  75. pud_t *pu_dir;
  76. pmd_t *pm_dir;
  77. pte_t *pt_dir;
  78. int ret = -ENOMEM;
  79. while (address < end) {
  80. pg_dir = pgd_offset_k(address);
  81. if (pgd_none(*pg_dir)) {
  82. pu_dir = vmem_pud_alloc();
  83. if (!pu_dir)
  84. goto out;
  85. pgd_populate(&init_mm, pg_dir, pu_dir);
  86. }
  87. pu_dir = pud_offset(pg_dir, address);
  88. #if defined(CONFIG_64BIT) && !defined(CONFIG_DEBUG_PAGEALLOC)
  89. if (MACHINE_HAS_EDAT2 && pud_none(*pu_dir) && address &&
  90. !(address & ~PUD_MASK) && (address + PUD_SIZE <= end)) {
  91. pud_val(*pu_dir) = __pa(address) |
  92. _REGION_ENTRY_TYPE_R3 | _REGION3_ENTRY_LARGE |
  93. (ro ? _REGION_ENTRY_PROTECT : 0);
  94. address += PUD_SIZE;
  95. continue;
  96. }
  97. #endif
  98. if (pud_none(*pu_dir)) {
  99. pm_dir = vmem_pmd_alloc();
  100. if (!pm_dir)
  101. goto out;
  102. pud_populate(&init_mm, pu_dir, pm_dir);
  103. }
  104. pm_dir = pmd_offset(pu_dir, address);
  105. #if defined(CONFIG_64BIT) && !defined(CONFIG_DEBUG_PAGEALLOC)
  106. if (MACHINE_HAS_EDAT1 && pmd_none(*pm_dir) && address &&
  107. !(address & ~PMD_MASK) && (address + PMD_SIZE <= end)) {
  108. pmd_val(*pm_dir) = __pa(address) |
  109. _SEGMENT_ENTRY | _SEGMENT_ENTRY_LARGE |
  110. _SEGMENT_ENTRY_YOUNG |
  111. (ro ? _SEGMENT_ENTRY_PROTECT : 0);
  112. address += PMD_SIZE;
  113. continue;
  114. }
  115. #endif
  116. if (pmd_none(*pm_dir)) {
  117. pt_dir = vmem_pte_alloc(address);
  118. if (!pt_dir)
  119. goto out;
  120. pmd_populate(&init_mm, pm_dir, pt_dir);
  121. }
  122. pt_dir = pte_offset_kernel(pm_dir, address);
  123. pte_val(*pt_dir) = __pa(address) |
  124. pgprot_val(ro ? PAGE_KERNEL_RO : PAGE_KERNEL);
  125. address += PAGE_SIZE;
  126. }
  127. ret = 0;
  128. out:
  129. return ret;
  130. }
  131. /*
  132. * Remove a physical memory range from the 1:1 mapping.
  133. * Currently only invalidates page table entries.
  134. */
  135. static void vmem_remove_range(unsigned long start, unsigned long size)
  136. {
  137. unsigned long end = start + size;
  138. unsigned long address = start;
  139. pgd_t *pg_dir;
  140. pud_t *pu_dir;
  141. pmd_t *pm_dir;
  142. pte_t *pt_dir;
  143. pte_t pte;
  144. pte_val(pte) = _PAGE_INVALID;
  145. while (address < end) {
  146. pg_dir = pgd_offset_k(address);
  147. if (pgd_none(*pg_dir)) {
  148. address += PGDIR_SIZE;
  149. continue;
  150. }
  151. pu_dir = pud_offset(pg_dir, address);
  152. if (pud_none(*pu_dir)) {
  153. address += PUD_SIZE;
  154. continue;
  155. }
  156. if (pud_large(*pu_dir)) {
  157. pud_clear(pu_dir);
  158. address += PUD_SIZE;
  159. continue;
  160. }
  161. pm_dir = pmd_offset(pu_dir, address);
  162. if (pmd_none(*pm_dir)) {
  163. address += PMD_SIZE;
  164. continue;
  165. }
  166. if (pmd_large(*pm_dir)) {
  167. pmd_clear(pm_dir);
  168. address += PMD_SIZE;
  169. continue;
  170. }
  171. pt_dir = pte_offset_kernel(pm_dir, address);
  172. *pt_dir = pte;
  173. address += PAGE_SIZE;
  174. }
  175. flush_tlb_kernel_range(start, end);
  176. }
  177. /*
  178. * Add a backed mem_map array to the virtual mem_map array.
  179. */
  180. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
  181. {
  182. unsigned long address = start;
  183. pgd_t *pg_dir;
  184. pud_t *pu_dir;
  185. pmd_t *pm_dir;
  186. pte_t *pt_dir;
  187. int ret = -ENOMEM;
  188. for (address = start; address < end;) {
  189. pg_dir = pgd_offset_k(address);
  190. if (pgd_none(*pg_dir)) {
  191. pu_dir = vmem_pud_alloc();
  192. if (!pu_dir)
  193. goto out;
  194. pgd_populate(&init_mm, pg_dir, pu_dir);
  195. }
  196. pu_dir = pud_offset(pg_dir, address);
  197. if (pud_none(*pu_dir)) {
  198. pm_dir = vmem_pmd_alloc();
  199. if (!pm_dir)
  200. goto out;
  201. pud_populate(&init_mm, pu_dir, pm_dir);
  202. }
  203. pm_dir = pmd_offset(pu_dir, address);
  204. if (pmd_none(*pm_dir)) {
  205. #ifdef CONFIG_64BIT
  206. /* Use 1MB frames for vmemmap if available. We always
  207. * use large frames even if they are only partially
  208. * used.
  209. * Otherwise we would have also page tables since
  210. * vmemmap_populate gets called for each section
  211. * separately. */
  212. if (MACHINE_HAS_EDAT1) {
  213. void *new_page;
  214. new_page = vmemmap_alloc_block(PMD_SIZE, node);
  215. if (!new_page)
  216. goto out;
  217. pmd_val(*pm_dir) = __pa(new_page) |
  218. _SEGMENT_ENTRY | _SEGMENT_ENTRY_LARGE |
  219. _SEGMENT_ENTRY_CO;
  220. address = (address + PMD_SIZE) & PMD_MASK;
  221. continue;
  222. }
  223. #endif
  224. pt_dir = vmem_pte_alloc(address);
  225. if (!pt_dir)
  226. goto out;
  227. pmd_populate(&init_mm, pm_dir, pt_dir);
  228. } else if (pmd_large(*pm_dir)) {
  229. address = (address + PMD_SIZE) & PMD_MASK;
  230. continue;
  231. }
  232. pt_dir = pte_offset_kernel(pm_dir, address);
  233. if (pte_none(*pt_dir)) {
  234. unsigned long new_page;
  235. new_page =__pa(vmem_alloc_pages(0));
  236. if (!new_page)
  237. goto out;
  238. pte_val(*pt_dir) =
  239. __pa(new_page) | pgprot_val(PAGE_KERNEL);
  240. }
  241. address += PAGE_SIZE;
  242. }
  243. memset((void *)start, 0, end - start);
  244. ret = 0;
  245. out:
  246. return ret;
  247. }
  248. void vmemmap_free(unsigned long start, unsigned long end)
  249. {
  250. }
  251. /*
  252. * Add memory segment to the segment list if it doesn't overlap with
  253. * an already present segment.
  254. */
  255. static int insert_memory_segment(struct memory_segment *seg)
  256. {
  257. struct memory_segment *tmp;
  258. if (seg->start + seg->size > VMEM_MAX_PHYS ||
  259. seg->start + seg->size < seg->start)
  260. return -ERANGE;
  261. list_for_each_entry(tmp, &mem_segs, list) {
  262. if (seg->start >= tmp->start + tmp->size)
  263. continue;
  264. if (seg->start + seg->size <= tmp->start)
  265. continue;
  266. return -ENOSPC;
  267. }
  268. list_add(&seg->list, &mem_segs);
  269. return 0;
  270. }
  271. /*
  272. * Remove memory segment from the segment list.
  273. */
  274. static void remove_memory_segment(struct memory_segment *seg)
  275. {
  276. list_del(&seg->list);
  277. }
  278. static void __remove_shared_memory(struct memory_segment *seg)
  279. {
  280. remove_memory_segment(seg);
  281. vmem_remove_range(seg->start, seg->size);
  282. }
  283. int vmem_remove_mapping(unsigned long start, unsigned long size)
  284. {
  285. struct memory_segment *seg;
  286. int ret;
  287. mutex_lock(&vmem_mutex);
  288. ret = -ENOENT;
  289. list_for_each_entry(seg, &mem_segs, list) {
  290. if (seg->start == start && seg->size == size)
  291. break;
  292. }
  293. if (seg->start != start || seg->size != size)
  294. goto out;
  295. ret = 0;
  296. __remove_shared_memory(seg);
  297. kfree(seg);
  298. out:
  299. mutex_unlock(&vmem_mutex);
  300. return ret;
  301. }
  302. int vmem_add_mapping(unsigned long start, unsigned long size)
  303. {
  304. struct memory_segment *seg;
  305. int ret;
  306. mutex_lock(&vmem_mutex);
  307. ret = -ENOMEM;
  308. seg = kzalloc(sizeof(*seg), GFP_KERNEL);
  309. if (!seg)
  310. goto out;
  311. seg->start = start;
  312. seg->size = size;
  313. ret = insert_memory_segment(seg);
  314. if (ret)
  315. goto out_free;
  316. ret = vmem_add_mem(start, size, 0);
  317. if (ret)
  318. goto out_remove;
  319. goto out;
  320. out_remove:
  321. __remove_shared_memory(seg);
  322. out_free:
  323. kfree(seg);
  324. out:
  325. mutex_unlock(&vmem_mutex);
  326. return ret;
  327. }
  328. /*
  329. * map whole physical memory to virtual memory (identity mapping)
  330. * we reserve enough space in the vmalloc area for vmemmap to hotplug
  331. * additional memory segments.
  332. */
  333. void __init vmem_map_init(void)
  334. {
  335. unsigned long ro_start, ro_end;
  336. struct memblock_region *reg;
  337. phys_addr_t start, end;
  338. ro_start = PFN_ALIGN((unsigned long)&_stext);
  339. ro_end = (unsigned long)&_eshared & PAGE_MASK;
  340. for_each_memblock(memory, reg) {
  341. start = reg->base;
  342. end = reg->base + reg->size - 1;
  343. if (start >= ro_end || end <= ro_start)
  344. vmem_add_mem(start, end - start, 0);
  345. else if (start >= ro_start && end <= ro_end)
  346. vmem_add_mem(start, end - start, 1);
  347. else if (start >= ro_start) {
  348. vmem_add_mem(start, ro_end - start, 1);
  349. vmem_add_mem(ro_end, end - ro_end, 0);
  350. } else if (end < ro_end) {
  351. vmem_add_mem(start, ro_start - start, 0);
  352. vmem_add_mem(ro_start, end - ro_start, 1);
  353. } else {
  354. vmem_add_mem(start, ro_start - start, 0);
  355. vmem_add_mem(ro_start, ro_end - ro_start, 1);
  356. vmem_add_mem(ro_end, end - ro_end, 0);
  357. }
  358. }
  359. }
  360. /*
  361. * Convert memblock.memory to a memory segment list so there is a single
  362. * list that contains all memory segments.
  363. */
  364. static int __init vmem_convert_memory_chunk(void)
  365. {
  366. struct memblock_region *reg;
  367. struct memory_segment *seg;
  368. mutex_lock(&vmem_mutex);
  369. for_each_memblock(memory, reg) {
  370. seg = kzalloc(sizeof(*seg), GFP_KERNEL);
  371. if (!seg)
  372. panic("Out of memory...\n");
  373. seg->start = reg->base;
  374. seg->size = reg->size;
  375. insert_memory_segment(seg);
  376. }
  377. mutex_unlock(&vmem_mutex);
  378. return 0;
  379. }
  380. core_initcall(vmem_convert_memory_chunk);