vmem.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * arch/s390/mm/vmem.c
  3. *
  4. * Copyright IBM Corp. 2006
  5. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  6. */
  7. #include <linux/bootmem.h>
  8. #include <linux/pfn.h>
  9. #include <linux/mm.h>
  10. #include <linux/module.h>
  11. #include <linux/list.h>
  12. #include <linux/hugetlb.h>
  13. #include <linux/slab.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. if (slab_is_available())
  29. return (void *)__get_free_pages(GFP_KERNEL, order);
  30. return alloc_bootmem_pages((1 << order) * PAGE_SIZE);
  31. }
  32. static inline pud_t *vmem_pud_alloc(void)
  33. {
  34. pud_t *pud = NULL;
  35. #ifdef CONFIG_64BIT
  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. #endif
  41. return pud;
  42. }
  43. static inline pmd_t *vmem_pmd_alloc(void)
  44. {
  45. pmd_t *pmd = NULL;
  46. #ifdef CONFIG_64BIT
  47. pmd = vmem_alloc_pages(2);
  48. if (!pmd)
  49. return NULL;
  50. clear_table((unsigned long *) pmd, _SEGMENT_ENTRY_EMPTY, PAGE_SIZE * 4);
  51. #endif
  52. return pmd;
  53. }
  54. static pte_t __ref *vmem_pte_alloc(void)
  55. {
  56. pte_t *pte;
  57. if (slab_is_available())
  58. pte = (pte_t *) page_table_alloc(&init_mm);
  59. else
  60. pte = alloc_bootmem(PTRS_PER_PTE * sizeof(pte_t));
  61. if (!pte)
  62. return NULL;
  63. clear_table((unsigned long *) pte, _PAGE_TYPE_EMPTY,
  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 address;
  73. pgd_t *pg_dir;
  74. pud_t *pu_dir;
  75. pmd_t *pm_dir;
  76. pte_t *pt_dir;
  77. pte_t pte;
  78. int ret = -ENOMEM;
  79. for (address = start; address < start + size; address += PAGE_SIZE) {
  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 (pud_none(*pu_dir)) {
  89. pm_dir = vmem_pmd_alloc();
  90. if (!pm_dir)
  91. goto out;
  92. pud_populate(&init_mm, pu_dir, pm_dir);
  93. }
  94. pte = mk_pte_phys(address, __pgprot(ro ? _PAGE_RO : 0));
  95. pm_dir = pmd_offset(pu_dir, address);
  96. #ifdef __s390x__
  97. if (MACHINE_HAS_HPAGE && !(address & ~HPAGE_MASK) &&
  98. (address + HPAGE_SIZE <= start + size) &&
  99. (address >= HPAGE_SIZE)) {
  100. pte_val(pte) |= _SEGMENT_ENTRY_LARGE;
  101. pmd_val(*pm_dir) = pte_val(pte);
  102. address += HPAGE_SIZE - PAGE_SIZE;
  103. continue;
  104. }
  105. #endif
  106. if (pmd_none(*pm_dir)) {
  107. pt_dir = vmem_pte_alloc();
  108. if (!pt_dir)
  109. goto out;
  110. pmd_populate(&init_mm, pm_dir, pt_dir);
  111. }
  112. pt_dir = pte_offset_kernel(pm_dir, address);
  113. *pt_dir = pte;
  114. }
  115. ret = 0;
  116. out:
  117. flush_tlb_kernel_range(start, start + size);
  118. return ret;
  119. }
  120. /*
  121. * Remove a physical memory range from the 1:1 mapping.
  122. * Currently only invalidates page table entries.
  123. */
  124. static void vmem_remove_range(unsigned long start, unsigned long size)
  125. {
  126. unsigned long address;
  127. pgd_t *pg_dir;
  128. pud_t *pu_dir;
  129. pmd_t *pm_dir;
  130. pte_t *pt_dir;
  131. pte_t pte;
  132. pte_val(pte) = _PAGE_TYPE_EMPTY;
  133. for (address = start; address < start + size; address += PAGE_SIZE) {
  134. pg_dir = pgd_offset_k(address);
  135. pu_dir = pud_offset(pg_dir, address);
  136. if (pud_none(*pu_dir))
  137. continue;
  138. pm_dir = pmd_offset(pu_dir, address);
  139. if (pmd_none(*pm_dir))
  140. continue;
  141. if (pmd_huge(*pm_dir)) {
  142. pmd_clear(pm_dir);
  143. address += HPAGE_SIZE - PAGE_SIZE;
  144. continue;
  145. }
  146. pt_dir = pte_offset_kernel(pm_dir, address);
  147. *pt_dir = pte;
  148. }
  149. flush_tlb_kernel_range(start, start + size);
  150. }
  151. /*
  152. * Add a backed mem_map array to the virtual mem_map array.
  153. */
  154. int __meminit vmemmap_populate(struct page *start, unsigned long nr, int node)
  155. {
  156. unsigned long address, start_addr, end_addr;
  157. pgd_t *pg_dir;
  158. pud_t *pu_dir;
  159. pmd_t *pm_dir;
  160. pte_t *pt_dir;
  161. pte_t pte;
  162. int ret = -ENOMEM;
  163. start_addr = (unsigned long) start;
  164. end_addr = (unsigned long) (start + nr);
  165. for (address = start_addr; address < end_addr; address += PAGE_SIZE) {
  166. pg_dir = pgd_offset_k(address);
  167. if (pgd_none(*pg_dir)) {
  168. pu_dir = vmem_pud_alloc();
  169. if (!pu_dir)
  170. goto out;
  171. pgd_populate(&init_mm, pg_dir, pu_dir);
  172. }
  173. pu_dir = pud_offset(pg_dir, address);
  174. if (pud_none(*pu_dir)) {
  175. pm_dir = vmem_pmd_alloc();
  176. if (!pm_dir)
  177. goto out;
  178. pud_populate(&init_mm, pu_dir, pm_dir);
  179. }
  180. pm_dir = pmd_offset(pu_dir, address);
  181. if (pmd_none(*pm_dir)) {
  182. pt_dir = vmem_pte_alloc();
  183. if (!pt_dir)
  184. goto out;
  185. pmd_populate(&init_mm, pm_dir, pt_dir);
  186. }
  187. pt_dir = pte_offset_kernel(pm_dir, address);
  188. if (pte_none(*pt_dir)) {
  189. unsigned long new_page;
  190. new_page =__pa(vmem_alloc_pages(0));
  191. if (!new_page)
  192. goto out;
  193. pte = pfn_pte(new_page >> PAGE_SHIFT, PAGE_KERNEL);
  194. *pt_dir = pte;
  195. }
  196. }
  197. memset(start, 0, nr * sizeof(struct page));
  198. ret = 0;
  199. out:
  200. flush_tlb_kernel_range(start_addr, end_addr);
  201. return ret;
  202. }
  203. /*
  204. * Add memory segment to the segment list if it doesn't overlap with
  205. * an already present segment.
  206. */
  207. static int insert_memory_segment(struct memory_segment *seg)
  208. {
  209. struct memory_segment *tmp;
  210. if (seg->start + seg->size > VMEM_MAX_PHYS ||
  211. seg->start + seg->size < seg->start)
  212. return -ERANGE;
  213. list_for_each_entry(tmp, &mem_segs, list) {
  214. if (seg->start >= tmp->start + tmp->size)
  215. continue;
  216. if (seg->start + seg->size <= tmp->start)
  217. continue;
  218. return -ENOSPC;
  219. }
  220. list_add(&seg->list, &mem_segs);
  221. return 0;
  222. }
  223. /*
  224. * Remove memory segment from the segment list.
  225. */
  226. static void remove_memory_segment(struct memory_segment *seg)
  227. {
  228. list_del(&seg->list);
  229. }
  230. static void __remove_shared_memory(struct memory_segment *seg)
  231. {
  232. remove_memory_segment(seg);
  233. vmem_remove_range(seg->start, seg->size);
  234. }
  235. int vmem_remove_mapping(unsigned long start, unsigned long size)
  236. {
  237. struct memory_segment *seg;
  238. int ret;
  239. mutex_lock(&vmem_mutex);
  240. ret = -ENOENT;
  241. list_for_each_entry(seg, &mem_segs, list) {
  242. if (seg->start == start && seg->size == size)
  243. break;
  244. }
  245. if (seg->start != start || seg->size != size)
  246. goto out;
  247. ret = 0;
  248. __remove_shared_memory(seg);
  249. kfree(seg);
  250. out:
  251. mutex_unlock(&vmem_mutex);
  252. return ret;
  253. }
  254. int vmem_add_mapping(unsigned long start, unsigned long size)
  255. {
  256. struct memory_segment *seg;
  257. int ret;
  258. mutex_lock(&vmem_mutex);
  259. ret = -ENOMEM;
  260. seg = kzalloc(sizeof(*seg), GFP_KERNEL);
  261. if (!seg)
  262. goto out;
  263. seg->start = start;
  264. seg->size = size;
  265. ret = insert_memory_segment(seg);
  266. if (ret)
  267. goto out_free;
  268. ret = vmem_add_mem(start, size, 0);
  269. if (ret)
  270. goto out_remove;
  271. goto out;
  272. out_remove:
  273. __remove_shared_memory(seg);
  274. out_free:
  275. kfree(seg);
  276. out:
  277. mutex_unlock(&vmem_mutex);
  278. return ret;
  279. }
  280. /*
  281. * map whole physical memory to virtual memory (identity mapping)
  282. * we reserve enough space in the vmalloc area for vmemmap to hotplug
  283. * additional memory segments.
  284. */
  285. void __init vmem_map_init(void)
  286. {
  287. unsigned long ro_start, ro_end;
  288. unsigned long start, end;
  289. int i;
  290. ro_start = ((unsigned long)&_stext) & PAGE_MASK;
  291. ro_end = PFN_ALIGN((unsigned long)&_eshared);
  292. for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
  293. start = memory_chunk[i].addr;
  294. end = memory_chunk[i].addr + memory_chunk[i].size;
  295. if (start >= ro_end || end <= ro_start)
  296. vmem_add_mem(start, end - start, 0);
  297. else if (start >= ro_start && end <= ro_end)
  298. vmem_add_mem(start, end - start, 1);
  299. else if (start >= ro_start) {
  300. vmem_add_mem(start, ro_end - start, 1);
  301. vmem_add_mem(ro_end, end - ro_end, 0);
  302. } else if (end < ro_end) {
  303. vmem_add_mem(start, ro_start - start, 0);
  304. vmem_add_mem(ro_start, end - ro_start, 1);
  305. } else {
  306. vmem_add_mem(start, ro_start - start, 0);
  307. vmem_add_mem(ro_start, ro_end - ro_start, 1);
  308. vmem_add_mem(ro_end, end - ro_end, 0);
  309. }
  310. }
  311. }
  312. /*
  313. * Convert memory chunk array to a memory segment list so there is a single
  314. * list that contains both r/w memory and shared memory segments.
  315. */
  316. static int __init vmem_convert_memory_chunk(void)
  317. {
  318. struct memory_segment *seg;
  319. int i;
  320. mutex_lock(&vmem_mutex);
  321. for (i = 0; i < MEMORY_CHUNKS; i++) {
  322. if (!memory_chunk[i].size)
  323. continue;
  324. seg = kzalloc(sizeof(*seg), GFP_KERNEL);
  325. if (!seg)
  326. panic("Out of memory...\n");
  327. seg->start = memory_chunk[i].addr;
  328. seg->size = memory_chunk[i].size;
  329. insert_memory_segment(seg);
  330. }
  331. mutex_unlock(&vmem_mutex);
  332. return 0;
  333. }
  334. core_initcall(vmem_convert_memory_chunk);