blob: 92d095802958c180a3579ec6a63dbcda1305dadf [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 readjust_prom_translations(void)
623{
624 int nents, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
David S. Miller405599b2005-09-22 00:12:35 -0700626 nents = read_obp_translations();
627 for (i = 0; i < nents; i++) {
David S. Millerb206fc42005-09-21 22:31:13 -0700628 unsigned long vaddr = prom_trans[i].virt;
629 unsigned long size = prom_trans[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 if (vaddr < 0xf0000000UL) {
632 unsigned long avoid_start = (unsigned long) KERNBASE;
633 unsigned long avoid_end = avoid_start + (4 * 1024 * 1024);
634
635 if (bigkernel)
636 avoid_end += (4 * 1024 * 1024);
637 if (vaddr < avoid_start) {
638 unsigned long top = vaddr + size;
639
640 if (top > avoid_start)
641 top = avoid_start;
642 prom_unmap(top - vaddr, vaddr);
643 }
644 if ((vaddr + size) > avoid_end) {
645 unsigned long bottom = vaddr;
646
647 if (bottom < avoid_end)
648 bottom = avoid_end;
649 prom_unmap((vaddr + size) - bottom, bottom);
650 }
651 }
652 }
David S. Miller405599b2005-09-22 00:12:35 -0700653}
654
655static void inherit_prom_mappings(void)
656{
657 int n;
658
659 n = read_obp_translations();
660 build_obp_pgtable(n);
661
662 /* Now fixup OBP's idea about where we really are mapped. */
663 prom_printf("Remapping the kernel... ");
664 remap_kernel();
665
666 readjust_prom_translations();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 prom_printf("done.\n");
669
670 register_prom_callbacks();
671}
672
673/* The OBP specifications for sun4u mark 0xfffffffc00000000 and
674 * upwards as reserved for use by the firmware (I wonder if this
675 * will be the same on Cheetah...). We use this virtual address
676 * range for the VPTE table mappings of the nucleus so we need
677 * to zap them when we enter the PROM. -DaveM
678 */
679static void __flush_nucleus_vptes(void)
680{
681 unsigned long prom_reserved_base = 0xfffffffc00000000UL;
682 int i;
683
684 /* Only DTLB must be checked for VPTE entries. */
685 if (tlb_type == spitfire) {
686 for (i = 0; i < 63; i++) {
687 unsigned long tag;
688
689 /* Spitfire Errata #32 workaround */
690 /* NOTE: Always runs on spitfire, so no cheetah+
691 * page size encodings.
692 */
693 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
694 "flush %%g6"
695 : /* No outputs */
696 : "r" (0),
697 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
698
699 tag = spitfire_get_dtlb_tag(i);
700 if (((tag & ~(PAGE_MASK)) == 0) &&
701 ((tag & (PAGE_MASK)) >= prom_reserved_base)) {
702 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
703 "membar #Sync"
704 : /* no outputs */
705 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
706 spitfire_put_dtlb_data(i, 0x0UL);
707 }
708 }
709 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
710 for (i = 0; i < 512; i++) {
711 unsigned long tag = cheetah_get_dtlb_tag(i, 2);
712
713 if ((tag & ~PAGE_MASK) == 0 &&
714 (tag & PAGE_MASK) >= prom_reserved_base) {
715 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
716 "membar #Sync"
717 : /* no outputs */
718 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
719 cheetah_put_dtlb_data(i, 0x0UL, 2);
720 }
721
722 if (tlb_type != cheetah_plus)
723 continue;
724
725 tag = cheetah_get_dtlb_tag(i, 3);
726
727 if ((tag & ~PAGE_MASK) == 0 &&
728 (tag & PAGE_MASK) >= prom_reserved_base) {
729 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
730 "membar #Sync"
731 : /* no outputs */
732 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
733 cheetah_put_dtlb_data(i, 0x0UL, 3);
734 }
735 }
736 } else {
737 /* Implement me :-) */
738 BUG();
739 }
740}
741
742static int prom_ditlb_set;
743struct prom_tlb_entry {
744 int tlb_ent;
745 unsigned long tlb_tag;
746 unsigned long tlb_data;
747};
748struct prom_tlb_entry prom_itlb[16], prom_dtlb[16];
749
750void prom_world(int enter)
751{
752 unsigned long pstate;
753 int i;
754
755 if (!enter)
756 set_fs((mm_segment_t) { get_thread_current_ds() });
757
758 if (!prom_ditlb_set)
759 return;
760
761 /* Make sure the following runs atomically. */
762 __asm__ __volatile__("flushw\n\t"
763 "rdpr %%pstate, %0\n\t"
764 "wrpr %0, %1, %%pstate"
765 : "=r" (pstate)
766 : "i" (PSTATE_IE));
767
768 if (enter) {
769 /* Kick out nucleus VPTEs. */
770 __flush_nucleus_vptes();
771
772 /* Install PROM world. */
773 for (i = 0; i < 16; i++) {
774 if (prom_dtlb[i].tlb_ent != -1) {
775 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
776 "membar #Sync"
777 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
778 "i" (ASI_DMMU));
779 if (tlb_type == spitfire)
780 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
781 prom_dtlb[i].tlb_data);
782 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
783 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
784 prom_dtlb[i].tlb_data);
785 }
786 if (prom_itlb[i].tlb_ent != -1) {
787 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
788 "membar #Sync"
789 : : "r" (prom_itlb[i].tlb_tag),
790 "r" (TLB_TAG_ACCESS),
791 "i" (ASI_IMMU));
792 if (tlb_type == spitfire)
793 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
794 prom_itlb[i].tlb_data);
795 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
796 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
797 prom_itlb[i].tlb_data);
798 }
799 }
800 } else {
801 for (i = 0; i < 16; i++) {
802 if (prom_dtlb[i].tlb_ent != -1) {
803 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
804 "membar #Sync"
805 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
806 if (tlb_type == spitfire)
807 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
808 else
809 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent, 0x0UL);
810 }
811 if (prom_itlb[i].tlb_ent != -1) {
812 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
813 "membar #Sync"
814 : : "r" (TLB_TAG_ACCESS),
815 "i" (ASI_IMMU));
816 if (tlb_type == spitfire)
817 spitfire_put_itlb_data(prom_itlb[i].tlb_ent, 0x0UL);
818 else
819 cheetah_put_litlb_data(prom_itlb[i].tlb_ent, 0x0UL);
820 }
821 }
822 }
823 __asm__ __volatile__("wrpr %0, 0, %%pstate"
824 : : "r" (pstate));
825}
826
827void inherit_locked_prom_mappings(int save_p)
828{
829 int i;
830 int dtlb_seen = 0;
831 int itlb_seen = 0;
832
833 /* Fucking losing PROM has more mappings in the TLB, but
834 * it (conveniently) fails to mention any of these in the
835 * translations property. The only ones that matter are
836 * the locked PROM tlb entries, so we impose the following
837 * irrecovable rule on the PROM, it is allowed 8 locked
838 * entries in the ITLB and 8 in the DTLB.
839 *
840 * Supposedly the upper 16GB of the address space is
841 * reserved for OBP, BUT I WISH THIS WAS DOCUMENTED
842 * SOMEWHERE!!!!!!!!!!!!!!!!! Furthermore the entire interface
843 * used between the client program and the firmware on sun5
844 * systems to coordinate mmu mappings is also COMPLETELY
845 * UNDOCUMENTED!!!!!! Thanks S(t)un!
846 */
847 if (save_p) {
848 for (i = 0; i < 16; i++) {
849 prom_itlb[i].tlb_ent = -1;
850 prom_dtlb[i].tlb_ent = -1;
851 }
852 }
853 if (tlb_type == spitfire) {
854 int high = SPITFIRE_HIGHEST_LOCKED_TLBENT - bigkernel;
855 for (i = 0; i < high; i++) {
856 unsigned long data;
857
858 /* Spitfire Errata #32 workaround */
859 /* NOTE: Always runs on spitfire, so no cheetah+
860 * page size encodings.
861 */
862 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
863 "flush %%g6"
864 : /* No outputs */
865 : "r" (0),
866 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
867
868 data = spitfire_get_dtlb_data(i);
869 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
870 unsigned long tag;
871
872 /* Spitfire Errata #32 workaround */
873 /* NOTE: Always runs on spitfire, so no
874 * cheetah+ page size encodings.
875 */
876 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
877 "flush %%g6"
878 : /* No outputs */
879 : "r" (0),
880 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
881
882 tag = spitfire_get_dtlb_tag(i);
883 if (save_p) {
884 prom_dtlb[dtlb_seen].tlb_ent = i;
885 prom_dtlb[dtlb_seen].tlb_tag = tag;
886 prom_dtlb[dtlb_seen].tlb_data = data;
887 }
888 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
889 "membar #Sync"
890 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
891 spitfire_put_dtlb_data(i, 0x0UL);
892
893 dtlb_seen++;
894 if (dtlb_seen > 15)
895 break;
896 }
897 }
898
899 for (i = 0; i < high; i++) {
900 unsigned long data;
901
902 /* Spitfire Errata #32 workaround */
903 /* NOTE: Always runs on spitfire, so no
904 * cheetah+ page size encodings.
905 */
906 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
907 "flush %%g6"
908 : /* No outputs */
909 : "r" (0),
910 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
911
912 data = spitfire_get_itlb_data(i);
913 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
914 unsigned long tag;
915
916 /* Spitfire Errata #32 workaround */
917 /* NOTE: Always runs on spitfire, so no
918 * cheetah+ page size encodings.
919 */
920 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
921 "flush %%g6"
922 : /* No outputs */
923 : "r" (0),
924 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
925
926 tag = spitfire_get_itlb_tag(i);
927 if (save_p) {
928 prom_itlb[itlb_seen].tlb_ent = i;
929 prom_itlb[itlb_seen].tlb_tag = tag;
930 prom_itlb[itlb_seen].tlb_data = data;
931 }
932 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
933 "membar #Sync"
934 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
935 spitfire_put_itlb_data(i, 0x0UL);
936
937 itlb_seen++;
938 if (itlb_seen > 15)
939 break;
940 }
941 }
942 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
943 int high = CHEETAH_HIGHEST_LOCKED_TLBENT - bigkernel;
944
945 for (i = 0; i < high; i++) {
946 unsigned long data;
947
948 data = cheetah_get_ldtlb_data(i);
949 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
950 unsigned long tag;
951
952 tag = cheetah_get_ldtlb_tag(i);
953 if (save_p) {
954 prom_dtlb[dtlb_seen].tlb_ent = i;
955 prom_dtlb[dtlb_seen].tlb_tag = tag;
956 prom_dtlb[dtlb_seen].tlb_data = data;
957 }
958 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
959 "membar #Sync"
960 : : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
961 cheetah_put_ldtlb_data(i, 0x0UL);
962
963 dtlb_seen++;
964 if (dtlb_seen > 15)
965 break;
966 }
967 }
968
969 for (i = 0; i < high; i++) {
970 unsigned long data;
971
972 data = cheetah_get_litlb_data(i);
973 if ((data & (_PAGE_L|_PAGE_VALID)) == (_PAGE_L|_PAGE_VALID)) {
974 unsigned long tag;
975
976 tag = cheetah_get_litlb_tag(i);
977 if (save_p) {
978 prom_itlb[itlb_seen].tlb_ent = i;
979 prom_itlb[itlb_seen].tlb_tag = tag;
980 prom_itlb[itlb_seen].tlb_data = data;
981 }
982 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
983 "membar #Sync"
984 : : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
985 cheetah_put_litlb_data(i, 0x0UL);
986
987 itlb_seen++;
988 if (itlb_seen > 15)
989 break;
990 }
991 }
992 } else {
993 /* Implement me :-) */
994 BUG();
995 }
996 if (save_p)
997 prom_ditlb_set = 1;
998}
999
1000/* Give PROM back his world, done during reboots... */
1001void prom_reload_locked(void)
1002{
1003 int i;
1004
1005 for (i = 0; i < 16; i++) {
1006 if (prom_dtlb[i].tlb_ent != -1) {
1007 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1008 "membar #Sync"
1009 : : "r" (prom_dtlb[i].tlb_tag), "r" (TLB_TAG_ACCESS),
1010 "i" (ASI_DMMU));
1011 if (tlb_type == spitfire)
1012 spitfire_put_dtlb_data(prom_dtlb[i].tlb_ent,
1013 prom_dtlb[i].tlb_data);
1014 else if (tlb_type == cheetah || tlb_type == cheetah_plus)
1015 cheetah_put_ldtlb_data(prom_dtlb[i].tlb_ent,
1016 prom_dtlb[i].tlb_data);
1017 }
1018
1019 if (prom_itlb[i].tlb_ent != -1) {
1020 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1021 "membar #Sync"
1022 : : "r" (prom_itlb[i].tlb_tag),
1023 "r" (TLB_TAG_ACCESS),
1024 "i" (ASI_IMMU));
1025 if (tlb_type == spitfire)
1026 spitfire_put_itlb_data(prom_itlb[i].tlb_ent,
1027 prom_itlb[i].tlb_data);
1028 else
1029 cheetah_put_litlb_data(prom_itlb[i].tlb_ent,
1030 prom_itlb[i].tlb_data);
1031 }
1032 }
1033}
1034
1035#ifdef DCACHE_ALIASING_POSSIBLE
1036void __flush_dcache_range(unsigned long start, unsigned long end)
1037{
1038 unsigned long va;
1039
1040 if (tlb_type == spitfire) {
1041 int n = 0;
1042
1043 for (va = start; va < end; va += 32) {
1044 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
1045 if (++n >= 512)
1046 break;
1047 }
1048 } else {
1049 start = __pa(start);
1050 end = __pa(end);
1051 for (va = start; va < end; va += 32)
1052 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1053 "membar #Sync"
1054 : /* no outputs */
1055 : "r" (va),
1056 "i" (ASI_DCACHE_INVALIDATE));
1057 }
1058}
1059#endif /* DCACHE_ALIASING_POSSIBLE */
1060
1061/* If not locked, zap it. */
1062void __flush_tlb_all(void)
1063{
1064 unsigned long pstate;
1065 int i;
1066
1067 __asm__ __volatile__("flushw\n\t"
1068 "rdpr %%pstate, %0\n\t"
1069 "wrpr %0, %1, %%pstate"
1070 : "=r" (pstate)
1071 : "i" (PSTATE_IE));
1072 if (tlb_type == spitfire) {
1073 for (i = 0; i < 64; i++) {
1074 /* Spitfire Errata #32 workaround */
1075 /* NOTE: Always runs on spitfire, so no
1076 * cheetah+ page size encodings.
1077 */
1078 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1079 "flush %%g6"
1080 : /* No outputs */
1081 : "r" (0),
1082 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1083
1084 if (!(spitfire_get_dtlb_data(i) & _PAGE_L)) {
1085 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1086 "membar #Sync"
1087 : /* no outputs */
1088 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
1089 spitfire_put_dtlb_data(i, 0x0UL);
1090 }
1091
1092 /* Spitfire Errata #32 workaround */
1093 /* NOTE: Always runs on spitfire, so no
1094 * cheetah+ page size encodings.
1095 */
1096 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1097 "flush %%g6"
1098 : /* No outputs */
1099 : "r" (0),
1100 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1101
1102 if (!(spitfire_get_itlb_data(i) & _PAGE_L)) {
1103 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1104 "membar #Sync"
1105 : /* no outputs */
1106 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1107 spitfire_put_itlb_data(i, 0x0UL);
1108 }
1109 }
1110 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1111 cheetah_flush_dtlb_all();
1112 cheetah_flush_itlb_all();
1113 }
1114 __asm__ __volatile__("wrpr %0, 0, %%pstate"
1115 : : "r" (pstate));
1116}
1117
1118/* Caller does TLB context flushing on local CPU if necessary.
1119 * The caller also ensures that CTX_VALID(mm->context) is false.
1120 *
1121 * We must be careful about boundary cases so that we never
1122 * let the user have CTX 0 (nucleus) or we ever use a CTX
1123 * version of zero (and thus NO_CONTEXT would not be caught
1124 * by version mis-match tests in mmu_context.h).
1125 */
1126void get_new_mmu_context(struct mm_struct *mm)
1127{
1128 unsigned long ctx, new_ctx;
1129 unsigned long orig_pgsz_bits;
1130
1131
1132 spin_lock(&ctx_alloc_lock);
1133 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
1134 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
1135 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
1136 if (new_ctx >= (1 << CTX_NR_BITS)) {
1137 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
1138 if (new_ctx >= ctx) {
1139 int i;
1140 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
1141 CTX_FIRST_VERSION;
1142 if (new_ctx == 1)
1143 new_ctx = CTX_FIRST_VERSION;
1144
1145 /* Don't call memset, for 16 entries that's just
1146 * plain silly...
1147 */
1148 mmu_context_bmap[0] = 3;
1149 mmu_context_bmap[1] = 0;
1150 mmu_context_bmap[2] = 0;
1151 mmu_context_bmap[3] = 0;
1152 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
1153 mmu_context_bmap[i + 0] = 0;
1154 mmu_context_bmap[i + 1] = 0;
1155 mmu_context_bmap[i + 2] = 0;
1156 mmu_context_bmap[i + 3] = 0;
1157 }
1158 goto out;
1159 }
1160 }
1161 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
1162 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
1163out:
1164 tlb_context_cache = new_ctx;
1165 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
1166 spin_unlock(&ctx_alloc_lock);
1167}
1168
1169#ifndef CONFIG_SMP
1170struct pgtable_cache_struct pgt_quicklists;
1171#endif
1172
1173/* OK, we have to color these pages. The page tables are accessed
1174 * by non-Dcache enabled mapping in the VPTE area by the dtlb_backend.S
1175 * code, as well as by PAGE_OFFSET range direct-mapped addresses by
1176 * other parts of the kernel. By coloring, we make sure that the tlbmiss
1177 * fast handlers do not get data from old/garbage dcache lines that
1178 * correspond to an old/stale virtual address (user/kernel) that
1179 * previously mapped the pagetable page while accessing vpte range
1180 * addresses. The idea is that if the vpte color and PAGE_OFFSET range
1181 * color is the same, then when the kernel initializes the pagetable
1182 * using the later address range, accesses with the first address
1183 * range will see the newly initialized data rather than the garbage.
1184 */
1185#ifdef DCACHE_ALIASING_POSSIBLE
1186#define DC_ALIAS_SHIFT 1
1187#else
1188#define DC_ALIAS_SHIFT 0
1189#endif
Christoph Hellwig8edf72e2005-05-05 14:27:56 -07001190pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 struct page *page;
1193 unsigned long color;
1194
1195 {
1196 pte_t *ptep = pte_alloc_one_fast(mm, address);
1197
1198 if (ptep)
1199 return ptep;
1200 }
1201
1202 color = VPTE_COLOR(address);
1203 page = alloc_pages(GFP_KERNEL|__GFP_REPEAT, DC_ALIAS_SHIFT);
1204 if (page) {
1205 unsigned long *to_free;
1206 unsigned long paddr;
1207 pte_t *pte;
1208
1209#ifdef DCACHE_ALIASING_POSSIBLE
1210 set_page_count(page, 1);
1211 ClearPageCompound(page);
1212
1213 set_page_count((page + 1), 1);
1214 ClearPageCompound(page + 1);
1215#endif
1216 paddr = (unsigned long) page_address(page);
1217 memset((char *)paddr, 0, (PAGE_SIZE << DC_ALIAS_SHIFT));
1218
1219 if (!color) {
1220 pte = (pte_t *) paddr;
1221 to_free = (unsigned long *) (paddr + PAGE_SIZE);
1222 } else {
1223 pte = (pte_t *) (paddr + PAGE_SIZE);
1224 to_free = (unsigned long *) paddr;
1225 }
1226
1227#ifdef DCACHE_ALIASING_POSSIBLE
1228 /* Now free the other one up, adjust cache size. */
1229 preempt_disable();
1230 *to_free = (unsigned long) pte_quicklist[color ^ 0x1];
1231 pte_quicklist[color ^ 0x1] = to_free;
1232 pgtable_cache_size++;
1233 preempt_enable();
1234#endif
1235
1236 return pte;
1237 }
1238 return NULL;
1239}
1240
1241void sparc_ultra_dump_itlb(void)
1242{
1243 int slot;
1244
1245 if (tlb_type == spitfire) {
1246 printk ("Contents of itlb: ");
1247 for (slot = 0; slot < 14; slot++) printk (" ");
1248 printk ("%2x:%016lx,%016lx\n",
1249 0,
1250 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
1251 for (slot = 1; slot < 64; slot+=3) {
1252 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1253 slot,
1254 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
1255 slot+1,
1256 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
1257 slot+2,
1258 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
1259 }
1260 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1261 printk ("Contents of itlb0:\n");
1262 for (slot = 0; slot < 16; slot+=2) {
1263 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1264 slot,
1265 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
1266 slot+1,
1267 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
1268 }
1269 printk ("Contents of itlb2:\n");
1270 for (slot = 0; slot < 128; slot+=2) {
1271 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1272 slot,
1273 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
1274 slot+1,
1275 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
1276 }
1277 }
1278}
1279
1280void sparc_ultra_dump_dtlb(void)
1281{
1282 int slot;
1283
1284 if (tlb_type == spitfire) {
1285 printk ("Contents of dtlb: ");
1286 for (slot = 0; slot < 14; slot++) printk (" ");
1287 printk ("%2x:%016lx,%016lx\n", 0,
1288 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
1289 for (slot = 1; slot < 64; slot+=3) {
1290 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1291 slot,
1292 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
1293 slot+1,
1294 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
1295 slot+2,
1296 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
1297 }
1298 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1299 printk ("Contents of dtlb0:\n");
1300 for (slot = 0; slot < 16; slot+=2) {
1301 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1302 slot,
1303 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
1304 slot+1,
1305 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
1306 }
1307 printk ("Contents of dtlb2:\n");
1308 for (slot = 0; slot < 512; slot+=2) {
1309 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1310 slot,
1311 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
1312 slot+1,
1313 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
1314 }
1315 if (tlb_type == cheetah_plus) {
1316 printk ("Contents of dtlb3:\n");
1317 for (slot = 0; slot < 512; slot+=2) {
1318 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1319 slot,
1320 cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
1321 slot+1,
1322 cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
1323 }
1324 }
1325 }
1326}
1327
1328extern unsigned long cmdline_memory_size;
1329
1330unsigned long __init bootmem_init(unsigned long *pages_avail)
1331{
1332 unsigned long bootmap_size, start_pfn, end_pfn;
1333 unsigned long end_of_phys_memory = 0UL;
1334 unsigned long bootmap_pfn, bytes_avail, size;
1335 int i;
1336
1337#ifdef CONFIG_DEBUG_BOOTMEM
1338 prom_printf("bootmem_init: Scan sp_banks, ");
1339#endif
1340
1341 bytes_avail = 0UL;
1342 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1343 end_of_phys_memory = sp_banks[i].base_addr +
1344 sp_banks[i].num_bytes;
1345 bytes_avail += sp_banks[i].num_bytes;
1346 if (cmdline_memory_size) {
1347 if (bytes_avail > cmdline_memory_size) {
1348 unsigned long slack = bytes_avail - cmdline_memory_size;
1349
1350 bytes_avail -= slack;
1351 end_of_phys_memory -= slack;
1352
1353 sp_banks[i].num_bytes -= slack;
1354 if (sp_banks[i].num_bytes == 0) {
1355 sp_banks[i].base_addr = 0xdeadbeef;
1356 } else {
1357 sp_banks[i+1].num_bytes = 0;
1358 sp_banks[i+1].base_addr = 0xdeadbeef;
1359 }
1360 break;
1361 }
1362 }
1363 }
1364
1365 *pages_avail = bytes_avail >> PAGE_SHIFT;
1366
1367 /* Start with page aligned address of last symbol in kernel
1368 * image. The kernel is hard mapped below PAGE_OFFSET in a
1369 * 4MB locked TLB translation.
1370 */
1371 start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
1372
1373 bootmap_pfn = start_pfn;
1374
1375 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
1376
1377#ifdef CONFIG_BLK_DEV_INITRD
1378 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
1379 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
1380 unsigned long ramdisk_image = sparc_ramdisk_image ?
1381 sparc_ramdisk_image : sparc_ramdisk_image64;
1382 if (ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
1383 ramdisk_image -= KERNBASE;
1384 initrd_start = ramdisk_image + phys_base;
1385 initrd_end = initrd_start + sparc_ramdisk_size;
1386 if (initrd_end > end_of_phys_memory) {
1387 printk(KERN_CRIT "initrd extends beyond end of memory "
1388 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
1389 initrd_end, end_of_phys_memory);
1390 initrd_start = 0;
1391 }
1392 if (initrd_start) {
1393 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
1394 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
1395 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
1396 }
1397 }
1398#endif
1399 /* Initialize the boot-time allocator. */
1400 max_pfn = max_low_pfn = end_pfn;
1401 min_low_pfn = pfn_base;
1402
1403#ifdef CONFIG_DEBUG_BOOTMEM
1404 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
1405 min_low_pfn, bootmap_pfn, max_low_pfn);
1406#endif
1407 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base, end_pfn);
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /* Now register the available physical memory with the
1410 * allocator.
1411 */
1412 for (i = 0; sp_banks[i].num_bytes != 0; i++) {
1413#ifdef CONFIG_DEBUG_BOOTMEM
1414 prom_printf("free_bootmem(sp_banks:%d): base[%lx] size[%lx]\n",
1415 i, sp_banks[i].base_addr, sp_banks[i].num_bytes);
1416#endif
1417 free_bootmem(sp_banks[i].base_addr, sp_banks[i].num_bytes);
1418 }
1419
1420#ifdef CONFIG_BLK_DEV_INITRD
1421 if (initrd_start) {
1422 size = initrd_end - initrd_start;
1423
1424 /* Resert the initrd image area. */
1425#ifdef CONFIG_DEBUG_BOOTMEM
1426 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
1427 initrd_start, initrd_end);
1428#endif
1429 reserve_bootmem(initrd_start, size);
1430 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1431
1432 initrd_start += PAGE_OFFSET;
1433 initrd_end += PAGE_OFFSET;
1434 }
1435#endif
1436 /* Reserve the kernel text/data/bss. */
1437#ifdef CONFIG_DEBUG_BOOTMEM
1438 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
1439#endif
1440 reserve_bootmem(kern_base, kern_size);
1441 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
1442
1443 /* Reserve the bootmem map. We do not account for it
1444 * in pages_avail because we will release that memory
1445 * in free_all_bootmem.
1446 */
1447 size = bootmap_size;
1448#ifdef CONFIG_DEBUG_BOOTMEM
1449 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
1450 (bootmap_pfn << PAGE_SHIFT), size);
1451#endif
1452 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
1453 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1454
1455 return end_pfn;
1456}
1457
1458/* paging_init() sets up the page tables */
1459
1460extern void cheetah_ecache_flush_init(void);
1461
1462static unsigned long last_valid_pfn;
1463
1464void __init paging_init(void)
1465{
1466 extern pmd_t swapper_pmd_dir[1024];
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001467 unsigned long end_pfn, pages_avail, shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 unsigned long real_end;
1469
1470 set_bit(0, mmu_context_bmap);
1471
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001472 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 real_end = (unsigned long)_end;
1475 if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1476 bigkernel = 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001477 if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
1478 prom_printf("paging_init: Kernel > 8MB, too large.\n");
1479 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001481
1482 /* Set kernel pgd to upper alias so physical page computations
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 * work.
1484 */
1485 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1486
1487 memset(swapper_pmd_dir, 0, sizeof(swapper_pmd_dir));
1488
1489 /* Now can init the kernel/bad page tables. */
1490 pud_set(pud_offset(&swapper_pg_dir[0], 0),
1491 swapper_pmd_dir + (shift / sizeof(pgd_t)));
1492
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001493 swapper_pgd_zero = pgd_val(swapper_pg_dir[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
David S. Miller5085b4a2005-09-22 00:45:41 -07001495 /* Inherit non-locked OBP mappings. */
1496 inherit_prom_mappings();
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 /* Ok, we can use our TLB miss and window trap handlers safely.
1499 * We need to do a quick peek here to see if we are on StarFire
1500 * or not, so setup_tba can setup the IRQ globals correctly (it
1501 * needs to get the hard smp processor id correctly).
1502 */
1503 {
1504 extern void setup_tba(int);
1505 setup_tba(this_is_starfire);
1506 }
1507
1508 inherit_locked_prom_mappings(1);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 __flush_tlb_all();
1511
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001512 /* Setup bootmem... */
1513 pages_avail = 0;
1514 last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 {
1517 unsigned long zones_size[MAX_NR_ZONES];
1518 unsigned long zholes_size[MAX_NR_ZONES];
1519 unsigned long npages;
1520 int znum;
1521
1522 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1523 zones_size[znum] = zholes_size[znum] = 0;
1524
1525 npages = end_pfn - pfn_base;
1526 zones_size[ZONE_DMA] = npages;
1527 zholes_size[ZONE_DMA] = npages - pages_avail;
1528
1529 free_area_init_node(0, &contig_page_data, zones_size,
1530 phys_base >> PAGE_SHIFT, zholes_size);
1531 }
1532
1533 device_scan();
1534}
1535
1536/* Ok, it seems that the prom can allocate some more memory chunks
1537 * as a side effect of some prom calls we perform during the
1538 * boot sequence. My most likely theory is that it is from the
1539 * prom_set_traptable() call, and OBP is allocating a scratchpad
1540 * for saving client program register state etc.
1541 */
1542static void __init sort_memlist(struct linux_mlist_p1275 *thislist)
1543{
1544 int swapi = 0;
1545 int i, mitr;
1546 unsigned long tmpaddr, tmpsize;
1547 unsigned long lowest;
1548
1549 for (i = 0; thislist[i].theres_more != 0; i++) {
1550 lowest = thislist[i].start_adr;
1551 for (mitr = i+1; thislist[mitr-1].theres_more != 0; mitr++)
1552 if (thislist[mitr].start_adr < lowest) {
1553 lowest = thislist[mitr].start_adr;
1554 swapi = mitr;
1555 }
1556 if (lowest == thislist[i].start_adr)
1557 continue;
1558 tmpaddr = thislist[swapi].start_adr;
1559 tmpsize = thislist[swapi].num_bytes;
1560 for (mitr = swapi; mitr > i; mitr--) {
1561 thislist[mitr].start_adr = thislist[mitr-1].start_adr;
1562 thislist[mitr].num_bytes = thislist[mitr-1].num_bytes;
1563 }
1564 thislist[i].start_adr = tmpaddr;
1565 thislist[i].num_bytes = tmpsize;
1566 }
1567}
1568
1569void __init rescan_sp_banks(void)
1570{
1571 struct linux_prom64_registers memlist[64];
1572 struct linux_mlist_p1275 avail[64], *mlist;
1573 unsigned long bytes, base_paddr;
1574 int num_regs, node = prom_finddevice("/memory");
1575 int i;
1576
1577 num_regs = prom_getproperty(node, "available",
1578 (char *) memlist, sizeof(memlist));
1579 num_regs = (num_regs / sizeof(struct linux_prom64_registers));
1580 for (i = 0; i < num_regs; i++) {
1581 avail[i].start_adr = memlist[i].phys_addr;
1582 avail[i].num_bytes = memlist[i].reg_size;
1583 avail[i].theres_more = &avail[i + 1];
1584 }
1585 avail[i - 1].theres_more = NULL;
1586 sort_memlist(avail);
1587
1588 mlist = &avail[0];
1589 i = 0;
1590 bytes = mlist->num_bytes;
1591 base_paddr = mlist->start_adr;
1592
1593 sp_banks[0].base_addr = base_paddr;
1594 sp_banks[0].num_bytes = bytes;
1595
1596 while (mlist->theres_more != NULL){
1597 i++;
1598 mlist = mlist->theres_more;
1599 bytes = mlist->num_bytes;
1600 if (i >= SPARC_PHYS_BANKS-1) {
1601 printk ("The machine has more banks than "
1602 "this kernel can support\n"
1603 "Increase the SPARC_PHYS_BANKS "
1604 "setting (currently %d)\n",
1605 SPARC_PHYS_BANKS);
1606 i = SPARC_PHYS_BANKS-1;
1607 break;
1608 }
1609
1610 sp_banks[i].base_addr = mlist->start_adr;
1611 sp_banks[i].num_bytes = mlist->num_bytes;
1612 }
1613
1614 i++;
1615 sp_banks[i].base_addr = 0xdeadbeefbeefdeadUL;
1616 sp_banks[i].num_bytes = 0;
1617
1618 for (i = 0; sp_banks[i].num_bytes != 0; i++)
1619 sp_banks[i].num_bytes &= PAGE_MASK;
1620}
1621
1622static void __init taint_real_pages(void)
1623{
1624 struct sparc_phys_banks saved_sp_banks[SPARC_PHYS_BANKS];
1625 int i;
1626
1627 for (i = 0; i < SPARC_PHYS_BANKS; i++) {
1628 saved_sp_banks[i].base_addr =
1629 sp_banks[i].base_addr;
1630 saved_sp_banks[i].num_bytes =
1631 sp_banks[i].num_bytes;
1632 }
1633
1634 rescan_sp_banks();
1635
1636 /* Find changes discovered in the sp_bank rescan and
1637 * reserve the lost portions in the bootmem maps.
1638 */
1639 for (i = 0; saved_sp_banks[i].num_bytes; i++) {
1640 unsigned long old_start, old_end;
1641
1642 old_start = saved_sp_banks[i].base_addr;
1643 old_end = old_start +
1644 saved_sp_banks[i].num_bytes;
1645 while (old_start < old_end) {
1646 int n;
1647
1648 for (n = 0; sp_banks[n].num_bytes; n++) {
1649 unsigned long new_start, new_end;
1650
1651 new_start = sp_banks[n].base_addr;
1652 new_end = new_start + sp_banks[n].num_bytes;
1653
1654 if (new_start <= old_start &&
1655 new_end >= (old_start + PAGE_SIZE)) {
1656 set_bit (old_start >> 22,
1657 sparc64_valid_addr_bitmap);
1658 goto do_next_page;
1659 }
1660 }
1661 reserve_bootmem(old_start, PAGE_SIZE);
1662
1663 do_next_page:
1664 old_start += PAGE_SIZE;
1665 }
1666 }
1667}
1668
1669void __init mem_init(void)
1670{
1671 unsigned long codepages, datapages, initpages;
1672 unsigned long addr, last;
1673 int i;
1674
1675 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1676 i += 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001677 sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (sparc64_valid_addr_bitmap == NULL) {
1679 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1680 prom_halt();
1681 }
1682 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1683
1684 addr = PAGE_OFFSET + kern_base;
1685 last = PAGE_ALIGN(kern_size) + addr;
1686 while (addr < last) {
1687 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1688 addr += PAGE_SIZE;
1689 }
1690
1691 taint_real_pages();
1692
1693 max_mapnr = last_valid_pfn - pfn_base;
1694 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1695
1696#ifdef CONFIG_DEBUG_BOOTMEM
1697 prom_printf("mem_init: Calling free_all_bootmem().\n");
1698#endif
1699 totalram_pages = num_physpages = free_all_bootmem() - 1;
1700
1701 /*
1702 * Set up the zero page, mark it reserved, so that page count
1703 * is not manipulated when freeing the page from user ptes.
1704 */
1705 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
1706 if (mem_map_zero == NULL) {
1707 prom_printf("paging_init: Cannot alloc zero page.\n");
1708 prom_halt();
1709 }
1710 SetPageReserved(mem_map_zero);
1711
1712 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1713 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1714 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1715 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1716 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1717 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1718
1719 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1720 nr_free_pages() << (PAGE_SHIFT-10),
1721 codepages << (PAGE_SHIFT-10),
1722 datapages << (PAGE_SHIFT-10),
1723 initpages << (PAGE_SHIFT-10),
1724 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1725
1726 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1727 cheetah_ecache_flush_init();
1728}
1729
1730void free_initmem (void)
1731{
1732 unsigned long addr, initend;
1733
1734 /*
1735 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1736 */
1737 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1738 initend = (unsigned long)(__init_end) & PAGE_MASK;
1739 for (; addr < initend; addr += PAGE_SIZE) {
1740 unsigned long page;
1741 struct page *p;
1742
1743 page = (addr +
1744 ((unsigned long) __va(kern_base)) -
1745 ((unsigned long) KERNBASE));
1746 memset((void *)addr, 0xcc, PAGE_SIZE);
1747 p = virt_to_page(page);
1748
1749 ClearPageReserved(p);
1750 set_page_count(p, 1);
1751 __free_page(p);
1752 num_physpages++;
1753 totalram_pages++;
1754 }
1755}
1756
1757#ifdef CONFIG_BLK_DEV_INITRD
1758void free_initrd_mem(unsigned long start, unsigned long end)
1759{
1760 if (start < end)
1761 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1762 for (; start < end; start += PAGE_SIZE) {
1763 struct page *p = virt_to_page(start);
1764
1765 ClearPageReserved(p);
1766 set_page_count(p, 1);
1767 __free_page(p);
1768 num_physpages++;
1769 totalram_pages++;
1770 }
1771}
1772#endif