blob: 4c95cf34075b7544b7d6755f0b7e8410e30e13a9 [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>
David S. Miller13edad72005-09-29 17:58:26 -070024#include <linux/sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/head.h>
27#include <asm/system.h>
28#include <asm/page.h>
29#include <asm/pgalloc.h>
30#include <asm/pgtable.h>
31#include <asm/oplib.h>
32#include <asm/iommu.h>
33#include <asm/io.h>
34#include <asm/uaccess.h>
35#include <asm/mmu_context.h>
36#include <asm/tlbflush.h>
37#include <asm/dma.h>
38#include <asm/starfire.h>
39#include <asm/tlb.h>
40#include <asm/spitfire.h>
41#include <asm/sections.h>
David S. Miller517af332006-02-01 15:55:21 -080042#include <asm/tsb.h>
David S. Miller481295f2006-02-07 21:51:08 -080043#include <asm/hypervisor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45extern void device_scan(void);
46
David S. Miller13edad72005-09-29 17:58:26 -070047#define MAX_BANKS 32
David S. Miller10147572005-09-28 21:46:43 -070048
David S. Miller13edad72005-09-29 17:58:26 -070049static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
50static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
51static int pavail_ents __initdata;
52static int pavail_rescan_ents __initdata;
David S. Miller10147572005-09-28 21:46:43 -070053
David S. Miller13edad72005-09-29 17:58:26 -070054static int cmp_p64(const void *a, const void *b)
55{
56 const struct linux_prom64_registers *x = a, *y = b;
57
58 if (x->phys_addr > y->phys_addr)
59 return 1;
60 if (x->phys_addr < y->phys_addr)
61 return -1;
62 return 0;
63}
64
65static void __init read_obp_memory(const char *property,
66 struct linux_prom64_registers *regs,
67 int *num_ents)
68{
69 int node = prom_finddevice("/memory");
70 int prop_size = prom_getproplen(node, property);
71 int ents, ret, i;
72
73 ents = prop_size / sizeof(struct linux_prom64_registers);
74 if (ents > MAX_BANKS) {
75 prom_printf("The machine has more %s property entries than "
76 "this kernel can support (%d).\n",
77 property, MAX_BANKS);
78 prom_halt();
79 }
80
81 ret = prom_getproperty(node, property, (char *) regs, prop_size);
82 if (ret == -1) {
83 prom_printf("Couldn't get %s property from /memory.\n");
84 prom_halt();
85 }
86
87 *num_ents = ents;
88
89 /* Sanitize what we got from the firmware, by page aligning
90 * everything.
91 */
92 for (i = 0; i < ents; i++) {
93 unsigned long base, size;
94
95 base = regs[i].phys_addr;
96 size = regs[i].reg_size;
97
98 size &= PAGE_MASK;
99 if (base & ~PAGE_MASK) {
100 unsigned long new_base = PAGE_ALIGN(base);
101
102 size -= new_base - base;
103 if ((long) size < 0L)
104 size = 0UL;
105 base = new_base;
106 }
107 regs[i].phys_addr = base;
108 regs[i].reg_size = size;
109 }
David S. Millerc9c10832005-10-12 12:22:46 -0700110 sort(regs, ents, sizeof(struct linux_prom64_registers),
David S. Miller13edad72005-09-29 17:58:26 -0700111 cmp_p64, NULL);
112}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
David S. Miller2bdb3cb2005-09-22 01:08:57 -0700114unsigned long *sparc64_valid_addr_bitmap __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/* Ugly, but necessary... -DaveM */
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700117unsigned long phys_base __read_mostly;
118unsigned long kern_base __read_mostly;
119unsigned long kern_size __read_mostly;
120unsigned long pfn_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* get_new_mmu_context() uses "cache + 1". */
123DEFINE_SPINLOCK(ctx_alloc_lock);
124unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
125#define CTX_BMAP_SLOTS (1UL << (CTX_NR_BITS - 6))
126unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
127
128/* References to special section boundaries */
129extern char _start[], _end[];
130
131/* Initial ramdisk setup */
132extern unsigned long sparc_ramdisk_image64;
133extern unsigned int sparc_ramdisk_image;
134extern unsigned int sparc_ramdisk_size;
135
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700136struct page *mem_map_zero __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
David S. Miller0835ae02005-10-04 15:23:20 -0700138unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
139
140unsigned long sparc64_kern_pri_context __read_mostly;
141unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
142unsigned long sparc64_kern_sec_context __read_mostly;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144int bigkernel = 0;
145
David S. Miller3c936462006-01-31 18:30:27 -0800146kmem_cache_t *pgtable_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
David S. Miller3c936462006-01-31 18:30:27 -0800148static void zero_ctor(void *addr, kmem_cache_t *cache, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
David S. Miller3c936462006-01-31 18:30:27 -0800150 clear_page(addr);
151}
152
153void pgtable_cache_init(void)
154{
155 pgtable_cache = kmem_cache_create("pgtable_cache",
156 PAGE_SIZE, PAGE_SIZE,
157 SLAB_HWCACHE_ALIGN |
158 SLAB_MUST_HWCACHE_ALIGN,
159 zero_ctor,
160 NULL);
161 if (!pgtable_cache) {
162 prom_printf("pgtable_cache_init(): Could not create!\n");
163 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167#ifdef CONFIG_DEBUG_DCFLUSH
168atomic_t dcpage_flushes = ATOMIC_INIT(0);
169#ifdef CONFIG_SMP
170atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
171#endif
172#endif
173
174__inline__ void flush_dcache_page_impl(struct page *page)
175{
176#ifdef CONFIG_DEBUG_DCFLUSH
177 atomic_inc(&dcpage_flushes);
178#endif
179
180#ifdef DCACHE_ALIASING_POSSIBLE
181 __flush_dcache_page(page_address(page),
182 ((tlb_type == spitfire) &&
183 page_mapping(page) != NULL));
184#else
185 if (page_mapping(page) != NULL &&
186 tlb_type == spitfire)
187 __flush_icache_page(__pa(page_address(page)));
188#endif
189}
190
191#define PG_dcache_dirty PG_arch_1
David S. Miller48b0e542005-07-27 16:08:44 -0700192#define PG_dcache_cpu_shift 24
193#define PG_dcache_cpu_mask (256 - 1)
194
195#if NR_CPUS > 256
196#error D-cache dirty tracking and thread_info->cpu need fixing for > 256 cpus
197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199#define dcache_dirty_cpu(page) \
David S. Miller48b0e542005-07-27 16:08:44 -0700200 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202static __inline__ void set_dcache_dirty(struct page *page, int this_cpu)
203{
204 unsigned long mask = this_cpu;
David S. Miller48b0e542005-07-27 16:08:44 -0700205 unsigned long non_cpu_bits;
206
207 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
208 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 __asm__ __volatile__("1:\n\t"
211 "ldx [%2], %%g7\n\t"
212 "and %%g7, %1, %%g1\n\t"
213 "or %%g1, %0, %%g1\n\t"
214 "casx [%2], %%g7, %%g1\n\t"
215 "cmp %%g7, %%g1\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700216 "membar #StoreLoad | #StoreStore\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700218 " nop"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 : /* no outputs */
220 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
221 : "g1", "g7");
222}
223
224static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
225{
226 unsigned long mask = (1UL << PG_dcache_dirty);
227
228 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
229 "1:\n\t"
230 "ldx [%2], %%g7\n\t"
David S. Miller48b0e542005-07-27 16:08:44 -0700231 "srlx %%g7, %4, %%g1\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 "and %%g1, %3, %%g1\n\t"
233 "cmp %%g1, %0\n\t"
234 "bne,pn %%icc, 2f\n\t"
235 " andn %%g7, %1, %%g1\n\t"
236 "casx [%2], %%g7, %%g1\n\t"
237 "cmp %%g7, %%g1\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700238 "membar #StoreLoad | #StoreStore\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700240 " nop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 "2:"
242 : /* no outputs */
243 : "r" (cpu), "r" (mask), "r" (&page->flags),
David S. Miller48b0e542005-07-27 16:08:44 -0700244 "i" (PG_dcache_cpu_mask),
245 "i" (PG_dcache_cpu_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 : "g1", "g7");
247}
248
David S. Miller517af332006-02-01 15:55:21 -0800249static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
250{
251 unsigned long tsb_addr = (unsigned long) ent;
252
253 if (tlb_type == cheetah_plus)
254 tsb_addr = __pa(tsb_addr);
255
256 __tsb_insert(tsb_addr, tag, pte);
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
260{
David S. Millerbd407912006-01-31 18:31:38 -0800261 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct page *page;
263 unsigned long pfn;
264 unsigned long pg_flags;
265
266 pfn = pte_pfn(pte);
267 if (pfn_valid(pfn) &&
268 (page = pfn_to_page(pfn), page_mapping(page)) &&
269 ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
David S. Miller48b0e542005-07-27 16:08:44 -0700270 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
271 PG_dcache_cpu_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 int this_cpu = get_cpu();
273
274 /* This is just to optimize away some function calls
275 * in the SMP case.
276 */
277 if (cpu == this_cpu)
278 flush_dcache_page_impl(page);
279 else
280 smp_flush_dcache_page_impl(page, cpu);
281
282 clear_dcache_dirty_cpu(page, cpu);
283
284 put_cpu();
285 }
David S. Millerbd407912006-01-31 18:31:38 -0800286
287 mm = vma->vm_mm;
David S. Millerb70c0fa2006-01-31 18:32:04 -0800288 if ((pte_val(pte) & _PAGE_ALL_SZ_BITS) == _PAGE_SZBITS) {
289 struct tsb *tsb;
290 unsigned long tag;
291
292 tsb = &mm->context.tsb[(address >> PAGE_SHIFT) &
293 (mm->context.tsb_nentries - 1UL)];
294 tag = (address >> 22UL) | CTX_HWBITS(mm->context) << 48UL;
295 tsb_insert(tsb, tag, pte_val(pte));
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299void flush_dcache_page(struct page *page)
300{
David S. Millera9546f52005-04-17 18:03:09 -0700301 struct address_space *mapping;
302 int this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
David S. Millera9546f52005-04-17 18:03:09 -0700304 /* Do not bother with the expensive D-cache flush if it
305 * is merely the zero page. The 'bigcore' testcase in GDB
306 * causes this case to run millions of times.
307 */
308 if (page == ZERO_PAGE(0))
309 return;
310
311 this_cpu = get_cpu();
312
313 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (mapping && !mapping_mapped(mapping)) {
David S. Millera9546f52005-04-17 18:03:09 -0700315 int dirty = test_bit(PG_dcache_dirty, &page->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (dirty) {
David S. Millera9546f52005-04-17 18:03:09 -0700317 int dirty_cpu = dcache_dirty_cpu(page);
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (dirty_cpu == this_cpu)
320 goto out;
321 smp_flush_dcache_page_impl(page, dirty_cpu);
322 }
323 set_dcache_dirty(page, this_cpu);
324 } else {
325 /* We could delay the flush for the !page_mapping
326 * case too. But that case is for exec env/arg
327 * pages and those are %99 certainly going to get
328 * faulted into the tlb (and thus flushed) anyways.
329 */
330 flush_dcache_page_impl(page);
331 }
332
333out:
334 put_cpu();
335}
336
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700337void __kprobes flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
David S. Millera43fe0e2006-02-04 03:10:53 -0800339 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (tlb_type == spitfire) {
341 unsigned long kaddr;
342
343 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
344 __flush_icache_page(__get_phys(kaddr));
345 }
346}
347
348unsigned long page_to_pfn(struct page *page)
349{
350 return (unsigned long) ((page - mem_map) + pfn_base);
351}
352
353struct page *pfn_to_page(unsigned long pfn)
354{
355 return (mem_map + (pfn - pfn_base));
356}
357
358void show_mem(void)
359{
360 printk("Mem-info:\n");
361 show_free_areas();
362 printk("Free swap: %6ldkB\n",
363 nr_swap_pages << (PAGE_SHIFT-10));
364 printk("%ld pages of RAM\n", num_physpages);
365 printk("%d free pages\n", nr_free_pages());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368void mmu_info(struct seq_file *m)
369{
370 if (tlb_type == cheetah)
371 seq_printf(m, "MMU Type\t: Cheetah\n");
372 else if (tlb_type == cheetah_plus)
373 seq_printf(m, "MMU Type\t: Cheetah+\n");
374 else if (tlb_type == spitfire)
375 seq_printf(m, "MMU Type\t: Spitfire\n");
David S. Millera43fe0e2006-02-04 03:10:53 -0800376 else if (tlb_type == hypervisor)
377 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 else
379 seq_printf(m, "MMU Type\t: ???\n");
380
381#ifdef CONFIG_DEBUG_DCFLUSH
382 seq_printf(m, "DCPageFlushes\t: %d\n",
383 atomic_read(&dcpage_flushes));
384#ifdef CONFIG_SMP
385 seq_printf(m, "DCPageFlushesXC\t: %d\n",
386 atomic_read(&dcpage_flushes_xcall));
387#endif /* CONFIG_SMP */
388#endif /* CONFIG_DEBUG_DCFLUSH */
389}
390
391struct linux_prom_translation {
392 unsigned long virt;
393 unsigned long size;
394 unsigned long data;
395};
David S. Millerc9c10832005-10-12 12:22:46 -0700396
397/* Exported for kernel TLB miss handling in ktlb.S */
398struct linux_prom_translation prom_trans[512] __read_mostly;
399unsigned int prom_trans_ents __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401extern unsigned long prom_boot_page;
402extern void prom_remap(unsigned long physpage, unsigned long virtpage, int mmu_ihandle);
403extern int prom_get_mmu_ihandle(void);
404extern void register_prom_callbacks(void);
405
406/* Exported for SMP bootup purposes. */
407unsigned long kern_locked_tte_data;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/*
410 * Translate PROM's mapping we capture at boot time into physical address.
411 * The second parameter is only set from prom_callback() invocations.
412 */
413unsigned long prom_virt_to_phys(unsigned long promva, int *error)
414{
David S. Millerc9c10832005-10-12 12:22:46 -0700415 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David S. Millerc9c10832005-10-12 12:22:46 -0700417 for (i = 0; i < prom_trans_ents; i++) {
418 struct linux_prom_translation *p = &prom_trans[i];
David S. Miller9ad98c52005-10-05 15:12:00 -0700419
David S. Millerc9c10832005-10-12 12:22:46 -0700420 if (promva >= p->virt &&
421 promva < (p->virt + p->size)) {
422 unsigned long base = p->data & _PAGE_PADDR;
423
424 if (error)
425 *error = 0;
426 return base + (promva & (8192 - 1));
427 }
428 }
429 if (error)
430 *error = 1;
431 return 0UL;
David S. Miller405599b2005-09-22 00:12:35 -0700432}
433
434/* The obp translations are saved based on 8k pagesize, since obp can
435 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
David S. Miller74bf4312006-01-31 18:29:18 -0800436 * HI_OBP_ADDRESS range are handled in ktlb.S.
David S. Miller405599b2005-09-22 00:12:35 -0700437 */
David S. Miller5085b4a2005-09-22 00:45:41 -0700438static inline int in_obp_range(unsigned long vaddr)
439{
440 return (vaddr >= LOW_OBP_ADDRESS &&
441 vaddr < HI_OBP_ADDRESS);
442}
443
David S. Millerc9c10832005-10-12 12:22:46 -0700444static int cmp_ptrans(const void *a, const void *b)
David S. Miller405599b2005-09-22 00:12:35 -0700445{
David S. Millerc9c10832005-10-12 12:22:46 -0700446 const struct linux_prom_translation *x = a, *y = b;
David S. Miller405599b2005-09-22 00:12:35 -0700447
David S. Millerc9c10832005-10-12 12:22:46 -0700448 if (x->virt > y->virt)
449 return 1;
450 if (x->virt < y->virt)
451 return -1;
452 return 0;
David S. Miller405599b2005-09-22 00:12:35 -0700453}
454
David S. Millerc9c10832005-10-12 12:22:46 -0700455/* Read OBP translations property into 'prom_trans[]'. */
David S. Miller9ad98c52005-10-05 15:12:00 -0700456static void __init read_obp_translations(void)
David S. Miller405599b2005-09-22 00:12:35 -0700457{
David S. Millerc9c10832005-10-12 12:22:46 -0700458 int n, node, ents, first, last, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 node = prom_finddevice("/virtual-memory");
461 n = prom_getproplen(node, "translations");
David S. Miller405599b2005-09-22 00:12:35 -0700462 if (unlikely(n == 0 || n == -1)) {
David S. Millerb206fc42005-09-21 22:31:13 -0700463 prom_printf("prom_mappings: Couldn't get size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 prom_halt();
465 }
David S. Miller405599b2005-09-22 00:12:35 -0700466 if (unlikely(n > sizeof(prom_trans))) {
467 prom_printf("prom_mappings: Size %Zd is too big.\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 prom_halt();
469 }
David S. Miller405599b2005-09-22 00:12:35 -0700470
David S. Millerb206fc42005-09-21 22:31:13 -0700471 if ((n = prom_getproperty(node, "translations",
David S. Miller405599b2005-09-22 00:12:35 -0700472 (char *)&prom_trans[0],
473 sizeof(prom_trans))) == -1) {
David S. Millerb206fc42005-09-21 22:31:13 -0700474 prom_printf("prom_mappings: Couldn't get property.\n");
475 prom_halt();
476 }
David S. Miller9ad98c52005-10-05 15:12:00 -0700477
David S. Millerb206fc42005-09-21 22:31:13 -0700478 n = n / sizeof(struct linux_prom_translation);
David S. Miller9ad98c52005-10-05 15:12:00 -0700479
David S. Millerc9c10832005-10-12 12:22:46 -0700480 ents = n;
481
482 sort(prom_trans, ents, sizeof(struct linux_prom_translation),
483 cmp_ptrans, NULL);
484
485 /* Now kick out all the non-OBP entries. */
486 for (i = 0; i < ents; i++) {
487 if (in_obp_range(prom_trans[i].virt))
488 break;
489 }
490 first = i;
491 for (; i < ents; i++) {
492 if (!in_obp_range(prom_trans[i].virt))
493 break;
494 }
495 last = i;
496
497 for (i = 0; i < (last - first); i++) {
498 struct linux_prom_translation *src = &prom_trans[i + first];
499 struct linux_prom_translation *dest = &prom_trans[i];
500
501 *dest = *src;
502 }
503 for (; i < ents; i++) {
504 struct linux_prom_translation *dest = &prom_trans[i];
505 dest->virt = dest->size = dest->data = 0x0UL;
506 }
507
508 prom_trans_ents = last - first;
509
510 if (tlb_type == spitfire) {
511 /* Clear diag TTE bits. */
512 for (i = 0; i < prom_trans_ents; i++)
513 prom_trans[i].data &= ~0x0003fe0000000000UL;
514 }
David S. Miller405599b2005-09-22 00:12:35 -0700515}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
David S. Miller898cf0e2005-09-23 11:59:44 -0700517static void __init remap_kernel(void)
David S. Miller405599b2005-09-22 00:12:35 -0700518{
519 unsigned long phys_page, tte_vaddr, tte_data;
David S. Miller405599b2005-09-22 00:12:35 -0700520 int tlb_ent = sparc64_highest_locked_tlbent();
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 tte_vaddr = (unsigned long) KERNBASE;
David S. Millerbff06d52005-09-22 20:11:33 -0700523 phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
524 tte_data = (phys_page | (_PAGE_VALID | _PAGE_SZ4MB |
525 _PAGE_CP | _PAGE_CV | _PAGE_P |
526 _PAGE_L | _PAGE_W));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 kern_locked_tte_data = tte_data;
529
David S. Millerbff06d52005-09-22 20:11:33 -0700530 /* Now lock us into the TLBs via OBP. */
David S. Miller405599b2005-09-22 00:12:35 -0700531 prom_dtlb_load(tlb_ent, tte_data, tte_vaddr);
532 prom_itlb_load(tlb_ent, tte_data, tte_vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (bigkernel) {
David S. Miller0835ae02005-10-04 15:23:20 -0700534 tlb_ent -= 1;
535 prom_dtlb_load(tlb_ent,
David S. Miller405599b2005-09-22 00:12:35 -0700536 tte_data + 0x400000,
537 tte_vaddr + 0x400000);
David S. Miller0835ae02005-10-04 15:23:20 -0700538 prom_itlb_load(tlb_ent,
David S. Miller405599b2005-09-22 00:12:35 -0700539 tte_data + 0x400000,
540 tte_vaddr + 0x400000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
David S. Miller0835ae02005-10-04 15:23:20 -0700542 sparc64_highest_unlocked_tlb_ent = tlb_ent - 1;
543 if (tlb_type == cheetah_plus) {
544 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
545 CTX_CHEETAH_PLUS_NUC);
546 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
547 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
548 }
David S. Miller405599b2005-09-22 00:12:35 -0700549}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
David S. Miller405599b2005-09-22 00:12:35 -0700551
David S. Millerc9c10832005-10-12 12:22:46 -0700552static void __init inherit_prom_mappings(void)
David S. Miller9ad98c52005-10-05 15:12:00 -0700553{
554 read_obp_translations();
David S. Miller405599b2005-09-22 00:12:35 -0700555
556 /* Now fixup OBP's idea about where we really are mapped. */
557 prom_printf("Remapping the kernel... ");
558 remap_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 prom_printf("done.\n");
560
David S. Millerc9c10832005-10-12 12:22:46 -0700561 prom_printf("Registering callbacks... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 register_prom_callbacks();
David S. Millerc9c10832005-10-12 12:22:46 -0700563 prom_printf("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566void prom_world(int enter)
567{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!enter)
569 set_fs((mm_segment_t) { get_thread_current_ds() });
570
David S. Miller3487d1d2006-01-31 18:33:25 -0800571 __asm__ __volatile__("flushw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574#ifdef DCACHE_ALIASING_POSSIBLE
575void __flush_dcache_range(unsigned long start, unsigned long end)
576{
577 unsigned long va;
578
579 if (tlb_type == spitfire) {
580 int n = 0;
581
582 for (va = start; va < end; va += 32) {
583 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
584 if (++n >= 512)
585 break;
586 }
David S. Millera43fe0e2006-02-04 03:10:53 -0800587 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 start = __pa(start);
589 end = __pa(end);
590 for (va = start; va < end; va += 32)
591 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
592 "membar #Sync"
593 : /* no outputs */
594 : "r" (va),
595 "i" (ASI_DCACHE_INVALIDATE));
596 }
597}
598#endif /* DCACHE_ALIASING_POSSIBLE */
599
600/* If not locked, zap it. */
601void __flush_tlb_all(void)
602{
603 unsigned long pstate;
604 int i;
605
606 __asm__ __volatile__("flushw\n\t"
607 "rdpr %%pstate, %0\n\t"
608 "wrpr %0, %1, %%pstate"
609 : "=r" (pstate)
610 : "i" (PSTATE_IE));
611 if (tlb_type == spitfire) {
612 for (i = 0; i < 64; i++) {
613 /* Spitfire Errata #32 workaround */
614 /* NOTE: Always runs on spitfire, so no
615 * cheetah+ page size encodings.
616 */
617 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
618 "flush %%g6"
619 : /* No outputs */
620 : "r" (0),
621 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
622
623 if (!(spitfire_get_dtlb_data(i) & _PAGE_L)) {
624 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
625 "membar #Sync"
626 : /* no outputs */
627 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
628 spitfire_put_dtlb_data(i, 0x0UL);
629 }
630
631 /* Spitfire Errata #32 workaround */
632 /* NOTE: Always runs on spitfire, so no
633 * cheetah+ page size encodings.
634 */
635 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
636 "flush %%g6"
637 : /* No outputs */
638 : "r" (0),
639 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
640
641 if (!(spitfire_get_itlb_data(i) & _PAGE_L)) {
642 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
643 "membar #Sync"
644 : /* no outputs */
645 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
646 spitfire_put_itlb_data(i, 0x0UL);
647 }
648 }
649 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
650 cheetah_flush_dtlb_all();
651 cheetah_flush_itlb_all();
652 }
653 __asm__ __volatile__("wrpr %0, 0, %%pstate"
654 : : "r" (pstate));
655}
656
657/* Caller does TLB context flushing on local CPU if necessary.
658 * The caller also ensures that CTX_VALID(mm->context) is false.
659 *
660 * We must be careful about boundary cases so that we never
661 * let the user have CTX 0 (nucleus) or we ever use a CTX
662 * version of zero (and thus NO_CONTEXT would not be caught
663 * by version mis-match tests in mmu_context.h).
664 */
665void get_new_mmu_context(struct mm_struct *mm)
666{
667 unsigned long ctx, new_ctx;
668 unsigned long orig_pgsz_bits;
669
670
671 spin_lock(&ctx_alloc_lock);
672 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
673 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
674 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
675 if (new_ctx >= (1 << CTX_NR_BITS)) {
676 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
677 if (new_ctx >= ctx) {
678 int i;
679 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
680 CTX_FIRST_VERSION;
681 if (new_ctx == 1)
682 new_ctx = CTX_FIRST_VERSION;
683
684 /* Don't call memset, for 16 entries that's just
685 * plain silly...
686 */
687 mmu_context_bmap[0] = 3;
688 mmu_context_bmap[1] = 0;
689 mmu_context_bmap[2] = 0;
690 mmu_context_bmap[3] = 0;
691 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
692 mmu_context_bmap[i + 0] = 0;
693 mmu_context_bmap[i + 1] = 0;
694 mmu_context_bmap[i + 2] = 0;
695 mmu_context_bmap[i + 3] = 0;
696 }
697 goto out;
698 }
699 }
700 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
701 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
702out:
703 tlb_context_cache = new_ctx;
704 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
705 spin_unlock(&ctx_alloc_lock);
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708void sparc_ultra_dump_itlb(void)
709{
710 int slot;
711
712 if (tlb_type == spitfire) {
713 printk ("Contents of itlb: ");
714 for (slot = 0; slot < 14; slot++) printk (" ");
715 printk ("%2x:%016lx,%016lx\n",
716 0,
717 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
718 for (slot = 1; slot < 64; slot+=3) {
719 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
720 slot,
721 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
722 slot+1,
723 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
724 slot+2,
725 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
726 }
727 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
728 printk ("Contents of itlb0:\n");
729 for (slot = 0; slot < 16; slot+=2) {
730 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
731 slot,
732 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
733 slot+1,
734 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
735 }
736 printk ("Contents of itlb2:\n");
737 for (slot = 0; slot < 128; slot+=2) {
738 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
739 slot,
740 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
741 slot+1,
742 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
743 }
744 }
745}
746
747void sparc_ultra_dump_dtlb(void)
748{
749 int slot;
750
751 if (tlb_type == spitfire) {
752 printk ("Contents of dtlb: ");
753 for (slot = 0; slot < 14; slot++) printk (" ");
754 printk ("%2x:%016lx,%016lx\n", 0,
755 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
756 for (slot = 1; slot < 64; slot+=3) {
757 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
758 slot,
759 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
760 slot+1,
761 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
762 slot+2,
763 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
764 }
765 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
766 printk ("Contents of dtlb0:\n");
767 for (slot = 0; slot < 16; slot+=2) {
768 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
769 slot,
770 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
771 slot+1,
772 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
773 }
774 printk ("Contents of dtlb2:\n");
775 for (slot = 0; slot < 512; slot+=2) {
776 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
777 slot,
778 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
779 slot+1,
780 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
781 }
782 if (tlb_type == cheetah_plus) {
783 printk ("Contents of dtlb3:\n");
784 for (slot = 0; slot < 512; slot+=2) {
785 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
786 slot,
787 cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
788 slot+1,
789 cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
790 }
791 }
792 }
793}
794
David S. Miller3487d1d2006-01-31 18:33:25 -0800795static inline void spitfire_errata32(void)
796{
797 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
798 "flush %%g6"
799 : /* No outputs */
800 : "r" (0),
801 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
802}
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804extern unsigned long cmdline_memory_size;
805
806unsigned long __init bootmem_init(unsigned long *pages_avail)
807{
808 unsigned long bootmap_size, start_pfn, end_pfn;
809 unsigned long end_of_phys_memory = 0UL;
810 unsigned long bootmap_pfn, bytes_avail, size;
811 int i;
812
813#ifdef CONFIG_DEBUG_BOOTMEM
David S. Miller13edad72005-09-29 17:58:26 -0700814 prom_printf("bootmem_init: Scan pavail, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815#endif
816
817 bytes_avail = 0UL;
David S. Miller13edad72005-09-29 17:58:26 -0700818 for (i = 0; i < pavail_ents; i++) {
819 end_of_phys_memory = pavail[i].phys_addr +
820 pavail[i].reg_size;
821 bytes_avail += pavail[i].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (cmdline_memory_size) {
823 if (bytes_avail > cmdline_memory_size) {
824 unsigned long slack = bytes_avail - cmdline_memory_size;
825
826 bytes_avail -= slack;
827 end_of_phys_memory -= slack;
828
David S. Miller13edad72005-09-29 17:58:26 -0700829 pavail[i].reg_size -= slack;
830 if ((long)pavail[i].reg_size <= 0L) {
831 pavail[i].phys_addr = 0xdeadbeefUL;
832 pavail[i].reg_size = 0UL;
833 pavail_ents = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 } else {
David S. Miller13edad72005-09-29 17:58:26 -0700835 pavail[i+1].reg_size = 0Ul;
836 pavail[i+1].phys_addr = 0xdeadbeefUL;
837 pavail_ents = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839 break;
840 }
841 }
842 }
843
844 *pages_avail = bytes_avail >> PAGE_SHIFT;
845
846 /* Start with page aligned address of last symbol in kernel
847 * image. The kernel is hard mapped below PAGE_OFFSET in a
848 * 4MB locked TLB translation.
849 */
850 start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
851
852 bootmap_pfn = start_pfn;
853
854 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
855
856#ifdef CONFIG_BLK_DEV_INITRD
857 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
858 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
859 unsigned long ramdisk_image = sparc_ramdisk_image ?
860 sparc_ramdisk_image : sparc_ramdisk_image64;
861 if (ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
862 ramdisk_image -= KERNBASE;
863 initrd_start = ramdisk_image + phys_base;
864 initrd_end = initrd_start + sparc_ramdisk_size;
865 if (initrd_end > end_of_phys_memory) {
866 printk(KERN_CRIT "initrd extends beyond end of memory "
867 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
868 initrd_end, end_of_phys_memory);
869 initrd_start = 0;
870 }
871 if (initrd_start) {
872 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
873 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
874 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
875 }
876 }
877#endif
878 /* Initialize the boot-time allocator. */
879 max_pfn = max_low_pfn = end_pfn;
880 min_low_pfn = pfn_base;
881
882#ifdef CONFIG_DEBUG_BOOTMEM
883 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
884 min_low_pfn, bootmap_pfn, max_low_pfn);
885#endif
886 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base, end_pfn);
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /* Now register the available physical memory with the
889 * allocator.
890 */
David S. Miller13edad72005-09-29 17:58:26 -0700891 for (i = 0; i < pavail_ents; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892#ifdef CONFIG_DEBUG_BOOTMEM
David S. Miller13edad72005-09-29 17:58:26 -0700893 prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
894 i, pavail[i].phys_addr, pavail[i].reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895#endif
David S. Miller13edad72005-09-29 17:58:26 -0700896 free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
899#ifdef CONFIG_BLK_DEV_INITRD
900 if (initrd_start) {
901 size = initrd_end - initrd_start;
902
903 /* Resert the initrd image area. */
904#ifdef CONFIG_DEBUG_BOOTMEM
905 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
906 initrd_start, initrd_end);
907#endif
908 reserve_bootmem(initrd_start, size);
909 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
910
911 initrd_start += PAGE_OFFSET;
912 initrd_end += PAGE_OFFSET;
913 }
914#endif
915 /* Reserve the kernel text/data/bss. */
916#ifdef CONFIG_DEBUG_BOOTMEM
917 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
918#endif
919 reserve_bootmem(kern_base, kern_size);
920 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
921
922 /* Reserve the bootmem map. We do not account for it
923 * in pages_avail because we will release that memory
924 * in free_all_bootmem.
925 */
926 size = bootmap_size;
927#ifdef CONFIG_DEBUG_BOOTMEM
928 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
929 (bootmap_pfn << PAGE_SHIFT), size);
930#endif
931 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
932 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
933
934 return end_pfn;
935}
936
David S. Miller56425302005-09-25 16:46:57 -0700937#ifdef CONFIG_DEBUG_PAGEALLOC
938static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend, pgprot_t prot)
939{
940 unsigned long vstart = PAGE_OFFSET + pstart;
941 unsigned long vend = PAGE_OFFSET + pend;
942 unsigned long alloc_bytes = 0UL;
943
944 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
David S. Miller13edad72005-09-29 17:58:26 -0700945 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
David S. Miller56425302005-09-25 16:46:57 -0700946 vstart, vend);
947 prom_halt();
948 }
949
950 while (vstart < vend) {
951 unsigned long this_end, paddr = __pa(vstart);
952 pgd_t *pgd = pgd_offset_k(vstart);
953 pud_t *pud;
954 pmd_t *pmd;
955 pte_t *pte;
956
957 pud = pud_offset(pgd, vstart);
958 if (pud_none(*pud)) {
959 pmd_t *new;
960
961 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
962 alloc_bytes += PAGE_SIZE;
963 pud_populate(&init_mm, pud, new);
964 }
965
966 pmd = pmd_offset(pud, vstart);
967 if (!pmd_present(*pmd)) {
968 pte_t *new;
969
970 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
971 alloc_bytes += PAGE_SIZE;
972 pmd_populate_kernel(&init_mm, pmd, new);
973 }
974
975 pte = pte_offset_kernel(pmd, vstart);
976 this_end = (vstart + PMD_SIZE) & PMD_MASK;
977 if (this_end > vend)
978 this_end = vend;
979
980 while (vstart < this_end) {
981 pte_val(*pte) = (paddr | pgprot_val(prot));
982
983 vstart += PAGE_SIZE;
984 paddr += PAGE_SIZE;
985 pte++;
986 }
987 }
988
989 return alloc_bytes;
990}
991
David S. Miller13edad72005-09-29 17:58:26 -0700992static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
993static int pall_ents __initdata;
994
David S. Miller56425302005-09-25 16:46:57 -0700995extern unsigned int kvmap_linear_patch[1];
996
997static void __init kernel_physical_mapping_init(void)
998{
David S. Miller13edad72005-09-29 17:58:26 -0700999 unsigned long i, mem_alloced = 0UL;
David S. Miller56425302005-09-25 16:46:57 -07001000
David S. Miller13edad72005-09-29 17:58:26 -07001001 read_obp_memory("reg", &pall[0], &pall_ents);
1002
1003 for (i = 0; i < pall_ents; i++) {
David S. Miller56425302005-09-25 16:46:57 -07001004 unsigned long phys_start, phys_end;
1005
David S. Miller13edad72005-09-29 17:58:26 -07001006 phys_start = pall[i].phys_addr;
1007 phys_end = phys_start + pall[i].reg_size;
David S. Miller56425302005-09-25 16:46:57 -07001008 mem_alloced += kernel_map_range(phys_start, phys_end,
1009 PAGE_KERNEL);
David S. Miller56425302005-09-25 16:46:57 -07001010 }
1011
1012 printk("Allocated %ld bytes for kernel page tables.\n",
1013 mem_alloced);
1014
1015 kvmap_linear_patch[0] = 0x01000000; /* nop */
1016 flushi(&kvmap_linear_patch[0]);
1017
1018 __flush_tlb_all();
1019}
1020
1021void kernel_map_pages(struct page *page, int numpages, int enable)
1022{
1023 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1024 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1025
1026 kernel_map_range(phys_start, phys_end,
1027 (enable ? PAGE_KERNEL : __pgprot(0)));
1028
David S. Miller74bf4312006-01-31 18:29:18 -08001029 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1030 PAGE_OFFSET + phys_end);
1031
David S. Miller56425302005-09-25 16:46:57 -07001032 /* we should perform an IPI and flush all tlbs,
1033 * but that can deadlock->flush only current cpu.
1034 */
1035 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1036 PAGE_OFFSET + phys_end);
1037}
1038#endif
1039
David S. Miller10147572005-09-28 21:46:43 -07001040unsigned long __init find_ecache_flush_span(unsigned long size)
1041{
David S. Miller13edad72005-09-29 17:58:26 -07001042 int i;
David S. Miller10147572005-09-28 21:46:43 -07001043
David S. Miller13edad72005-09-29 17:58:26 -07001044 for (i = 0; i < pavail_ents; i++) {
1045 if (pavail[i].reg_size >= size)
1046 return pavail[i].phys_addr;
David S. Miller10147572005-09-28 21:46:43 -07001047 }
1048
1049 return ~0UL;
1050}
1051
David S. Miller517af332006-02-01 15:55:21 -08001052static void __init tsb_phys_patch(void)
1053{
David S. Millerd257d5d2006-02-06 23:44:37 -08001054 struct tsb_ldquad_phys_patch_entry *pquad;
David S. Miller517af332006-02-01 15:55:21 -08001055 struct tsb_phys_patch_entry *p;
1056
David S. Millerd257d5d2006-02-06 23:44:37 -08001057 pquad = &__tsb_ldquad_phys_patch;
1058 while (pquad < &__tsb_ldquad_phys_patch_end) {
1059 unsigned long addr = pquad->addr;
1060
1061 if (tlb_type == hypervisor)
1062 *(unsigned int *) addr = pquad->sun4v_insn;
1063 else
1064 *(unsigned int *) addr = pquad->sun4u_insn;
1065 wmb();
1066 __asm__ __volatile__("flush %0"
1067 : /* no outputs */
1068 : "r" (addr));
1069
1070 pquad++;
1071 }
1072
David S. Miller517af332006-02-01 15:55:21 -08001073 p = &__tsb_phys_patch;
1074 while (p < &__tsb_phys_patch_end) {
1075 unsigned long addr = p->addr;
1076
1077 *(unsigned int *) addr = p->insn;
1078 wmb();
1079 __asm__ __volatile__("flush %0"
1080 : /* no outputs */
1081 : "r" (addr));
1082
1083 p++;
1084 }
1085}
1086
David S. Miller481295f2006-02-07 21:51:08 -08001087/* Register this cpu's fault status area with the hypervisor. */
1088void __cpuinit sun4v_register_fault_status(void)
1089{
1090 register unsigned long arg0 asm("%o0");
1091 register unsigned long arg1 asm("%o1");
1092 int cpu = hard_smp_processor_id();
1093 struct trap_per_cpu *tb = &trap_block[cpu];
1094 unsigned long pa;
1095
1096 pa = kern_base + ((unsigned long) tb - KERNBASE);
1097 arg0 = HV_FAST_MMU_FAULT_AREA_CONF;
1098 arg1 = pa;
1099 __asm__ __volatile__("ta %4"
1100 : "=&r" (arg0), "=&r" (arg1)
1101 : "0" (arg0), "1" (arg1),
1102 "i" (HV_FAST_TRAP));
1103}
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105/* paging_init() sets up the page tables */
1106
1107extern void cheetah_ecache_flush_init(void);
David S. Millerd257d5d2006-02-06 23:44:37 -08001108extern void sun4v_patch_tlb_handlers(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110static unsigned long last_valid_pfn;
David S. Miller56425302005-09-25 16:46:57 -07001111pgd_t swapper_pg_dir[2048];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113void __init paging_init(void)
1114{
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001115 unsigned long end_pfn, pages_avail, shift;
David S. Miller0836a0e2005-09-28 21:38:08 -07001116 unsigned long real_end, i;
1117
David S. Miller481295f2006-02-07 21:51:08 -08001118 kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
1119 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
1120
David S. Millerd257d5d2006-02-06 23:44:37 -08001121 if (tlb_type == cheetah_plus ||
1122 tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -08001123 tsb_phys_patch();
1124
David S. Miller481295f2006-02-07 21:51:08 -08001125 if (tlb_type == hypervisor) {
David S. Millerd257d5d2006-02-06 23:44:37 -08001126 sun4v_patch_tlb_handlers();
David S. Miller481295f2006-02-07 21:51:08 -08001127 sun4v_register_fault_status();
1128 }
David S. Millerd257d5d2006-02-06 23:44:37 -08001129
David S. Miller13edad72005-09-29 17:58:26 -07001130 /* Find available physical memory... */
1131 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller0836a0e2005-09-28 21:38:08 -07001132
1133 phys_base = 0xffffffffffffffffUL;
David S. Miller13edad72005-09-29 17:58:26 -07001134 for (i = 0; i < pavail_ents; i++)
1135 phys_base = min(phys_base, pavail[i].phys_addr);
David S. Miller0836a0e2005-09-28 21:38:08 -07001136
David S. Miller0836a0e2005-09-28 21:38:08 -07001137 pfn_base = phys_base >> PAGE_SHIFT;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 set_bit(0, mmu_context_bmap);
1140
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001141 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 real_end = (unsigned long)_end;
1144 if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1145 bigkernel = 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001146 if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
1147 prom_printf("paging_init: Kernel > 8MB, too large.\n");
1148 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001150
1151 /* Set kernel pgd to upper alias so physical page computations
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 * work.
1153 */
1154 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1155
David S. Miller56425302005-09-25 16:46:57 -07001156 memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /* Now can init the kernel/bad page tables. */
1159 pud_set(pud_offset(&swapper_pg_dir[0], 0),
David S. Miller56425302005-09-25 16:46:57 -07001160 swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
David S. Millerc9c10832005-10-12 12:22:46 -07001162 inherit_prom_mappings();
David S. Miller5085b4a2005-09-22 00:45:41 -07001163
David S. Millera8b900d2006-01-31 18:33:37 -08001164 /* Ok, we can use our TLB miss and window trap handlers safely. */
1165 setup_tba();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
David S. Millerc9c10832005-10-12 12:22:46 -07001167 __flush_tlb_all();
David S. Miller9ad98c52005-10-05 15:12:00 -07001168
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001169 /* Setup bootmem... */
1170 pages_avail = 0;
1171 last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
1172
David S. Miller56425302005-09-25 16:46:57 -07001173#ifdef CONFIG_DEBUG_PAGEALLOC
1174 kernel_physical_mapping_init();
1175#endif
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 {
1178 unsigned long zones_size[MAX_NR_ZONES];
1179 unsigned long zholes_size[MAX_NR_ZONES];
1180 unsigned long npages;
1181 int znum;
1182
1183 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1184 zones_size[znum] = zholes_size[znum] = 0;
1185
1186 npages = end_pfn - pfn_base;
1187 zones_size[ZONE_DMA] = npages;
1188 zholes_size[ZONE_DMA] = npages - pages_avail;
1189
1190 free_area_init_node(0, &contig_page_data, zones_size,
1191 phys_base >> PAGE_SHIFT, zholes_size);
1192 }
1193
1194 device_scan();
1195}
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197static void __init taint_real_pages(void)
1198{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 int i;
1200
David S. Miller13edad72005-09-29 17:58:26 -07001201 read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
David S. Miller13edad72005-09-29 17:58:26 -07001203 /* Find changes discovered in the physmem available rescan and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 * reserve the lost portions in the bootmem maps.
1205 */
David S. Miller13edad72005-09-29 17:58:26 -07001206 for (i = 0; i < pavail_ents; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 unsigned long old_start, old_end;
1208
David S. Miller13edad72005-09-29 17:58:26 -07001209 old_start = pavail[i].phys_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 old_end = old_start +
David S. Miller13edad72005-09-29 17:58:26 -07001211 pavail[i].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 while (old_start < old_end) {
1213 int n;
1214
David S. Miller13edad72005-09-29 17:58:26 -07001215 for (n = 0; pavail_rescan_ents; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 unsigned long new_start, new_end;
1217
David S. Miller13edad72005-09-29 17:58:26 -07001218 new_start = pavail_rescan[n].phys_addr;
1219 new_end = new_start +
1220 pavail_rescan[n].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 if (new_start <= old_start &&
1223 new_end >= (old_start + PAGE_SIZE)) {
David S. Miller13edad72005-09-29 17:58:26 -07001224 set_bit(old_start >> 22,
1225 sparc64_valid_addr_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 goto do_next_page;
1227 }
1228 }
1229 reserve_bootmem(old_start, PAGE_SIZE);
1230
1231 do_next_page:
1232 old_start += PAGE_SIZE;
1233 }
1234 }
1235}
1236
1237void __init mem_init(void)
1238{
1239 unsigned long codepages, datapages, initpages;
1240 unsigned long addr, last;
1241 int i;
1242
1243 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1244 i += 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001245 sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (sparc64_valid_addr_bitmap == NULL) {
1247 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1248 prom_halt();
1249 }
1250 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1251
1252 addr = PAGE_OFFSET + kern_base;
1253 last = PAGE_ALIGN(kern_size) + addr;
1254 while (addr < last) {
1255 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1256 addr += PAGE_SIZE;
1257 }
1258
1259 taint_real_pages();
1260
1261 max_mapnr = last_valid_pfn - pfn_base;
1262 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1263
1264#ifdef CONFIG_DEBUG_BOOTMEM
1265 prom_printf("mem_init: Calling free_all_bootmem().\n");
1266#endif
1267 totalram_pages = num_physpages = free_all_bootmem() - 1;
1268
1269 /*
1270 * Set up the zero page, mark it reserved, so that page count
1271 * is not manipulated when freeing the page from user ptes.
1272 */
1273 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
1274 if (mem_map_zero == NULL) {
1275 prom_printf("paging_init: Cannot alloc zero page.\n");
1276 prom_halt();
1277 }
1278 SetPageReserved(mem_map_zero);
1279
1280 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1281 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1282 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1283 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1284 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1285 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1286
1287 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1288 nr_free_pages() << (PAGE_SHIFT-10),
1289 codepages << (PAGE_SHIFT-10),
1290 datapages << (PAGE_SHIFT-10),
1291 initpages << (PAGE_SHIFT-10),
1292 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1293
1294 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1295 cheetah_ecache_flush_init();
1296}
1297
David S. Miller898cf0e2005-09-23 11:59:44 -07001298void free_initmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 unsigned long addr, initend;
1301
1302 /*
1303 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1304 */
1305 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1306 initend = (unsigned long)(__init_end) & PAGE_MASK;
1307 for (; addr < initend; addr += PAGE_SIZE) {
1308 unsigned long page;
1309 struct page *p;
1310
1311 page = (addr +
1312 ((unsigned long) __va(kern_base)) -
1313 ((unsigned long) KERNBASE));
1314 memset((void *)addr, 0xcc, PAGE_SIZE);
1315 p = virt_to_page(page);
1316
1317 ClearPageReserved(p);
1318 set_page_count(p, 1);
1319 __free_page(p);
1320 num_physpages++;
1321 totalram_pages++;
1322 }
1323}
1324
1325#ifdef CONFIG_BLK_DEV_INITRD
1326void free_initrd_mem(unsigned long start, unsigned long end)
1327{
1328 if (start < end)
1329 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1330 for (; start < end; start += PAGE_SIZE) {
1331 struct page *p = virt_to_page(start);
1332
1333 ClearPageReserved(p);
1334 set_page_count(p, 1);
1335 __free_page(p);
1336 num_physpages++;
1337 totalram_pages++;
1338 }
1339}
1340#endif