blob: ccf083aecb65359dc455126f4938279cf7bb8ff2 [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>
David S. Millerc4bce902006-02-11 21:57:54 -08009#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/string.h>
13#include <linux/init.h>
14#include <linux/bootmem.h>
15#include <linux/mm.h>
16#include <linux/hugetlb.h>
17#include <linux/slab.h>
18#include <linux/initrd.h>
19#include <linux/swap.h>
20#include <linux/pagemap.h>
21#include <linux/fs.h>
22#include <linux/seq_file.h>
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070023#include <linux/kprobes.h>
David S. Miller1ac4f5e2005-09-21 21:49:32 -070024#include <linux/cache.h>
David S. Miller13edad72005-09-29 17:58:26 -070025#include <linux/sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/head.h>
28#include <asm/system.h>
29#include <asm/page.h>
30#include <asm/pgalloc.h>
31#include <asm/pgtable.h>
32#include <asm/oplib.h>
33#include <asm/iommu.h>
34#include <asm/io.h>
35#include <asm/uaccess.h>
36#include <asm/mmu_context.h>
37#include <asm/tlbflush.h>
38#include <asm/dma.h>
39#include <asm/starfire.h>
40#include <asm/tlb.h>
41#include <asm/spitfire.h>
42#include <asm/sections.h>
David S. Miller517af332006-02-01 15:55:21 -080043#include <asm/tsb.h>
David S. Miller481295f2006-02-07 21:51:08 -080044#include <asm/hypervisor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46extern void device_scan(void);
47
David S. Miller9cc3a1a2006-02-21 20:51:13 -080048#define MAX_PHYS_ADDRESS (1UL << 42UL)
49#define KPTE_BITMAP_CHUNK_SZ (256UL * 1024UL * 1024UL)
50#define KPTE_BITMAP_BYTES \
51 ((MAX_PHYS_ADDRESS / KPTE_BITMAP_CHUNK_SZ) / 8)
52
53unsigned long kern_linear_pte_xor[2] __read_mostly;
54
55/* A bitmap, one bit for every 256MB of physical memory. If the bit
56 * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
57 * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
58 */
59unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
60
David S. Millerd7744a02006-02-21 22:31:11 -080061/* A special kernel TSB for 4MB and 256MB linear mappings. */
62struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
63
David S. Miller13edad72005-09-29 17:58:26 -070064#define MAX_BANKS 32
David S. Miller10147572005-09-28 21:46:43 -070065
David S. Miller13edad72005-09-29 17:58:26 -070066static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
67static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
68static int pavail_ents __initdata;
69static int pavail_rescan_ents __initdata;
David S. Miller10147572005-09-28 21:46:43 -070070
David S. Miller13edad72005-09-29 17:58:26 -070071static int cmp_p64(const void *a, const void *b)
72{
73 const struct linux_prom64_registers *x = a, *y = b;
74
75 if (x->phys_addr > y->phys_addr)
76 return 1;
77 if (x->phys_addr < y->phys_addr)
78 return -1;
79 return 0;
80}
81
82static void __init read_obp_memory(const char *property,
83 struct linux_prom64_registers *regs,
84 int *num_ents)
85{
86 int node = prom_finddevice("/memory");
87 int prop_size = prom_getproplen(node, property);
88 int ents, ret, i;
89
90 ents = prop_size / sizeof(struct linux_prom64_registers);
91 if (ents > MAX_BANKS) {
92 prom_printf("The machine has more %s property entries than "
93 "this kernel can support (%d).\n",
94 property, MAX_BANKS);
95 prom_halt();
96 }
97
98 ret = prom_getproperty(node, property, (char *) regs, prop_size);
99 if (ret == -1) {
100 prom_printf("Couldn't get %s property from /memory.\n");
101 prom_halt();
102 }
103
104 *num_ents = ents;
105
106 /* Sanitize what we got from the firmware, by page aligning
107 * everything.
108 */
109 for (i = 0; i < ents; i++) {
110 unsigned long base, size;
111
112 base = regs[i].phys_addr;
113 size = regs[i].reg_size;
114
115 size &= PAGE_MASK;
116 if (base & ~PAGE_MASK) {
117 unsigned long new_base = PAGE_ALIGN(base);
118
119 size -= new_base - base;
120 if ((long) size < 0L)
121 size = 0UL;
122 base = new_base;
123 }
124 regs[i].phys_addr = base;
125 regs[i].reg_size = size;
126 }
David S. Millerc9c10832005-10-12 12:22:46 -0700127 sort(regs, ents, sizeof(struct linux_prom64_registers),
David S. Miller13edad72005-09-29 17:58:26 -0700128 cmp_p64, NULL);
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
David S. Miller2bdb3cb2005-09-22 01:08:57 -0700131unsigned long *sparc64_valid_addr_bitmap __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/* Ugly, but necessary... -DaveM */
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700134unsigned long phys_base __read_mostly;
135unsigned long kern_base __read_mostly;
136unsigned long kern_size __read_mostly;
137unsigned long pfn_base __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/* get_new_mmu_context() uses "cache + 1". */
140DEFINE_SPINLOCK(ctx_alloc_lock);
141unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
142#define CTX_BMAP_SLOTS (1UL << (CTX_NR_BITS - 6))
143unsigned long mmu_context_bmap[CTX_BMAP_SLOTS];
144
145/* References to special section boundaries */
146extern char _start[], _end[];
147
148/* Initial ramdisk setup */
149extern unsigned long sparc_ramdisk_image64;
150extern unsigned int sparc_ramdisk_image;
151extern unsigned int sparc_ramdisk_size;
152
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700153struct page *mem_map_zero __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
David S. Miller0835ae02005-10-04 15:23:20 -0700155unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
156
157unsigned long sparc64_kern_pri_context __read_mostly;
158unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
159unsigned long sparc64_kern_sec_context __read_mostly;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161int bigkernel = 0;
162
David S. Miller3c936462006-01-31 18:30:27 -0800163kmem_cache_t *pgtable_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
David S. Miller3c936462006-01-31 18:30:27 -0800165static void zero_ctor(void *addr, kmem_cache_t *cache, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
David S. Miller3c936462006-01-31 18:30:27 -0800167 clear_page(addr);
168}
169
170void pgtable_cache_init(void)
171{
172 pgtable_cache = kmem_cache_create("pgtable_cache",
173 PAGE_SIZE, PAGE_SIZE,
174 SLAB_HWCACHE_ALIGN |
175 SLAB_MUST_HWCACHE_ALIGN,
176 zero_ctor,
177 NULL);
178 if (!pgtable_cache) {
179 prom_printf("pgtable_cache_init(): Could not create!\n");
180 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184#ifdef CONFIG_DEBUG_DCFLUSH
185atomic_t dcpage_flushes = ATOMIC_INIT(0);
186#ifdef CONFIG_SMP
187atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
188#endif
189#endif
190
191__inline__ void flush_dcache_page_impl(struct page *page)
192{
193#ifdef CONFIG_DEBUG_DCFLUSH
194 atomic_inc(&dcpage_flushes);
195#endif
196
197#ifdef DCACHE_ALIASING_POSSIBLE
198 __flush_dcache_page(page_address(page),
199 ((tlb_type == spitfire) &&
200 page_mapping(page) != NULL));
201#else
202 if (page_mapping(page) != NULL &&
203 tlb_type == spitfire)
204 __flush_icache_page(__pa(page_address(page)));
205#endif
206}
207
208#define PG_dcache_dirty PG_arch_1
David S. Miller48b0e542005-07-27 16:08:44 -0700209#define PG_dcache_cpu_shift 24
210#define PG_dcache_cpu_mask (256 - 1)
211
212#if NR_CPUS > 256
213#error D-cache dirty tracking and thread_info->cpu need fixing for > 256 cpus
214#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216#define dcache_dirty_cpu(page) \
David S. Miller48b0e542005-07-27 16:08:44 -0700217 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219static __inline__ void set_dcache_dirty(struct page *page, int this_cpu)
220{
221 unsigned long mask = this_cpu;
David S. Miller48b0e542005-07-27 16:08:44 -0700222 unsigned long non_cpu_bits;
223
224 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
225 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 __asm__ __volatile__("1:\n\t"
228 "ldx [%2], %%g7\n\t"
229 "and %%g7, %1, %%g1\n\t"
230 "or %%g1, %0, %%g1\n\t"
231 "casx [%2], %%g7, %%g1\n\t"
232 "cmp %%g7, %%g1\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700233 "membar #StoreLoad | #StoreStore\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700235 " nop"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 : /* no outputs */
237 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
238 : "g1", "g7");
239}
240
241static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
242{
243 unsigned long mask = (1UL << PG_dcache_dirty);
244
245 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
246 "1:\n\t"
247 "ldx [%2], %%g7\n\t"
David S. Miller48b0e542005-07-27 16:08:44 -0700248 "srlx %%g7, %4, %%g1\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 "and %%g1, %3, %%g1\n\t"
250 "cmp %%g1, %0\n\t"
251 "bne,pn %%icc, 2f\n\t"
252 " andn %%g7, %1, %%g1\n\t"
253 "casx [%2], %%g7, %%g1\n\t"
254 "cmp %%g7, %%g1\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700255 "membar #StoreLoad | #StoreStore\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700257 " nop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 "2:"
259 : /* no outputs */
260 : "r" (cpu), "r" (mask), "r" (&page->flags),
David S. Miller48b0e542005-07-27 16:08:44 -0700261 "i" (PG_dcache_cpu_mask),
262 "i" (PG_dcache_cpu_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 : "g1", "g7");
264}
265
David S. Miller517af332006-02-01 15:55:21 -0800266static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
267{
268 unsigned long tsb_addr = (unsigned long) ent;
269
David S. Miller3b3ab2e2006-02-17 09:54:42 -0800270 if (tlb_type == cheetah_plus || tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -0800271 tsb_addr = __pa(tsb_addr);
272
273 __tsb_insert(tsb_addr, tag, pte);
274}
275
David S. Millerc4bce902006-02-11 21:57:54 -0800276unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
277unsigned long _PAGE_SZBITS __read_mostly;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
280{
David S. Millerbd407912006-01-31 18:31:38 -0800281 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct page *page;
283 unsigned long pfn;
284 unsigned long pg_flags;
285
286 pfn = pte_pfn(pte);
287 if (pfn_valid(pfn) &&
288 (page = pfn_to_page(pfn), page_mapping(page)) &&
289 ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
David S. Miller48b0e542005-07-27 16:08:44 -0700290 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
291 PG_dcache_cpu_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int this_cpu = get_cpu();
293
294 /* This is just to optimize away some function calls
295 * in the SMP case.
296 */
297 if (cpu == this_cpu)
298 flush_dcache_page_impl(page);
299 else
300 smp_flush_dcache_page_impl(page, cpu);
301
302 clear_dcache_dirty_cpu(page, cpu);
303
304 put_cpu();
305 }
David S. Millerbd407912006-01-31 18:31:38 -0800306
307 mm = vma->vm_mm;
David S. Millerb70c0fa2006-01-31 18:32:04 -0800308 if ((pte_val(pte) & _PAGE_ALL_SZ_BITS) == _PAGE_SZBITS) {
309 struct tsb *tsb;
310 unsigned long tag;
311
312 tsb = &mm->context.tsb[(address >> PAGE_SHIFT) &
313 (mm->context.tsb_nentries - 1UL)];
David S. Miller8b234272006-02-17 18:01:02 -0800314 tag = (address >> 22UL);
David S. Millerb70c0fa2006-01-31 18:32:04 -0800315 tsb_insert(tsb, tag, pte_val(pte));
316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319void flush_dcache_page(struct page *page)
320{
David S. Millera9546f52005-04-17 18:03:09 -0700321 struct address_space *mapping;
322 int this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
David S. Millera9546f52005-04-17 18:03:09 -0700324 /* Do not bother with the expensive D-cache flush if it
325 * is merely the zero page. The 'bigcore' testcase in GDB
326 * causes this case to run millions of times.
327 */
328 if (page == ZERO_PAGE(0))
329 return;
330
331 this_cpu = get_cpu();
332
333 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (mapping && !mapping_mapped(mapping)) {
David S. Millera9546f52005-04-17 18:03:09 -0700335 int dirty = test_bit(PG_dcache_dirty, &page->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (dirty) {
David S. Millera9546f52005-04-17 18:03:09 -0700337 int dirty_cpu = dcache_dirty_cpu(page);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (dirty_cpu == this_cpu)
340 goto out;
341 smp_flush_dcache_page_impl(page, dirty_cpu);
342 }
343 set_dcache_dirty(page, this_cpu);
344 } else {
345 /* We could delay the flush for the !page_mapping
346 * case too. But that case is for exec env/arg
347 * pages and those are %99 certainly going to get
348 * faulted into the tlb (and thus flushed) anyways.
349 */
350 flush_dcache_page_impl(page);
351 }
352
353out:
354 put_cpu();
355}
356
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700357void __kprobes flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
David S. Millera43fe0e2006-02-04 03:10:53 -0800359 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (tlb_type == spitfire) {
361 unsigned long kaddr;
362
363 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE)
364 __flush_icache_page(__get_phys(kaddr));
365 }
366}
367
368unsigned long page_to_pfn(struct page *page)
369{
370 return (unsigned long) ((page - mem_map) + pfn_base);
371}
372
373struct page *pfn_to_page(unsigned long pfn)
374{
375 return (mem_map + (pfn - pfn_base));
376}
377
378void show_mem(void)
379{
380 printk("Mem-info:\n");
381 show_free_areas();
382 printk("Free swap: %6ldkB\n",
383 nr_swap_pages << (PAGE_SHIFT-10));
384 printk("%ld pages of RAM\n", num_physpages);
385 printk("%d free pages\n", nr_free_pages());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388void mmu_info(struct seq_file *m)
389{
390 if (tlb_type == cheetah)
391 seq_printf(m, "MMU Type\t: Cheetah\n");
392 else if (tlb_type == cheetah_plus)
393 seq_printf(m, "MMU Type\t: Cheetah+\n");
394 else if (tlb_type == spitfire)
395 seq_printf(m, "MMU Type\t: Spitfire\n");
David S. Millera43fe0e2006-02-04 03:10:53 -0800396 else if (tlb_type == hypervisor)
397 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 else
399 seq_printf(m, "MMU Type\t: ???\n");
400
401#ifdef CONFIG_DEBUG_DCFLUSH
402 seq_printf(m, "DCPageFlushes\t: %d\n",
403 atomic_read(&dcpage_flushes));
404#ifdef CONFIG_SMP
405 seq_printf(m, "DCPageFlushesXC\t: %d\n",
406 atomic_read(&dcpage_flushes_xcall));
407#endif /* CONFIG_SMP */
408#endif /* CONFIG_DEBUG_DCFLUSH */
409}
410
411struct linux_prom_translation {
412 unsigned long virt;
413 unsigned long size;
414 unsigned long data;
415};
David S. Millerc9c10832005-10-12 12:22:46 -0700416
417/* Exported for kernel TLB miss handling in ktlb.S */
418struct linux_prom_translation prom_trans[512] __read_mostly;
419unsigned int prom_trans_ents __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/* Exported for SMP bootup purposes. */
422unsigned long kern_locked_tte_data;
423
David S. Miller405599b2005-09-22 00:12:35 -0700424/* The obp translations are saved based on 8k pagesize, since obp can
425 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
David S. Miller74bf4312006-01-31 18:29:18 -0800426 * HI_OBP_ADDRESS range are handled in ktlb.S.
David S. Miller405599b2005-09-22 00:12:35 -0700427 */
David S. Miller5085b4a2005-09-22 00:45:41 -0700428static inline int in_obp_range(unsigned long vaddr)
429{
430 return (vaddr >= LOW_OBP_ADDRESS &&
431 vaddr < HI_OBP_ADDRESS);
432}
433
David S. Millerc9c10832005-10-12 12:22:46 -0700434static int cmp_ptrans(const void *a, const void *b)
David S. Miller405599b2005-09-22 00:12:35 -0700435{
David S. Millerc9c10832005-10-12 12:22:46 -0700436 const struct linux_prom_translation *x = a, *y = b;
David S. Miller405599b2005-09-22 00:12:35 -0700437
David S. Millerc9c10832005-10-12 12:22:46 -0700438 if (x->virt > y->virt)
439 return 1;
440 if (x->virt < y->virt)
441 return -1;
442 return 0;
David S. Miller405599b2005-09-22 00:12:35 -0700443}
444
David S. Millerc9c10832005-10-12 12:22:46 -0700445/* Read OBP translations property into 'prom_trans[]'. */
David S. Miller9ad98c52005-10-05 15:12:00 -0700446static void __init read_obp_translations(void)
David S. Miller405599b2005-09-22 00:12:35 -0700447{
David S. Millerc9c10832005-10-12 12:22:46 -0700448 int n, node, ents, first, last, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 node = prom_finddevice("/virtual-memory");
451 n = prom_getproplen(node, "translations");
David S. Miller405599b2005-09-22 00:12:35 -0700452 if (unlikely(n == 0 || n == -1)) {
David S. Millerb206fc42005-09-21 22:31:13 -0700453 prom_printf("prom_mappings: Couldn't get size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 prom_halt();
455 }
David S. Miller405599b2005-09-22 00:12:35 -0700456 if (unlikely(n > sizeof(prom_trans))) {
457 prom_printf("prom_mappings: Size %Zd is too big.\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 prom_halt();
459 }
David S. Miller405599b2005-09-22 00:12:35 -0700460
David S. Millerb206fc42005-09-21 22:31:13 -0700461 if ((n = prom_getproperty(node, "translations",
David S. Miller405599b2005-09-22 00:12:35 -0700462 (char *)&prom_trans[0],
463 sizeof(prom_trans))) == -1) {
David S. Millerb206fc42005-09-21 22:31:13 -0700464 prom_printf("prom_mappings: Couldn't get property.\n");
465 prom_halt();
466 }
David S. Miller9ad98c52005-10-05 15:12:00 -0700467
David S. Millerb206fc42005-09-21 22:31:13 -0700468 n = n / sizeof(struct linux_prom_translation);
David S. Miller9ad98c52005-10-05 15:12:00 -0700469
David S. Millerc9c10832005-10-12 12:22:46 -0700470 ents = n;
471
472 sort(prom_trans, ents, sizeof(struct linux_prom_translation),
473 cmp_ptrans, NULL);
474
475 /* Now kick out all the non-OBP entries. */
476 for (i = 0; i < ents; i++) {
477 if (in_obp_range(prom_trans[i].virt))
478 break;
479 }
480 first = i;
481 for (; i < ents; i++) {
482 if (!in_obp_range(prom_trans[i].virt))
483 break;
484 }
485 last = i;
486
487 for (i = 0; i < (last - first); i++) {
488 struct linux_prom_translation *src = &prom_trans[i + first];
489 struct linux_prom_translation *dest = &prom_trans[i];
490
491 *dest = *src;
492 }
493 for (; i < ents; i++) {
494 struct linux_prom_translation *dest = &prom_trans[i];
495 dest->virt = dest->size = dest->data = 0x0UL;
496 }
497
498 prom_trans_ents = last - first;
499
500 if (tlb_type == spitfire) {
501 /* Clear diag TTE bits. */
502 for (i = 0; i < prom_trans_ents; i++)
503 prom_trans[i].data &= ~0x0003fe0000000000UL;
504 }
David S. Miller405599b2005-09-22 00:12:35 -0700505}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
David S. Millerd82ace72006-02-09 02:52:44 -0800507static void __init hypervisor_tlb_lock(unsigned long vaddr,
508 unsigned long pte,
509 unsigned long mmu)
510{
David S. Miller164c2202006-02-09 22:57:21 -0800511 register unsigned long func asm("%o5");
512 register unsigned long arg0 asm("%o0");
513 register unsigned long arg1 asm("%o1");
514 register unsigned long arg2 asm("%o2");
515 register unsigned long arg3 asm("%o3");
David S. Millerd82ace72006-02-09 02:52:44 -0800516
517 func = HV_FAST_MMU_MAP_PERM_ADDR;
518 arg0 = vaddr;
519 arg1 = 0;
520 arg2 = pte;
521 arg3 = mmu;
522 __asm__ __volatile__("ta 0x80"
523 : "=&r" (func), "=&r" (arg0),
524 "=&r" (arg1), "=&r" (arg2),
525 "=&r" (arg3)
526 : "0" (func), "1" (arg0), "2" (arg1),
527 "3" (arg2), "4" (arg3));
David S. Miller12e126a2006-02-17 14:40:30 -0800528 if (arg0 != 0) {
529 prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
530 "errors with %lx\n", vaddr, 0, pte, mmu, arg0);
531 prom_halt();
532 }
David S. Millerd82ace72006-02-09 02:52:44 -0800533}
534
David S. Millerc4bce902006-02-11 21:57:54 -0800535static unsigned long kern_large_tte(unsigned long paddr);
536
David S. Miller898cf0e2005-09-23 11:59:44 -0700537static void __init remap_kernel(void)
David S. Miller405599b2005-09-22 00:12:35 -0700538{
539 unsigned long phys_page, tte_vaddr, tte_data;
David S. Miller405599b2005-09-22 00:12:35 -0700540 int tlb_ent = sparc64_highest_locked_tlbent();
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 tte_vaddr = (unsigned long) KERNBASE;
David S. Millerbff06d52005-09-22 20:11:33 -0700543 phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
David S. Millerc4bce902006-02-11 21:57:54 -0800544 tte_data = kern_large_tte(phys_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 kern_locked_tte_data = tte_data;
547
David S. Millerd82ace72006-02-09 02:52:44 -0800548 /* Now lock us into the TLBs via Hypervisor or OBP. */
549 if (tlb_type == hypervisor) {
550 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
551 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
552 if (bigkernel) {
553 tte_vaddr += 0x400000;
554 tte_data += 0x400000;
555 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
556 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
557 }
558 } else {
559 prom_dtlb_load(tlb_ent, tte_data, tte_vaddr);
560 prom_itlb_load(tlb_ent, tte_data, tte_vaddr);
561 if (bigkernel) {
562 tlb_ent -= 1;
563 prom_dtlb_load(tlb_ent,
564 tte_data + 0x400000,
565 tte_vaddr + 0x400000);
566 prom_itlb_load(tlb_ent,
567 tte_data + 0x400000,
568 tte_vaddr + 0x400000);
569 }
570 sparc64_highest_unlocked_tlb_ent = tlb_ent - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
David S. Miller0835ae02005-10-04 15:23:20 -0700572 if (tlb_type == cheetah_plus) {
573 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
574 CTX_CHEETAH_PLUS_NUC);
575 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
576 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
577 }
David S. Miller405599b2005-09-22 00:12:35 -0700578}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
David S. Miller405599b2005-09-22 00:12:35 -0700580
David S. Millerc9c10832005-10-12 12:22:46 -0700581static void __init inherit_prom_mappings(void)
David S. Miller9ad98c52005-10-05 15:12:00 -0700582{
583 read_obp_translations();
David S. Miller405599b2005-09-22 00:12:35 -0700584
585 /* Now fixup OBP's idea about where we really are mapped. */
586 prom_printf("Remapping the kernel... ");
587 remap_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 prom_printf("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591void prom_world(int enter)
592{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!enter)
594 set_fs((mm_segment_t) { get_thread_current_ds() });
595
David S. Miller3487d1d2006-01-31 18:33:25 -0800596 __asm__ __volatile__("flushw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599#ifdef DCACHE_ALIASING_POSSIBLE
600void __flush_dcache_range(unsigned long start, unsigned long end)
601{
602 unsigned long va;
603
604 if (tlb_type == spitfire) {
605 int n = 0;
606
607 for (va = start; va < end; va += 32) {
608 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
609 if (++n >= 512)
610 break;
611 }
David S. Millera43fe0e2006-02-04 03:10:53 -0800612 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 start = __pa(start);
614 end = __pa(end);
615 for (va = start; va < end; va += 32)
616 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
617 "membar #Sync"
618 : /* no outputs */
619 : "r" (va),
620 "i" (ASI_DCACHE_INVALIDATE));
621 }
622}
623#endif /* DCACHE_ALIASING_POSSIBLE */
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/* Caller does TLB context flushing on local CPU if necessary.
626 * The caller also ensures that CTX_VALID(mm->context) is false.
627 *
628 * We must be careful about boundary cases so that we never
629 * let the user have CTX 0 (nucleus) or we ever use a CTX
630 * version of zero (and thus NO_CONTEXT would not be caught
631 * by version mis-match tests in mmu_context.h).
David S. Millera0663a72006-02-23 14:19:28 -0800632 *
633 * Always invoked with interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
635void get_new_mmu_context(struct mm_struct *mm)
636{
637 unsigned long ctx, new_ctx;
638 unsigned long orig_pgsz_bits;
David S. Millera0663a72006-02-23 14:19:28 -0800639 int new_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 spin_lock(&ctx_alloc_lock);
642 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
643 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
644 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
David S. Millera0663a72006-02-23 14:19:28 -0800645 new_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (new_ctx >= (1 << CTX_NR_BITS)) {
647 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
648 if (new_ctx >= ctx) {
649 int i;
650 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
651 CTX_FIRST_VERSION;
652 if (new_ctx == 1)
653 new_ctx = CTX_FIRST_VERSION;
654
655 /* Don't call memset, for 16 entries that's just
656 * plain silly...
657 */
658 mmu_context_bmap[0] = 3;
659 mmu_context_bmap[1] = 0;
660 mmu_context_bmap[2] = 0;
661 mmu_context_bmap[3] = 0;
662 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
663 mmu_context_bmap[i + 0] = 0;
664 mmu_context_bmap[i + 1] = 0;
665 mmu_context_bmap[i + 2] = 0;
666 mmu_context_bmap[i + 3] = 0;
667 }
David S. Millera0663a72006-02-23 14:19:28 -0800668 new_version = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto out;
670 }
671 }
672 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
673 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
674out:
675 tlb_context_cache = new_ctx;
676 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
677 spin_unlock(&ctx_alloc_lock);
David S. Millera0663a72006-02-23 14:19:28 -0800678
679 if (unlikely(new_version))
680 smp_new_mmu_context_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683void sparc_ultra_dump_itlb(void)
684{
685 int slot;
686
687 if (tlb_type == spitfire) {
688 printk ("Contents of itlb: ");
689 for (slot = 0; slot < 14; slot++) printk (" ");
690 printk ("%2x:%016lx,%016lx\n",
691 0,
692 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
693 for (slot = 1; slot < 64; slot+=3) {
694 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
695 slot,
696 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
697 slot+1,
698 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
699 slot+2,
700 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
701 }
702 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
703 printk ("Contents of itlb0:\n");
704 for (slot = 0; slot < 16; slot+=2) {
705 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
706 slot,
707 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
708 slot+1,
709 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
710 }
711 printk ("Contents of itlb2:\n");
712 for (slot = 0; slot < 128; slot+=2) {
713 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
714 slot,
715 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
716 slot+1,
717 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
718 }
719 }
720}
721
722void sparc_ultra_dump_dtlb(void)
723{
724 int slot;
725
726 if (tlb_type == spitfire) {
727 printk ("Contents of dtlb: ");
728 for (slot = 0; slot < 14; slot++) printk (" ");
729 printk ("%2x:%016lx,%016lx\n", 0,
730 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
731 for (slot = 1; slot < 64; slot+=3) {
732 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
733 slot,
734 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
735 slot+1,
736 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
737 slot+2,
738 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
739 }
740 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
741 printk ("Contents of dtlb0:\n");
742 for (slot = 0; slot < 16; slot+=2) {
743 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
744 slot,
745 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
746 slot+1,
747 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
748 }
749 printk ("Contents of dtlb2:\n");
750 for (slot = 0; slot < 512; slot+=2) {
751 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
752 slot,
753 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
754 slot+1,
755 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
756 }
757 if (tlb_type == cheetah_plus) {
758 printk ("Contents of dtlb3:\n");
759 for (slot = 0; slot < 512; slot+=2) {
760 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
761 slot,
762 cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
763 slot+1,
764 cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
765 }
766 }
767 }
768}
769
770extern unsigned long cmdline_memory_size;
771
772unsigned long __init bootmem_init(unsigned long *pages_avail)
773{
774 unsigned long bootmap_size, start_pfn, end_pfn;
775 unsigned long end_of_phys_memory = 0UL;
776 unsigned long bootmap_pfn, bytes_avail, size;
777 int i;
778
779#ifdef CONFIG_DEBUG_BOOTMEM
David S. Miller13edad72005-09-29 17:58:26 -0700780 prom_printf("bootmem_init: Scan pavail, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781#endif
782
783 bytes_avail = 0UL;
David S. Miller13edad72005-09-29 17:58:26 -0700784 for (i = 0; i < pavail_ents; i++) {
785 end_of_phys_memory = pavail[i].phys_addr +
786 pavail[i].reg_size;
787 bytes_avail += pavail[i].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (cmdline_memory_size) {
789 if (bytes_avail > cmdline_memory_size) {
790 unsigned long slack = bytes_avail - cmdline_memory_size;
791
792 bytes_avail -= slack;
793 end_of_phys_memory -= slack;
794
David S. Miller13edad72005-09-29 17:58:26 -0700795 pavail[i].reg_size -= slack;
796 if ((long)pavail[i].reg_size <= 0L) {
797 pavail[i].phys_addr = 0xdeadbeefUL;
798 pavail[i].reg_size = 0UL;
799 pavail_ents = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 } else {
David S. Miller13edad72005-09-29 17:58:26 -0700801 pavail[i+1].reg_size = 0Ul;
802 pavail[i+1].phys_addr = 0xdeadbeefUL;
803 pavail_ents = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 break;
806 }
807 }
808 }
809
810 *pages_avail = bytes_avail >> PAGE_SHIFT;
811
812 /* Start with page aligned address of last symbol in kernel
813 * image. The kernel is hard mapped below PAGE_OFFSET in a
814 * 4MB locked TLB translation.
815 */
816 start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
817
818 bootmap_pfn = start_pfn;
819
820 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
821
822#ifdef CONFIG_BLK_DEV_INITRD
823 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
824 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
825 unsigned long ramdisk_image = sparc_ramdisk_image ?
826 sparc_ramdisk_image : sparc_ramdisk_image64;
827 if (ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
828 ramdisk_image -= KERNBASE;
829 initrd_start = ramdisk_image + phys_base;
830 initrd_end = initrd_start + sparc_ramdisk_size;
831 if (initrd_end > end_of_phys_memory) {
832 printk(KERN_CRIT "initrd extends beyond end of memory "
833 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
834 initrd_end, end_of_phys_memory);
835 initrd_start = 0;
836 }
837 if (initrd_start) {
838 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
839 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
840 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
841 }
842 }
843#endif
844 /* Initialize the boot-time allocator. */
845 max_pfn = max_low_pfn = end_pfn;
846 min_low_pfn = pfn_base;
847
848#ifdef CONFIG_DEBUG_BOOTMEM
849 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
850 min_low_pfn, bootmap_pfn, max_low_pfn);
851#endif
852 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base, end_pfn);
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* Now register the available physical memory with the
855 * allocator.
856 */
David S. Miller13edad72005-09-29 17:58:26 -0700857 for (i = 0; i < pavail_ents; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858#ifdef CONFIG_DEBUG_BOOTMEM
David S. Miller13edad72005-09-29 17:58:26 -0700859 prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
860 i, pavail[i].phys_addr, pavail[i].reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861#endif
David S. Miller13edad72005-09-29 17:58:26 -0700862 free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
865#ifdef CONFIG_BLK_DEV_INITRD
866 if (initrd_start) {
867 size = initrd_end - initrd_start;
868
869 /* Resert the initrd image area. */
870#ifdef CONFIG_DEBUG_BOOTMEM
871 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
872 initrd_start, initrd_end);
873#endif
874 reserve_bootmem(initrd_start, size);
875 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
876
877 initrd_start += PAGE_OFFSET;
878 initrd_end += PAGE_OFFSET;
879 }
880#endif
881 /* Reserve the kernel text/data/bss. */
882#ifdef CONFIG_DEBUG_BOOTMEM
883 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
884#endif
885 reserve_bootmem(kern_base, kern_size);
886 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
887
888 /* Reserve the bootmem map. We do not account for it
889 * in pages_avail because we will release that memory
890 * in free_all_bootmem.
891 */
892 size = bootmap_size;
893#ifdef CONFIG_DEBUG_BOOTMEM
894 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
895 (bootmap_pfn << PAGE_SHIFT), size);
896#endif
897 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
898 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
899
900 return end_pfn;
901}
902
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800903static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
904static int pall_ents __initdata;
905
David S. Miller56425302005-09-25 16:46:57 -0700906#ifdef CONFIG_DEBUG_PAGEALLOC
907static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend, pgprot_t prot)
908{
909 unsigned long vstart = PAGE_OFFSET + pstart;
910 unsigned long vend = PAGE_OFFSET + pend;
911 unsigned long alloc_bytes = 0UL;
912
913 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
David S. Miller13edad72005-09-29 17:58:26 -0700914 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
David S. Miller56425302005-09-25 16:46:57 -0700915 vstart, vend);
916 prom_halt();
917 }
918
919 while (vstart < vend) {
920 unsigned long this_end, paddr = __pa(vstart);
921 pgd_t *pgd = pgd_offset_k(vstart);
922 pud_t *pud;
923 pmd_t *pmd;
924 pte_t *pte;
925
926 pud = pud_offset(pgd, vstart);
927 if (pud_none(*pud)) {
928 pmd_t *new;
929
930 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
931 alloc_bytes += PAGE_SIZE;
932 pud_populate(&init_mm, pud, new);
933 }
934
935 pmd = pmd_offset(pud, vstart);
936 if (!pmd_present(*pmd)) {
937 pte_t *new;
938
939 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
940 alloc_bytes += PAGE_SIZE;
941 pmd_populate_kernel(&init_mm, pmd, new);
942 }
943
944 pte = pte_offset_kernel(pmd, vstart);
945 this_end = (vstart + PMD_SIZE) & PMD_MASK;
946 if (this_end > vend)
947 this_end = vend;
948
949 while (vstart < this_end) {
950 pte_val(*pte) = (paddr | pgprot_val(prot));
951
952 vstart += PAGE_SIZE;
953 paddr += PAGE_SIZE;
954 pte++;
955 }
956 }
957
958 return alloc_bytes;
959}
960
David S. Miller56425302005-09-25 16:46:57 -0700961extern unsigned int kvmap_linear_patch[1];
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800962#endif /* CONFIG_DEBUG_PAGEALLOC */
963
964static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
965{
966 const unsigned long shift_256MB = 28;
967 const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
968 const unsigned long size_256MB = (1UL << shift_256MB);
969
970 while (start < end) {
971 long remains;
972
973 if (start & mask_256MB) {
974 start = (start + size_256MB) & ~mask_256MB;
975 continue;
976 }
977
978 remains = end - start;
979 while (remains >= size_256MB) {
980 unsigned long index = start >> shift_256MB;
981
982 __set_bit(index, kpte_linear_bitmap);
983
984 start += size_256MB;
985 remains -= size_256MB;
986 }
987 }
988}
David S. Miller56425302005-09-25 16:46:57 -0700989
990static void __init kernel_physical_mapping_init(void)
991{
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800992 unsigned long i;
993#ifdef CONFIG_DEBUG_PAGEALLOC
994 unsigned long mem_alloced = 0UL;
995#endif
David S. Miller56425302005-09-25 16:46:57 -0700996
David S. Miller13edad72005-09-29 17:58:26 -0700997 read_obp_memory("reg", &pall[0], &pall_ents);
998
999 for (i = 0; i < pall_ents; i++) {
David S. Miller56425302005-09-25 16:46:57 -07001000 unsigned long phys_start, phys_end;
1001
David S. Miller13edad72005-09-29 17:58:26 -07001002 phys_start = pall[i].phys_addr;
1003 phys_end = phys_start + pall[i].reg_size;
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001004
1005 mark_kpte_bitmap(phys_start, phys_end);
1006
1007#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller56425302005-09-25 16:46:57 -07001008 mem_alloced += kernel_map_range(phys_start, phys_end,
1009 PAGE_KERNEL);
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001010#endif
David S. Miller56425302005-09-25 16:46:57 -07001011 }
1012
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001013#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller56425302005-09-25 16:46:57 -07001014 printk("Allocated %ld bytes for kernel page tables.\n",
1015 mem_alloced);
1016
1017 kvmap_linear_patch[0] = 0x01000000; /* nop */
1018 flushi(&kvmap_linear_patch[0]);
1019
1020 __flush_tlb_all();
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001021#endif
David S. Miller56425302005-09-25 16:46:57 -07001022}
1023
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001024#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller56425302005-09-25 16:46:57 -07001025void kernel_map_pages(struct page *page, int numpages, int enable)
1026{
1027 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1028 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1029
1030 kernel_map_range(phys_start, phys_end,
1031 (enable ? PAGE_KERNEL : __pgprot(0)));
1032
David S. Miller74bf4312006-01-31 18:29:18 -08001033 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1034 PAGE_OFFSET + phys_end);
1035
David S. Miller56425302005-09-25 16:46:57 -07001036 /* we should perform an IPI and flush all tlbs,
1037 * but that can deadlock->flush only current cpu.
1038 */
1039 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1040 PAGE_OFFSET + phys_end);
1041}
1042#endif
1043
David S. Miller10147572005-09-28 21:46:43 -07001044unsigned long __init find_ecache_flush_span(unsigned long size)
1045{
David S. Miller13edad72005-09-29 17:58:26 -07001046 int i;
David S. Miller10147572005-09-28 21:46:43 -07001047
David S. Miller13edad72005-09-29 17:58:26 -07001048 for (i = 0; i < pavail_ents; i++) {
1049 if (pavail[i].reg_size >= size)
1050 return pavail[i].phys_addr;
David S. Miller10147572005-09-28 21:46:43 -07001051 }
1052
1053 return ~0UL;
1054}
1055
David S. Miller517af332006-02-01 15:55:21 -08001056static void __init tsb_phys_patch(void)
1057{
David S. Millerd257d5d2006-02-06 23:44:37 -08001058 struct tsb_ldquad_phys_patch_entry *pquad;
David S. Miller517af332006-02-01 15:55:21 -08001059 struct tsb_phys_patch_entry *p;
1060
David S. Millerd257d5d2006-02-06 23:44:37 -08001061 pquad = &__tsb_ldquad_phys_patch;
1062 while (pquad < &__tsb_ldquad_phys_patch_end) {
1063 unsigned long addr = pquad->addr;
1064
1065 if (tlb_type == hypervisor)
1066 *(unsigned int *) addr = pquad->sun4v_insn;
1067 else
1068 *(unsigned int *) addr = pquad->sun4u_insn;
1069 wmb();
1070 __asm__ __volatile__("flush %0"
1071 : /* no outputs */
1072 : "r" (addr));
1073
1074 pquad++;
1075 }
1076
David S. Miller517af332006-02-01 15:55:21 -08001077 p = &__tsb_phys_patch;
1078 while (p < &__tsb_phys_patch_end) {
1079 unsigned long addr = p->addr;
1080
1081 *(unsigned int *) addr = p->insn;
1082 wmb();
1083 __asm__ __volatile__("flush %0"
1084 : /* no outputs */
1085 : "r" (addr));
1086
1087 p++;
1088 }
1089}
1090
David S. Miller490384e2006-02-11 14:41:18 -08001091/* Don't mark as init, we give this to the Hypervisor. */
1092static struct hv_tsb_descr ktsb_descr[2];
1093extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
1094
1095static void __init sun4v_ktsb_init(void)
1096{
1097 unsigned long ktsb_pa;
1098
David S. Millerd7744a02006-02-21 22:31:11 -08001099 /* First KTSB for PAGE_SIZE mappings. */
David S. Miller490384e2006-02-11 14:41:18 -08001100 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
1101
1102 switch (PAGE_SIZE) {
1103 case 8 * 1024:
1104 default:
1105 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
1106 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
1107 break;
1108
1109 case 64 * 1024:
1110 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
1111 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
1112 break;
1113
1114 case 512 * 1024:
1115 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
1116 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
1117 break;
1118
1119 case 4 * 1024 * 1024:
1120 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
1121 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
1122 break;
1123 };
1124
David S. Miller3f19a842006-02-17 12:03:20 -08001125 ktsb_descr[0].assoc = 1;
David S. Miller490384e2006-02-11 14:41:18 -08001126 ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
1127 ktsb_descr[0].ctx_idx = 0;
1128 ktsb_descr[0].tsb_base = ktsb_pa;
1129 ktsb_descr[0].resv = 0;
1130
David S. Millerd7744a02006-02-21 22:31:11 -08001131 /* Second KTSB for 4MB/256MB mappings. */
1132 ktsb_pa = (kern_base +
1133 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
1134
1135 ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
1136 ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
1137 HV_PGSZ_MASK_256MB);
1138 ktsb_descr[1].assoc = 1;
1139 ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
1140 ktsb_descr[1].ctx_idx = 0;
1141 ktsb_descr[1].tsb_base = ktsb_pa;
1142 ktsb_descr[1].resv = 0;
David S. Miller490384e2006-02-11 14:41:18 -08001143}
1144
1145void __cpuinit sun4v_ktsb_register(void)
1146{
1147 register unsigned long func asm("%o5");
1148 register unsigned long arg0 asm("%o0");
1149 register unsigned long arg1 asm("%o1");
1150 unsigned long pa;
1151
1152 pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
1153
1154 func = HV_FAST_MMU_TSB_CTX0;
David S. Millerd7744a02006-02-21 22:31:11 -08001155 arg0 = 2;
David S. Miller490384e2006-02-11 14:41:18 -08001156 arg1 = pa;
1157 __asm__ __volatile__("ta %6"
1158 : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
1159 : "0" (func), "1" (arg0), "2" (arg1),
1160 "i" (HV_FAST_TRAP));
1161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/* paging_init() sets up the page tables */
1164
1165extern void cheetah_ecache_flush_init(void);
David S. Millerd257d5d2006-02-06 23:44:37 -08001166extern void sun4v_patch_tlb_handlers(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168static unsigned long last_valid_pfn;
David S. Miller56425302005-09-25 16:46:57 -07001169pgd_t swapper_pg_dir[2048];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
David S. Millerc4bce902006-02-11 21:57:54 -08001171static void sun4u_pgprot_init(void);
1172static void sun4v_pgprot_init(void);
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174void __init paging_init(void)
1175{
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001176 unsigned long end_pfn, pages_avail, shift;
David S. Miller0836a0e2005-09-28 21:38:08 -07001177 unsigned long real_end, i;
1178
David S. Miller481295f2006-02-07 21:51:08 -08001179 kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
1180 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
1181
David S. Millerd7744a02006-02-21 22:31:11 -08001182 /* Invalidate both kernel TSBs. */
David S. Miller8b234272006-02-17 18:01:02 -08001183 memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
David S. Millerd7744a02006-02-21 22:31:11 -08001184 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
David S. Miller8b234272006-02-17 18:01:02 -08001185
David S. Millerc4bce902006-02-11 21:57:54 -08001186 if (tlb_type == hypervisor)
1187 sun4v_pgprot_init();
1188 else
1189 sun4u_pgprot_init();
1190
David S. Millerd257d5d2006-02-06 23:44:37 -08001191 if (tlb_type == cheetah_plus ||
1192 tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -08001193 tsb_phys_patch();
1194
David S. Miller490384e2006-02-11 14:41:18 -08001195 if (tlb_type == hypervisor) {
David S. Millerd257d5d2006-02-06 23:44:37 -08001196 sun4v_patch_tlb_handlers();
David S. Miller490384e2006-02-11 14:41:18 -08001197 sun4v_ktsb_init();
1198 }
David S. Millerd257d5d2006-02-06 23:44:37 -08001199
David S. Miller13edad72005-09-29 17:58:26 -07001200 /* Find available physical memory... */
1201 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller0836a0e2005-09-28 21:38:08 -07001202
1203 phys_base = 0xffffffffffffffffUL;
David S. Miller13edad72005-09-29 17:58:26 -07001204 for (i = 0; i < pavail_ents; i++)
1205 phys_base = min(phys_base, pavail[i].phys_addr);
David S. Miller0836a0e2005-09-28 21:38:08 -07001206
David S. Miller0836a0e2005-09-28 21:38:08 -07001207 pfn_base = phys_base >> PAGE_SHIFT;
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 set_bit(0, mmu_context_bmap);
1210
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001211 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 real_end = (unsigned long)_end;
1214 if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1215 bigkernel = 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001216 if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
1217 prom_printf("paging_init: Kernel > 8MB, too large.\n");
1218 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001220
1221 /* Set kernel pgd to upper alias so physical page computations
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 * work.
1223 */
1224 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1225
David S. Miller56425302005-09-25 16:46:57 -07001226 memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /* Now can init the kernel/bad page tables. */
1229 pud_set(pud_offset(&swapper_pg_dir[0], 0),
David S. Miller56425302005-09-25 16:46:57 -07001230 swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
David S. Millerc9c10832005-10-12 12:22:46 -07001232 inherit_prom_mappings();
David S. Miller5085b4a2005-09-22 00:45:41 -07001233
David S. Millera8b900d2006-01-31 18:33:37 -08001234 /* Ok, we can use our TLB miss and window trap handlers safely. */
1235 setup_tba();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
David S. Millerc9c10832005-10-12 12:22:46 -07001237 __flush_tlb_all();
David S. Miller9ad98c52005-10-05 15:12:00 -07001238
David S. Miller490384e2006-02-11 14:41:18 -08001239 if (tlb_type == hypervisor)
1240 sun4v_ktsb_register();
1241
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001242 /* Setup bootmem... */
1243 pages_avail = 0;
1244 last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
1245
David S. Miller56425302005-09-25 16:46:57 -07001246 kernel_physical_mapping_init();
David S. Miller56425302005-09-25 16:46:57 -07001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 {
1249 unsigned long zones_size[MAX_NR_ZONES];
1250 unsigned long zholes_size[MAX_NR_ZONES];
1251 unsigned long npages;
1252 int znum;
1253
1254 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1255 zones_size[znum] = zholes_size[znum] = 0;
1256
1257 npages = end_pfn - pfn_base;
1258 zones_size[ZONE_DMA] = npages;
1259 zholes_size[ZONE_DMA] = npages - pages_avail;
1260
1261 free_area_init_node(0, &contig_page_data, zones_size,
1262 phys_base >> PAGE_SHIFT, zholes_size);
1263 }
1264
1265 device_scan();
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268static void __init taint_real_pages(void)
1269{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 int i;
1271
David S. Miller13edad72005-09-29 17:58:26 -07001272 read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
David S. Miller13edad72005-09-29 17:58:26 -07001274 /* Find changes discovered in the physmem available rescan and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 * reserve the lost portions in the bootmem maps.
1276 */
David S. Miller13edad72005-09-29 17:58:26 -07001277 for (i = 0; i < pavail_ents; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 unsigned long old_start, old_end;
1279
David S. Miller13edad72005-09-29 17:58:26 -07001280 old_start = pavail[i].phys_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 old_end = old_start +
David S. Miller13edad72005-09-29 17:58:26 -07001282 pavail[i].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 while (old_start < old_end) {
1284 int n;
1285
David S. Miller13edad72005-09-29 17:58:26 -07001286 for (n = 0; pavail_rescan_ents; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 unsigned long new_start, new_end;
1288
David S. Miller13edad72005-09-29 17:58:26 -07001289 new_start = pavail_rescan[n].phys_addr;
1290 new_end = new_start +
1291 pavail_rescan[n].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (new_start <= old_start &&
1294 new_end >= (old_start + PAGE_SIZE)) {
David S. Miller13edad72005-09-29 17:58:26 -07001295 set_bit(old_start >> 22,
1296 sparc64_valid_addr_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 goto do_next_page;
1298 }
1299 }
1300 reserve_bootmem(old_start, PAGE_SIZE);
1301
1302 do_next_page:
1303 old_start += PAGE_SIZE;
1304 }
1305 }
1306}
1307
1308void __init mem_init(void)
1309{
1310 unsigned long codepages, datapages, initpages;
1311 unsigned long addr, last;
1312 int i;
1313
1314 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1315 i += 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001316 sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (sparc64_valid_addr_bitmap == NULL) {
1318 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1319 prom_halt();
1320 }
1321 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1322
1323 addr = PAGE_OFFSET + kern_base;
1324 last = PAGE_ALIGN(kern_size) + addr;
1325 while (addr < last) {
1326 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1327 addr += PAGE_SIZE;
1328 }
1329
1330 taint_real_pages();
1331
1332 max_mapnr = last_valid_pfn - pfn_base;
1333 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1334
1335#ifdef CONFIG_DEBUG_BOOTMEM
1336 prom_printf("mem_init: Calling free_all_bootmem().\n");
1337#endif
1338 totalram_pages = num_physpages = free_all_bootmem() - 1;
1339
1340 /*
1341 * Set up the zero page, mark it reserved, so that page count
1342 * is not manipulated when freeing the page from user ptes.
1343 */
1344 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
1345 if (mem_map_zero == NULL) {
1346 prom_printf("paging_init: Cannot alloc zero page.\n");
1347 prom_halt();
1348 }
1349 SetPageReserved(mem_map_zero);
1350
1351 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1352 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1353 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1354 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1355 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1356 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1357
1358 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1359 nr_free_pages() << (PAGE_SHIFT-10),
1360 codepages << (PAGE_SHIFT-10),
1361 datapages << (PAGE_SHIFT-10),
1362 initpages << (PAGE_SHIFT-10),
1363 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1364
1365 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1366 cheetah_ecache_flush_init();
1367}
1368
David S. Miller898cf0e2005-09-23 11:59:44 -07001369void free_initmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
1371 unsigned long addr, initend;
1372
1373 /*
1374 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1375 */
1376 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1377 initend = (unsigned long)(__init_end) & PAGE_MASK;
1378 for (; addr < initend; addr += PAGE_SIZE) {
1379 unsigned long page;
1380 struct page *p;
1381
1382 page = (addr +
1383 ((unsigned long) __va(kern_base)) -
1384 ((unsigned long) KERNBASE));
1385 memset((void *)addr, 0xcc, PAGE_SIZE);
1386 p = virt_to_page(page);
1387
1388 ClearPageReserved(p);
1389 set_page_count(p, 1);
1390 __free_page(p);
1391 num_physpages++;
1392 totalram_pages++;
1393 }
1394}
1395
1396#ifdef CONFIG_BLK_DEV_INITRD
1397void free_initrd_mem(unsigned long start, unsigned long end)
1398{
1399 if (start < end)
1400 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1401 for (; start < end; start += PAGE_SIZE) {
1402 struct page *p = virt_to_page(start);
1403
1404 ClearPageReserved(p);
1405 set_page_count(p, 1);
1406 __free_page(p);
1407 num_physpages++;
1408 totalram_pages++;
1409 }
1410}
1411#endif
David S. Millerc4bce902006-02-11 21:57:54 -08001412
David S. Millerc4bce902006-02-11 21:57:54 -08001413#define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
1414#define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
1415#define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
1416#define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
1417#define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
1418#define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
1419
1420pgprot_t PAGE_KERNEL __read_mostly;
1421EXPORT_SYMBOL(PAGE_KERNEL);
1422
1423pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
1424pgprot_t PAGE_COPY __read_mostly;
David S. Miller0f159522006-02-18 12:43:16 -08001425
1426pgprot_t PAGE_SHARED __read_mostly;
1427EXPORT_SYMBOL(PAGE_SHARED);
1428
David S. Millerc4bce902006-02-11 21:57:54 -08001429pgprot_t PAGE_EXEC __read_mostly;
1430unsigned long pg_iobits __read_mostly;
1431
1432unsigned long _PAGE_IE __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08001433
David S. Millerc4bce902006-02-11 21:57:54 -08001434unsigned long _PAGE_E __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08001435EXPORT_SYMBOL(_PAGE_E);
1436
David S. Millerc4bce902006-02-11 21:57:54 -08001437unsigned long _PAGE_CACHE __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08001438EXPORT_SYMBOL(_PAGE_CACHE);
David S. Millerc4bce902006-02-11 21:57:54 -08001439
1440static void prot_init_common(unsigned long page_none,
1441 unsigned long page_shared,
1442 unsigned long page_copy,
1443 unsigned long page_readonly,
1444 unsigned long page_exec_bit)
1445{
1446 PAGE_COPY = __pgprot(page_copy);
David S. Miller0f159522006-02-18 12:43:16 -08001447 PAGE_SHARED = __pgprot(page_shared);
David S. Millerc4bce902006-02-11 21:57:54 -08001448
1449 protection_map[0x0] = __pgprot(page_none);
1450 protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
1451 protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
1452 protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
1453 protection_map[0x4] = __pgprot(page_readonly);
1454 protection_map[0x5] = __pgprot(page_readonly);
1455 protection_map[0x6] = __pgprot(page_copy);
1456 protection_map[0x7] = __pgprot(page_copy);
1457 protection_map[0x8] = __pgprot(page_none);
1458 protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
1459 protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
1460 protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
1461 protection_map[0xc] = __pgprot(page_readonly);
1462 protection_map[0xd] = __pgprot(page_readonly);
1463 protection_map[0xe] = __pgprot(page_shared);
1464 protection_map[0xf] = __pgprot(page_shared);
1465}
1466
1467static void __init sun4u_pgprot_init(void)
1468{
1469 unsigned long page_none, page_shared, page_copy, page_readonly;
1470 unsigned long page_exec_bit;
1471
1472 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
1473 _PAGE_CACHE_4U | _PAGE_P_4U |
1474 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
1475 _PAGE_EXEC_4U);
1476 PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
1477 _PAGE_CACHE_4U | _PAGE_P_4U |
1478 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
1479 _PAGE_EXEC_4U | _PAGE_L_4U);
1480 PAGE_EXEC = __pgprot(_PAGE_EXEC_4U);
1481
1482 _PAGE_IE = _PAGE_IE_4U;
1483 _PAGE_E = _PAGE_E_4U;
1484 _PAGE_CACHE = _PAGE_CACHE_4U;
1485
1486 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
1487 __ACCESS_BITS_4U | _PAGE_E_4U);
1488
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001489 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
David S. Millerc4bce902006-02-11 21:57:54 -08001490 0xfffff80000000000;
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001491 kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
1492 _PAGE_P_4U | _PAGE_W_4U);
1493
1494 /* XXX Should use 256MB on Panther. XXX */
1495 kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
David S. Millerc4bce902006-02-11 21:57:54 -08001496
1497 _PAGE_SZBITS = _PAGE_SZBITS_4U;
1498 _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
1499 _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
1500 _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
1501
1502
1503 page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
1504 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1505 __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
1506 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1507 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
1508 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1509 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
1510
1511 page_exec_bit = _PAGE_EXEC_4U;
1512
1513 prot_init_common(page_none, page_shared, page_copy, page_readonly,
1514 page_exec_bit);
1515}
1516
1517static void __init sun4v_pgprot_init(void)
1518{
1519 unsigned long page_none, page_shared, page_copy, page_readonly;
1520 unsigned long page_exec_bit;
1521
1522 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
1523 _PAGE_CACHE_4V | _PAGE_P_4V |
1524 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
1525 _PAGE_EXEC_4V);
1526 PAGE_KERNEL_LOCKED = PAGE_KERNEL;
1527 PAGE_EXEC = __pgprot(_PAGE_EXEC_4V);
1528
1529 _PAGE_IE = _PAGE_IE_4V;
1530 _PAGE_E = _PAGE_E_4V;
1531 _PAGE_CACHE = _PAGE_CACHE_4V;
1532
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001533 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
David S. Millerc4bce902006-02-11 21:57:54 -08001534 0xfffff80000000000;
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001535 kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
1536 _PAGE_P_4V | _PAGE_W_4V);
1537
1538 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
1539 0xfffff80000000000;
1540 kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
1541 _PAGE_P_4V | _PAGE_W_4V);
David S. Millerc4bce902006-02-11 21:57:54 -08001542
1543 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
1544 __ACCESS_BITS_4V | _PAGE_E_4V);
1545
1546 _PAGE_SZBITS = _PAGE_SZBITS_4V;
1547 _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
1548 _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
1549 _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
1550 _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
1551
1552 page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
1553 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1554 __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
1555 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1556 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
1557 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1558 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
1559
1560 page_exec_bit = _PAGE_EXEC_4V;
1561
1562 prot_init_common(page_none, page_shared, page_copy, page_readonly,
1563 page_exec_bit);
1564}
1565
1566unsigned long pte_sz_bits(unsigned long sz)
1567{
1568 if (tlb_type == hypervisor) {
1569 switch (sz) {
1570 case 8 * 1024:
1571 default:
1572 return _PAGE_SZ8K_4V;
1573 case 64 * 1024:
1574 return _PAGE_SZ64K_4V;
1575 case 512 * 1024:
1576 return _PAGE_SZ512K_4V;
1577 case 4 * 1024 * 1024:
1578 return _PAGE_SZ4MB_4V;
1579 };
1580 } else {
1581 switch (sz) {
1582 case 8 * 1024:
1583 default:
1584 return _PAGE_SZ8K_4U;
1585 case 64 * 1024:
1586 return _PAGE_SZ64K_4U;
1587 case 512 * 1024:
1588 return _PAGE_SZ512K_4U;
1589 case 4 * 1024 * 1024:
1590 return _PAGE_SZ4MB_4U;
1591 };
1592 }
1593}
1594
1595pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
1596{
1597 pte_t pte;
David S. Millercf627152006-02-12 21:10:07 -08001598
1599 pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
David S. Millerc4bce902006-02-11 21:57:54 -08001600 pte_val(pte) |= (((unsigned long)space) << 32);
1601 pte_val(pte) |= pte_sz_bits(page_size);
David S. Millercf627152006-02-12 21:10:07 -08001602
David S. Millerc4bce902006-02-11 21:57:54 -08001603 return pte;
1604}
1605
David S. Millerc4bce902006-02-11 21:57:54 -08001606static unsigned long kern_large_tte(unsigned long paddr)
1607{
1608 unsigned long val;
1609
1610 val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
1611 _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
1612 _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
1613 if (tlb_type == hypervisor)
1614 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
1615 _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
1616 _PAGE_EXEC_4V | _PAGE_W_4V);
1617
1618 return val | paddr;
1619}
1620
1621/*
1622 * Translate PROM's mapping we capture at boot time into physical address.
1623 * The second parameter is only set from prom_callback() invocations.
1624 */
1625unsigned long prom_virt_to_phys(unsigned long promva, int *error)
1626{
1627 unsigned long mask;
1628 int i;
1629
1630 mask = _PAGE_PADDR_4U;
1631 if (tlb_type == hypervisor)
1632 mask = _PAGE_PADDR_4V;
1633
1634 for (i = 0; i < prom_trans_ents; i++) {
1635 struct linux_prom_translation *p = &prom_trans[i];
1636
1637 if (promva >= p->virt &&
1638 promva < (p->virt + p->size)) {
1639 unsigned long base = p->data & mask;
1640
1641 if (error)
1642 *error = 0;
1643 return base + (promva & (8192 - 1));
1644 }
1645 }
1646 if (error)
1647 *error = 1;
1648 return 0UL;
1649}
1650
1651/* XXX We should kill off this ugly thing at so me point. XXX */
1652unsigned long sun4u_get_pte(unsigned long addr)
1653{
1654 pgd_t *pgdp;
1655 pud_t *pudp;
1656 pmd_t *pmdp;
1657 pte_t *ptep;
1658 unsigned long mask = _PAGE_PADDR_4U;
1659
1660 if (tlb_type == hypervisor)
1661 mask = _PAGE_PADDR_4V;
1662
1663 if (addr >= PAGE_OFFSET)
1664 return addr & mask;
1665
1666 if ((addr >= LOW_OBP_ADDRESS) && (addr < HI_OBP_ADDRESS))
1667 return prom_virt_to_phys(addr, NULL);
1668
1669 pgdp = pgd_offset_k(addr);
1670 pudp = pud_offset(pgdp, addr);
1671 pmdp = pmd_offset(pudp, addr);
1672 ptep = pte_offset_kernel(pmdp, addr);
1673
1674 return pte_val(*ptep) & mask;
1675}
1676
1677/* If not locked, zap it. */
1678void __flush_tlb_all(void)
1679{
1680 unsigned long pstate;
1681 int i;
1682
1683 __asm__ __volatile__("flushw\n\t"
1684 "rdpr %%pstate, %0\n\t"
1685 "wrpr %0, %1, %%pstate"
1686 : "=r" (pstate)
1687 : "i" (PSTATE_IE));
1688 if (tlb_type == spitfire) {
1689 for (i = 0; i < 64; i++) {
1690 /* Spitfire Errata #32 workaround */
1691 /* NOTE: Always runs on spitfire, so no
1692 * cheetah+ page size encodings.
1693 */
1694 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1695 "flush %%g6"
1696 : /* No outputs */
1697 : "r" (0),
1698 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1699
1700 if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
1701 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1702 "membar #Sync"
1703 : /* no outputs */
1704 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
1705 spitfire_put_dtlb_data(i, 0x0UL);
1706 }
1707
1708 /* Spitfire Errata #32 workaround */
1709 /* NOTE: Always runs on spitfire, so no
1710 * cheetah+ page size encodings.
1711 */
1712 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1713 "flush %%g6"
1714 : /* No outputs */
1715 : "r" (0),
1716 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1717
1718 if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
1719 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1720 "membar #Sync"
1721 : /* no outputs */
1722 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1723 spitfire_put_itlb_data(i, 0x0UL);
1724 }
1725 }
1726 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1727 cheetah_flush_dtlb_all();
1728 cheetah_flush_itlb_all();
1729 }
1730 __asm__ __volatile__("wrpr %0, 0, %%pstate"
1731 : : "r" (pstate));
1732}