blob: ec47de494c1fba1791b8c7880214eb6817f73214 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: init.c,v 1.209 2002/02/09 19:49:31 davem Exp $
2 * arch/sparc64/mm/init.c
3 *
4 * Copyright (C) 1996-1999 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 */
7
8#include <linux/config.h>
9#include <linux/kernel.h>
10#include <linux/sched.h>
11#include <linux/string.h>
12#include <linux/init.h>
13#include <linux/bootmem.h>
14#include <linux/mm.h>
15#include <linux/hugetlb.h>
16#include <linux/slab.h>
17#include <linux/initrd.h>
18#include <linux/swap.h>
19#include <linux/pagemap.h>
20#include <linux/fs.h>
21#include <linux/seq_file.h>
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070022#include <linux/kprobes.h>
David S. Miller1ac4f5e2005-09-21 21:49:32 -070023#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/head.h>
26#include <asm/system.h>
27#include <asm/page.h>
28#include <asm/pgalloc.h>
29#include <asm/pgtable.h>
30#include <asm/oplib.h>
31#include <asm/iommu.h>
32#include <asm/io.h>
33#include <asm/uaccess.h>
34#include <asm/mmu_context.h>
35#include <asm/tlbflush.h>
36#include <asm/dma.h>
37#include <asm/starfire.h>
38#include <asm/tlb.h>
39#include <asm/spitfire.h>
40#include <asm/sections.h>
41
42extern void device_scan(void);
43
44struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS];
45
David S. Miller2bdb3cb2005-09-22 01:08:57 -070046unsigned long *sparc64_valid_addr_bitmap __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* Ugly, but necessary... -DaveM */
David S. Miller1ac4f5e2005-09-21 21:49:32 -070049unsigned long phys_base __read_mostly;
50unsigned long kern_base __read_mostly;
51unsigned long kern_size __read_mostly;
52unsigned long pfn_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* get_new_mmu_context() uses "cache + 1". */
55DEFINE_SPINLOCK(ctx_alloc_lock);
56unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
57#define CTX_BMAP_SLOTS (1UL << (CTX_NR_BITS - 6))
58unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
59
60/* References to special section boundaries */
61extern char _start[], _end[];
62
63/* Initial ramdisk setup */
64extern unsigned long sparc_ramdisk_image64;
65extern unsigned int sparc_ramdisk_image;
66extern unsigned int sparc_ramdisk_size;
67
David S. Miller1ac4f5e2005-09-21 21:49:32 -070068struct page *mem_map_zero __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70int bigkernel = 0;
71
72/* XXX Tune this... */
73#define PGT_CACHE_LOW 25
74#define PGT_CACHE_HIGH 50
75
76void check_pgt_cache(void)
77{
78 preempt_disable();
79 if (pgtable_cache_size > PGT_CACHE_HIGH) {
80 do {
81 if (pgd_quicklist)
82 free_pgd_slow(get_pgd_fast());
83 if (pte_quicklist[0])
84 free_pte_slow(pte_alloc_one_fast(NULL, 0));
85 if (pte_quicklist[1])
86 free_pte_slow(pte_alloc_one_fast(NULL, 1 << (PAGE_SHIFT + 10)));
87 } while (pgtable_cache_size > PGT_CACHE_LOW);
88 }
89 preempt_enable();
90}
91
92#ifdef CONFIG_DEBUG_DCFLUSH
93atomic_t dcpage_flushes = ATOMIC_INIT(0);
94#ifdef CONFIG_SMP
95atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
96#endif
97#endif
98
99__inline__ void flush_dcache_page_impl(struct page *page)
100{
101#ifdef CONFIG_DEBUG_DCFLUSH
102 atomic_inc(&dcpage_flushes);
103#endif
104
105#ifdef DCACHE_ALIASING_POSSIBLE
106 __flush_dcache_page(page_address(page),
107 ((tlb_type == spitfire) &&
108 page_mapping(page) != NULL));
109#else
110 if (page_mapping(page) != NULL &&
111 tlb_type == spitfire)
112 __flush_icache_page(__pa(page_address(page)));
113#endif
114}
115
116#define PG_dcache_dirty PG_arch_1
David S. Miller48b0e542005-07-27 16:08:44 -0700117#define PG_dcache_cpu_shift 24
118#define PG_dcache_cpu_mask (256 - 1)
119
120#if NR_CPUS > 256
121#error D-cache dirty tracking and thread_info->cpu need fixing for > 256 cpus
122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124#define dcache_dirty_cpu(page) \
David S. Miller48b0e542005-07-27 16:08:44 -0700125 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127static __inline__ void set_dcache_dirty(struct page *page, int this_cpu)
128{
129 unsigned long mask = this_cpu;
David S. Miller48b0e542005-07-27 16:08:44 -0700130 unsigned long non_cpu_bits;
131
132 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
133 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 __asm__ __volatile__("1:\n\t"
136 "ldx [%2], %%g7\n\t"
137 "and %%g7, %1, %%g1\n\t"
138 "or %%g1, %0, %%g1\n\t"
139 "casx [%2], %%g7, %%g1\n\t"
140 "cmp %%g7, %%g1\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700141 "membar #StoreLoad | #StoreStore\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700143 " nop"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 : /* no outputs */
145 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
146 : "g1", "g7");
147}
148
149static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
150{
151 unsigned long mask = (1UL << PG_dcache_dirty);
152
153 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
154 "1:\n\t"
155 "ldx [%2], %%g7\n\t"
David S. Miller48b0e542005-07-27 16:08:44 -0700156 "srlx %%g7, %4, %%g1\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 "and %%g1, %3, %%g1\n\t"
158 "cmp %%g1, %0\n\t"
159 "bne,pn %%icc, 2f\n\t"
160 " andn %%g7, %1, %%g1\n\t"
161 "casx [%2], %%g7, %%g1\n\t"
162 "cmp %%g7, %%g1\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700163 "membar #StoreLoad | #StoreStore\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700165 " nop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 "2:"
167 : /* no outputs */
168 : "r" (cpu), "r" (mask), "r" (&page->flags),
David S. Miller48b0e542005-07-27 16:08:44 -0700169 "i" (PG_dcache_cpu_mask),
170 "i" (PG_dcache_cpu_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 : "g1", "g7");
172}
173
174extern void __update_mmu_cache(unsigned long mmu_context_hw, unsigned long address, pte_t pte, int code);
175
176void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
177{
178 struct page *page;
179 unsigned long pfn;
180 unsigned long pg_flags;
181
182 pfn = pte_pfn(pte);
183 if (pfn_valid(pfn) &&
184 (page = pfn_to_page(pfn), page_mapping(page)) &&
185 ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
David S. Miller48b0e542005-07-27 16:08:44 -0700186 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
187 PG_dcache_cpu_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int this_cpu = get_cpu();
189
190 /* This is just to optimize away some function calls
191 * in the SMP case.
192 */
193 if (cpu == this_cpu)
194 flush_dcache_page_impl(page);
195 else
196 smp_flush_dcache_page_impl(page, cpu);
197
198 clear_dcache_dirty_cpu(page, cpu);
199
200 put_cpu();
201 }
202
203 if (get_thread_fault_code())
204 __update_mmu_cache(CTX_NRBITS(vma->vm_mm->context),
205 address, pte, get_thread_fault_code());
206}
207
208void flush_dcache_page(struct page *page)
209{
David S. Millera9546f52005-04-17 18:03:09 -0700210 struct address_space *mapping;
211 int this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
David S. Millera9546f52005-04-17 18:03:09 -0700213 /* Do not bother with the expensive D-cache flush if it
214 * is merely the zero page. The 'bigcore' testcase in GDB
215 * causes this case to run millions of times.
216 */
217 if (page == ZERO_PAGE(0))
218 return;
219
220 this_cpu = get_cpu();
221
222 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (mapping && !mapping_mapped(mapping)) {
David S. Millera9546f52005-04-17 18:03:09 -0700224 int dirty = test_bit(PG_dcache_dirty, &page->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (dirty) {
David S. Millera9546f52005-04-17 18:03:09 -0700226 int dirty_cpu = dcache_dirty_cpu(page);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (dirty_cpu == this_cpu)
229 goto out;
230 smp_flush_dcache_page_impl(page, dirty_cpu);
231 }
232 set_dcache_dirty(page, this_cpu);
233 } else {
234 /* We could delay the flush for the !page_mapping
235 * case too. But that case is for exec env/arg
236 * pages and those are %99 certainly going to get
237 * faulted into the tlb (and thus flushed) anyways.
238 */
239 flush_dcache_page_impl(page);
240 }
241
242out:
243 put_cpu();
244}
245
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700246void __kprobes flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 /* Cheetah has coherent I-cache. */
249 if (tlb_type == spitfire) {
250 unsigned long kaddr;
251
252 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
253 __flush_icache_page(__get_phys(kaddr));
254 }
255}
256
257unsigned long page_to_pfn(struct page *page)
258{
259 return (unsigned long) ((page - mem_map) + pfn_base);
260}
261
262struct page *pfn_to_page(unsigned long pfn)
263{
264 return (mem_map + (pfn - pfn_base));
265}
266
267void show_mem(void)
268{
269 printk("Mem-info:\n");
270 show_free_areas();
271 printk("Free swap: %6ldkB\n",
272 nr_swap_pages << (PAGE_SHIFT-10));
273 printk("%ld pages of RAM\n", num_physpages);
274 printk("%d free pages\n", nr_free_pages());
275 printk("%d pages in page table cache\n",pgtable_cache_size);
276}
277
278void mmu_info(struct seq_file *m)
279{
280 if (tlb_type == cheetah)
281 seq_printf(m, "MMU Type\t: Cheetah\n");
282 else if (tlb_type == cheetah_plus)
283 seq_printf(m, "MMU Type\t: Cheetah+\n");
284 else if (tlb_type == spitfire)
285 seq_printf(m, "MMU Type\t: Spitfire\n");
286 else
287 seq_printf(m, "MMU Type\t: ???\n");
288
289#ifdef CONFIG_DEBUG_DCFLUSH
290 seq_printf(m, "DCPageFlushes\t: %d\n",
291 atomic_read(&dcpage_flushes));
292#ifdef CONFIG_SMP
293 seq_printf(m, "DCPageFlushesXC\t: %d\n",
294 atomic_read(&dcpage_flushes_xcall));
295#endif /* CONFIG_SMP */
296#endif /* CONFIG_DEBUG_DCFLUSH */
297}
298
299struct linux_prom_translation {
300 unsigned long virt;
301 unsigned long size;
302 unsigned long data;
303};
David S. Millerb206fc42005-09-21 22:31:13 -0700304static struct linux_prom_translation prom_trans[512] __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306extern unsigned long prom_boot_page;
307extern void prom_remap(unsigned long physpage, unsigned long virtpage, int mmu_ihandle);
308extern int prom_get_mmu_ihandle(void);
309extern void register_prom_callbacks(void);
310
311/* Exported for SMP bootup purposes. */
312unsigned long kern_locked_tte_data;
313
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700314/* Exported for kernel TLB miss handling in ktlb.S */
315unsigned long prom_pmd_phys __read_mostly;
316unsigned int swapper_pgd_zero __read_mostly;
317
David S. Miller5085b4a2005-09-22 00:45:41 -0700318/* Allocate power-of-2 aligned chunks from the end of the
319 * kernel image. Return physical address.
320 */
321static inline unsigned long early_alloc_phys(unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
David S. Miller5085b4a2005-09-22 00:45:41 -0700323 unsigned long base;
324
325 BUILD_BUG_ON(size & (size - 1));
326
327 kern_size = (kern_size + (size - 1)) & ~(size - 1);
328 base = kern_base + kern_size;
329 kern_size += size;
330
331 return base;
332}
333
334static inline unsigned long load_phys32(unsigned long pa)
335{
336 unsigned long val;
337
338 __asm__ __volatile__("lduwa [%1] %2, %0"
339 : "=&r" (val)
340 : "r" (pa), "i" (ASI_PHYS_USE_EC));
341
342 return val;
343}
344
345static inline unsigned long load_phys64(unsigned long pa)
346{
347 unsigned long val;
348
349 __asm__ __volatile__("ldxa [%1] %2, %0"
350 : "=&r" (val)
351 : "r" (pa), "i" (ASI_PHYS_USE_EC));
352
353 return val;
354}
355
356static inline void store_phys32(unsigned long pa, unsigned long val)
357{
358 __asm__ __volatile__("stwa %0, [%1] %2"
359 : /* no outputs */
360 : "r" (val), "r" (pa), "i" (ASI_PHYS_USE_EC));
361}
362
363static inline void store_phys64(unsigned long pa, unsigned long val)
364{
365 __asm__ __volatile__("stxa %0, [%1] %2"
366 : /* no outputs */
367 : "r" (val), "r" (pa), "i" (ASI_PHYS_USE_EC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370#define BASE_PAGE_SIZE 8192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372/*
373 * Translate PROM's mapping we capture at boot time into physical address.
374 * The second parameter is only set from prom_callback() invocations.
375 */
376unsigned long prom_virt_to_phys(unsigned long promva, int *error)
377{
David S. Miller5085b4a2005-09-22 00:45:41 -0700378 unsigned long pmd_phys = (prom_pmd_phys +
379 ((promva >> 23) & 0x7ff) * sizeof(pmd_t));
380 unsigned long pte_phys;
381 pmd_t pmd_ent;
382 pte_t pte_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 unsigned long base;
384
David S. Miller5085b4a2005-09-22 00:45:41 -0700385 pmd_val(pmd_ent) = load_phys32(pmd_phys);
386 if (pmd_none(pmd_ent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (error)
388 *error = 1;
David S. Miller5085b4a2005-09-22 00:45:41 -0700389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
David S. Miller5085b4a2005-09-22 00:45:41 -0700391
392 pte_phys = (unsigned long)pmd_val(pmd_ent) << 11UL;
393 pte_phys += ((promva >> 13) & 0x3ff) * sizeof(pte_t);
394 pte_val(pte_ent) = load_phys64(pte_phys);
395 if (!pte_present(pte_ent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (error)
397 *error = 1;
David S. Miller5085b4a2005-09-22 00:45:41 -0700398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400 if (error) {
401 *error = 0;
David S. Miller5085b4a2005-09-22 00:45:41 -0700402 return pte_val(pte_ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
David S. Miller5085b4a2005-09-22 00:45:41 -0700404 base = pte_val(pte_ent) & _PAGE_PADDR;
405 return (base + (promva & (BASE_PAGE_SIZE - 1)));
David S. Miller405599b2005-09-22 00:12:35 -0700406}
407
408/* The obp translations are saved based on 8k pagesize, since obp can
409 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
410 * HI_OBP_ADDRESS range are handled in entry.S and do not use the vpte
411 * scheme (also, see rant in inherit_locked_prom_mappings()).
412 */
413static void build_obp_range(unsigned long start, unsigned long end, unsigned long data)
414{
415 unsigned long vaddr;
416
417 for (vaddr = start; vaddr < end; vaddr += BASE_PAGE_SIZE) {
David S. Miller5085b4a2005-09-22 00:45:41 -0700418 unsigned long val, pte_phys, pmd_phys;
419 pmd_t pmd_ent;
420 int i;
David S. Miller405599b2005-09-22 00:12:35 -0700421
David S. Miller5085b4a2005-09-22 00:45:41 -0700422 pmd_phys = (prom_pmd_phys +
423 (((vaddr >> 23) & 0x7ff) * sizeof(pmd_t)));
424 pmd_val(pmd_ent) = load_phys32(pmd_phys);
425 if (pmd_none(pmd_ent)) {
426 pte_phys = early_alloc_phys(BASE_PAGE_SIZE);
427
428 for (i = 0; i < BASE_PAGE_SIZE / sizeof(pte_t); i++)
429 store_phys64(pte_phys+i*sizeof(pte_t),0);
430
431 pmd_val(pmd_ent) = pte_phys >> 11UL;
432 store_phys32(pmd_phys, pmd_val(pmd_ent));
David S. Miller405599b2005-09-22 00:12:35 -0700433 }
David S. Miller5085b4a2005-09-22 00:45:41 -0700434
435 pte_phys = (unsigned long)pmd_val(pmd_ent) << 11UL;
436 pte_phys += (((vaddr >> 13) & 0x3ff) * sizeof(pte_t));
David S. Miller405599b2005-09-22 00:12:35 -0700437
438 val = data;
439
440 /* Clear diag TTE bits. */
441 if (tlb_type == spitfire)
442 val &= ~0x0003fe0000000000UL;
443
David S. Miller5085b4a2005-09-22 00:45:41 -0700444 store_phys64(pte_phys, val | _PAGE_MODIFIED);
445
David S. Miller405599b2005-09-22 00:12:35 -0700446 data += BASE_PAGE_SIZE;
447 }
448}
449
David S. Miller5085b4a2005-09-22 00:45:41 -0700450static inline int in_obp_range(unsigned long vaddr)
451{
452 return (vaddr >= LOW_OBP_ADDRESS &&
453 vaddr < HI_OBP_ADDRESS);
454}
455
David S. Miller405599b2005-09-22 00:12:35 -0700456#define OBP_PMD_SIZE 2048
457static void build_obp_pgtable(int prom_trans_ents)
458{
David S. Miller5085b4a2005-09-22 00:45:41 -0700459 unsigned long i;
David S. Miller405599b2005-09-22 00:12:35 -0700460
David S. Miller5085b4a2005-09-22 00:45:41 -0700461 prom_pmd_phys = early_alloc_phys(OBP_PMD_SIZE);
462 for (i = 0; i < OBP_PMD_SIZE; i += 4)
463 store_phys32(prom_pmd_phys + i, 0);
464
David S. Miller405599b2005-09-22 00:12:35 -0700465 for (i = 0; i < prom_trans_ents; i++) {
466 unsigned long start, end;
467
468 if (!in_obp_range(prom_trans[i].virt))
469 continue;
470
471 start = prom_trans[i].virt;
472 end = start + prom_trans[i].size;
473 if (end > HI_OBP_ADDRESS)
474 end = HI_OBP_ADDRESS;
475
476 build_obp_range(start, end, prom_trans[i].data);
477 }
David S. Miller405599b2005-09-22 00:12:35 -0700478}
479
480/* Read OBP translations property into 'prom_trans[]'.
481 * Return the number of entries.
482 */
483static int read_obp_translations(void)
484{
485 int n, node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 node = prom_finddevice("/virtual-memory");
488 n = prom_getproplen(node, "translations");
David S. Miller405599b2005-09-22 00:12:35 -0700489 if (unlikely(n == 0 || n == -1)) {
David S. Millerb206fc42005-09-21 22:31:13 -0700490 prom_printf("prom_mappings: Couldn't get size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 prom_halt();
492 }
David S. Miller405599b2005-09-22 00:12:35 -0700493 if (unlikely(n > sizeof(prom_trans))) {
494 prom_printf("prom_mappings: Size %Zd is too big.\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 prom_halt();
496 }
David S. Miller405599b2005-09-22 00:12:35 -0700497
David S. Millerb206fc42005-09-21 22:31:13 -0700498 if ((n = prom_getproperty(node, "translations",
David S. Miller405599b2005-09-22 00:12:35 -0700499 (char *)&prom_trans[0],
500 sizeof(prom_trans))) == -1) {
David S. Millerb206fc42005-09-21 22:31:13 -0700501 prom_printf("prom_mappings: Couldn't get property.\n");
502 prom_halt();
503 }
504 n = n / sizeof(struct linux_prom_translation);
David S. Miller405599b2005-09-22 00:12:35 -0700505 return n;
506}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
David S. Miller405599b2005-09-22 00:12:35 -0700508static inline void early_spitfire_errata32(void)
509{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* Spitfire Errata #32 workaround */
511 /* NOTE: Using plain zero for the context value is
512 * correct here, we are not using the Linux trap
513 * tables yet so we should not use the special
514 * UltraSPARC-III+ page size encodings yet.
515 */
516 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
517 "flush %%g6"
518 : /* No outputs */
David S. Miller405599b2005-09-22 00:12:35 -0700519 : "r" (0), "r" (PRIMARY_CONTEXT),
520 "i" (ASI_DMMU));
521}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
David S. Miller405599b2005-09-22 00:12:35 -0700523static void lock_remap_func_page(unsigned long phys_page)
524{
525 unsigned long tte_data = (phys_page | pgprot_val(PAGE_KERNEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 if (tlb_type == spitfire) {
528 /* Lock this into i/d tlb entry 59 */
529 __asm__ __volatile__(
530 "stxa %%g0, [%2] %3\n\t"
531 "stxa %0, [%1] %4\n\t"
532 "membar #Sync\n\t"
533 "flush %%g6\n\t"
534 "stxa %%g0, [%2] %5\n\t"
535 "stxa %0, [%1] %6\n\t"
536 "membar #Sync\n\t"
537 "flush %%g6"
David S. Miller405599b2005-09-22 00:12:35 -0700538 : /* no outputs */
539 : "r" (tte_data), "r" (59 << 3), "r" (TLB_TAG_ACCESS),
540 "i" (ASI_DMMU), "i" (ASI_DTLB_DATA_ACCESS),
541 "i" (ASI_IMMU), "i" (ASI_ITLB_DATA_ACCESS)
542 : "memory");
543 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* Lock this into i/d tlb-0 entry 11 */
545 __asm__ __volatile__(
546 "stxa %%g0, [%2] %3\n\t"
547 "stxa %0, [%1] %4\n\t"
548 "membar #Sync\n\t"
549 "flush %%g6\n\t"
550 "stxa %%g0, [%2] %5\n\t"
551 "stxa %0, [%1] %6\n\t"
552 "membar #Sync\n\t"
553 "flush %%g6"
David S. Miller405599b2005-09-22 00:12:35 -0700554 : /* no outputs */
555 : "r" (tte_data), "r" ((0 << 16) | (11 << 3)),
556 "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU),
557 "i" (ASI_DTLB_DATA_ACCESS), "i" (ASI_IMMU),
558 "i" (ASI_ITLB_DATA_ACCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
David S. Miller405599b2005-09-22 00:12:35 -0700561}
562
563static void remap_kernel(void)
564{
565 unsigned long phys_page, tte_vaddr, tte_data;
566 void (*remap_func)(unsigned long, unsigned long, int);
567 int tlb_ent = sparc64_highest_locked_tlbent();
568
569 early_spitfire_errata32();
570
571 if (tlb_type == spitfire)
572 phys_page = spitfire_get_dtlb_data(tlb_ent);
573 else
574 phys_page = cheetah_get_ldtlb_data(tlb_ent);
575
576 phys_page &= _PAGE_PADDR;
577 phys_page += ((unsigned long)&prom_boot_page -
578 (unsigned long)KERNBASE);
579
580 lock_remap_func_page(phys_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 tte_vaddr = (unsigned long) KERNBASE;
583
David S. Miller405599b2005-09-22 00:12:35 -0700584 early_spitfire_errata32();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 if (tlb_type == spitfire)
David S. Miller405599b2005-09-22 00:12:35 -0700587 tte_data = spitfire_get_dtlb_data(tlb_ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 else
David S. Miller405599b2005-09-22 00:12:35 -0700589 tte_data = cheetah_get_ldtlb_data(tlb_ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 kern_locked_tte_data = tte_data;
592
593 remap_func = (void *) ((unsigned long) &prom_remap -
594 (unsigned long) &prom_boot_page);
595
David S. Miller405599b2005-09-22 00:12:35 -0700596 early_spitfire_errata32();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
David S. Miller405599b2005-09-22 00:12:35 -0700598 phys_page = tte_data & _PAGE_PADDR;
599 remap_func(phys_page, KERNBASE, prom_get_mmu_ihandle());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (bigkernel)
David S. Miller405599b2005-09-22 00:12:35 -0700601 remap_func(phys_page + 0x400000,
602 KERNBASE + 0x400000,
603 prom_get_mmu_ihandle());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* Flush out that temporary mapping. */
606 spitfire_flush_dtlb_nucleus_page(0x0);
607 spitfire_flush_itlb_nucleus_page(0x0);
608
609 /* Now lock us back into the TLBs via OBP. */
David S. Miller405599b2005-09-22 00:12:35 -0700610 prom_dtlb_load(tlb_ent, tte_data, tte_vaddr);
611 prom_itlb_load(tlb_ent, tte_data, tte_vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (bigkernel) {
David S. Miller405599b2005-09-22 00:12:35 -0700613 prom_dtlb_load(tlb_ent - 1,
614 tte_data + 0x400000,
615 tte_vaddr + 0x400000);
616 prom_itlb_load(tlb_ent - 1,
617 tte_data + 0x400000,
618 tte_vaddr + 0x400000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
David S. Miller405599b2005-09-22 00:12:35 -0700620}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
David S. Miller405599b2005-09-22 00:12:35 -0700622static void inherit_prom_mappings(void)
623{
624 int n;
625
626 n = read_obp_translations();
627 build_obp_pgtable(n);
628
629 /* Now fixup OBP's idea about where we really are mapped. */
630 prom_printf("Remapping the kernel... ");
631 remap_kernel();
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 prom_printf("done.\n");
634
635 register_prom_callbacks();
636}
637
638/* The OBP specifications for sun4u mark 0xfffffffc00000000 and
639 * upwards as reserved for use by the firmware (I wonder if this
640 * will be the same on Cheetah...). We use this virtual address
641 * range for the VPTE table mappings of the nucleus so we need
642 * to zap them when we enter the PROM. -DaveM
643 */
644static void __flush_nucleus_vptes(void)
645{
646 unsigned long prom_reserved_base = 0xfffffffc00000000UL;
647 int i;
648
649 /* Only DTLB must be checked for VPTE entries. */
650 if (tlb_type == spitfire) {
651 for (i = 0; i < 63; i++) {
652 unsigned long tag;
653
654 /* Spitfire Errata #32 workaround */
655 /* NOTE: Always runs on spitfire, so no cheetah+
656 * page size encodings.
657 */
658 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
659 "flush %%g6"
660 : /* No outputs */
661 : "r" (0),
662 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
663
664 tag = spitfire_get_dtlb_tag(i);
665 if (((tag & ~(PAGE_MASK)) == 0) &&
666 ((tag & (PAGE_MASK)) >= prom_reserved_base)) {
667 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
668 "membar #Sync"
669 : /* no outputs */
670 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
671 spitfire_put_dtlb_data(i, 0x0UL);
672 }
673 }
674 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
675 for (i = 0; i < 512; i++) {
676 unsigned long tag = cheetah_get_dtlb_tag(i, 2);
677
678 if ((tag & ~PAGE_MASK) == 0 &&
679 (tag & PAGE_MASK) >= prom_reserved_base) {
680 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
681 "membar #Sync"
682 : /* no outputs */
683 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
684 cheetah_put_dtlb_data(i, 0x0UL, 2);
685 }
686
687 if (tlb_type != cheetah_plus)
688 continue;
689
690 tag = cheetah_get_dtlb_tag(i, 3);
691
692 if ((tag & ~PAGE_MASK) == 0 &&
693 (tag & PAGE_MASK) >= prom_reserved_base) {
694 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
695 "membar #Sync"
696 : /* no outputs */
697 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
698 cheetah_put_dtlb_data(i, 0x0UL, 3);
699 }
700 }
701 } else {
702 /* Implement me :-) */
703 BUG();
704 }
705}
706
707static int prom_ditlb_set;
708struct prom_tlb_entry {
709 int tlb_ent;
710 unsigned long tlb_tag;
711 unsigned long tlb_data;
712};
713struct prom_tlb_entry prom_itlb[16], prom_dtlb[16];
714
715void prom_world(int enter)
716{
717 unsigned long pstate;
718 int i;
719
720 if (!enter)
721 set_fs((mm_segment_t) { get_thread_current_ds() });
722
723 if (!prom_ditlb_set)
724 return;
725
726 /* Make sure the following runs atomically. */
727 __asm__ __volatile__("flushw\n\t"
728 "rdpr %%pstate, %0\n\t"
729 "wrpr %0, %1, %%pstate"
730 : "=r" (pstate)
731 : "i" (PSTATE_IE));
732
733 if (enter) {
734 /* Kick out nucleus VPTEs. */
735 __flush_nucleus_vptes();
736
737 /* Install PROM world. */
738 for (i = 0; i < 16; i++) {
739 if (prom_dtlb[i].tlb_ent != -1) {
740 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
741 "membar #Sync"
742 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
743 "i" (ASI_DMMU));
744 if (tlb_type == spitfire)
745 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
746 prom_dtlb[i].tlb_data);
747 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
748 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
749 prom_dtlb[i].tlb_data);
750 }
751 if (prom_itlb[i].tlb_ent != -1) {
752 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
753 "membar #Sync"
754 : : "r" (prom_itlb[i].tlb_tag),
755 "r" (TLB_TAG_ACCESS),
756 "i" (ASI_IMMU));
757 if (tlb_type == spitfire)
758 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
759 prom_itlb[i].tlb_data);
760 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
761 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
762 prom_itlb[i].tlb_data);
763 }
764 }
765 } else {
766 for (i = 0; i < 16; i++) {
767 if (prom_dtlb[i].tlb_ent != -1) {
768 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
769 "membar #Sync"
770 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
771 if (tlb_type == spitfire)
772 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
773 else
774 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
775 }
776 if (prom_itlb[i].tlb_ent != -1) {
777 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
778 "membar #Sync"
779 : : "r" (TLB_TAG_ACCESS),
780 "i" (ASI_IMMU));
781 if (tlb_type == spitfire)
782 spitfire_put_itlb_data(prom_itlb[i].tlb_ent, 0x0UL);
783 else
784 cheetah_put_litlb_data(prom_itlb[i].tlb_ent, 0x0UL);
785 }
786 }
787 }
788 __asm__ __volatile__("wrpr %0, 0, %%pstate"
789 : : "r" (pstate));
790}
791
792void inherit_locked_prom_mappings(int save_p)
793{
794 int i;
795 int dtlb_seen = 0;
796 int itlb_seen = 0;
797
798 /* Fucking losing PROM has more mappings in the TLB, but
799 * it (conveniently) fails to mention any of these in the
800 * translations property. The only ones that matter are
801 * the locked PROM tlb entries, so we impose the following
802 * irrecovable rule on the PROM, it is allowed 8 locked
803 * entries in the ITLB and 8 in the DTLB.
804 *
805 * Supposedly the upper 16GB of the address space is
806 * reserved for OBP, BUT I WISH THIS WAS DOCUMENTED
807 * SOMEWHERE!!!!!!!!!!!!!!!!! Furthermore the entire interface
808 * used between the client program and the firmware on sun5
809 * systems to coordinate mmu mappings is also COMPLETELY
810 * UNDOCUMENTED!!!!!! Thanks S(t)un!
811 */
812 if (save_p) {
813 for (i = 0; i < 16; i++) {
814 prom_itlb[i].tlb_ent = -1;
815 prom_dtlb[i].tlb_ent = -1;
816 }
817 }
818 if (tlb_type == spitfire) {
819 int high = SPITFIRE_HIGHEST_LOCKED_TLBENT - bigkernel;
820 for (i = 0; i < high; i++) {
821 unsigned long data;
822
823 /* Spitfire Errata #32 workaround */
824 /* NOTE: Always runs on spitfire, so no cheetah+
825 * page size encodings.
826 */
827 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
828 "flush %%g6"
829 : /* No outputs */
830 : "r" (0),
831 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
832
833 data = spitfire_get_dtlb_data(i);
834 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
835 unsigned long tag;
836
837 /* Spitfire Errata #32 workaround */
838 /* NOTE: Always runs on spitfire, so no
839 * cheetah+ page size encodings.
840 */
841 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
842 "flush %%g6"
843 : /* No outputs */
844 : "r" (0),
845 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
846
847 tag = spitfire_get_dtlb_tag(i);
848 if (save_p) {
849 prom_dtlb[dtlb_seen].tlb_ent = i;
850 prom_dtlb[dtlb_seen].tlb_tag = tag;
851 prom_dtlb[dtlb_seen].tlb_data = data;
852 }
853 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
854 "membar #Sync"
855 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
856 spitfire_put_dtlb_data(i, 0x0UL);
857
858 dtlb_seen++;
859 if (dtlb_seen > 15)
860 break;
861 }
862 }
863
864 for (i = 0; i < high; i++) {
865 unsigned long data;
866
867 /* Spitfire Errata #32 workaround */
868 /* NOTE: Always runs on spitfire, so no
869 * cheetah+ page size encodings.
870 */
871 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
872 "flush %%g6"
873 : /* No outputs */
874 : "r" (0),
875 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
876
877 data = spitfire_get_itlb_data(i);
878 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
879 unsigned long tag;
880
881 /* Spitfire Errata #32 workaround */
882 /* NOTE: Always runs on spitfire, so no
883 * cheetah+ page size encodings.
884 */
885 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
886 "flush %%g6"
887 : /* No outputs */
888 : "r" (0),
889 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
890
891 tag = spitfire_get_itlb_tag(i);
892 if (save_p) {
893 prom_itlb[itlb_seen].tlb_ent = i;
894 prom_itlb[itlb_seen].tlb_tag = tag;
895 prom_itlb[itlb_seen].tlb_data = data;
896 }
897 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
898 "membar #Sync"
899 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
900 spitfire_put_itlb_data(i, 0x0UL);
901
902 itlb_seen++;
903 if (itlb_seen > 15)
904 break;
905 }
906 }
907 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
908 int high = CHEETAH_HIGHEST_LOCKED_TLBENT - bigkernel;
909
910 for (i = 0; i < high; i++) {
911 unsigned long data;
912
913 data = cheetah_get_ldtlb_data(i);
914 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
915 unsigned long tag;
916
917 tag = cheetah_get_ldtlb_tag(i);
918 if (save_p) {
919 prom_dtlb[dtlb_seen].tlb_ent = i;
920 prom_dtlb[dtlb_seen].tlb_tag = tag;
921 prom_dtlb[dtlb_seen].tlb_data = data;
922 }
923 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
924 "membar #Sync"
925 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
926 cheetah_put_ldtlb_data(i, 0x0UL);
927
928 dtlb_seen++;
929 if (dtlb_seen > 15)
930 break;
931 }
932 }
933
934 for (i = 0; i < high; i++) {
935 unsigned long data;
936
937 data = cheetah_get_litlb_data(i);
938 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
939 unsigned long tag;
940
941 tag = cheetah_get_litlb_tag(i);
942 if (save_p) {
943 prom_itlb[itlb_seen].tlb_ent = i;
944 prom_itlb[itlb_seen].tlb_tag = tag;
945 prom_itlb[itlb_seen].tlb_data = data;
946 }
947 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
948 "membar #Sync"
949 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
950 cheetah_put_litlb_data(i, 0x0UL);
951
952 itlb_seen++;
953 if (itlb_seen > 15)
954 break;
955 }
956 }
957 } else {
958 /* Implement me :-) */
959 BUG();
960 }
961 if (save_p)
962 prom_ditlb_set = 1;
963}
964
965/* Give PROM back his world, done during reboots... */
966void prom_reload_locked(void)
967{
968 int i;
969
970 for (i = 0; i < 16; i++) {
971 if (prom_dtlb[i].tlb_ent != -1) {
972 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
973 "membar #Sync"
974 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
975 "i" (ASI_DMMU));
976 if (tlb_type == spitfire)
977 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
978 prom_dtlb[i].tlb_data);
979 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
980 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
981 prom_dtlb[i].tlb_data);
982 }
983
984 if (prom_itlb[i].tlb_ent != -1) {
985 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
986 "membar #Sync"
987 : : "r" (prom_itlb[i].tlb_tag),
988 "r" (TLB_TAG_ACCESS),
989 "i" (ASI_IMMU));
990 if (tlb_type == spitfire)
991 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
992 prom_itlb[i].tlb_data);
993 else
994 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
995 prom_itlb[i].tlb_data);
996 }
997 }
998}
999
1000#ifdef DCACHE_ALIASING_POSSIBLE
1001void __flush_dcache_range(unsigned long start, unsigned long end)
1002{
1003 unsigned long va;
1004
1005 if (tlb_type == spitfire) {
1006 int n = 0;
1007
1008 for (va = start; va < end; va += 32) {
1009 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
1010 if (++n >= 512)
1011 break;
1012 }
1013 } else {
1014 start = __pa(start);
1015 end = __pa(end);
1016 for (va = start; va < end; va += 32)
1017 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1018 "membar #Sync"
1019 : /* no outputs */
1020 : "r" (va),
1021 "i" (ASI_DCACHE_INVALIDATE));
1022 }
1023}
1024#endif /* DCACHE_ALIASING_POSSIBLE */
1025
1026/* If not locked, zap it. */
1027void __flush_tlb_all(void)
1028{
1029 unsigned long pstate;
1030 int i;
1031
1032 __asm__ __volatile__("flushw\n\t"
1033 "rdpr %%pstate, %0\n\t"
1034 "wrpr %0, %1, %%pstate"
1035 : "=r" (pstate)
1036 : "i" (PSTATE_IE));
1037 if (tlb_type == spitfire) {
1038 for (i = 0; i < 64; i++) {
1039 /* Spitfire Errata #32 workaround */
1040 /* NOTE: Always runs on spitfire, so no
1041 * cheetah+ page size encodings.
1042 */
1043 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1044 "flush %%g6"
1045 : /* No outputs */
1046 : "r" (0),
1047 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1048
1049 if (!(spitfire_get_dtlb_data(i) & _PAGE_L)) {
1050 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1051 "membar #Sync"
1052 : /* no outputs */
1053 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
1054 spitfire_put_dtlb_data(i, 0x0UL);
1055 }
1056
1057 /* Spitfire Errata #32 workaround */
1058 /* NOTE: Always runs on spitfire, so no
1059 * cheetah+ page size encodings.
1060 */
1061 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1062 "flush %%g6"
1063 : /* No outputs */
1064 : "r" (0),
1065 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1066
1067 if (!(spitfire_get_itlb_data(i) & _PAGE_L)) {
1068 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1069 "membar #Sync"
1070 : /* no outputs */
1071 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1072 spitfire_put_itlb_data(i, 0x0UL);
1073 }
1074 }
1075 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1076 cheetah_flush_dtlb_all();
1077 cheetah_flush_itlb_all();
1078 }
1079 __asm__ __volatile__("wrpr %0, 0, %%pstate"
1080 : : "r" (pstate));
1081}
1082
1083/* Caller does TLB context flushing on local CPU if necessary.
1084 * The caller also ensures that CTX_VALID(mm->context) is false.
1085 *
1086 * We must be careful about boundary cases so that we never
1087 * let the user have CTX 0 (nucleus) or we ever use a CTX
1088 * version of zero (and thus NO_CONTEXT would not be caught
1089 * by version mis-match tests in mmu_context.h).
1090 */
1091void get_new_mmu_context(struct mm_struct *mm)
1092{
1093 unsigned long ctx, new_ctx;
1094 unsigned long orig_pgsz_bits;
1095
1096
1097 spin_lock(&ctx_alloc_lock);
1098 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
1099 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
1100 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
1101 if (new_ctx >= (1 << CTX_NR_BITS)) {
1102 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
1103 if (new_ctx >= ctx) {
1104 int i;
1105 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
1106 CTX_FIRST_VERSION;
1107 if (new_ctx == 1)
1108 new_ctx = CTX_FIRST_VERSION;
1109
1110 /* Don't call memset, for 16 entries that's just
1111 * plain silly...
1112 */
1113 mmu_context_bmap[0] = 3;
1114 mmu_context_bmap[1] = 0;
1115 mmu_context_bmap[2] = 0;
1116 mmu_context_bmap[3] = 0;
1117 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
1118 mmu_context_bmap[i + 0] = 0;
1119 mmu_context_bmap[i + 1] = 0;
1120 mmu_context_bmap[i + 2] = 0;
1121 mmu_context_bmap[i + 3] = 0;
1122 }
1123 goto out;
1124 }
1125 }
1126 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
1127 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
1128out:
1129 tlb_context_cache = new_ctx;
1130 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
1131 spin_unlock(&ctx_alloc_lock);
1132}
1133
1134#ifndef CONFIG_SMP
1135struct pgtable_cache_struct pgt_quicklists;
1136#endif
1137
1138/* OK, we have to color these pages. The page tables are accessed
1139 * by non-Dcache enabled mapping in the VPTE area by the dtlb_backend.S
1140 * code, as well as by PAGE_OFFSET range direct-mapped addresses by
1141 * other parts of the kernel. By coloring, we make sure that the tlbmiss
1142 * fast handlers do not get data from old/garbage dcache lines that
1143 * correspond to an old/stale virtual address (user/kernel) that
1144 * previously mapped the pagetable page while accessing vpte range
1145 * addresses. The idea is that if the vpte color and PAGE_OFFSET range
1146 * color is the same, then when the kernel initializes the pagetable
1147 * using the later address range, accesses with the first address
1148 * range will see the newly initialized data rather than the garbage.
1149 */
1150#ifdef DCACHE_ALIASING_POSSIBLE
1151#define DC_ALIAS_SHIFT 1
1152#else
1153#define DC_ALIAS_SHIFT 0
1154#endif
Christoph Hellwig8edf72e2005-05-05 14:27:56 -07001155pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 struct page *page;
1158 unsigned long color;
1159
1160 {
1161 pte_t *ptep = pte_alloc_one_fast(mm, address);
1162
1163 if (ptep)
1164 return ptep;
1165 }
1166
1167 color = VPTE_COLOR(address);
1168 page = alloc_pages(GFP_KERNEL|__GFP_REPEAT, DC_ALIAS_SHIFT);
1169 if (page) {
1170 unsigned long *to_free;
1171 unsigned long paddr;
1172 pte_t *pte;
1173
1174#ifdef DCACHE_ALIASING_POSSIBLE
1175 set_page_count(page, 1);
1176 ClearPageCompound(page);
1177
1178 set_page_count((page + 1), 1);
1179 ClearPageCompound(page + 1);
1180#endif
1181 paddr = (unsigned long) page_address(page);
1182 memset((char *)paddr, 0, (PAGE_SIZE << DC_ALIAS_SHIFT));
1183
1184 if (!color) {
1185 pte = (pte_t *) paddr;
1186 to_free = (unsigned long *) (paddr + PAGE_SIZE);
1187 } else {
1188 pte = (pte_t *) (paddr + PAGE_SIZE);
1189 to_free = (unsigned long *) paddr;
1190 }
1191
1192#ifdef DCACHE_ALIASING_POSSIBLE
1193 /* Now free the other one up, adjust cache size. */
1194 preempt_disable();
1195 *to_free = (unsigned long) pte_quicklist[color ^ 0x1];
1196 pte_quicklist[color ^ 0x1] = to_free;
1197 pgtable_cache_size++;
1198 preempt_enable();
1199#endif
1200
1201 return pte;
1202 }
1203 return NULL;
1204}
1205
1206void sparc_ultra_dump_itlb(void)
1207{
1208 int slot;
1209
1210 if (tlb_type == spitfire) {
1211 printk ("Contents of itlb: ");
1212 for (slot = 0; slot < 14; slot++) printk (" ");
1213 printk ("%2x:%016lx,%016lx\n",
1214 0,
1215 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
1216 for (slot = 1; slot < 64; slot+=3) {
1217 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1218 slot,
1219 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
1220 slot+1,
1221 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
1222 slot+2,
1223 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
1224 }
1225 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1226 printk ("Contents of itlb0:\n");
1227 for (slot = 0; slot < 16; slot+=2) {
1228 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1229 slot,
1230 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
1231 slot+1,
1232 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
1233 }
1234 printk ("Contents of itlb2:\n");
1235 for (slot = 0; slot < 128; slot+=2) {
1236 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1237 slot,
1238 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
1239 slot+1,
1240 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
1241 }
1242 }
1243}
1244
1245void sparc_ultra_dump_dtlb(void)
1246{
1247 int slot;
1248
1249 if (tlb_type == spitfire) {
1250 printk ("Contents of dtlb: ");
1251 for (slot = 0; slot < 14; slot++) printk (" ");
1252 printk ("%2x:%016lx,%016lx\n", 0,
1253 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
1254 for (slot = 1; slot < 64; slot+=3) {
1255 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1256 slot,
1257 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
1258 slot+1,
1259 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
1260 slot+2,
1261 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
1262 }
1263 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1264 printk ("Contents of dtlb0:\n");
1265 for (slot = 0; slot < 16; slot+=2) {
1266 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1267 slot,
1268 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
1269 slot+1,
1270 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
1271 }
1272 printk ("Contents of dtlb2:\n");
1273 for (slot = 0; slot < 512; slot+=2) {
1274 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1275 slot,
1276 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
1277 slot+1,
1278 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
1279 }
1280 if (tlb_type == cheetah_plus) {
1281 printk ("Contents of dtlb3:\n");
1282 for (slot = 0; slot < 512; slot+=2) {
1283 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1284 slot,
1285 cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
1286 slot+1,
1287 cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
1288 }
1289 }
1290 }
1291}
1292
1293extern unsigned long cmdline_memory_size;
1294
1295unsigned long __init bootmem_init(unsigned long *pages_avail)
1296{
1297 unsigned long bootmap_size, start_pfn, end_pfn;
1298 unsigned long end_of_phys_memory = 0UL;
1299 unsigned long bootmap_pfn, bytes_avail, size;
1300 int i;
1301
1302#ifdef CONFIG_DEBUG_BOOTMEM
1303 prom_printf("bootmem_init: Scan sp_banks, ");
1304#endif
1305
1306 bytes_avail = 0UL;
1307 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1308 end_of_phys_memory = sp_banks[i].base_addr +
1309 sp_banks[i].num_bytes;
1310 bytes_avail += sp_banks[i].num_bytes;
1311 if (cmdline_memory_size) {
1312 if (bytes_avail > cmdline_memory_size) {
1313 unsigned long slack = bytes_avail - cmdline_memory_size;
1314
1315 bytes_avail -= slack;
1316 end_of_phys_memory -= slack;
1317
1318 sp_banks[i].num_bytes -= slack;
1319 if (sp_banks[i].num_bytes == 0) {
1320 sp_banks[i].base_addr = 0xdeadbeef;
1321 } else {
1322 sp_banks[i+1].num_bytes = 0;
1323 sp_banks[i+1].base_addr = 0xdeadbeef;
1324 }
1325 break;
1326 }
1327 }
1328 }
1329
1330 *pages_avail = bytes_avail >> PAGE_SHIFT;
1331
1332 /* Start with page aligned address of last symbol in kernel
1333 * image. The kernel is hard mapped below PAGE_OFFSET in a
1334 * 4MB locked TLB translation.
1335 */
1336 start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
1337
1338 bootmap_pfn = start_pfn;
1339
1340 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
1341
1342#ifdef CONFIG_BLK_DEV_INITRD
1343 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
1344 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
1345 unsigned long ramdisk_image = sparc_ramdisk_image ?
1346 sparc_ramdisk_image : sparc_ramdisk_image64;
1347 if (ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
1348 ramdisk_image -= KERNBASE;
1349 initrd_start = ramdisk_image + phys_base;
1350 initrd_end = initrd_start + sparc_ramdisk_size;
1351 if (initrd_end > end_of_phys_memory) {
1352 printk(KERN_CRIT "initrd extends beyond end of memory "
1353 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
1354 initrd_end, end_of_phys_memory);
1355 initrd_start = 0;
1356 }
1357 if (initrd_start) {
1358 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
1359 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
1360 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
1361 }
1362 }
1363#endif
1364 /* Initialize the boot-time allocator. */
1365 max_pfn = max_low_pfn = end_pfn;
1366 min_low_pfn = pfn_base;
1367
1368#ifdef CONFIG_DEBUG_BOOTMEM
1369 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
1370 min_low_pfn, bootmap_pfn, max_low_pfn);
1371#endif
1372 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base, end_pfn);
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* Now register the available physical memory with the
1375 * allocator.
1376 */
1377 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1378#ifdef CONFIG_DEBUG_BOOTMEM
1379 prom_printf("free_bootmem(sp_banks:%d): base[%lx] size[%lx]\n",
1380 i, sp_banks[i].base_addr, sp_banks[i].num_bytes);
1381#endif
1382 free_bootmem(sp_banks[i].base_addr, sp_banks[i].num_bytes);
1383 }
1384
1385#ifdef CONFIG_BLK_DEV_INITRD
1386 if (initrd_start) {
1387 size = initrd_end - initrd_start;
1388
1389 /* Resert the initrd image area. */
1390#ifdef CONFIG_DEBUG_BOOTMEM
1391 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
1392 initrd_start, initrd_end);
1393#endif
1394 reserve_bootmem(initrd_start, size);
1395 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1396
1397 initrd_start += PAGE_OFFSET;
1398 initrd_end += PAGE_OFFSET;
1399 }
1400#endif
1401 /* Reserve the kernel text/data/bss. */
1402#ifdef CONFIG_DEBUG_BOOTMEM
1403 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
1404#endif
1405 reserve_bootmem(kern_base, kern_size);
1406 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
1407
1408 /* Reserve the bootmem map. We do not account for it
1409 * in pages_avail because we will release that memory
1410 * in free_all_bootmem.
1411 */
1412 size = bootmap_size;
1413#ifdef CONFIG_DEBUG_BOOTMEM
1414 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
1415 (bootmap_pfn << PAGE_SHIFT), size);
1416#endif
1417 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
1418 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1419
1420 return end_pfn;
1421}
1422
1423/* paging_init() sets up the page tables */
1424
1425extern void cheetah_ecache_flush_init(void);
1426
1427static unsigned long last_valid_pfn;
1428
1429void __init paging_init(void)
1430{
1431 extern pmd_t swapper_pmd_dir[1024];
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001432 unsigned long end_pfn, pages_avail, shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 unsigned long real_end;
1434
1435 set_bit(0, mmu_context_bmap);
1436
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001437 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 real_end = (unsigned long)_end;
1440 if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1441 bigkernel = 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001442 if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
1443 prom_printf("paging_init: Kernel > 8MB, too large.\n");
1444 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001446
1447 /* Set kernel pgd to upper alias so physical page computations
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 * work.
1449 */
1450 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1451
1452 memset(swapper_pmd_dir, 0, sizeof(swapper_pmd_dir));
1453
1454 /* Now can init the kernel/bad page tables. */
1455 pud_set(pud_offset(&swapper_pg_dir[0], 0),
1456 swapper_pmd_dir + (shift / sizeof(pgd_t)));
1457
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001458 swapper_pgd_zero = pgd_val(swapper_pg_dir[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
David S. Miller5085b4a2005-09-22 00:45:41 -07001460 /* Inherit non-locked OBP mappings. */
1461 inherit_prom_mappings();
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /* Ok, we can use our TLB miss and window trap handlers safely.
1464 * We need to do a quick peek here to see if we are on StarFire
1465 * or not, so setup_tba can setup the IRQ globals correctly (it
1466 * needs to get the hard smp processor id correctly).
1467 */
1468 {
1469 extern void setup_tba(int);
1470 setup_tba(this_is_starfire);
1471 }
1472
1473 inherit_locked_prom_mappings(1);
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 __flush_tlb_all();
1476
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001477 /* Setup bootmem... */
1478 pages_avail = 0;
1479 last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 {
1482 unsigned long zones_size[MAX_NR_ZONES];
1483 unsigned long zholes_size[MAX_NR_ZONES];
1484 unsigned long npages;
1485 int znum;
1486
1487 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1488 zones_size[znum] = zholes_size[znum] = 0;
1489
1490 npages = end_pfn - pfn_base;
1491 zones_size[ZONE_DMA] = npages;
1492 zholes_size[ZONE_DMA] = npages - pages_avail;
1493
1494 free_area_init_node(0, &contig_page_data, zones_size,
1495 phys_base >> PAGE_SHIFT, zholes_size);
1496 }
1497
1498 device_scan();
1499}
1500
1501/* Ok, it seems that the prom can allocate some more memory chunks
1502 * as a side effect of some prom calls we perform during the
1503 * boot sequence. My most likely theory is that it is from the
1504 * prom_set_traptable() call, and OBP is allocating a scratchpad
1505 * for saving client program register state etc.
1506 */
1507static void __init sort_memlist(struct linux_mlist_p1275 *thislist)
1508{
1509 int swapi = 0;
1510 int i, mitr;
1511 unsigned long tmpaddr, tmpsize;
1512 unsigned long lowest;
1513
1514 for (i = 0; thislist[i].theres_more != 0; i++) {
1515 lowest = thislist[i].start_adr;
1516 for (mitr = i+1; thislist[mitr-1].theres_more != 0; mitr++)
1517 if (thislist[mitr].start_adr < lowest) {
1518 lowest = thislist[mitr].start_adr;
1519 swapi = mitr;
1520 }
1521 if (lowest == thislist[i].start_adr)
1522 continue;
1523 tmpaddr = thislist[swapi].start_adr;
1524 tmpsize = thislist[swapi].num_bytes;
1525 for (mitr = swapi; mitr > i; mitr--) {
1526 thislist[mitr].start_adr = thislist[mitr-1].start_adr;
1527 thislist[mitr].num_bytes = thislist[mitr-1].num_bytes;
1528 }
1529 thislist[i].start_adr = tmpaddr;
1530 thislist[i].num_bytes = tmpsize;
1531 }
1532}
1533
1534void __init rescan_sp_banks(void)
1535{
1536 struct linux_prom64_registers memlist[64];
1537 struct linux_mlist_p1275 avail[64], *mlist;
1538 unsigned long bytes, base_paddr;
1539 int num_regs, node = prom_finddevice("/memory");
1540 int i;
1541
1542 num_regs = prom_getproperty(node, "available",
1543 (char *) memlist, sizeof(memlist));
1544 num_regs = (num_regs / sizeof(struct linux_prom64_registers));
1545 for (i = 0; i < num_regs; i++) {
1546 avail[i].start_adr = memlist[i].phys_addr;
1547 avail[i].num_bytes = memlist[i].reg_size;
1548 avail[i].theres_more = &avail[i + 1];
1549 }
1550 avail[i - 1].theres_more = NULL;
1551 sort_memlist(avail);
1552
1553 mlist = &avail[0];
1554 i = 0;
1555 bytes = mlist->num_bytes;
1556 base_paddr = mlist->start_adr;
1557
1558 sp_banks[0].base_addr = base_paddr;
1559 sp_banks[0].num_bytes = bytes;
1560
1561 while (mlist->theres_more != NULL){
1562 i++;
1563 mlist = mlist->theres_more;
1564 bytes = mlist->num_bytes;
1565 if (i >= SPARC_PHYS_BANKS-1) {
1566 printk ("The machine has more banks than "
1567 "this kernel can support\n"
1568 "Increase the SPARC_PHYS_BANKS "
1569 "setting (currently %d)\n",
1570 SPARC_PHYS_BANKS);
1571 i = SPARC_PHYS_BANKS-1;
1572 break;
1573 }
1574
1575 sp_banks[i].base_addr = mlist->start_adr;
1576 sp_banks[i].num_bytes = mlist->num_bytes;
1577 }
1578
1579 i++;
1580 sp_banks[i].base_addr = 0xdeadbeefbeefdeadUL;
1581 sp_banks[i].num_bytes = 0;
1582
1583 for (i = 0; sp_banks[i].num_bytes != 0; i++)
1584 sp_banks[i].num_bytes &= PAGE_MASK;
1585}
1586
1587static void __init taint_real_pages(void)
1588{
1589 struct sparc_phys_banks saved_sp_banks[SPARC_PHYS_BANKS];
1590 int i;
1591
1592 for (i = 0; i < SPARC_PHYS_BANKS; i++) {
1593 saved_sp_banks[i].base_addr =
1594 sp_banks[i].base_addr;
1595 saved_sp_banks[i].num_bytes =
1596 sp_banks[i].num_bytes;
1597 }
1598
1599 rescan_sp_banks();
1600
1601 /* Find changes discovered in the sp_bank rescan and
1602 * reserve the lost portions in the bootmem maps.
1603 */
1604 for (i = 0; saved_sp_banks[i].num_bytes; i++) {
1605 unsigned long old_start, old_end;
1606
1607 old_start = saved_sp_banks[i].base_addr;
1608 old_end = old_start +
1609 saved_sp_banks[i].num_bytes;
1610 while (old_start < old_end) {
1611 int n;
1612
1613 for (n = 0; sp_banks[n].num_bytes; n++) {
1614 unsigned long new_start, new_end;
1615
1616 new_start = sp_banks[n].base_addr;
1617 new_end = new_start + sp_banks[n].num_bytes;
1618
1619 if (new_start <= old_start &&
1620 new_end >= (old_start + PAGE_SIZE)) {
1621 set_bit (old_start >> 22,
1622 sparc64_valid_addr_bitmap);
1623 goto do_next_page;
1624 }
1625 }
1626 reserve_bootmem(old_start, PAGE_SIZE);
1627
1628 do_next_page:
1629 old_start += PAGE_SIZE;
1630 }
1631 }
1632}
1633
1634void __init mem_init(void)
1635{
1636 unsigned long codepages, datapages, initpages;
1637 unsigned long addr, last;
1638 int i;
1639
1640 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1641 i += 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001642 sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (sparc64_valid_addr_bitmap == NULL) {
1644 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1645 prom_halt();
1646 }
1647 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1648
1649 addr = PAGE_OFFSET + kern_base;
1650 last = PAGE_ALIGN(kern_size) + addr;
1651 while (addr < last) {
1652 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1653 addr += PAGE_SIZE;
1654 }
1655
1656 taint_real_pages();
1657
1658 max_mapnr = last_valid_pfn - pfn_base;
1659 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1660
1661#ifdef CONFIG_DEBUG_BOOTMEM
1662 prom_printf("mem_init: Calling free_all_bootmem().\n");
1663#endif
1664 totalram_pages = num_physpages = free_all_bootmem() - 1;
1665
1666 /*
1667 * Set up the zero page, mark it reserved, so that page count
1668 * is not manipulated when freeing the page from user ptes.
1669 */
1670 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
1671 if (mem_map_zero == NULL) {
1672 prom_printf("paging_init: Cannot alloc zero page.\n");
1673 prom_halt();
1674 }
1675 SetPageReserved(mem_map_zero);
1676
1677 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1678 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1679 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1680 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1681 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1682 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1683
1684 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1685 nr_free_pages() << (PAGE_SHIFT-10),
1686 codepages << (PAGE_SHIFT-10),
1687 datapages << (PAGE_SHIFT-10),
1688 initpages << (PAGE_SHIFT-10),
1689 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1690
1691 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1692 cheetah_ecache_flush_init();
1693}
1694
1695void free_initmem (void)
1696{
1697 unsigned long addr, initend;
1698
1699 /*
1700 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1701 */
1702 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1703 initend = (unsigned long)(__init_end) & PAGE_MASK;
1704 for (; addr < initend; addr += PAGE_SIZE) {
1705 unsigned long page;
1706 struct page *p;
1707
1708 page = (addr +
1709 ((unsigned long) __va(kern_base)) -
1710 ((unsigned long) KERNBASE));
1711 memset((void *)addr, 0xcc, PAGE_SIZE);
1712 p = virt_to_page(page);
1713
1714 ClearPageReserved(p);
1715 set_page_count(p, 1);
1716 __free_page(p);
1717 num_physpages++;
1718 totalram_pages++;
1719 }
1720}
1721
1722#ifdef CONFIG_BLK_DEV_INITRD
1723void free_initrd_mem(unsigned long start, unsigned long end)
1724{
1725 if (start < end)
1726 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1727 for (; start < end; start += PAGE_SIZE) {
1728 struct page *p = virt_to_page(start);
1729
1730 ClearPageReserved(p);
1731 set_page_count(p, 1);
1732 __free_page(p);
1733 num_physpages++;
1734 totalram_pages++;
1735 }
1736}
1737#endif