blob: 3ffc0211dc85c68d0758966774a5f7e4e20ff3fe [file] [log] [blame]
Heiko Carstensf4eb07c2006-12-08 15:56:07 +01001/*
2 * arch/s390/mm/vmem.c
3 *
4 * Copyright IBM Corp. 2006
5 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
6 */
7
8#include <linux/bootmem.h>
9#include <linux/pfn.h>
10#include <linux/mm.h>
11#include <linux/module.h>
12#include <linux/list.h>
13#include <asm/pgalloc.h>
14#include <asm/pgtable.h>
15#include <asm/setup.h>
16#include <asm/tlbflush.h>
17
Heiko Carstensf4eb07c2006-12-08 15:56:07 +010018static DEFINE_MUTEX(vmem_mutex);
19
20struct memory_segment {
21 struct list_head list;
22 unsigned long start;
23 unsigned long size;
24};
25
26static LIST_HEAD(mem_segs);
27
Heiko Carstense62133b2007-07-27 12:29:13 +020028void __meminit memmap_init(unsigned long size, int nid, unsigned long zone,
29 unsigned long start_pfn)
Heiko Carstensf4eb07c2006-12-08 15:56:07 +010030{
31 struct page *start, *end;
32 struct page *map_start, *map_end;
33 int i;
34
35 start = pfn_to_page(start_pfn);
36 end = start + size;
37
38 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
39 unsigned long cstart, cend;
40
41 cstart = PFN_DOWN(memory_chunk[i].addr);
42 cend = cstart + PFN_DOWN(memory_chunk[i].size);
43
44 map_start = mem_map + cstart;
45 map_end = mem_map + cend;
46
47 if (map_start < start)
48 map_start = start;
49 if (map_end > end)
50 map_end = end;
51
52 map_start -= ((unsigned long) map_start & (PAGE_SIZE - 1))
53 / sizeof(struct page);
54 map_end += ((PFN_ALIGN((unsigned long) map_end)
55 - (unsigned long) map_end)
56 / sizeof(struct page));
57
58 if (map_start < map_end)
59 memmap_init_zone((unsigned long)(map_end - map_start),
Dave Hansena2f3aa022007-01-10 23:15:30 -080060 nid, zone, page_to_pfn(map_start),
61 MEMMAP_EARLY);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +010062 }
63}
64
Heiko Carstens2bc89b52008-02-05 16:50:40 +010065static void __ref *vmem_alloc_pages(unsigned int order)
Heiko Carstensf4eb07c2006-12-08 15:56:07 +010066{
67 if (slab_is_available())
68 return (void *)__get_free_pages(GFP_KERNEL, order);
69 return alloc_bootmem_pages((1 << order) * PAGE_SIZE);
70}
71
Martin Schwidefsky5a216a22008-02-09 18:24:36 +010072static inline pud_t *vmem_pud_alloc(void)
73{
74 pud_t *pud = NULL;
75
76#ifdef CONFIG_64BIT
77 pud = vmem_alloc_pages(2);
78 if (!pud)
79 return NULL;
Heiko Carstens8fc63652008-04-30 13:38:44 +020080 clear_table((unsigned long *) pud, _REGION3_ENTRY_EMPTY, PAGE_SIZE * 4);
Martin Schwidefsky5a216a22008-02-09 18:24:36 +010081#endif
82 return pud;
83}
Martin Schwidefsky190a1d72007-10-22 12:52:48 +020084
Heiko Carstensf4eb07c2006-12-08 15:56:07 +010085static inline pmd_t *vmem_pmd_alloc(void)
86{
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020087 pmd_t *pmd = NULL;
Heiko Carstensf4eb07c2006-12-08 15:56:07 +010088
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020089#ifdef CONFIG_64BIT
90 pmd = vmem_alloc_pages(2);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +010091 if (!pmd)
92 return NULL;
Heiko Carstens8fc63652008-04-30 13:38:44 +020093 clear_table((unsigned long *) pmd, _SEGMENT_ENTRY_EMPTY, PAGE_SIZE * 4);
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020094#endif
Heiko Carstensf4eb07c2006-12-08 15:56:07 +010095 return pmd;
96}
97
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010098static pte_t __init_refok *vmem_pte_alloc(void)
Heiko Carstensf4eb07c2006-12-08 15:56:07 +010099{
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100100 pte_t *pte;
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100101
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100102 if (slab_is_available())
103 pte = (pte_t *) page_table_alloc(&init_mm);
104 else
105 pte = alloc_bootmem(PTRS_PER_PTE * sizeof(pte_t));
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100106 if (!pte)
107 return NULL;
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100108 clear_table((unsigned long *) pte, _PAGE_TYPE_EMPTY,
109 PTRS_PER_PTE * sizeof(pte_t));
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100110 return pte;
111}
112
113/*
114 * Add a physical memory range to the 1:1 mapping.
115 */
116static int vmem_add_range(unsigned long start, unsigned long size)
117{
118 unsigned long address;
119 pgd_t *pg_dir;
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200120 pud_t *pu_dir;
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100121 pmd_t *pm_dir;
122 pte_t *pt_dir;
123 pte_t pte;
124 int ret = -ENOMEM;
125
126 for (address = start; address < start + size; address += PAGE_SIZE) {
127 pg_dir = pgd_offset_k(address);
128 if (pgd_none(*pg_dir)) {
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200129 pu_dir = vmem_pud_alloc();
130 if (!pu_dir)
131 goto out;
132 pgd_populate_kernel(&init_mm, pg_dir, pu_dir);
133 }
134
135 pu_dir = pud_offset(pg_dir, address);
136 if (pud_none(*pu_dir)) {
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100137 pm_dir = vmem_pmd_alloc();
138 if (!pm_dir)
139 goto out;
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200140 pud_populate_kernel(&init_mm, pu_dir, pm_dir);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100141 }
142
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200143 pm_dir = pmd_offset(pu_dir, address);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100144 if (pmd_none(*pm_dir)) {
145 pt_dir = vmem_pte_alloc();
146 if (!pt_dir)
147 goto out;
148 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
149 }
150
151 pt_dir = pte_offset_kernel(pm_dir, address);
152 pte = pfn_pte(address >> PAGE_SHIFT, PAGE_KERNEL);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100153 *pt_dir = pte;
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100154 }
155 ret = 0;
156out:
157 flush_tlb_kernel_range(start, start + size);
158 return ret;
159}
160
161/*
162 * Remove a physical memory range from the 1:1 mapping.
163 * Currently only invalidates page table entries.
164 */
165static void vmem_remove_range(unsigned long start, unsigned long size)
166{
167 unsigned long address;
168 pgd_t *pg_dir;
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200169 pud_t *pu_dir;
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100170 pmd_t *pm_dir;
171 pte_t *pt_dir;
172 pte_t pte;
173
174 pte_val(pte) = _PAGE_TYPE_EMPTY;
175 for (address = start; address < start + size; address += PAGE_SIZE) {
176 pg_dir = pgd_offset_k(address);
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200177 pu_dir = pud_offset(pg_dir, address);
178 if (pud_none(*pu_dir))
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100179 continue;
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200180 pm_dir = pmd_offset(pu_dir, address);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100181 if (pmd_none(*pm_dir))
182 continue;
183 pt_dir = pte_offset_kernel(pm_dir, address);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100184 *pt_dir = pte;
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100185 }
186 flush_tlb_kernel_range(start, start + size);
187}
188
189/*
190 * Add a backed mem_map array to the virtual mem_map array.
191 */
192static int vmem_add_mem_map(unsigned long start, unsigned long size)
193{
194 unsigned long address, start_addr, end_addr;
195 struct page *map_start, *map_end;
196 pgd_t *pg_dir;
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200197 pud_t *pu_dir;
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100198 pmd_t *pm_dir;
199 pte_t *pt_dir;
200 pte_t pte;
201 int ret = -ENOMEM;
202
Christian Borntraeger5fd9c6e2008-01-26 14:11:00 +0100203 map_start = VMEM_MAP + PFN_DOWN(start);
204 map_end = VMEM_MAP + PFN_DOWN(start + size);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100205
206 start_addr = (unsigned long) map_start & PAGE_MASK;
207 end_addr = PFN_ALIGN((unsigned long) map_end);
208
209 for (address = start_addr; address < end_addr; address += PAGE_SIZE) {
210 pg_dir = pgd_offset_k(address);
211 if (pgd_none(*pg_dir)) {
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200212 pu_dir = vmem_pud_alloc();
213 if (!pu_dir)
214 goto out;
215 pgd_populate_kernel(&init_mm, pg_dir, pu_dir);
216 }
217
218 pu_dir = pud_offset(pg_dir, address);
219 if (pud_none(*pu_dir)) {
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100220 pm_dir = vmem_pmd_alloc();
221 if (!pm_dir)
222 goto out;
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200223 pud_populate_kernel(&init_mm, pu_dir, pm_dir);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100224 }
225
Martin Schwidefsky190a1d72007-10-22 12:52:48 +0200226 pm_dir = pmd_offset(pu_dir, address);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100227 if (pmd_none(*pm_dir)) {
228 pt_dir = vmem_pte_alloc();
229 if (!pt_dir)
230 goto out;
231 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
232 }
233
234 pt_dir = pte_offset_kernel(pm_dir, address);
235 if (pte_none(*pt_dir)) {
236 unsigned long new_page;
237
238 new_page =__pa(vmem_alloc_pages(0));
239 if (!new_page)
240 goto out;
241 pte = pfn_pte(new_page >> PAGE_SHIFT, PAGE_KERNEL);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100242 *pt_dir = pte;
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100243 }
244 }
245 ret = 0;
246out:
247 flush_tlb_kernel_range(start_addr, end_addr);
248 return ret;
249}
250
251static int vmem_add_mem(unsigned long start, unsigned long size)
252{
253 int ret;
254
Christian Borntraegera2fd64d2008-01-26 14:11:16 +0100255 ret = vmem_add_mem_map(start, size);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100256 if (ret)
257 return ret;
Christian Borntraegera2fd64d2008-01-26 14:11:16 +0100258 return vmem_add_range(start, size);
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100259}
260
261/*
262 * Add memory segment to the segment list if it doesn't overlap with
263 * an already present segment.
264 */
265static int insert_memory_segment(struct memory_segment *seg)
266{
267 struct memory_segment *tmp;
268
Heiko Carstens01891032008-02-05 16:50:49 +0100269 if (seg->start + seg->size >= VMEM_MAX_PHYS ||
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100270 seg->start + seg->size < seg->start)
271 return -ERANGE;
272
273 list_for_each_entry(tmp, &mem_segs, list) {
274 if (seg->start >= tmp->start + tmp->size)
275 continue;
276 if (seg->start + seg->size <= tmp->start)
277 continue;
278 return -ENOSPC;
279 }
280 list_add(&seg->list, &mem_segs);
281 return 0;
282}
283
284/*
285 * Remove memory segment from the segment list.
286 */
287static void remove_memory_segment(struct memory_segment *seg)
288{
289 list_del(&seg->list);
290}
291
292static void __remove_shared_memory(struct memory_segment *seg)
293{
294 remove_memory_segment(seg);
295 vmem_remove_range(seg->start, seg->size);
296}
297
298int remove_shared_memory(unsigned long start, unsigned long size)
299{
300 struct memory_segment *seg;
301 int ret;
302
303 mutex_lock(&vmem_mutex);
304
305 ret = -ENOENT;
306 list_for_each_entry(seg, &mem_segs, list) {
307 if (seg->start == start && seg->size == size)
308 break;
309 }
310
311 if (seg->start != start || seg->size != size)
312 goto out;
313
314 ret = 0;
315 __remove_shared_memory(seg);
316 kfree(seg);
317out:
318 mutex_unlock(&vmem_mutex);
319 return ret;
320}
321
322int add_shared_memory(unsigned long start, unsigned long size)
323{
324 struct memory_segment *seg;
325 struct page *page;
326 unsigned long pfn, num_pfn, end_pfn;
327 int ret;
328
329 mutex_lock(&vmem_mutex);
330 ret = -ENOMEM;
331 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
332 if (!seg)
333 goto out;
334 seg->start = start;
335 seg->size = size;
336
337 ret = insert_memory_segment(seg);
338 if (ret)
339 goto out_free;
340
341 ret = vmem_add_mem(start, size);
342 if (ret)
343 goto out_remove;
344
345 pfn = PFN_DOWN(start);
346 num_pfn = PFN_DOWN(size);
347 end_pfn = pfn + num_pfn;
348
349 page = pfn_to_page(pfn);
350 memset(page, 0, num_pfn * sizeof(struct page));
351
352 for (; pfn < end_pfn; pfn++) {
353 page = pfn_to_page(pfn);
354 init_page_count(page);
355 reset_page_mapcount(page);
356 SetPageReserved(page);
357 INIT_LIST_HEAD(&page->lru);
358 }
359 goto out;
360
361out_remove:
362 __remove_shared_memory(seg);
363out_free:
364 kfree(seg);
365out:
366 mutex_unlock(&vmem_mutex);
367 return ret;
368}
369
370/*
371 * map whole physical memory to virtual memory (identity mapping)
Christian Borntraeger5fd9c6e2008-01-26 14:11:00 +0100372 * we reserve enough space in the vmalloc area for vmemmap to hotplug
373 * additional memory segments.
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100374 */
375void __init vmem_map_init(void)
376{
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100377 int i;
378
Martin Schwidefsky146e4b32008-02-09 18:24:35 +0100379 INIT_LIST_HEAD(&init_mm.context.crst_list);
380 INIT_LIST_HEAD(&init_mm.context.pgtable_list);
381 init_mm.context.noexec = 0;
Christian Borntraeger5fd9c6e2008-01-26 14:11:00 +0100382 NODE_DATA(0)->node_mem_map = VMEM_MAP;
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100383 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++)
384 vmem_add_mem(memory_chunk[i].addr, memory_chunk[i].size);
385}
386
387/*
388 * Convert memory chunk array to a memory segment list so there is a single
389 * list that contains both r/w memory and shared memory segments.
390 */
391static int __init vmem_convert_memory_chunk(void)
392{
393 struct memory_segment *seg;
394 int i;
395
396 mutex_lock(&vmem_mutex);
Heiko Carstens9f4b0ba2008-01-26 14:11:02 +0100397 for (i = 0; i < MEMORY_CHUNKS; i++) {
Heiko Carstensf4eb07c2006-12-08 15:56:07 +0100398 if (!memory_chunk[i].size)
399 continue;
400 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
401 if (!seg)
402 panic("Out of memory...\n");
403 seg->start = memory_chunk[i].addr;
404 seg->size = memory_chunk[i].size;
405 insert_memory_segment(seg);
406 }
407 mutex_unlock(&vmem_mutex);
408 return 0;
409}
410
411core_initcall(vmem_convert_memory_chunk);