]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/powerpc/mm/mem.c
[POWERPC] Enable hotplug memory remove for 64-bit powerpc
[net-next-2.6.git] / arch / powerpc / mm / mem.c
CommitLineData
14cf11af
PM
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
14cf11af
PM
8 * PPC44x/36-bit changes by Matt Porter (mporter@mvista.com)
9 *
10 * Derived from "arch/i386/mm/init.c"
11 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 *
18 */
19
14cf11af
PM
20#include <linux/module.h>
21#include <linux/sched.h>
22#include <linux/kernel.h>
23#include <linux/errno.h>
24#include <linux/string.h>
25#include <linux/types.h>
26#include <linux/mm.h>
27#include <linux/stddef.h>
28#include <linux/init.h>
29#include <linux/bootmem.h>
30#include <linux/highmem.h>
31#include <linux/initrd.h>
32#include <linux/pagemap.h>
4e8ad3e8 33#include <linux/suspend.h>
14cf11af
PM
34
35#include <asm/pgalloc.h>
36#include <asm/prom.h>
37#include <asm/io.h>
38#include <asm/mmu_context.h>
39#include <asm/pgtable.h>
40#include <asm/mmu.h>
41#include <asm/smp.h>
42#include <asm/machdep.h>
43#include <asm/btext.h>
44#include <asm/tlb.h>
7c8c6b97
PM
45#include <asm/lmb.h>
46#include <asm/sections.h>
ab1f9dac 47#include <asm/vdso.h>
14cf11af 48
14cf11af
PM
49#include "mmu_decl.h"
50
51#ifndef CPU_FTR_COHERENT_ICACHE
52#define CPU_FTR_COHERENT_ICACHE 0 /* XXX for now */
53#define CPU_FTR_NOEXECUTE 0
54#endif
55
7c8c6b97
PM
56int init_bootmem_done;
57int mem_init_done;
cf00a8d1 58unsigned long memory_limit;
7c8c6b97 59
14cf11af
PM
60int page_is_ram(unsigned long pfn)
61{
62 unsigned long paddr = (pfn << PAGE_SHIFT);
63
64#ifndef CONFIG_PPC64 /* XXX for now */
65 return paddr < __pa(high_memory);
66#else
67 int i;
68 for (i=0; i < lmb.memory.cnt; i++) {
69 unsigned long base;
70
71 base = lmb.memory.region[i].base;
72
73 if ((paddr >= base) &&
74 (paddr < (base + lmb.memory.region[i].size))) {
75 return 1;
76 }
77 }
78
79 return 0;
80#endif
81}
14cf11af 82
8b150478 83pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
14cf11af
PM
84 unsigned long size, pgprot_t vma_prot)
85{
86 if (ppc_md.phys_mem_access_prot)
8b150478 87 return ppc_md.phys_mem_access_prot(file, pfn, size, vma_prot);
14cf11af 88
8b150478 89 if (!page_is_ram(pfn))
14cf11af
PM
90 vma_prot = __pgprot(pgprot_val(vma_prot)
91 | _PAGE_GUARDED | _PAGE_NO_CACHE);
92 return vma_prot;
93}
94EXPORT_SYMBOL(phys_mem_access_prot);
95
23fd0775
PM
96#ifdef CONFIG_MEMORY_HOTPLUG
97
98void online_page(struct page *page)
99{
100 ClearPageReserved(page);
7835e98b 101 init_page_count(page);
70dc991d 102 __free_page(page);
23fd0775
PM
103 totalram_pages++;
104 num_physpages++;
105}
106
bc02af93
YG
107#ifdef CONFIG_NUMA
108int memory_add_physaddr_to_nid(u64 start)
109{
110 return hot_add_scn_to_nid(start);
111}
112#endif
113
114int __devinit arch_add_memory(int nid, u64 start, u64 size)
23fd0775 115{
237a0989 116 struct pglist_data *pgdata;
23fd0775
PM
117 struct zone *zone;
118 unsigned long start_pfn = start >> PAGE_SHIFT;
119 unsigned long nr_pages = size >> PAGE_SHIFT;
120
237a0989
MK
121 pgdata = NODE_DATA(nid);
122
2d0eee14 123 start = (unsigned long)__va(start);
54b79248
MK
124 create_section_mapping(start, start + size);
125
23fd0775
PM
126 /* this should work for most non-highmem platforms */
127 zone = pgdata->node_zones;
128
129 return __add_pages(zone, start_pfn, nr_pages);
23fd0775
PM
130}
131
aa620abe
BP
132#ifdef CONFIG_MEMORY_HOTREMOVE
133int remove_memory(u64 start, u64 size)
134{
135 unsigned long start_pfn, end_pfn;
136 int ret;
137
138 start_pfn = start >> PAGE_SHIFT;
139 end_pfn = start_pfn + (size >> PAGE_SHIFT);
140 ret = offline_pages(start_pfn, end_pfn, 120 * HZ);
141 if (ret)
142 goto out;
143 /* Arch-specific calls go here - next patch */
144out:
145 return ret;
146}
147#endif /* CONFIG_MEMORY_HOTREMOVE */
23fd0775
PM
148#endif /* CONFIG_MEMORY_HOTPLUG */
149
14cf11af
PM
150void show_mem(void)
151{
152 unsigned long total = 0, reserved = 0;
153 unsigned long shared = 0, cached = 0;
154 unsigned long highmem = 0;
155 struct page *page;
156 pg_data_t *pgdat;
157 unsigned long i;
158
159 printk("Mem-info:\n");
160 show_free_areas();
161 printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
ec936fc5 162 for_each_online_pgdat(pgdat) {
23fd0775
PM
163 unsigned long flags;
164 pgdat_resize_lock(pgdat, &flags);
14cf11af 165 for (i = 0; i < pgdat->node_spanned_pages; i++) {
fb6d73d3
PM
166 if (!pfn_valid(pgdat->node_start_pfn + i))
167 continue;
14cf11af
PM
168 page = pgdat_page_nr(pgdat, i);
169 total++;
170 if (PageHighMem(page))
171 highmem++;
172 if (PageReserved(page))
173 reserved++;
174 else if (PageSwapCache(page))
175 cached++;
176 else if (page_count(page))
177 shared += page_count(page) - 1;
178 }
23fd0775 179 pgdat_resize_unlock(pgdat, &flags);
14cf11af
PM
180 }
181 printk("%ld pages of RAM\n", total);
182#ifdef CONFIG_HIGHMEM
183 printk("%ld pages of HIGHMEM\n", highmem);
184#endif
185 printk("%ld reserved pages\n", reserved);
186 printk("%ld pages shared\n", shared);
187 printk("%ld pages swap cached\n", cached);
188}
189
7c8c6b97
PM
190/*
191 * Initialize the bootmem system and give it all the memory we
192 * have available. If we are using highmem, we only put the
193 * lowmem into the bootmem system.
194 */
195#ifndef CONFIG_NEED_MULTIPLE_NODES
196void __init do_init_bootmem(void)
197{
198 unsigned long i;
199 unsigned long start, bootmap_pages;
200 unsigned long total_pages;
201 int boot_mapsize;
202
203 max_pfn = total_pages = lmb_end_of_DRAM() >> PAGE_SHIFT;
204#ifdef CONFIG_HIGHMEM
205 total_pages = total_lowmem >> PAGE_SHIFT;
206#endif
207
208 /*
209 * Find an area to use for the bootmem bitmap. Calculate the size of
210 * bitmap required as (Total Memory) / PAGE_SIZE / BITS_PER_BYTE.
211 * Add 1 additional page in case the address isn't page-aligned.
212 */
213 bootmap_pages = bootmem_bootmap_pages(total_pages);
214
215 start = lmb_alloc(bootmap_pages << PAGE_SHIFT, PAGE_SIZE);
7c8c6b97
PM
216
217 boot_mapsize = init_bootmem(start >> PAGE_SHIFT, total_pages);
218
c67c3cb4
MG
219 /* Add active regions with valid PFNs */
220 for (i = 0; i < lmb.memory.cnt; i++) {
221 unsigned long start_pfn, end_pfn;
222 start_pfn = lmb.memory.region[i].base >> PAGE_SHIFT;
223 end_pfn = start_pfn + lmb_size_pages(&lmb.memory, i);
224 add_active_range(0, start_pfn, end_pfn);
225 }
226
7c8c6b97
PM
227 /* Add all physical memory to the bootmem map, mark each area
228 * present.
229 */
7c8c6b97 230#ifdef CONFIG_HIGHMEM
c67c3cb4 231 free_bootmem_with_active_regions(0, total_lowmem >> PAGE_SHIFT);
f98eeb4e
KG
232
233 /* reserve the sections we're already using */
234 for (i = 0; i < lmb.reserved.cnt; i++) {
235 unsigned long addr = lmb.reserved.region[i].base +
236 lmb_size_bytes(&lmb.reserved, i) - 1;
237 if (addr < total_lowmem)
238 reserve_bootmem(lmb.reserved.region[i].base,
72a7fe39
BW
239 lmb_size_bytes(&lmb.reserved, i),
240 BOOTMEM_DEFAULT);
f98eeb4e
KG
241 else if (lmb.reserved.region[i].base < total_lowmem) {
242 unsigned long adjusted_size = total_lowmem -
243 lmb.reserved.region[i].base;
244 reserve_bootmem(lmb.reserved.region[i].base,
72a7fe39 245 adjusted_size, BOOTMEM_DEFAULT);
f98eeb4e
KG
246 }
247 }
c67c3cb4
MG
248#else
249 free_bootmem_with_active_regions(0, max_pfn);
7c8c6b97
PM
250
251 /* reserve the sections we're already using */
252 for (i = 0; i < lmb.reserved.cnt; i++)
253 reserve_bootmem(lmb.reserved.region[i].base,
72a7fe39
BW
254 lmb_size_bytes(&lmb.reserved, i),
255 BOOTMEM_DEFAULT);
7c8c6b97 256
f98eeb4e 257#endif
7c8c6b97 258 /* XXX need to clip this if using highmem? */
c67c3cb4
MG
259 sparse_memory_present_with_active_regions(0);
260
7c8c6b97
PM
261 init_bootmem_done = 1;
262}
263
4e8ad3e8
JB
264/* mark pages that don't exist as nosave */
265static int __init mark_nonram_nosave(void)
266{
267 unsigned long lmb_next_region_start_pfn,
268 lmb_region_max_pfn;
269 int i;
270
271 for (i = 0; i < lmb.memory.cnt - 1; i++) {
272 lmb_region_max_pfn =
273 (lmb.memory.region[i].base >> PAGE_SHIFT) +
274 (lmb.memory.region[i].size >> PAGE_SHIFT);
275 lmb_next_region_start_pfn =
276 lmb.memory.region[i+1].base >> PAGE_SHIFT;
277
278 if (lmb_region_max_pfn < lmb_next_region_start_pfn)
279 register_nosave_region(lmb_region_max_pfn,
280 lmb_next_region_start_pfn);
281 }
282
283 return 0;
284}
285
7c8c6b97
PM
286/*
287 * paging_init() sets up the page tables - in fact we've already done this.
288 */
289void __init paging_init(void)
290{
7c8c6b97
PM
291 unsigned long total_ram = lmb_phys_mem_size();
292 unsigned long top_of_ram = lmb_end_of_DRAM();
c67c3cb4 293 unsigned long max_zone_pfns[MAX_NR_ZONES];
7c8c6b97
PM
294
295#ifdef CONFIG_HIGHMEM
296 map_page(PKMAP_BASE, 0, 0); /* XXX gross */
5453e772
BH
297 pkmap_page_table = pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k
298 (PKMAP_BASE), PKMAP_BASE), PKMAP_BASE), PKMAP_BASE);
7c8c6b97 299 map_page(KMAP_FIX_BEGIN, 0, 0); /* XXX gross */
5453e772
BH
300 kmap_pte = pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k
301 (KMAP_FIX_BEGIN), KMAP_FIX_BEGIN), KMAP_FIX_BEGIN),
302 KMAP_FIX_BEGIN);
7c8c6b97
PM
303 kmap_prot = PAGE_KERNEL;
304#endif /* CONFIG_HIGHMEM */
305
e110b281 306 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
7c8c6b97 307 top_of_ram, total_ram);
e110b281 308 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
7c8c6b97 309 (top_of_ram - total_ram) >> 20);
6391af17 310 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
7c8c6b97 311#ifdef CONFIG_HIGHMEM
6391af17
MG
312 max_zone_pfns[ZONE_DMA] = total_lowmem >> PAGE_SHIFT;
313 max_zone_pfns[ZONE_HIGHMEM] = top_of_ram >> PAGE_SHIFT;
7c8c6b97 314#else
6391af17 315 max_zone_pfns[ZONE_DMA] = top_of_ram >> PAGE_SHIFT;
c67c3cb4
MG
316#endif
317 free_area_init_nodes(max_zone_pfns);
4e8ad3e8
JB
318
319 mark_nonram_nosave();
7c8c6b97
PM
320}
321#endif /* ! CONFIG_NEED_MULTIPLE_NODES */
322
323void __init mem_init(void)
324{
325#ifdef CONFIG_NEED_MULTIPLE_NODES
326 int nid;
327#endif
328 pg_data_t *pgdat;
329 unsigned long i;
330 struct page *page;
331 unsigned long reservedpages = 0, codesize, initsize, datasize, bsssize;
332
fb6d73d3 333 num_physpages = lmb.memory.size >> PAGE_SHIFT;
7c8c6b97
PM
334 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
335
336#ifdef CONFIG_NEED_MULTIPLE_NODES
337 for_each_online_node(nid) {
338 if (NODE_DATA(nid)->node_spanned_pages != 0) {
c258dd40 339 printk("freeing bootmem node %d\n", nid);
7c8c6b97
PM
340 totalram_pages +=
341 free_all_bootmem_node(NODE_DATA(nid));
342 }
343 }
344#else
fb6d73d3 345 max_mapnr = max_pfn;
7c8c6b97
PM
346 totalram_pages += free_all_bootmem();
347#endif
ec936fc5 348 for_each_online_pgdat(pgdat) {
7c8c6b97 349 for (i = 0; i < pgdat->node_spanned_pages; i++) {
fb6d73d3
PM
350 if (!pfn_valid(pgdat->node_start_pfn + i))
351 continue;
7c8c6b97
PM
352 page = pgdat_page_nr(pgdat, i);
353 if (PageReserved(page))
354 reservedpages++;
355 }
356 }
357
358 codesize = (unsigned long)&_sdata - (unsigned long)&_stext;
bcb35576 359 datasize = (unsigned long)&_edata - (unsigned long)&_sdata;
7c8c6b97
PM
360 initsize = (unsigned long)&__init_end - (unsigned long)&__init_begin;
361 bsssize = (unsigned long)&__bss_stop - (unsigned long)&__bss_start;
362
363#ifdef CONFIG_HIGHMEM
364 {
365 unsigned long pfn, highmem_mapnr;
366
367 highmem_mapnr = total_lowmem >> PAGE_SHIFT;
368 for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
369 struct page *page = pfn_to_page(pfn);
f98eeb4e
KG
370 if (lmb_is_reserved(pfn << PAGE_SHIFT))
371 continue;
7c8c6b97 372 ClearPageReserved(page);
7835e98b 373 init_page_count(page);
7c8c6b97
PM
374 __free_page(page);
375 totalhigh_pages++;
f98eeb4e 376 reservedpages--;
7c8c6b97
PM
377 }
378 totalram_pages += totalhigh_pages;
e110b281 379 printk(KERN_DEBUG "High memory: %luk\n",
7c8c6b97
PM
380 totalhigh_pages << (PAGE_SHIFT-10));
381 }
382#endif /* CONFIG_HIGHMEM */
383
384 printk(KERN_INFO "Memory: %luk/%luk available (%luk kernel code, "
385 "%luk reserved, %luk data, %luk bss, %luk init)\n",
386 (unsigned long)nr_free_pages() << (PAGE_SHIFT-10),
387 num_physpages << (PAGE_SHIFT-10),
388 codesize >> 10,
389 reservedpages << (PAGE_SHIFT-10),
390 datasize >> 10,
391 bsssize >> 10,
392 initsize >> 10);
393
394 mem_init_done = 1;
7c8c6b97
PM
395}
396
14cf11af
PM
397/*
398 * This is called when a page has been modified by the kernel.
399 * It just marks the page as not i-cache clean. We do the i-cache
400 * flush later when the page is given to a user process, if necessary.
401 */
402void flush_dcache_page(struct page *page)
403{
404 if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
405 return;
406 /* avoid an atomic op if possible */
407 if (test_bit(PG_arch_1, &page->flags))
408 clear_bit(PG_arch_1, &page->flags);
409}
410EXPORT_SYMBOL(flush_dcache_page);
411
412void flush_dcache_icache_page(struct page *page)
413{
414#ifdef CONFIG_BOOKE
415 void *start = kmap_atomic(page, KM_PPC_SYNC_ICACHE);
416 __flush_dcache_icache(start);
417 kunmap_atomic(start, KM_PPC_SYNC_ICACHE);
ab1f9dac 418#elif defined(CONFIG_8xx) || defined(CONFIG_PPC64)
14cf11af
PM
419 /* On 8xx there is no need to kmap since highmem is not supported */
420 __flush_dcache_icache(page_address(page));
421#else
422 __flush_dcache_icache_phys(page_to_pfn(page) << PAGE_SHIFT);
423#endif
424
425}
426void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
427{
428 clear_page(page);
429
14cf11af
PM
430 /*
431 * We shouldnt have to do this, but some versions of glibc
432 * require it (ld.so assumes zero filled pages are icache clean)
433 * - Anton
434 */
09f5dc44 435 flush_dcache_page(pg);
14cf11af
PM
436}
437EXPORT_SYMBOL(clear_user_page);
438
439void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
440 struct page *pg)
441{
442 copy_page(vto, vfrom);
443
444 /*
445 * We should be able to use the following optimisation, however
446 * there are two problems.
447 * Firstly a bug in some versions of binutils meant PLT sections
448 * were not marked executable.
449 * Secondly the first word in the GOT section is blrl, used
450 * to establish the GOT address. Until recently the GOT was
451 * not marked executable.
452 * - Anton
453 */
454#if 0
455 if (!vma->vm_file && ((vma->vm_flags & VM_EXEC) == 0))
456 return;
457#endif
458
09f5dc44 459 flush_dcache_page(pg);
14cf11af
PM
460}
461
462void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
463 unsigned long addr, int len)
464{
465 unsigned long maddr;
466
467 maddr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
468 flush_icache_range(maddr, maddr + len);
469 kunmap(page);
470}
471EXPORT_SYMBOL(flush_icache_user_range);
472
473/*
474 * This is called at the end of handling a user page fault, when the
475 * fault has been handled by updating a PTE in the linux page tables.
476 * We use it to preload an HPTE into the hash table corresponding to
477 * the updated linux PTE.
478 *
01edcd89 479 * This must always be called with the pte lock held.
14cf11af
PM
480 */
481void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
482 pte_t pte)
483{
3c726f8d
BH
484#ifdef CONFIG_PPC_STD_MMU
485 unsigned long access = 0, trap;
14cf11af 486#endif
3c726f8d 487 unsigned long pfn = pte_pfn(pte);
14cf11af
PM
488
489 /* handle i-cache coherency */
490 if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE) &&
491 !cpu_has_feature(CPU_FTR_NOEXECUTE) &&
492 pfn_valid(pfn)) {
493 struct page *page = pfn_to_page(pfn);
dbbb06b7
VB
494#ifdef CONFIG_8xx
495 /* On 8xx, cache control instructions (particularly
496 * "dcbst" from flush_dcache_icache) fault as write
497 * operation if there is an unpopulated TLB entry
498 * for the address in question. To workaround that,
499 * we invalidate the TLB here, thus avoiding dcbst
500 * misbehaviour.
501 */
0b47759d 502 _tlbie(address, 0 /* 8xx doesn't care about PID */);
dbbb06b7 503#endif
551ed332
SW
504 /* The _PAGE_USER test should really be _PAGE_EXEC, but
505 * older glibc versions execute some code from no-exec
506 * pages, which for now we are supporting. If exec-only
507 * pages are ever implemented, this will have to change.
508 */
509 if (!PageReserved(page) && (pte_val(pte) & _PAGE_USER)
14cf11af
PM
510 && !test_bit(PG_arch_1, &page->flags)) {
511 if (vma->vm_mm == current->active_mm) {
14cf11af
PM
512 __flush_dcache_icache((void *) address);
513 } else
514 flush_dcache_icache_page(page);
515 set_bit(PG_arch_1, &page->flags);
516 }
517 }
518
519#ifdef CONFIG_PPC_STD_MMU
520 /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
521 if (!pte_young(pte) || address >= TASK_SIZE)
522 return;
14cf11af 523
3c726f8d
BH
524 /* We try to figure out if we are coming from an instruction
525 * access fault and pass that down to __hash_page so we avoid
526 * double-faulting on execution of fresh text. We have to test
527 * for regs NULL since init will get here first thing at boot
528 *
529 * We also avoid filling the hash if not coming from a fault
530 */
531 if (current->thread.regs == NULL)
14cf11af 532 return;
3c726f8d
BH
533 trap = TRAP(current->thread.regs);
534 if (trap == 0x400)
535 access |= _PAGE_EXEC;
536 else if (trap != 0x300)
537 return;
538 hash_preload(vma->vm_mm, address, access, trap);
539#endif /* CONFIG_PPC_STD_MMU */
14cf11af 540}