include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-2.6-block.git] / arch / s390 / mm / vmem.c
CommitLineData
f4eb07c1
HC
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
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>
53492b1d 13#include <linux/hugetlb.h>
5a0e3ad6 14#include <linux/slab.h>
f4eb07c1
HC
15#include <asm/pgalloc.h>
16#include <asm/pgtable.h>
17#include <asm/setup.h>
18#include <asm/tlbflush.h>
53492b1d 19#include <asm/sections.h>
f4eb07c1 20
f4eb07c1
HC
21static DEFINE_MUTEX(vmem_mutex);
22
23struct memory_segment {
24 struct list_head list;
25 unsigned long start;
26 unsigned long size;
27};
28
29static LIST_HEAD(mem_segs);
30
67060d9c
HC
31static void __ref *vmem_alloc_pages(unsigned int order)
32{
33 if (slab_is_available())
34 return (void *)__get_free_pages(GFP_KERNEL, order);
35 return alloc_bootmem_pages((1 << order) * PAGE_SIZE);
36}
37
38static inline pud_t *vmem_pud_alloc(void)
5a216a20
MS
39{
40 pud_t *pud = NULL;
41
42#ifdef CONFIG_64BIT
67060d9c 43 pud = vmem_alloc_pages(2);
5a216a20
MS
44 if (!pud)
45 return NULL;
8fc63658 46 clear_table((unsigned long *) pud, _REGION3_ENTRY_EMPTY, PAGE_SIZE * 4);
5a216a20
MS
47#endif
48 return pud;
49}
190a1d72 50
67060d9c 51static inline pmd_t *vmem_pmd_alloc(void)
f4eb07c1 52{
3610cce8 53 pmd_t *pmd = NULL;
f4eb07c1 54
3610cce8 55#ifdef CONFIG_64BIT
67060d9c 56 pmd = vmem_alloc_pages(2);
f4eb07c1
HC
57 if (!pmd)
58 return NULL;
8fc63658 59 clear_table((unsigned long *) pmd, _SEGMENT_ENTRY_EMPTY, PAGE_SIZE * 4);
3610cce8 60#endif
f4eb07c1
HC
61 return pmd;
62}
63
2069e978 64static pte_t __ref *vmem_pte_alloc(void)
f4eb07c1 65{
146e4b3c 66 pte_t *pte;
f4eb07c1 67
146e4b3c
MS
68 if (slab_is_available())
69 pte = (pte_t *) page_table_alloc(&init_mm);
70 else
71 pte = alloc_bootmem(PTRS_PER_PTE * sizeof(pte_t));
f4eb07c1
HC
72 if (!pte)
73 return NULL;
6a985c61
CB
74 if (MACHINE_HAS_HPAGE)
75 clear_table((unsigned long *) pte, _PAGE_TYPE_EMPTY | _PAGE_CO,
76 PTRS_PER_PTE * sizeof(pte_t));
77 else
78 clear_table((unsigned long *) pte, _PAGE_TYPE_EMPTY,
79 PTRS_PER_PTE * sizeof(pte_t));
f4eb07c1
HC
80 return pte;
81}
82
83/*
84 * Add a physical memory range to the 1:1 mapping.
85 */
17f34580 86static int vmem_add_mem(unsigned long start, unsigned long size, int ro)
f4eb07c1
HC
87{
88 unsigned long address;
89 pgd_t *pg_dir;
190a1d72 90 pud_t *pu_dir;
f4eb07c1
HC
91 pmd_t *pm_dir;
92 pte_t *pt_dir;
93 pte_t pte;
94 int ret = -ENOMEM;
95
96 for (address = start; address < start + size; address += PAGE_SIZE) {
97 pg_dir = pgd_offset_k(address);
98 if (pgd_none(*pg_dir)) {
190a1d72
MS
99 pu_dir = vmem_pud_alloc();
100 if (!pu_dir)
101 goto out;
102 pgd_populate_kernel(&init_mm, pg_dir, pu_dir);
103 }
104
105 pu_dir = pud_offset(pg_dir, address);
106 if (pud_none(*pu_dir)) {
f4eb07c1
HC
107 pm_dir = vmem_pmd_alloc();
108 if (!pm_dir)
109 goto out;
190a1d72 110 pud_populate_kernel(&init_mm, pu_dir, pm_dir);
f4eb07c1
HC
111 }
112
53492b1d 113 pte = mk_pte_phys(address, __pgprot(ro ? _PAGE_RO : 0));
190a1d72 114 pm_dir = pmd_offset(pu_dir, address);
53492b1d
GS
115
116#ifdef __s390x__
117 if (MACHINE_HAS_HPAGE && !(address & ~HPAGE_MASK) &&
118 (address + HPAGE_SIZE <= start + size) &&
119 (address >= HPAGE_SIZE)) {
6a985c61
CB
120 pte_val(pte) |= _SEGMENT_ENTRY_LARGE |
121 _SEGMENT_ENTRY_CO;
53492b1d
GS
122 pmd_val(*pm_dir) = pte_val(pte);
123 address += HPAGE_SIZE - PAGE_SIZE;
124 continue;
125 }
126#endif
f4eb07c1
HC
127 if (pmd_none(*pm_dir)) {
128 pt_dir = vmem_pte_alloc();
129 if (!pt_dir)
130 goto out;
131 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
132 }
133
134 pt_dir = pte_offset_kernel(pm_dir, address);
c1821c2e 135 *pt_dir = pte;
f4eb07c1
HC
136 }
137 ret = 0;
138out:
139 flush_tlb_kernel_range(start, start + size);
140 return ret;
141}
142
143/*
144 * Remove a physical memory range from the 1:1 mapping.
145 * Currently only invalidates page table entries.
146 */
147static void vmem_remove_range(unsigned long start, unsigned long size)
148{
149 unsigned long address;
150 pgd_t *pg_dir;
190a1d72 151 pud_t *pu_dir;
f4eb07c1
HC
152 pmd_t *pm_dir;
153 pte_t *pt_dir;
154 pte_t pte;
155
156 pte_val(pte) = _PAGE_TYPE_EMPTY;
157 for (address = start; address < start + size; address += PAGE_SIZE) {
158 pg_dir = pgd_offset_k(address);
190a1d72
MS
159 pu_dir = pud_offset(pg_dir, address);
160 if (pud_none(*pu_dir))
f4eb07c1 161 continue;
190a1d72 162 pm_dir = pmd_offset(pu_dir, address);
f4eb07c1
HC
163 if (pmd_none(*pm_dir))
164 continue;
53492b1d
GS
165
166 if (pmd_huge(*pm_dir)) {
167 pmd_clear_kernel(pm_dir);
168 address += HPAGE_SIZE - PAGE_SIZE;
169 continue;
170 }
171
f4eb07c1 172 pt_dir = pte_offset_kernel(pm_dir, address);
c1821c2e 173 *pt_dir = pte;
f4eb07c1
HC
174 }
175 flush_tlb_kernel_range(start, start + size);
176}
177
178/*
179 * Add a backed mem_map array to the virtual mem_map array.
180 */
17f34580 181int __meminit vmemmap_populate(struct page *start, unsigned long nr, int node)
f4eb07c1
HC
182{
183 unsigned long address, start_addr, end_addr;
f4eb07c1 184 pgd_t *pg_dir;
190a1d72 185 pud_t *pu_dir;
f4eb07c1
HC
186 pmd_t *pm_dir;
187 pte_t *pt_dir;
188 pte_t pte;
189 int ret = -ENOMEM;
190
17f34580
HC
191 start_addr = (unsigned long) start;
192 end_addr = (unsigned long) (start + nr);
f4eb07c1
HC
193
194 for (address = start_addr; address < end_addr; address += PAGE_SIZE) {
195 pg_dir = pgd_offset_k(address);
196 if (pgd_none(*pg_dir)) {
190a1d72
MS
197 pu_dir = vmem_pud_alloc();
198 if (!pu_dir)
199 goto out;
200 pgd_populate_kernel(&init_mm, pg_dir, pu_dir);
201 }
202
203 pu_dir = pud_offset(pg_dir, address);
204 if (pud_none(*pu_dir)) {
f4eb07c1
HC
205 pm_dir = vmem_pmd_alloc();
206 if (!pm_dir)
207 goto out;
190a1d72 208 pud_populate_kernel(&init_mm, pu_dir, pm_dir);
f4eb07c1
HC
209 }
210
190a1d72 211 pm_dir = pmd_offset(pu_dir, address);
f4eb07c1
HC
212 if (pmd_none(*pm_dir)) {
213 pt_dir = vmem_pte_alloc();
214 if (!pt_dir)
215 goto out;
216 pmd_populate_kernel(&init_mm, pm_dir, pt_dir);
217 }
218
219 pt_dir = pte_offset_kernel(pm_dir, address);
220 if (pte_none(*pt_dir)) {
221 unsigned long new_page;
222
67060d9c 223 new_page =__pa(vmem_alloc_pages(0));
f4eb07c1
HC
224 if (!new_page)
225 goto out;
226 pte = pfn_pte(new_page >> PAGE_SHIFT, PAGE_KERNEL);
c1821c2e 227 *pt_dir = pte;
f4eb07c1
HC
228 }
229 }
67060d9c 230 memset(start, 0, nr * sizeof(struct page));
f4eb07c1
HC
231 ret = 0;
232out:
233 flush_tlb_kernel_range(start_addr, end_addr);
234 return ret;
235}
236
f4eb07c1
HC
237/*
238 * Add memory segment to the segment list if it doesn't overlap with
239 * an already present segment.
240 */
241static int insert_memory_segment(struct memory_segment *seg)
242{
243 struct memory_segment *tmp;
244
ee0ddadd 245 if (seg->start + seg->size > VMEM_MAX_PHYS ||
f4eb07c1
HC
246 seg->start + seg->size < seg->start)
247 return -ERANGE;
248
249 list_for_each_entry(tmp, &mem_segs, list) {
250 if (seg->start >= tmp->start + tmp->size)
251 continue;
252 if (seg->start + seg->size <= tmp->start)
253 continue;
254 return -ENOSPC;
255 }
256 list_add(&seg->list, &mem_segs);
257 return 0;
258}
259
260/*
261 * Remove memory segment from the segment list.
262 */
263static void remove_memory_segment(struct memory_segment *seg)
264{
265 list_del(&seg->list);
266}
267
268static void __remove_shared_memory(struct memory_segment *seg)
269{
270 remove_memory_segment(seg);
271 vmem_remove_range(seg->start, seg->size);
272}
273
17f34580 274int vmem_remove_mapping(unsigned long start, unsigned long size)
f4eb07c1
HC
275{
276 struct memory_segment *seg;
277 int ret;
278
279 mutex_lock(&vmem_mutex);
280
281 ret = -ENOENT;
282 list_for_each_entry(seg, &mem_segs, list) {
283 if (seg->start == start && seg->size == size)
284 break;
285 }
286
287 if (seg->start != start || seg->size != size)
288 goto out;
289
290 ret = 0;
291 __remove_shared_memory(seg);
292 kfree(seg);
293out:
294 mutex_unlock(&vmem_mutex);
295 return ret;
296}
297
17f34580 298int vmem_add_mapping(unsigned long start, unsigned long size)
f4eb07c1
HC
299{
300 struct memory_segment *seg;
f4eb07c1
HC
301 int ret;
302
303 mutex_lock(&vmem_mutex);
304 ret = -ENOMEM;
305 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
306 if (!seg)
307 goto out;
308 seg->start = start;
309 seg->size = size;
310
311 ret = insert_memory_segment(seg);
312 if (ret)
313 goto out_free;
314
53492b1d 315 ret = vmem_add_mem(start, size, 0);
f4eb07c1
HC
316 if (ret)
317 goto out_remove;
f4eb07c1
HC
318 goto out;
319
320out_remove:
321 __remove_shared_memory(seg);
322out_free:
323 kfree(seg);
324out:
325 mutex_unlock(&vmem_mutex);
326 return ret;
327}
328
329/*
330 * map whole physical memory to virtual memory (identity mapping)
5fd9c6e2
CB
331 * we reserve enough space in the vmalloc area for vmemmap to hotplug
332 * additional memory segments.
f4eb07c1
HC
333 */
334void __init vmem_map_init(void)
335{
53492b1d
GS
336 unsigned long ro_start, ro_end;
337 unsigned long start, end;
f4eb07c1
HC
338 int i;
339
50aa98ba 340 spin_lock_init(&init_mm.context.list_lock);
146e4b3c
MS
341 INIT_LIST_HEAD(&init_mm.context.crst_list);
342 INIT_LIST_HEAD(&init_mm.context.pgtable_list);
343 init_mm.context.noexec = 0;
53492b1d
GS
344 ro_start = ((unsigned long)&_stext) & PAGE_MASK;
345 ro_end = PFN_ALIGN((unsigned long)&_eshared);
346 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
347 start = memory_chunk[i].addr;
348 end = memory_chunk[i].addr + memory_chunk[i].size;
349 if (start >= ro_end || end <= ro_start)
350 vmem_add_mem(start, end - start, 0);
351 else if (start >= ro_start && end <= ro_end)
352 vmem_add_mem(start, end - start, 1);
353 else if (start >= ro_start) {
354 vmem_add_mem(start, ro_end - start, 1);
355 vmem_add_mem(ro_end, end - ro_end, 0);
356 } else if (end < ro_end) {
357 vmem_add_mem(start, ro_start - start, 0);
358 vmem_add_mem(ro_start, end - ro_start, 1);
359 } else {
360 vmem_add_mem(start, ro_start - start, 0);
361 vmem_add_mem(ro_start, ro_end - ro_start, 1);
362 vmem_add_mem(ro_end, end - ro_end, 0);
363 }
364 }
f4eb07c1
HC
365}
366
367/*
368 * Convert memory chunk array to a memory segment list so there is a single
369 * list that contains both r/w memory and shared memory segments.
370 */
371static int __init vmem_convert_memory_chunk(void)
372{
373 struct memory_segment *seg;
374 int i;
375
376 mutex_lock(&vmem_mutex);
9f4b0ba8 377 for (i = 0; i < MEMORY_CHUNKS; i++) {
f4eb07c1
HC
378 if (!memory_chunk[i].size)
379 continue;
380 seg = kzalloc(sizeof(*seg), GFP_KERNEL);
381 if (!seg)
382 panic("Out of memory...\n");
383 seg->start = memory_chunk[i].addr;
384 seg->size = memory_chunk[i].size;
385 insert_memory_segment(seg);
386 }
387 mutex_unlock(&vmem_mutex);
388 return 0;
389}
390
391core_initcall(vmem_convert_memory_chunk);