2 * arch/xtensa/mm/init.c
4 * Derived from MIPS, PPC.
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 * Copyright (C) 2001 - 2005 Tensilica Inc.
12 * Chris Zankel <chris@zankel.net>
13 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
18 #include <linux/config.h>
19 #include <linux/init.h>
20 #include <linux/signal.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/ptrace.h>
27 #include <linux/bootmem.h>
28 #include <linux/swap.h>
30 #include <asm/pgtable.h>
31 #include <asm/bootparam.h>
32 #include <asm/mmu_context.h>
34 #include <asm/tlbflush.h>
36 #include <asm/pgalloc.h>
37 #include <asm/pgtable.h>
42 DEFINE_PER_CPU(struct mmu_gather
, mmu_gathers
);
43 //static DEFINE_SPINLOCK(tlb_lock);
46 * This flag is used to indicate that the page was mapped and modified in
47 * kernel space, so the cache is probably dirty at that address.
48 * If cache aliasing is enabled and the page color mismatches, update_mmu_cache
49 * synchronizes the caches if this bit is set.
52 #define PG_cache_clean PG_arch_1
54 /* References to section boundaries */
56 extern char _ftext
, _etext
, _fdata
, _edata
, _rodata_end
;
57 extern char __init_begin
, __init_end
;
60 * mem_reserve(start, end, must_exist)
62 * Reserve some memory from the memory pool.
65 * start Start of region,
67 * must_exist Must exist in memory pool.
70 * 0 (memory area couldn't be mapped)
74 int __init
mem_reserve(unsigned long start
, unsigned long end
, int must_exist
)
81 start
= start
& PAGE_MASK
;
82 end
= PAGE_ALIGN(end
);
84 for (i
= 0; i
< sysmem
.nr_banks
; i
++)
85 if (start
< sysmem
.bank
[i
].end
86 && end
>= sysmem
.bank
[i
].start
)
89 if (i
== sysmem
.nr_banks
) {
91 printk (KERN_WARNING
"mem_reserve: [0x%0lx, 0x%0lx) "
92 "not in any region!\n", start
, end
);
96 if (start
> sysmem
.bank
[i
].start
) {
97 if (end
< sysmem
.bank
[i
].end
) {
99 if (sysmem
.nr_banks
>= SYSMEM_BANKS_MAX
)
100 panic("meminfo overflow\n");
101 sysmem
.bank
[sysmem
.nr_banks
].start
= end
;
102 sysmem
.bank
[sysmem
.nr_banks
].end
= sysmem
.bank
[i
].end
;
105 sysmem
.bank
[i
].end
= start
;
107 if (end
< sysmem
.bank
[i
].end
)
108 sysmem
.bank
[i
].start
= end
;
112 sysmem
.bank
[i
].start
= sysmem
.bank
[sysmem
.nr_banks
].start
;
113 sysmem
.bank
[i
].end
= sysmem
.bank
[sysmem
.nr_banks
].end
;
121 * Initialize the bootmem system and give it all the memory we have available.
124 void __init
bootmem_init(void)
127 unsigned long bootmap_start
, bootmap_size
;
130 max_low_pfn
= max_pfn
= 0;
133 for (i
=0; i
< sysmem
.nr_banks
; i
++) {
134 pfn
= PAGE_ALIGN(sysmem
.bank
[i
].start
) >> PAGE_SHIFT
;
135 if (pfn
< min_low_pfn
)
137 pfn
= PAGE_ALIGN(sysmem
.bank
[i
].end
- 1) >> PAGE_SHIFT
;
142 if (min_low_pfn
> max_pfn
)
143 panic("No memory found!\n");
145 max_low_pfn
= max_pfn
< MAX_LOW_MEMORY
>> PAGE_SHIFT
?
146 max_pfn
: MAX_LOW_MEMORY
>> PAGE_SHIFT
;
148 /* Find an area to use for the bootmem bitmap. */
150 bootmap_size
= bootmem_bootmap_pages(max_low_pfn
) << PAGE_SHIFT
;
153 for (i
=0; i
<sysmem
.nr_banks
; i
++)
154 if (sysmem
.bank
[i
].end
- sysmem
.bank
[i
].start
>= bootmap_size
) {
155 bootmap_start
= sysmem
.bank
[i
].start
;
159 if (bootmap_start
== ~0UL)
160 panic("Cannot find %ld bytes for bootmap\n", bootmap_size
);
162 /* Reserve the bootmem bitmap area */
164 mem_reserve(bootmap_start
, bootmap_start
+ bootmap_size
, 1);
165 bootmap_size
= init_bootmem_node(NODE_DATA(0), min_low_pfn
,
166 bootmap_start
>> PAGE_SHIFT
,
169 /* Add all remaining memory pieces into the bootmem map */
171 for (i
=0; i
<sysmem
.nr_banks
; i
++)
172 free_bootmem(sysmem
.bank
[i
].start
,
173 sysmem
.bank
[i
].end
- sysmem
.bank
[i
].start
);
178 void __init
paging_init(void)
180 unsigned long zones_size
[MAX_NR_ZONES
];
183 /* All pages are DMA-able, so we put them all in the DMA zone. */
185 zones_size
[ZONE_DMA
] = max_low_pfn
;
186 for (i
= 1; i
< MAX_NR_ZONES
; i
++)
189 #ifdef CONFIG_HIGHMEM
190 zones_size
[ZONE_HIGHMEM
] = max_pfn
- max_low_pfn
;
193 /* Initialize the kernel's page tables. */
195 memset(swapper_pg_dir
, 0, PAGE_SIZE
);
197 free_area_init(zones_size
);
201 * Flush the mmu and reset associated register to default values.
204 void __init
init_mmu (void)
206 /* Writing zeros to the <t>TLBCFG special registers ensure
207 * that valid values exist in the register. For existing
208 * PGSZID<w> fields, zero selects the first element of the
209 * page-size array. For nonexistant PGSZID<w> fields, zero is
210 * the best value to write. Also, when changing PGSZID<w>
211 * fields, the corresponding TLB must be flushed.
213 set_itlbcfg_register (0);
214 set_dtlbcfg_register (0);
217 /* Set rasid register to a known value. */
219 set_rasid_register (ASID_ALL_RESERVED
);
221 /* Set PTEVADDR special register to the start of the page
222 * table, which is in kernel mappable space (ie. not
223 * statically mapped). This register's value is undefined on
226 set_ptevaddr_register (PGTABLE_START
);
230 * Initialize memory pages.
233 void __init
mem_init(void)
235 unsigned long codesize
, reservedpages
, datasize
, initsize
;
236 unsigned long highmemsize
, tmp
, ram
;
238 max_mapnr
= num_physpages
= max_low_pfn
;
239 high_memory
= (void *) __va(max_mapnr
<< PAGE_SHIFT
);
242 #ifdef CONFIG_HIGHMEM
243 #error HIGHGMEM not implemented in init.c
246 totalram_pages
+= free_all_bootmem();
248 reservedpages
= ram
= 0;
249 for (tmp
= 0; tmp
< max_low_pfn
; tmp
++) {
251 if (PageReserved(mem_map
+tmp
))
255 codesize
= (unsigned long) &_etext
- (unsigned long) &_ftext
;
256 datasize
= (unsigned long) &_edata
- (unsigned long) &_fdata
;
257 initsize
= (unsigned long) &__init_end
- (unsigned long) &__init_begin
;
259 printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, "
260 "%ldk data, %ldk init %ldk highmem)\n",
261 (unsigned long) nr_free_pages() << (PAGE_SHIFT
-10),
262 ram
<< (PAGE_SHIFT
-10),
264 reservedpages
<< (PAGE_SHIFT
-10),
271 free_reserved_mem(void *start
, void *end
)
273 for (; start
< end
; start
+= PAGE_SIZE
) {
274 ClearPageReserved(virt_to_page(start
));
275 init_page_count(virt_to_page(start
));
276 free_page((unsigned long)start
);
281 #ifdef CONFIG_BLK_DEV_INITRD
282 extern int initrd_is_mapped
;
284 void free_initrd_mem(unsigned long start
, unsigned long end
)
286 if (initrd_is_mapped
) {
287 free_reserved_mem((void*)start
, (void*)end
);
288 printk ("Freeing initrd memory: %ldk freed\n",(end
-start
)>>10);
293 void free_initmem(void)
295 free_reserved_mem(&__init_begin
, &__init_end
);
296 printk("Freeing unused kernel memory: %dk freed\n",
297 (&__init_end
- &__init_begin
) >> 10);
302 int i
, free
= 0, total
= 0, reserved
= 0;
303 int shared
= 0, cached
= 0;
305 printk("Mem-info:\n");
307 printk("Free swap: %6ldkB\n", nr_swap_pages
<<(PAGE_SHIFT
-10));
311 if (PageReserved(mem_map
+i
))
313 else if (PageSwapCache(mem_map
+i
))
315 else if (!page_count(mem_map
+ i
))
318 shared
+= page_count(mem_map
+ i
) - 1;
320 printk("%d pages of RAM\n", total
);
321 printk("%d reserved pages\n", reserved
);
322 printk("%d pages shared\n", shared
);
323 printk("%d pages swap cached\n",cached
);
324 printk("%d free pages\n", free
);
327 /* ------------------------------------------------------------------------- */
329 #if (DCACHE_WAY_SIZE > PAGE_SIZE)
332 * With cache aliasing, the page color of the page in kernel space and user
333 * space might mismatch. We temporarily map the page to a different virtual
334 * address with the same color and clear the page there.
337 void clear_user_page(void *kaddr
, unsigned long vaddr
, struct page
* page
)
340 /* There shouldn't be any entries for this page. */
342 __flush_invalidate_dcache_page_phys(__pa(page_address(page
)));
344 if (!PAGE_COLOR_EQ(vaddr
, kaddr
)) {
347 /* Temporarily map page to DTLB_WAY_DCACHE_ALIAS0. */
349 spin_lock(&tlb_lock
);
351 p
= (unsigned long)pte_val((mk_pte(page
,PAGE_KERNEL
)));
352 kaddr
= (void*)PAGE_COLOR_MAP0(vaddr
);
353 v
= (unsigned long)kaddr
| DTLB_WAY_DCACHE_ALIAS0
;
354 __asm__
__volatile__("wdtlb %0,%1; dsync" : :"a" (p
), "a" (v
));
358 spin_unlock(&tlb_lock
);
363 /* We need to make sure that i$ and d$ are coherent. */
365 clear_bit(PG_cache_clean
, &page
->flags
);
369 * With cache aliasing, we have to make sure that the page color of the page
370 * in kernel space matches that of the virtual user address before we read
371 * the page. If the page color differ, we create a temporary DTLB entry with
372 * the corrent page color and use this 'temporary' address as the source.
373 * We then use the same approach as in clear_user_page and copy the data
374 * to the kernel space and clear the PG_cache_clean bit to synchronize caches
378 * Instead of using another 'way' for the temporary DTLB entry, we could
379 * probably use the same entry that points to the kernel address (after
380 * saving the original value and restoring it when we are done).
383 void copy_user_page(void* to
, void* from
, unsigned long vaddr
,
384 struct page
* to_page
)
386 /* There shouldn't be any entries for the new page. */
388 __flush_invalidate_dcache_page_phys(__pa(page_address(to_page
)));
390 spin_lock(&tlb_lock
);
392 if (!PAGE_COLOR_EQ(vaddr
, from
)) {
393 unsigned long v
, p
, t
;
395 __asm__
__volatile__ ("pdtlb %1,%2; rdtlb1 %0,%1"
396 : "=a"(p
), "=a"(t
) : "a"(from
));
397 from
= (void*)PAGE_COLOR_MAP0(vaddr
);
398 v
= (unsigned long)from
| DTLB_WAY_DCACHE_ALIAS0
;
399 __asm__
__volatile__ ("wdtlb %0,%1; dsync" ::"a" (p
), "a" (v
));
402 if (!PAGE_COLOR_EQ(vaddr
, to
)) {
405 p
= (unsigned long)pte_val((mk_pte(to_page
,PAGE_KERNEL
)));
406 to
= (void*)PAGE_COLOR_MAP1(vaddr
);
407 v
= (unsigned long)to
| DTLB_WAY_DCACHE_ALIAS1
;
408 __asm__
__volatile__ ("wdtlb %0,%1; dsync" ::"a" (p
), "a" (v
));
412 spin_unlock(&tlb_lock
);
414 /* We need to make sure that i$ and d$ are coherent. */
416 clear_bit(PG_cache_clean
, &to_page
->flags
);
422 * Any time the kernel writes to a user page cache page, or it is about to
423 * read from a page cache page this routine is called.
426 * The kernel currently only provides one architecture bit in the page
427 * flags that we use for I$/D$ coherency. Maybe, in future, we can
428 * use a sepearte bit for deferred dcache aliasing:
429 * If the page is not mapped yet, we only need to set a flag,
430 * if mapped, we need to invalidate the page.
432 // FIXME: we probably need this for WB caches not only for Page Coloring..
434 void flush_dcache_page(struct page
*page
)
436 unsigned long addr
= __pa(page_address(page
));
437 struct address_space
*mapping
= page_mapping(page
);
439 __flush_invalidate_dcache_page_phys(addr
);
441 if (!test_bit(PG_cache_clean
, &page
->flags
))
444 /* If this page hasn't been mapped, yet, handle I$/D$ coherency later.*/
446 if (mapping
&& !mapping_mapped(mapping
))
447 clear_bit(PG_cache_clean
, &page
->flags
);
450 __invalidate_icache_page_phys(addr
);
453 void flush_cache_range(struct vm_area_struct
* vma
, unsigned long s
,
456 __flush_invalidate_cache_all();
459 void flush_cache_page(struct vm_area_struct
* vma
, unsigned long address
,
462 struct page
*page
= pfn_to_page(pfn
);
464 /* Remove any entry for the old mapping. */
466 if (current
->active_mm
== vma
->vm_mm
) {
467 unsigned long addr
= __pa(page_address(page
));
468 __flush_invalidate_dcache_page_phys(addr
);
469 if ((vma
->vm_flags
& VM_EXEC
) != 0)
470 __invalidate_icache_page_phys(addr
);
476 #endif /* (DCACHE_WAY_SIZE > PAGE_SIZE) */
479 pte_t
* pte_alloc_one_kernel (struct mm_struct
* mm
, unsigned long addr
)
481 pte_t
* pte
= (pte_t
*)__get_free_pages(GFP_KERNEL
|__GFP_REPEAT
, 0);
483 pte_t
* ptep
= (pte_t
*)(pte_val(*pte
) + PAGE_OFFSET
);
485 for (i
= 0; i
< 1024; i
++, ptep
++)
486 pte_clear(mm
, addr
, ptep
);
491 struct page
* pte_alloc_one(struct mm_struct
*mm
, unsigned long addr
)
495 page
= alloc_pages(GFP_KERNEL
| __GFP_REPEAT
, 0);
498 pte_t
* ptep
= kmap_atomic(page
, KM_USER0
);
501 for (i
= 0; i
< 1024; i
++, ptep
++)
502 pte_clear(mm
, addr
, ptep
);
504 kunmap_atomic(ptep
, KM_USER0
);
511 * Handle D$/I$ coherency.
514 * We only have one architecture bit for the page flags, so we cannot handle
515 * cache aliasing, yet.
519 update_mmu_cache(struct vm_area_struct
* vma
, unsigned long addr
, pte_t pte
)
521 unsigned long pfn
= pte_pfn(pte
);
523 unsigned long vaddr
= addr
& PAGE_MASK
;
528 page
= pfn_to_page(pfn
);
530 invalidate_itlb_mapping(addr
);
531 invalidate_dtlb_mapping(addr
);
533 /* We have a new mapping. Use it. */
535 write_dtlb_entry(pte
, dtlb_probe(addr
));
537 /* If the processor can execute from this page, synchronize D$/I$. */
539 if ((vma
->vm_flags
& VM_EXEC
) != 0) {
541 write_itlb_entry(pte
, itlb_probe(addr
));
543 /* Synchronize caches, if not clean. */
545 if (!test_and_set_bit(PG_cache_clean
, &page
->flags
)) {
546 __flush_dcache_page(vaddr
);
547 __invalidate_icache_page(vaddr
);