blob: f0a1fd268b7a3752ffea47fe1696efafefbe10e9 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
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
Paul Mackerras14cf11a2005-09-26 16:04:21 +10008 * 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
Paul Mackerras14cf11a2005-09-26 16:04:21 +100020#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>
Johannes Berg4e8ad3e2007-05-08 19:25:00 +100033#include <linux/suspend.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100034
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>
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100045#include <asm/lmb.h>
46#include <asm/sections.h>
Paul Mackerrasab1f9da2005-10-10 21:58:35 +100047#include <asm/vdso.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048
Paul Mackerras14cf11a2005-09-26 16:04:21 +100049#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
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100056int init_bootmem_done;
57int mem_init_done;
Paul Mackerrascf00a8d2005-10-31 13:07:02 +110058unsigned long memory_limit;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +100059
Paul Mackerras14cf11a2005-09-26 16:04:21 +100060int 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}
Paul Mackerras14cf11a2005-09-26 16:04:21 +100082
Roland Dreier8b150472005-10-28 17:46:18 -070083pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
Paul Mackerras14cf11a2005-09-26 16:04:21 +100084 unsigned long size, pgprot_t vma_prot)
85{
86 if (ppc_md.phys_mem_access_prot)
Roland Dreier8b150472005-10-28 17:46:18 -070087 return ppc_md.phys_mem_access_prot(file, pfn, size, vma_prot);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100088
Roland Dreier8b150472005-10-28 17:46:18 -070089 if (!page_is_ram(pfn))
Paul Mackerras14cf11a2005-09-26 16:04:21 +100090 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
Paul Mackerras23fd0772005-10-31 13:37:12 +110096#ifdef CONFIG_MEMORY_HOTPLUG
97
98void online_page(struct page *page)
99{
100 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800101 init_page_count(page);
Nick Piggin70dc9912006-03-22 00:08:35 -0800102 __free_page(page);
Paul Mackerras23fd0772005-10-31 13:37:12 +1100103 totalram_pages++;
104 num_physpages++;
105}
106
Yasunori Gotobc02af92006-06-27 02:53:30 -0700107#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)
Paul Mackerras23fd0772005-10-31 13:37:12 +1100115{
Mike Kravetz237a09892005-12-05 12:06:42 -0800116 struct pglist_data *pgdata;
Paul Mackerras23fd0772005-10-31 13:37:12 +1100117 struct zone *zone;
118 unsigned long start_pfn = start >> PAGE_SHIFT;
119 unsigned long nr_pages = size >> PAGE_SHIFT;
120
Mike Kravetz237a09892005-12-05 12:06:42 -0800121 pgdata = NODE_DATA(nid);
122
Andrew Morton2d0eee12006-03-21 23:00:05 -0800123 start = (unsigned long)__va(start);
Mike Kravetz54b79242005-11-07 16:25:48 -0800124 create_section_mapping(start, start + size);
125
Paul Mackerras23fd0772005-10-31 13:37:12 +1100126 /* this should work for most non-highmem platforms */
127 zone = pgdata->node_zones;
128
129 return __add_pages(zone, start_pfn, nr_pages);
Paul Mackerras23fd0772005-10-31 13:37:12 +1100130}
131
Badari Pulavartyaa620ab2008-02-05 00:10:16 -0800132#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 */
Paul Mackerras23fd0772005-10-31 13:37:12 +1100148#endif /* CONFIG_MEMORY_HOTPLUG */
149
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000150void 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));
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -0800162 for_each_online_pgdat(pgdat) {
Paul Mackerras23fd0772005-10-31 13:37:12 +1100163 unsigned long flags;
164 pgdat_resize_lock(pgdat, &flags);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000165 for (i = 0; i < pgdat->node_spanned_pages; i++) {
Paul Mackerrasfb6d73d2005-11-16 11:43:26 +1100166 if (!pfn_valid(pgdat->node_start_pfn + i))
167 continue;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000168 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 }
Paul Mackerras23fd0772005-10-31 13:37:12 +1100179 pgdat_resize_unlock(pgdat, &flags);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000180 }
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
190/*
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000191 * 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);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000216
217 boot_mapsize = init_bootmem(start >> PAGE_SHIFT, total_pages);
218
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700219 /* 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
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000227 /* Add all physical memory to the bootmem map, mark each area
228 * present.
229 */
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000230#ifdef CONFIG_HIGHMEM
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700231 free_bootmem_with_active_regions(0, total_lowmem >> PAGE_SHIFT);
Kumar Galaf98eeb42008-01-09 11:27:23 -0600232
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,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800239 lmb_size_bytes(&lmb.reserved, i),
240 BOOTMEM_DEFAULT);
Kumar Galaf98eeb42008-01-09 11:27:23 -0600241 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,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800245 adjusted_size, BOOTMEM_DEFAULT);
Kumar Galaf98eeb42008-01-09 11:27:23 -0600246 }
247 }
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700248#else
249 free_bootmem_with_active_regions(0, max_pfn);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000250
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,
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800254 lmb_size_bytes(&lmb.reserved, i),
255 BOOTMEM_DEFAULT);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000256
Kumar Galaf98eeb42008-01-09 11:27:23 -0600257#endif
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000258 /* XXX need to clip this if using highmem? */
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700259 sparse_memory_present_with_active_regions(0);
260
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000261 init_bootmem_done = 1;
262}
263
Johannes Berg4e8ad3e2007-05-08 19:25:00 +1000264/* 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
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000286/*
287 * paging_init() sets up the page tables - in fact we've already done this.
288 */
289void __init paging_init(void)
290{
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000291 unsigned long total_ram = lmb_phys_mem_size();
292 unsigned long top_of_ram = lmb_end_of_DRAM();
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700293 unsigned long max_zone_pfns[MAX_NR_ZONES];
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000294
295#ifdef CONFIG_HIGHMEM
296 map_page(PKMAP_BASE, 0, 0); /* XXX gross */
Benjamin Herrenschmidt5453e772007-05-22 16:25:51 +1000297 pkmap_page_table = pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k
298 (PKMAP_BASE), PKMAP_BASE), PKMAP_BASE), PKMAP_BASE);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000299 map_page(KMAP_FIX_BEGIN, 0, 0); /* XXX gross */
Benjamin Herrenschmidt5453e772007-05-22 16:25:51 +1000300 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);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000303 kmap_prot = PAGE_KERNEL;
304#endif /* CONFIG_HIGHMEM */
305
Olof Johanssone110b282006-04-12 15:25:01 -0500306 printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000307 top_of_ram, total_ram);
Olof Johanssone110b282006-04-12 15:25:01 -0500308 printk(KERN_DEBUG "Memory hole size: %ldMB\n",
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000309 (top_of_ram - total_ram) >> 20);
Mel Gorman6391af12006-10-11 01:20:39 -0700310 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000311#ifdef CONFIG_HIGHMEM
Mel Gorman6391af12006-10-11 01:20:39 -0700312 max_zone_pfns[ZONE_DMA] = total_lowmem >> PAGE_SHIFT;
313 max_zone_pfns[ZONE_HIGHMEM] = top_of_ram >> PAGE_SHIFT;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000314#else
Mel Gorman6391af12006-10-11 01:20:39 -0700315 max_zone_pfns[ZONE_DMA] = top_of_ram >> PAGE_SHIFT;
Mel Gormanc67c3cb2006-09-27 01:49:49 -0700316#endif
317 free_area_init_nodes(max_zone_pfns);
Johannes Berg4e8ad3e2007-05-08 19:25:00 +1000318
319 mark_nonram_nosave();
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000320}
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
Paul Mackerrasfb6d73d2005-11-16 11:43:26 +1100333 num_physpages = lmb.memory.size >> PAGE_SHIFT;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000334 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) {
Anton Blanchardc258dd42006-03-25 17:27:09 +1100339 printk("freeing bootmem node %d\n", nid);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000340 totalram_pages +=
341 free_all_bootmem_node(NODE_DATA(nid));
342 }
343 }
344#else
Paul Mackerrasfb6d73d2005-11-16 11:43:26 +1100345 max_mapnr = max_pfn;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000346 totalram_pages += free_all_bootmem();
347#endif
KAMEZAWA Hiroyukiec936fc2006-03-27 01:15:59 -0800348 for_each_online_pgdat(pgdat) {
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000349 for (i = 0; i < pgdat->node_spanned_pages; i++) {
Paul Mackerrasfb6d73d2005-11-16 11:43:26 +1100350 if (!pfn_valid(pgdat->node_start_pfn + i))
351 continue;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000352 page = pgdat_page_nr(pgdat, i);
353 if (PageReserved(page))
354 reservedpages++;
355 }
356 }
357
358 codesize = (unsigned long)&_sdata - (unsigned long)&_stext;
Anton Blanchardbcb35572005-11-07 17:43:07 +1100359 datasize = (unsigned long)&_edata - (unsigned long)&_sdata;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000360 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);
Kumar Galaf98eeb42008-01-09 11:27:23 -0600370 if (lmb_is_reserved(pfn << PAGE_SHIFT))
371 continue;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000372 ClearPageReserved(page);
Nick Piggin7835e982006-03-22 00:08:40 -0800373 init_page_count(page);
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000374 __free_page(page);
375 totalhigh_pages++;
Kumar Galaf98eeb42008-01-09 11:27:23 -0600376 reservedpages--;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000377 }
378 totalram_pages += totalhigh_pages;
Olof Johanssone110b282006-04-12 15:25:01 -0500379 printk(KERN_DEBUG "High memory: %luk\n",
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000380 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;
Paul Mackerras7c8c6b92005-10-06 12:23:33 +1000395}
396
397/*
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000398 * 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);
Paul Mackerrasab1f9da2005-10-10 21:58:35 +1000418#elif defined(CONFIG_8xx) || defined(CONFIG_PPC64)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000419 /* 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
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000430 /*
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 */
David Gibson09f5dc42006-02-06 13:24:53 +1100435 flush_dcache_page(pg);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000436}
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
David Gibson09f5dc42006-02-06 13:24:53 +1100459 flush_dcache_page(pg);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000460}
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 *
Hugh Dickins01edcd82005-11-23 13:37:39 -0800479 * This must always be called with the pte lock held.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000480 */
481void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
482 pte_t pte)
483{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100484#ifdef CONFIG_PPC_STD_MMU
485 unsigned long access = 0, trap;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000486#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100487 unsigned long pfn = pte_pfn(pte);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000488
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);
Vitaly Bordugdbbb06b2007-01-24 22:40:57 +0300494#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 */
Benjamin Herrenschmidt0b477592007-11-20 18:32:12 +1100502 _tlbie(address, 0 /* 8xx doesn't care about PID */);
Vitaly Bordugdbbb06b2007-01-24 22:40:57 +0300503#endif
Scott Wood551ed332008-02-06 06:43:26 +1100504 /* 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)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000510 && !test_bit(PG_arch_1, &page->flags)) {
511 if (vma->vm_mm == current->active_mm) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000512 __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;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100523
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)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000532 return;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100533 trap = TRAP(current->thread.regs);
534 if (trap == 0x400)
535 access |= _PAGE_EXEC;
536 else if (trap != 0x300)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000537 return;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100538 hash_preload(vma->vm_mm, address, access, trap);
539#endif /* CONFIG_PPC_STD_MMU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000540}