Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * PowerPC version | |
3 | * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) | |
4 | * | |
5 | * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au) | |
6 | * and Cort Dougan (PReP) (cort@cs.nmt.edu) | |
7 | * Copyright (C) 1996 Paul Mackerras | |
8 | * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk). | |
9 | * | |
10 | * Derived from "arch/i386/mm/init.c" | |
11 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds | |
12 | * | |
13 | * Dave Engebretsen <engebret@us.ibm.com> | |
14 | * Rework for PPC64 port. | |
15 | * | |
16 | * This program is free software; you can redistribute it and/or | |
17 | * modify it under the terms of the GNU General Public License | |
18 | * as published by the Free Software Foundation; either version | |
19 | * 2 of the License, or (at your option) any later version. | |
20 | * | |
21 | */ | |
22 | ||
23 | #include <linux/config.h> | |
24 | #include <linux/signal.h> | |
25 | #include <linux/sched.h> | |
26 | #include <linux/kernel.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/string.h> | |
29 | #include <linux/types.h> | |
30 | #include <linux/mman.h> | |
31 | #include <linux/mm.h> | |
32 | #include <linux/swap.h> | |
33 | #include <linux/stddef.h> | |
34 | #include <linux/vmalloc.h> | |
35 | #include <linux/init.h> | |
36 | #include <linux/delay.h> | |
37 | #include <linux/bootmem.h> | |
38 | #include <linux/highmem.h> | |
39 | #include <linux/idr.h> | |
40 | #include <linux/nodemask.h> | |
41 | #include <linux/module.h> | |
42 | ||
43 | #include <asm/pgalloc.h> | |
44 | #include <asm/page.h> | |
45 | #include <asm/abs_addr.h> | |
46 | #include <asm/prom.h> | |
47 | #include <asm/lmb.h> | |
48 | #include <asm/rtas.h> | |
49 | #include <asm/io.h> | |
50 | #include <asm/mmu_context.h> | |
51 | #include <asm/pgtable.h> | |
52 | #include <asm/mmu.h> | |
53 | #include <asm/uaccess.h> | |
54 | #include <asm/smp.h> | |
55 | #include <asm/machdep.h> | |
56 | #include <asm/tlb.h> | |
57 | #include <asm/eeh.h> | |
58 | #include <asm/processor.h> | |
59 | #include <asm/mmzone.h> | |
60 | #include <asm/cputable.h> | |
61 | #include <asm/ppcdebug.h> | |
62 | #include <asm/sections.h> | |
63 | #include <asm/system.h> | |
64 | #include <asm/iommu.h> | |
65 | #include <asm/abs_addr.h> | |
66 | #include <asm/vdso.h> | |
1f8d419e | 67 | #include <asm/imalloc.h> |
1da177e4 | 68 | |
e28f7faf DG |
69 | #if PGTABLE_RANGE > USER_VSID_RANGE |
70 | #warning Limited user VSID range means pagetable space is wasted | |
71 | #endif | |
72 | ||
73 | #if (TASK_SIZE_USER64 < PGTABLE_RANGE) && (TASK_SIZE_USER64 < USER_VSID_RANGE) | |
74 | #warning TASK_SIZE is smaller than it needs to be. | |
75 | #endif | |
76 | ||
1da177e4 LT |
77 | int mem_init_done; |
78 | unsigned long ioremap_bot = IMALLOC_BASE; | |
79 | static unsigned long phbs_io_bot = PHBS_IO_BASE; | |
80 | ||
81 | extern pgd_t swapper_pg_dir[]; | |
82 | extern struct task_struct *current_set[NR_CPUS]; | |
83 | ||
1da177e4 LT |
84 | unsigned long klimit = (unsigned long)_end; |
85 | ||
86 | unsigned long _SDR1=0; | |
87 | unsigned long _ASR=0; | |
88 | ||
89 | /* max amount of RAM to use */ | |
90 | unsigned long __max_memory; | |
91 | ||
92 | /* info on what we think the IO hole is */ | |
93 | unsigned long io_hole_start; | |
94 | unsigned long io_hole_size; | |
95 | ||
96 | void show_mem(void) | |
97 | { | |
98 | unsigned long total = 0, reserved = 0; | |
99 | unsigned long shared = 0, cached = 0; | |
100 | struct page *page; | |
101 | pg_data_t *pgdat; | |
102 | unsigned long i; | |
103 | ||
104 | printk("Mem-info:\n"); | |
105 | show_free_areas(); | |
106 | printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10)); | |
107 | for_each_pgdat(pgdat) { | |
108 | for (i = 0; i < pgdat->node_spanned_pages; i++) { | |
408fde81 | 109 | page = pgdat_page_nr(pgdat, i); |
1da177e4 LT |
110 | total++; |
111 | if (PageReserved(page)) | |
112 | reserved++; | |
113 | else if (PageSwapCache(page)) | |
114 | cached++; | |
115 | else if (page_count(page)) | |
116 | shared += page_count(page) - 1; | |
117 | } | |
118 | } | |
119 | printk("%ld pages of RAM\n", total); | |
120 | printk("%ld reserved pages\n", reserved); | |
121 | printk("%ld pages shared\n", shared); | |
122 | printk("%ld pages swap cached\n", cached); | |
123 | } | |
124 | ||
125 | #ifdef CONFIG_PPC_ISERIES | |
126 | ||
127 | void __iomem *ioremap(unsigned long addr, unsigned long size) | |
128 | { | |
129 | return (void __iomem *)addr; | |
130 | } | |
131 | ||
132 | extern void __iomem *__ioremap(unsigned long addr, unsigned long size, | |
133 | unsigned long flags) | |
134 | { | |
135 | return (void __iomem *)addr; | |
136 | } | |
137 | ||
138 | void iounmap(volatile void __iomem *addr) | |
139 | { | |
140 | return; | |
141 | } | |
142 | ||
143 | #else | |
144 | ||
145 | /* | |
146 | * map_io_page currently only called by __ioremap | |
147 | * map_io_page adds an entry to the ioremap page table | |
148 | * and adds an entry to the HPT, possibly bolting it | |
149 | */ | |
58366af5 | 150 | static int map_io_page(unsigned long ea, unsigned long pa, int flags) |
1da177e4 LT |
151 | { |
152 | pgd_t *pgdp; | |
58366af5 | 153 | pud_t *pudp; |
1da177e4 LT |
154 | pmd_t *pmdp; |
155 | pte_t *ptep; | |
156 | unsigned long vsid; | |
157 | ||
158 | if (mem_init_done) { | |
20cee16c DG |
159 | spin_lock(&init_mm.page_table_lock); |
160 | pgdp = pgd_offset_k(ea); | |
161 | pudp = pud_alloc(&init_mm, pgdp, ea); | |
58366af5 BH |
162 | if (!pudp) |
163 | return -ENOMEM; | |
20cee16c | 164 | pmdp = pmd_alloc(&init_mm, pudp, ea); |
58366af5 BH |
165 | if (!pmdp) |
166 | return -ENOMEM; | |
20cee16c | 167 | ptep = pte_alloc_kernel(&init_mm, pmdp, ea); |
58366af5 BH |
168 | if (!ptep) |
169 | return -ENOMEM; | |
1da177e4 | 170 | pa = abs_to_phys(pa); |
20cee16c | 171 | set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT, |
dfbacdc1 | 172 | __pgprot(flags))); |
20cee16c | 173 | spin_unlock(&init_mm.page_table_lock); |
1da177e4 LT |
174 | } else { |
175 | unsigned long va, vpn, hash, hpteg; | |
176 | ||
177 | /* | |
178 | * If the mm subsystem is not fully up, we cannot create a | |
179 | * linux page table entry for this mapping. Simply bolt an | |
180 | * entry in the hardware page table. | |
181 | */ | |
182 | vsid = get_kernel_vsid(ea); | |
183 | va = (vsid << 28) | (ea & 0xFFFFFFF); | |
184 | vpn = va >> PAGE_SHIFT; | |
185 | ||
186 | hash = hpt_hash(vpn, 0); | |
187 | ||
188 | hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP); | |
189 | ||
190 | /* Panic if a pte grpup is full */ | |
96e28449 DG |
191 | if (ppc_md.hpte_insert(hpteg, va, pa >> PAGE_SHIFT, |
192 | HPTE_V_BOLTED, | |
193 | _PAGE_NO_CACHE|_PAGE_GUARDED|PP_RWXX) | |
194 | == -1) { | |
1da177e4 LT |
195 | panic("map_io_page: could not insert mapping"); |
196 | } | |
197 | } | |
58366af5 | 198 | return 0; |
1da177e4 LT |
199 | } |
200 | ||
201 | ||
202 | static void __iomem * __ioremap_com(unsigned long addr, unsigned long pa, | |
203 | unsigned long ea, unsigned long size, | |
204 | unsigned long flags) | |
205 | { | |
206 | unsigned long i; | |
207 | ||
208 | if ((flags & _PAGE_PRESENT) == 0) | |
209 | flags |= pgprot_val(PAGE_KERNEL); | |
1da177e4 | 210 | |
dfbacdc1 | 211 | for (i = 0; i < size; i += PAGE_SIZE) |
58366af5 | 212 | if (map_io_page(ea+i, pa+i, flags)) |
20cee16c | 213 | return NULL; |
1da177e4 LT |
214 | |
215 | return (void __iomem *) (ea + (addr & ~PAGE_MASK)); | |
216 | } | |
217 | ||
218 | ||
219 | void __iomem * | |
220 | ioremap(unsigned long addr, unsigned long size) | |
221 | { | |
dfbacdc1 | 222 | return __ioremap(addr, size, _PAGE_NO_CACHE | _PAGE_GUARDED); |
1da177e4 LT |
223 | } |
224 | ||
58366af5 BH |
225 | void __iomem * __ioremap(unsigned long addr, unsigned long size, |
226 | unsigned long flags) | |
1da177e4 LT |
227 | { |
228 | unsigned long pa, ea; | |
58366af5 | 229 | void __iomem *ret; |
1da177e4 LT |
230 | |
231 | /* | |
232 | * Choose an address to map it to. | |
233 | * Once the imalloc system is running, we use it. | |
234 | * Before that, we map using addresses going | |
235 | * up from ioremap_bot. imalloc will use | |
236 | * the addresses from ioremap_bot through | |
e28f7faf | 237 | * IMALLOC_END |
1da177e4 LT |
238 | * |
239 | */ | |
240 | pa = addr & PAGE_MASK; | |
241 | size = PAGE_ALIGN(addr + size) - pa; | |
242 | ||
243 | if (size == 0) | |
244 | return NULL; | |
245 | ||
246 | if (mem_init_done) { | |
247 | struct vm_struct *area; | |
248 | area = im_get_free_area(size); | |
249 | if (area == NULL) | |
250 | return NULL; | |
251 | ea = (unsigned long)(area->addr); | |
58366af5 BH |
252 | ret = __ioremap_com(addr, pa, ea, size, flags); |
253 | if (!ret) | |
254 | im_free(area->addr); | |
1da177e4 LT |
255 | } else { |
256 | ea = ioremap_bot; | |
58366af5 BH |
257 | ret = __ioremap_com(addr, pa, ea, size, flags); |
258 | if (ret) | |
259 | ioremap_bot += size; | |
1da177e4 | 260 | } |
58366af5 | 261 | return ret; |
1da177e4 LT |
262 | } |
263 | ||
264 | #define IS_PAGE_ALIGNED(_val) ((_val) == ((_val) & PAGE_MASK)) | |
265 | ||
266 | int __ioremap_explicit(unsigned long pa, unsigned long ea, | |
267 | unsigned long size, unsigned long flags) | |
268 | { | |
269 | struct vm_struct *area; | |
58366af5 | 270 | void __iomem *ret; |
1da177e4 LT |
271 | |
272 | /* For now, require page-aligned values for pa, ea, and size */ | |
273 | if (!IS_PAGE_ALIGNED(pa) || !IS_PAGE_ALIGNED(ea) || | |
274 | !IS_PAGE_ALIGNED(size)) { | |
275 | printk(KERN_ERR "unaligned value in %s\n", __FUNCTION__); | |
276 | return 1; | |
277 | } | |
278 | ||
279 | if (!mem_init_done) { | |
280 | /* Two things to consider in this case: | |
281 | * 1) No records will be kept (imalloc, etc) that the region | |
282 | * has been remapped | |
283 | * 2) It won't be easy to iounmap() the region later (because | |
284 | * of 1) | |
285 | */ | |
286 | ; | |
287 | } else { | |
288 | area = im_get_area(ea, size, | |
289 | IM_REGION_UNUSED|IM_REGION_SUBSET|IM_REGION_EXISTS); | |
290 | if (area == NULL) { | |
291 | /* Expected when PHB-dlpar is in play */ | |
292 | return 1; | |
293 | } | |
294 | if (ea != (unsigned long) area->addr) { | |
dfbacdc1 BH |
295 | printk(KERN_ERR "unexpected addr return from " |
296 | "im_get_area\n"); | |
1da177e4 LT |
297 | return 1; |
298 | } | |
299 | } | |
300 | ||
58366af5 BH |
301 | ret = __ioremap_com(pa, pa, ea, size, flags); |
302 | if (ret == NULL) { | |
303 | printk(KERN_ERR "ioremap_explicit() allocation failure !\n"); | |
304 | return 1; | |
305 | } | |
306 | if (ret != (void *) ea) { | |
1da177e4 LT |
307 | printk(KERN_ERR "__ioremap_com() returned unexpected addr\n"); |
308 | return 1; | |
309 | } | |
310 | ||
311 | return 0; | |
312 | } | |
313 | ||
1da177e4 LT |
314 | /* |
315 | * Unmap an IO region and remove it from imalloc'd list. | |
316 | * Access to IO memory should be serialized by driver. | |
317 | * This code is modeled after vmalloc code - unmap_vm_area() | |
318 | * | |
dfbacdc1 | 319 | * XXX what about calls before mem_init_done (ie python_countermeasures()) |
1da177e4 LT |
320 | */ |
321 | void iounmap(volatile void __iomem *token) | |
322 | { | |
1da177e4 LT |
323 | void *addr; |
324 | ||
58366af5 | 325 | if (!mem_init_done) |
1da177e4 | 326 | return; |
1da177e4 LT |
327 | |
328 | addr = (void *) ((unsigned long __force) token & PAGE_MASK); | |
1da177e4 | 329 | |
20cee16c | 330 | im_free(addr); |
1da177e4 LT |
331 | } |
332 | ||
333 | static int iounmap_subset_regions(unsigned long addr, unsigned long size) | |
334 | { | |
335 | struct vm_struct *area; | |
336 | ||
337 | /* Check whether subsets of this region exist */ | |
338 | area = im_get_area(addr, size, IM_REGION_SUPERSET); | |
339 | if (area == NULL) | |
340 | return 1; | |
341 | ||
342 | while (area) { | |
343 | iounmap((void __iomem *) area->addr); | |
344 | area = im_get_area(addr, size, | |
345 | IM_REGION_SUPERSET); | |
346 | } | |
347 | ||
348 | return 0; | |
349 | } | |
350 | ||
351 | int iounmap_explicit(volatile void __iomem *start, unsigned long size) | |
352 | { | |
353 | struct vm_struct *area; | |
354 | unsigned long addr; | |
355 | int rc; | |
356 | ||
357 | addr = (unsigned long __force) start & PAGE_MASK; | |
358 | ||
359 | /* Verify that the region either exists or is a subset of an existing | |
360 | * region. In the latter case, split the parent region to create | |
361 | * the exact region | |
362 | */ | |
363 | area = im_get_area(addr, size, | |
364 | IM_REGION_EXISTS | IM_REGION_SUBSET); | |
365 | if (area == NULL) { | |
366 | /* Determine whether subset regions exist. If so, unmap */ | |
367 | rc = iounmap_subset_regions(addr, size); | |
368 | if (rc) { | |
369 | printk(KERN_ERR | |
370 | "%s() cannot unmap nonexistent range 0x%lx\n", | |
371 | __FUNCTION__, addr); | |
372 | return 1; | |
373 | } | |
374 | } else { | |
375 | iounmap((void __iomem *) area->addr); | |
376 | } | |
377 | /* | |
378 | * FIXME! This can't be right: | |
379 | iounmap(area->addr); | |
380 | * Maybe it should be "iounmap(area);" | |
381 | */ | |
382 | return 0; | |
383 | } | |
384 | ||
385 | #endif | |
386 | ||
387 | EXPORT_SYMBOL(ioremap); | |
388 | EXPORT_SYMBOL(__ioremap); | |
389 | EXPORT_SYMBOL(iounmap); | |
390 | ||
391 | void free_initmem(void) | |
392 | { | |
393 | unsigned long addr; | |
394 | ||
395 | addr = (unsigned long)__init_begin; | |
396 | for (; addr < (unsigned long)__init_end; addr += PAGE_SIZE) { | |
397 | ClearPageReserved(virt_to_page(addr)); | |
398 | set_page_count(virt_to_page(addr), 1); | |
399 | free_page(addr); | |
400 | totalram_pages++; | |
401 | } | |
402 | printk ("Freeing unused kernel memory: %luk freed\n", | |
403 | ((unsigned long)__init_end - (unsigned long)__init_begin) >> 10); | |
404 | } | |
405 | ||
406 | #ifdef CONFIG_BLK_DEV_INITRD | |
407 | void free_initrd_mem(unsigned long start, unsigned long end) | |
408 | { | |
409 | if (start < end) | |
410 | printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10); | |
411 | for (; start < end; start += PAGE_SIZE) { | |
412 | ClearPageReserved(virt_to_page(start)); | |
413 | set_page_count(virt_to_page(start), 1); | |
414 | free_page(start); | |
415 | totalram_pages++; | |
416 | } | |
417 | } | |
418 | #endif | |
419 | ||
420 | static DEFINE_SPINLOCK(mmu_context_lock); | |
421 | static DEFINE_IDR(mmu_context_idr); | |
422 | ||
423 | int init_new_context(struct task_struct *tsk, struct mm_struct *mm) | |
424 | { | |
425 | int index; | |
426 | int err; | |
427 | ||
1da177e4 LT |
428 | again: |
429 | if (!idr_pre_get(&mmu_context_idr, GFP_KERNEL)) | |
430 | return -ENOMEM; | |
431 | ||
432 | spin_lock(&mmu_context_lock); | |
433 | err = idr_get_new_above(&mmu_context_idr, NULL, 1, &index); | |
434 | spin_unlock(&mmu_context_lock); | |
435 | ||
436 | if (err == -EAGAIN) | |
437 | goto again; | |
438 | else if (err) | |
439 | return err; | |
440 | ||
441 | if (index > MAX_CONTEXT) { | |
442 | idr_remove(&mmu_context_idr, index); | |
443 | return -ENOMEM; | |
444 | } | |
445 | ||
446 | mm->context.id = index; | |
447 | ||
448 | return 0; | |
449 | } | |
450 | ||
451 | void destroy_context(struct mm_struct *mm) | |
452 | { | |
453 | spin_lock(&mmu_context_lock); | |
454 | idr_remove(&mmu_context_idr, mm->context.id); | |
455 | spin_unlock(&mmu_context_lock); | |
456 | ||
457 | mm->context.id = NO_CONTEXT; | |
1da177e4 LT |
458 | } |
459 | ||
460 | /* | |
461 | * Do very early mm setup. | |
462 | */ | |
463 | void __init mm_init_ppc64(void) | |
464 | { | |
465 | #ifndef CONFIG_PPC_ISERIES | |
466 | unsigned long i; | |
467 | #endif | |
468 | ||
469 | ppc64_boot_msg(0x100, "MM Init"); | |
470 | ||
471 | /* This is the story of the IO hole... please, keep seated, | |
472 | * unfortunately, we are out of oxygen masks at the moment. | |
473 | * So we need some rough way to tell where your big IO hole | |
474 | * is. On pmac, it's between 2G and 4G, on POWER3, it's around | |
475 | * that area as well, on POWER4 we don't have one, etc... | |
476 | * We need that as a "hint" when sizing the TCE table on POWER3 | |
477 | * So far, the simplest way that seem work well enough for us it | |
478 | * to just assume that the first discontinuity in our physical | |
479 | * RAM layout is the IO hole. That may not be correct in the future | |
480 | * (and isn't on iSeries but then we don't care ;) | |
481 | */ | |
482 | ||
483 | #ifndef CONFIG_PPC_ISERIES | |
484 | for (i = 1; i < lmb.memory.cnt; i++) { | |
485 | unsigned long base, prevbase, prevsize; | |
486 | ||
487 | prevbase = lmb.memory.region[i-1].physbase; | |
488 | prevsize = lmb.memory.region[i-1].size; | |
489 | base = lmb.memory.region[i].physbase; | |
490 | if (base > (prevbase + prevsize)) { | |
491 | io_hole_start = prevbase + prevsize; | |
492 | io_hole_size = base - (prevbase + prevsize); | |
493 | break; | |
494 | } | |
495 | } | |
496 | #endif /* CONFIG_PPC_ISERIES */ | |
497 | if (io_hole_start) | |
498 | printk("IO Hole assumed to be %lx -> %lx\n", | |
499 | io_hole_start, io_hole_start + io_hole_size - 1); | |
500 | ||
501 | ppc64_boot_msg(0x100, "MM Init Done"); | |
502 | } | |
503 | ||
504 | /* | |
505 | * This is called by /dev/mem to know if a given address has to | |
506 | * be mapped non-cacheable or not | |
507 | */ | |
508 | int page_is_ram(unsigned long pfn) | |
509 | { | |
510 | int i; | |
511 | unsigned long paddr = (pfn << PAGE_SHIFT); | |
512 | ||
513 | for (i=0; i < lmb.memory.cnt; i++) { | |
514 | unsigned long base; | |
515 | ||
516 | #ifdef CONFIG_MSCHUNKS | |
517 | base = lmb.memory.region[i].physbase; | |
518 | #else | |
519 | base = lmb.memory.region[i].base; | |
520 | #endif | |
521 | if ((paddr >= base) && | |
522 | (paddr < (base + lmb.memory.region[i].size))) { | |
523 | return 1; | |
524 | } | |
525 | } | |
526 | ||
527 | return 0; | |
528 | } | |
529 | EXPORT_SYMBOL(page_is_ram); | |
530 | ||
531 | /* | |
532 | * Initialize the bootmem system and give it all the memory we | |
533 | * have available. | |
534 | */ | |
145e6642 | 535 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
1da177e4 LT |
536 | void __init do_init_bootmem(void) |
537 | { | |
538 | unsigned long i; | |
539 | unsigned long start, bootmap_pages; | |
540 | unsigned long total_pages = lmb_end_of_DRAM() >> PAGE_SHIFT; | |
541 | int boot_mapsize; | |
542 | ||
543 | /* | |
544 | * Find an area to use for the bootmem bitmap. Calculate the size of | |
545 | * bitmap required as (Total Memory) / PAGE_SIZE / BITS_PER_BYTE. | |
546 | * Add 1 additional page in case the address isn't page-aligned. | |
547 | */ | |
548 | bootmap_pages = bootmem_bootmap_pages(total_pages); | |
549 | ||
550 | start = abs_to_phys(lmb_alloc(bootmap_pages<<PAGE_SHIFT, PAGE_SIZE)); | |
551 | BUG_ON(!start); | |
552 | ||
553 | boot_mapsize = init_bootmem(start >> PAGE_SHIFT, total_pages); | |
554 | ||
555 | max_pfn = max_low_pfn; | |
556 | ||
145e6642 AW |
557 | /* Add all physical memory to the bootmem map, mark each area |
558 | * present. | |
559 | */ | |
1da177e4 LT |
560 | for (i=0; i < lmb.memory.cnt; i++) { |
561 | unsigned long physbase, size; | |
145e6642 | 562 | unsigned long start_pfn, end_pfn; |
1da177e4 LT |
563 | |
564 | physbase = lmb.memory.region[i].physbase; | |
565 | size = lmb.memory.region[i].size; | |
145e6642 AW |
566 | |
567 | start_pfn = physbase >> PAGE_SHIFT; | |
568 | end_pfn = start_pfn + (size >> PAGE_SHIFT); | |
569 | memory_present(0, start_pfn, end_pfn); | |
570 | ||
1da177e4 LT |
571 | free_bootmem(physbase, size); |
572 | } | |
573 | ||
574 | /* reserve the sections we're already using */ | |
575 | for (i=0; i < lmb.reserved.cnt; i++) { | |
576 | unsigned long physbase = lmb.reserved.region[i].physbase; | |
577 | unsigned long size = lmb.reserved.region[i].size; | |
578 | ||
579 | reserve_bootmem(physbase, size); | |
580 | } | |
581 | } | |
582 | ||
583 | /* | |
584 | * paging_init() sets up the page tables - in fact we've already done this. | |
585 | */ | |
586 | void __init paging_init(void) | |
587 | { | |
588 | unsigned long zones_size[MAX_NR_ZONES]; | |
589 | unsigned long zholes_size[MAX_NR_ZONES]; | |
590 | unsigned long total_ram = lmb_phys_mem_size(); | |
591 | unsigned long top_of_ram = lmb_end_of_DRAM(); | |
592 | ||
593 | printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n", | |
594 | top_of_ram, total_ram); | |
595 | printk(KERN_INFO "Memory hole size: %ldMB\n", | |
596 | (top_of_ram - total_ram) >> 20); | |
597 | /* | |
598 | * All pages are DMA-able so we put them all in the DMA zone. | |
599 | */ | |
600 | memset(zones_size, 0, sizeof(zones_size)); | |
601 | memset(zholes_size, 0, sizeof(zholes_size)); | |
602 | ||
603 | zones_size[ZONE_DMA] = top_of_ram >> PAGE_SHIFT; | |
604 | zholes_size[ZONE_DMA] = (top_of_ram - total_ram) >> PAGE_SHIFT; | |
605 | ||
25128092 | 606 | free_area_init_node(0, NODE_DATA(0), zones_size, |
1da177e4 LT |
607 | __pa(PAGE_OFFSET) >> PAGE_SHIFT, zholes_size); |
608 | } | |
145e6642 | 609 | #endif /* ! CONFIG_NEED_MULTIPLE_NODES */ |
1da177e4 LT |
610 | |
611 | static struct kcore_list kcore_vmem; | |
612 | ||
613 | static int __init setup_kcore(void) | |
614 | { | |
615 | int i; | |
616 | ||
617 | for (i=0; i < lmb.memory.cnt; i++) { | |
618 | unsigned long physbase, size; | |
619 | struct kcore_list *kcore_mem; | |
620 | ||
621 | physbase = lmb.memory.region[i].physbase; | |
622 | size = lmb.memory.region[i].size; | |
623 | ||
624 | /* GFP_ATOMIC to avoid might_sleep warnings during boot */ | |
625 | kcore_mem = kmalloc(sizeof(struct kcore_list), GFP_ATOMIC); | |
626 | if (!kcore_mem) | |
627 | panic("mem_init: kmalloc failed\n"); | |
628 | ||
629 | kclist_add(kcore_mem, __va(physbase), size); | |
630 | } | |
631 | ||
632 | kclist_add(&kcore_vmem, (void *)VMALLOC_START, VMALLOC_END-VMALLOC_START); | |
633 | ||
634 | return 0; | |
635 | } | |
636 | module_init(setup_kcore); | |
637 | ||
638 | void __init mem_init(void) | |
639 | { | |
145e6642 | 640 | #ifdef CONFIG_NEED_MULTIPLE_NODES |
1da177e4 LT |
641 | int nid; |
642 | #endif | |
643 | pg_data_t *pgdat; | |
644 | unsigned long i; | |
645 | struct page *page; | |
646 | unsigned long reservedpages = 0, codesize, initsize, datasize, bsssize; | |
647 | ||
648 | num_physpages = max_low_pfn; /* RAM is assumed contiguous */ | |
649 | high_memory = (void *) __va(max_low_pfn * PAGE_SIZE); | |
650 | ||
145e6642 | 651 | #ifdef CONFIG_NEED_MULTIPLE_NODES |
1da177e4 LT |
652 | for_each_online_node(nid) { |
653 | if (NODE_DATA(nid)->node_spanned_pages != 0) { | |
654 | printk("freeing bootmem node %x\n", nid); | |
655 | totalram_pages += | |
656 | free_all_bootmem_node(NODE_DATA(nid)); | |
657 | } | |
658 | } | |
659 | #else | |
660 | max_mapnr = num_physpages; | |
661 | totalram_pages += free_all_bootmem(); | |
662 | #endif | |
663 | ||
664 | for_each_pgdat(pgdat) { | |
665 | for (i = 0; i < pgdat->node_spanned_pages; i++) { | |
408fde81 | 666 | page = pgdat_page_nr(pgdat, i); |
1da177e4 LT |
667 | if (PageReserved(page)) |
668 | reservedpages++; | |
669 | } | |
670 | } | |
671 | ||
672 | codesize = (unsigned long)&_etext - (unsigned long)&_stext; | |
673 | initsize = (unsigned long)&__init_end - (unsigned long)&__init_begin; | |
674 | datasize = (unsigned long)&_edata - (unsigned long)&__init_end; | |
675 | bsssize = (unsigned long)&__bss_stop - (unsigned long)&__bss_start; | |
676 | ||
677 | printk(KERN_INFO "Memory: %luk/%luk available (%luk kernel code, " | |
678 | "%luk reserved, %luk data, %luk bss, %luk init)\n", | |
679 | (unsigned long)nr_free_pages() << (PAGE_SHIFT-10), | |
680 | num_physpages << (PAGE_SHIFT-10), | |
681 | codesize >> 10, | |
682 | reservedpages << (PAGE_SHIFT-10), | |
683 | datasize >> 10, | |
684 | bsssize >> 10, | |
685 | initsize >> 10); | |
686 | ||
687 | mem_init_done = 1; | |
688 | ||
1da177e4 LT |
689 | /* Initialize the vDSO */ |
690 | vdso_init(); | |
691 | } | |
692 | ||
693 | /* | |
694 | * This is called when a page has been modified by the kernel. | |
695 | * It just marks the page as not i-cache clean. We do the i-cache | |
696 | * flush later when the page is given to a user process, if necessary. | |
697 | */ | |
698 | void flush_dcache_page(struct page *page) | |
699 | { | |
700 | if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) | |
701 | return; | |
702 | /* avoid an atomic op if possible */ | |
703 | if (test_bit(PG_arch_1, &page->flags)) | |
704 | clear_bit(PG_arch_1, &page->flags); | |
705 | } | |
706 | EXPORT_SYMBOL(flush_dcache_page); | |
707 | ||
708 | void clear_user_page(void *page, unsigned long vaddr, struct page *pg) | |
709 | { | |
710 | clear_page(page); | |
711 | ||
712 | if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) | |
713 | return; | |
714 | /* | |
715 | * We shouldnt have to do this, but some versions of glibc | |
716 | * require it (ld.so assumes zero filled pages are icache clean) | |
717 | * - Anton | |
718 | */ | |
719 | ||
720 | /* avoid an atomic op if possible */ | |
721 | if (test_bit(PG_arch_1, &pg->flags)) | |
722 | clear_bit(PG_arch_1, &pg->flags); | |
723 | } | |
724 | EXPORT_SYMBOL(clear_user_page); | |
725 | ||
726 | void copy_user_page(void *vto, void *vfrom, unsigned long vaddr, | |
727 | struct page *pg) | |
728 | { | |
729 | copy_page(vto, vfrom); | |
730 | ||
731 | /* | |
732 | * We should be able to use the following optimisation, however | |
733 | * there are two problems. | |
734 | * Firstly a bug in some versions of binutils meant PLT sections | |
735 | * were not marked executable. | |
736 | * Secondly the first word in the GOT section is blrl, used | |
737 | * to establish the GOT address. Until recently the GOT was | |
738 | * not marked executable. | |
739 | * - Anton | |
740 | */ | |
741 | #if 0 | |
742 | if (!vma->vm_file && ((vma->vm_flags & VM_EXEC) == 0)) | |
743 | return; | |
744 | #endif | |
745 | ||
746 | if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) | |
747 | return; | |
748 | ||
749 | /* avoid an atomic op if possible */ | |
750 | if (test_bit(PG_arch_1, &pg->flags)) | |
751 | clear_bit(PG_arch_1, &pg->flags); | |
752 | } | |
753 | ||
754 | void flush_icache_user_range(struct vm_area_struct *vma, struct page *page, | |
755 | unsigned long addr, int len) | |
756 | { | |
757 | unsigned long maddr; | |
758 | ||
759 | maddr = (unsigned long)page_address(page) + (addr & ~PAGE_MASK); | |
760 | flush_icache_range(maddr, maddr + len); | |
761 | } | |
762 | EXPORT_SYMBOL(flush_icache_user_range); | |
763 | ||
764 | /* | |
765 | * This is called at the end of handling a user page fault, when the | |
766 | * fault has been handled by updating a PTE in the linux page tables. | |
767 | * We use it to preload an HPTE into the hash table corresponding to | |
768 | * the updated linux PTE. | |
769 | * | |
770 | * This must always be called with the mm->page_table_lock held | |
771 | */ | |
772 | void update_mmu_cache(struct vm_area_struct *vma, unsigned long ea, | |
773 | pte_t pte) | |
774 | { | |
775 | unsigned long vsid; | |
776 | void *pgdir; | |
777 | pte_t *ptep; | |
778 | int local = 0; | |
779 | cpumask_t tmp; | |
780 | unsigned long flags; | |
781 | ||
782 | /* handle i-cache coherency */ | |
783 | if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE) && | |
784 | !cpu_has_feature(CPU_FTR_NOEXECUTE)) { | |
785 | unsigned long pfn = pte_pfn(pte); | |
786 | if (pfn_valid(pfn)) { | |
787 | struct page *page = pfn_to_page(pfn); | |
788 | if (!PageReserved(page) | |
789 | && !test_bit(PG_arch_1, &page->flags)) { | |
790 | __flush_dcache_icache(page_address(page)); | |
791 | set_bit(PG_arch_1, &page->flags); | |
792 | } | |
793 | } | |
794 | } | |
795 | ||
796 | /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */ | |
797 | if (!pte_young(pte)) | |
798 | return; | |
799 | ||
800 | pgdir = vma->vm_mm->pgd; | |
801 | if (pgdir == NULL) | |
802 | return; | |
803 | ||
804 | ptep = find_linux_pte(pgdir, ea); | |
805 | if (!ptep) | |
806 | return; | |
807 | ||
808 | vsid = get_vsid(vma->vm_mm->context.id, ea); | |
809 | ||
810 | local_irq_save(flags); | |
811 | tmp = cpumask_of_cpu(smp_processor_id()); | |
812 | if (cpus_equal(vma->vm_mm->cpu_vm_mask, tmp)) | |
813 | local = 1; | |
814 | ||
815 | __hash_page(ea, pte_val(pte) & (_PAGE_USER|_PAGE_RW), vsid, ptep, | |
816 | 0x300, local); | |
817 | local_irq_restore(flags); | |
818 | } | |
819 | ||
820 | void __iomem * reserve_phb_iospace(unsigned long size) | |
821 | { | |
822 | void __iomem *virt_addr; | |
823 | ||
824 | if (phbs_io_bot >= IMALLOC_BASE) | |
825 | panic("reserve_phb_iospace(): phb io space overflow\n"); | |
826 | ||
827 | virt_addr = (void __iomem *) phbs_io_bot; | |
828 | phbs_io_bot += size; | |
829 | ||
830 | return virt_addr; | |
831 | } | |
832 | ||
e28f7faf | 833 | static void zero_ctor(void *addr, kmem_cache_t *cache, unsigned long flags) |
1da177e4 | 834 | { |
e28f7faf | 835 | memset(addr, 0, kmem_cache_size(cache)); |
1da177e4 LT |
836 | } |
837 | ||
e28f7faf DG |
838 | static const int pgtable_cache_size[2] = { |
839 | PTE_TABLE_SIZE, PMD_TABLE_SIZE | |
840 | }; | |
841 | static const char *pgtable_cache_name[ARRAY_SIZE(pgtable_cache_size)] = { | |
842 | "pgd_pte_cache", "pud_pmd_cache", | |
843 | }; | |
844 | ||
845 | kmem_cache_t *pgtable_cache[ARRAY_SIZE(pgtable_cache_size)]; | |
846 | ||
1da177e4 LT |
847 | void pgtable_cache_init(void) |
848 | { | |
e28f7faf DG |
849 | int i; |
850 | ||
851 | BUILD_BUG_ON(PTE_TABLE_SIZE != pgtable_cache_size[PTE_CACHE_NUM]); | |
852 | BUILD_BUG_ON(PMD_TABLE_SIZE != pgtable_cache_size[PMD_CACHE_NUM]); | |
853 | BUILD_BUG_ON(PUD_TABLE_SIZE != pgtable_cache_size[PUD_CACHE_NUM]); | |
854 | BUILD_BUG_ON(PGD_TABLE_SIZE != pgtable_cache_size[PGD_CACHE_NUM]); | |
855 | ||
856 | for (i = 0; i < ARRAY_SIZE(pgtable_cache_size); i++) { | |
857 | int size = pgtable_cache_size[i]; | |
858 | const char *name = pgtable_cache_name[i]; | |
859 | ||
860 | pgtable_cache[i] = kmem_cache_create(name, | |
861 | size, size, | |
862 | SLAB_HWCACHE_ALIGN | |
863 | | SLAB_MUST_HWCACHE_ALIGN, | |
864 | zero_ctor, | |
865 | NULL); | |
866 | if (! pgtable_cache[i]) | |
867 | panic("pgtable_cache_init(): could not create %s!\n", | |
868 | name); | |
869 | } | |
1da177e4 LT |
870 | } |
871 | ||
872 | pgprot_t phys_mem_access_prot(struct file *file, unsigned long addr, | |
873 | unsigned long size, pgprot_t vma_prot) | |
874 | { | |
875 | if (ppc_md.phys_mem_access_prot) | |
876 | return ppc_md.phys_mem_access_prot(file, addr, size, vma_prot); | |
877 | ||
878 | if (!page_is_ram(addr >> PAGE_SHIFT)) | |
879 | vma_prot = __pgprot(pgprot_val(vma_prot) | |
880 | | _PAGE_GUARDED | _PAGE_NO_CACHE); | |
881 | return vma_prot; | |
882 | } | |
883 | EXPORT_SYMBOL(phys_mem_access_prot); |