vmem.c 9.3 KB

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