vmem.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/cacheflush.h>
  14. #include <asm/pgalloc.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/setup.h>
  17. #include <asm/tlbflush.h>
  18. #include <asm/sections.h>
  19. static DEFINE_MUTEX(vmem_mutex);
  20. struct memory_segment {
  21. struct list_head list;
  22. unsigned long start;
  23. unsigned long size;
  24. };
  25. static LIST_HEAD(mem_segs);
  26. static void __ref *vmem_alloc_pages(unsigned int order)
  27. {
  28. unsigned long size = PAGE_SIZE << order;
  29. if (slab_is_available())
  30. return (void *)__get_free_pages(GFP_KERNEL, order);
  31. return alloc_bootmem_align(size, size);
  32. }
  33. static inline pud_t *vmem_pud_alloc(void)
  34. {
  35. pud_t *pud = NULL;
  36. pud = vmem_alloc_pages(2);
  37. if (!pud)
  38. return NULL;
  39. clear_table((unsigned long *) pud, _REGION3_ENTRY_EMPTY, PAGE_SIZE * 4);
  40. return pud;
  41. }
  42. pmd_t *vmem_pmd_alloc(void)
  43. {
  44. pmd_t *pmd = NULL;
  45. pmd = vmem_alloc_pages(2);
  46. if (!pmd)
  47. return NULL;
  48. clear_table((unsigned long *) pmd, _SEGMENT_ENTRY_EMPTY, PAGE_SIZE * 4);
  49. return pmd;
  50. }
  51. pte_t __ref *vmem_pte_alloc(void)
  52. {
  53. pte_t *pte;
  54. if (slab_is_available())
  55. pte = (pte_t *) page_table_alloc(&init_mm);
  56. else
  57. pte = alloc_bootmem_align(PTRS_PER_PTE * sizeof(pte_t),
  58. PTRS_PER_PTE * sizeof(pte_t));
  59. if (!pte)
  60. return NULL;
  61. clear_table((unsigned long *) pte, _PAGE_INVALID,
  62. PTRS_PER_PTE * sizeof(pte_t));
  63. return pte;
  64. }
  65. /*
  66. * Add a physical memory range to the 1:1 mapping.
  67. */
  68. static int vmem_add_mem(unsigned long start, unsigned long size)
  69. {
  70. unsigned long pages4k, pages1m, pages2g;
  71. unsigned long end = start + size;
  72. unsigned long address = start;
  73. pgd_t *pg_dir;
  74. pud_t *pu_dir;
  75. pmd_t *pm_dir;
  76. pte_t *pt_dir;
  77. int ret = -ENOMEM;
  78. pages4k = pages1m = pages2g = 0;
  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 (MACHINE_HAS_EDAT2 && pud_none(*pu_dir) && address &&
  89. !(address & ~PUD_MASK) && (address + PUD_SIZE <= end) &&
  90. !debug_pagealloc_enabled()) {
  91. pud_val(*pu_dir) = address | pgprot_val(REGION3_KERNEL);
  92. address += PUD_SIZE;
  93. pages2g++;
  94. continue;
  95. }
  96. if (pud_none(*pu_dir)) {
  97. pm_dir = vmem_pmd_alloc();
  98. if (!pm_dir)
  99. goto out;
  100. pud_populate(&init_mm, pu_dir, pm_dir);
  101. }
  102. pm_dir = pmd_offset(pu_dir, address);
  103. if (MACHINE_HAS_EDAT1 && pmd_none(*pm_dir) && address &&
  104. !(address & ~PMD_MASK) && (address + PMD_SIZE <= end) &&
  105. !debug_pagealloc_enabled()) {
  106. pmd_val(*pm_dir) = address | pgprot_val(SEGMENT_KERNEL);
  107. address += PMD_SIZE;
  108. pages1m++;
  109. continue;
  110. }
  111. if (pmd_none(*pm_dir)) {
  112. pt_dir = vmem_pte_alloc();
  113. if (!pt_dir)
  114. goto out;
  115. pmd_populate(&init_mm, pm_dir, pt_dir);
  116. }
  117. pt_dir = pte_offset_kernel(pm_dir, address);
  118. pte_val(*pt_dir) = address | pgprot_val(PAGE_KERNEL);
  119. address += PAGE_SIZE;
  120. pages4k++;
  121. }
  122. ret = 0;
  123. out:
  124. update_page_count(PG_DIRECT_MAP_4K, pages4k);
  125. update_page_count(PG_DIRECT_MAP_1M, pages1m);
  126. update_page_count(PG_DIRECT_MAP_2G, pages2g);
  127. return ret;
  128. }
  129. /*
  130. * Remove a physical memory range from the 1:1 mapping.
  131. * Currently only invalidates page table entries.
  132. */
  133. static void vmem_remove_range(unsigned long start, unsigned long size)
  134. {
  135. unsigned long pages4k, pages1m, pages2g;
  136. unsigned long end = start + size;
  137. unsigned long address = start;
  138. pgd_t *pg_dir;
  139. pud_t *pu_dir;
  140. pmd_t *pm_dir;
  141. pte_t *pt_dir;
  142. pages4k = pages1m = pages2g = 0;
  143. while (address < end) {
  144. pg_dir = pgd_offset_k(address);
  145. if (pgd_none(*pg_dir)) {
  146. address += PGDIR_SIZE;
  147. continue;
  148. }
  149. pu_dir = pud_offset(pg_dir, address);
  150. if (pud_none(*pu_dir)) {
  151. address += PUD_SIZE;
  152. continue;
  153. }
  154. if (pud_large(*pu_dir)) {
  155. pud_clear(pu_dir);
  156. address += PUD_SIZE;
  157. pages2g++;
  158. continue;
  159. }
  160. pm_dir = pmd_offset(pu_dir, address);
  161. if (pmd_none(*pm_dir)) {
  162. address += PMD_SIZE;
  163. continue;
  164. }
  165. if (pmd_large(*pm_dir)) {
  166. pmd_clear(pm_dir);
  167. address += PMD_SIZE;
  168. pages1m++;
  169. continue;
  170. }
  171. pt_dir = pte_offset_kernel(pm_dir, address);
  172. pte_clear(&init_mm, address, pt_dir);
  173. address += PAGE_SIZE;
  174. pages4k++;
  175. }
  176. flush_tlb_kernel_range(start, end);
  177. update_page_count(PG_DIRECT_MAP_4K, -pages4k);
  178. update_page_count(PG_DIRECT_MAP_1M, -pages1m);
  179. update_page_count(PG_DIRECT_MAP_2G, -pages2g);
  180. }
  181. /*
  182. * Add a backed mem_map array to the virtual mem_map array.
  183. */
  184. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
  185. {
  186. unsigned long address = start;
  187. pgd_t *pg_dir;
  188. pud_t *pu_dir;
  189. pmd_t *pm_dir;
  190. pte_t *pt_dir;
  191. int ret = -ENOMEM;
  192. for (address = start; address < end;) {
  193. pg_dir = pgd_offset_k(address);
  194. if (pgd_none(*pg_dir)) {
  195. pu_dir = vmem_pud_alloc();
  196. if (!pu_dir)
  197. goto out;
  198. pgd_populate(&init_mm, pg_dir, pu_dir);
  199. }
  200. pu_dir = pud_offset(pg_dir, address);
  201. if (pud_none(*pu_dir)) {
  202. pm_dir = vmem_pmd_alloc();
  203. if (!pm_dir)
  204. goto out;
  205. pud_populate(&init_mm, pu_dir, pm_dir);
  206. }
  207. pm_dir = pmd_offset(pu_dir, address);
  208. if (pmd_none(*pm_dir)) {
  209. /* Use 1MB frames for vmemmap if available. We always
  210. * use large frames even if they are only partially
  211. * used.
  212. * Otherwise we would have also page tables since
  213. * vmemmap_populate gets called for each section
  214. * separately. */
  215. if (MACHINE_HAS_EDAT1) {
  216. void *new_page;
  217. new_page = vmemmap_alloc_block(PMD_SIZE, node);
  218. if (!new_page)
  219. goto out;
  220. pmd_val(*pm_dir) = __pa(new_page) |
  221. _SEGMENT_ENTRY | _SEGMENT_ENTRY_LARGE;
  222. address = (address + PMD_SIZE) & PMD_MASK;
  223. continue;
  224. }
  225. pt_dir = vmem_pte_alloc();
  226. if (!pt_dir)
  227. goto out;
  228. pmd_populate(&init_mm, pm_dir, pt_dir);
  229. } else if (pmd_large(*pm_dir)) {
  230. address = (address + PMD_SIZE) & PMD_MASK;
  231. continue;
  232. }
  233. pt_dir = pte_offset_kernel(pm_dir, address);
  234. if (pte_none(*pt_dir)) {
  235. void *new_page;
  236. new_page = vmemmap_alloc_block(PAGE_SIZE, node);
  237. if (!new_page)
  238. goto out;
  239. pte_val(*pt_dir) =
  240. __pa(new_page) | pgprot_val(PAGE_KERNEL);
  241. }
  242. address += PAGE_SIZE;
  243. }
  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);
  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 size = _eshared - _stext;
  336. struct memblock_region *reg;
  337. for_each_memblock(memory, reg)
  338. vmem_add_mem(reg->base, reg->size);
  339. set_memory_ro((unsigned long)_stext, size >> PAGE_SHIFT);
  340. pr_info("Write protected kernel read-only data: %luk\n", size >> 10);
  341. }
  342. /*
  343. * Convert memblock.memory to a memory segment list so there is a single
  344. * list that contains all memory segments.
  345. */
  346. static int __init vmem_convert_memory_chunk(void)
  347. {
  348. struct memblock_region *reg;
  349. struct memory_segment *seg;
  350. mutex_lock(&vmem_mutex);
  351. for_each_memblock(memory, reg) {
  352. seg = kzalloc(sizeof(*seg), GFP_KERNEL);
  353. if (!seg)
  354. panic("Out of memory...\n");
  355. seg->start = reg->base;
  356. seg->size = reg->size;
  357. insert_memory_segment(seg);
  358. }
  359. mutex_unlock(&vmem_mutex);
  360. return 0;
  361. }
  362. core_initcall(vmem_convert_memory_chunk);