blob: 2a123135b04281f646a2037b9a5164956107191f [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).
632 */
633void get_new_mmu_context(struct mm_struct *mm)
634{
635 unsigned long ctx, new_ctx;
636 unsigned long orig_pgsz_bits;
637
638
639 spin_lock(&ctx_alloc_lock);
640 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
641 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
642 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
643 if (new_ctx >= (1 << CTX_NR_BITS)) {
644 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
645 if (new_ctx >= ctx) {
646 int i;
647 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
648 CTX_FIRST_VERSION;
649 if (new_ctx == 1)
650 new_ctx = CTX_FIRST_VERSION;
651
652 /* Don't call memset, for 16 entries that's just
653 * plain silly...
654 */
655 mmu_context_bmap[0] = 3;
656 mmu_context_bmap[1] = 0;
657 mmu_context_bmap[2] = 0;
658 mmu_context_bmap[3] = 0;
659 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
660 mmu_context_bmap[i + 0] = 0;
661 mmu_context_bmap[i + 1] = 0;
662 mmu_context_bmap[i + 2] = 0;
663 mmu_context_bmap[i + 3] = 0;
664 }
665 goto out;
666 }
667 }
668 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
669 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
670out:
671 tlb_context_cache = new_ctx;
672 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
673 spin_unlock(&ctx_alloc_lock);
674}
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676void sparc_ultra_dump_itlb(void)
677{
678 int slot;
679
680 if (tlb_type == spitfire) {
681 printk ("Contents of itlb: ");
682 for (slot = 0; slot < 14; slot++) printk (" ");
683 printk ("%2x:%016lx,%016lx\n",
684 0,
685 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
686 for (slot = 1; slot < 64; slot+=3) {
687 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
688 slot,
689 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
690 slot+1,
691 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
692 slot+2,
693 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
694 }
695 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
696 printk ("Contents of itlb0:\n");
697 for (slot = 0; slot < 16; slot+=2) {
698 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
699 slot,
700 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
701 slot+1,
702 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
703 }
704 printk ("Contents of itlb2:\n");
705 for (slot = 0; slot < 128; slot+=2) {
706 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
707 slot,
708 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
709 slot+1,
710 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
711 }
712 }
713}
714
715void sparc_ultra_dump_dtlb(void)
716{
717 int slot;
718
719 if (tlb_type == spitfire) {
720 printk ("Contents of dtlb: ");
721 for (slot = 0; slot < 14; slot++) printk (" ");
722 printk ("%2x:%016lx,%016lx\n", 0,
723 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
724 for (slot = 1; slot < 64; slot+=3) {
725 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
726 slot,
727 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
728 slot+1,
729 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
730 slot+2,
731 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
732 }
733 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
734 printk ("Contents of dtlb0:\n");
735 for (slot = 0; slot < 16; slot+=2) {
736 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
737 slot,
738 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
739 slot+1,
740 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
741 }
742 printk ("Contents of dtlb2:\n");
743 for (slot = 0; slot < 512; slot+=2) {
744 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
745 slot,
746 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
747 slot+1,
748 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
749 }
750 if (tlb_type == cheetah_plus) {
751 printk ("Contents of dtlb3:\n");
752 for (slot = 0; slot < 512; slot+=2) {
753 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
754 slot,
755 cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
756 slot+1,
757 cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
758 }
759 }
760 }
761}
762
763extern unsigned long cmdline_memory_size;
764
765unsigned long __init bootmem_init(unsigned long *pages_avail)
766{
767 unsigned long bootmap_size, start_pfn, end_pfn;
768 unsigned long end_of_phys_memory = 0UL;
769 unsigned long bootmap_pfn, bytes_avail, size;
770 int i;
771
772#ifdef CONFIG_DEBUG_BOOTMEM
David S. Miller13edad72005-09-29 17:58:26 -0700773 prom_printf("bootmem_init: Scan pavail, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774#endif
775
776 bytes_avail = 0UL;
David S. Miller13edad72005-09-29 17:58:26 -0700777 for (i = 0; i < pavail_ents; i++) {
778 end_of_phys_memory = pavail[i].phys_addr +
779 pavail[i].reg_size;
780 bytes_avail += pavail[i].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (cmdline_memory_size) {
782 if (bytes_avail > cmdline_memory_size) {
783 unsigned long slack = bytes_avail - cmdline_memory_size;
784
785 bytes_avail -= slack;
786 end_of_phys_memory -= slack;
787
David S. Miller13edad72005-09-29 17:58:26 -0700788 pavail[i].reg_size -= slack;
789 if ((long)pavail[i].reg_size <= 0L) {
790 pavail[i].phys_addr = 0xdeadbeefUL;
791 pavail[i].reg_size = 0UL;
792 pavail_ents = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 } else {
David S. Miller13edad72005-09-29 17:58:26 -0700794 pavail[i+1].reg_size = 0Ul;
795 pavail[i+1].phys_addr = 0xdeadbeefUL;
796 pavail_ents = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798 break;
799 }
800 }
801 }
802
803 *pages_avail = bytes_avail >> PAGE_SHIFT;
804
805 /* Start with page aligned address of last symbol in kernel
806 * image. The kernel is hard mapped below PAGE_OFFSET in a
807 * 4MB locked TLB translation.
808 */
809 start_pfn = PAGE_ALIGN(kern_base + kern_size) >> PAGE_SHIFT;
810
811 bootmap_pfn = start_pfn;
812
813 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
814
815#ifdef CONFIG_BLK_DEV_INITRD
816 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
817 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
818 unsigned long ramdisk_image = sparc_ramdisk_image ?
819 sparc_ramdisk_image : sparc_ramdisk_image64;
820 if (ramdisk_image >= (unsigned long)_end - 2 * PAGE_SIZE)
821 ramdisk_image -= KERNBASE;
822 initrd_start = ramdisk_image + phys_base;
823 initrd_end = initrd_start + sparc_ramdisk_size;
824 if (initrd_end > end_of_phys_memory) {
825 printk(KERN_CRIT "initrd extends beyond end of memory "
826 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
827 initrd_end, end_of_phys_memory);
828 initrd_start = 0;
829 }
830 if (initrd_start) {
831 if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
832 initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
833 bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
834 }
835 }
836#endif
837 /* Initialize the boot-time allocator. */
838 max_pfn = max_low_pfn = end_pfn;
839 min_low_pfn = pfn_base;
840
841#ifdef CONFIG_DEBUG_BOOTMEM
842 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
843 min_low_pfn, bootmap_pfn, max_low_pfn);
844#endif
845 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base, end_pfn);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /* Now register the available physical memory with the
848 * allocator.
849 */
David S. Miller13edad72005-09-29 17:58:26 -0700850 for (i = 0; i < pavail_ents; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851#ifdef CONFIG_DEBUG_BOOTMEM
David S. Miller13edad72005-09-29 17:58:26 -0700852 prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
853 i, pavail[i].phys_addr, pavail[i].reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854#endif
David S. Miller13edad72005-09-29 17:58:26 -0700855 free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
858#ifdef CONFIG_BLK_DEV_INITRD
859 if (initrd_start) {
860 size = initrd_end - initrd_start;
861
862 /* Resert the initrd image area. */
863#ifdef CONFIG_DEBUG_BOOTMEM
864 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
865 initrd_start, initrd_end);
866#endif
867 reserve_bootmem(initrd_start, size);
868 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
869
870 initrd_start += PAGE_OFFSET;
871 initrd_end += PAGE_OFFSET;
872 }
873#endif
874 /* Reserve the kernel text/data/bss. */
875#ifdef CONFIG_DEBUG_BOOTMEM
876 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
877#endif
878 reserve_bootmem(kern_base, kern_size);
879 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
880
881 /* Reserve the bootmem map. We do not account for it
882 * in pages_avail because we will release that memory
883 * in free_all_bootmem.
884 */
885 size = bootmap_size;
886#ifdef CONFIG_DEBUG_BOOTMEM
887 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
888 (bootmap_pfn << PAGE_SHIFT), size);
889#endif
890 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
891 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
892
893 return end_pfn;
894}
895
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800896static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
897static int pall_ents __initdata;
898
David S. Miller56425302005-09-25 16:46:57 -0700899#ifdef CONFIG_DEBUG_PAGEALLOC
900static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend, pgprot_t prot)
901{
902 unsigned long vstart = PAGE_OFFSET + pstart;
903 unsigned long vend = PAGE_OFFSET + pend;
904 unsigned long alloc_bytes = 0UL;
905
906 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
David S. Miller13edad72005-09-29 17:58:26 -0700907 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
David S. Miller56425302005-09-25 16:46:57 -0700908 vstart, vend);
909 prom_halt();
910 }
911
912 while (vstart < vend) {
913 unsigned long this_end, paddr = __pa(vstart);
914 pgd_t *pgd = pgd_offset_k(vstart);
915 pud_t *pud;
916 pmd_t *pmd;
917 pte_t *pte;
918
919 pud = pud_offset(pgd, vstart);
920 if (pud_none(*pud)) {
921 pmd_t *new;
922
923 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
924 alloc_bytes += PAGE_SIZE;
925 pud_populate(&init_mm, pud, new);
926 }
927
928 pmd = pmd_offset(pud, vstart);
929 if (!pmd_present(*pmd)) {
930 pte_t *new;
931
932 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
933 alloc_bytes += PAGE_SIZE;
934 pmd_populate_kernel(&init_mm, pmd, new);
935 }
936
937 pte = pte_offset_kernel(pmd, vstart);
938 this_end = (vstart + PMD_SIZE) & PMD_MASK;
939 if (this_end > vend)
940 this_end = vend;
941
942 while (vstart < this_end) {
943 pte_val(*pte) = (paddr | pgprot_val(prot));
944
945 vstart += PAGE_SIZE;
946 paddr += PAGE_SIZE;
947 pte++;
948 }
949 }
950
951 return alloc_bytes;
952}
953
David S. Miller56425302005-09-25 16:46:57 -0700954extern unsigned int kvmap_linear_patch[1];
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800955#endif /* CONFIG_DEBUG_PAGEALLOC */
956
957static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
958{
959 const unsigned long shift_256MB = 28;
960 const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
961 const unsigned long size_256MB = (1UL << shift_256MB);
962
963 while (start < end) {
964 long remains;
965
966 if (start & mask_256MB) {
967 start = (start + size_256MB) & ~mask_256MB;
968 continue;
969 }
970
971 remains = end - start;
972 while (remains >= size_256MB) {
973 unsigned long index = start >> shift_256MB;
974
975 __set_bit(index, kpte_linear_bitmap);
976
977 start += size_256MB;
978 remains -= size_256MB;
979 }
980 }
981}
David S. Miller56425302005-09-25 16:46:57 -0700982
983static void __init kernel_physical_mapping_init(void)
984{
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800985 unsigned long i;
986#ifdef CONFIG_DEBUG_PAGEALLOC
987 unsigned long mem_alloced = 0UL;
988#endif
David S. Miller56425302005-09-25 16:46:57 -0700989
David S. Miller13edad72005-09-29 17:58:26 -0700990 read_obp_memory("reg", &pall[0], &pall_ents);
991
992 for (i = 0; i < pall_ents; i++) {
David S. Miller56425302005-09-25 16:46:57 -0700993 unsigned long phys_start, phys_end;
994
David S. Miller13edad72005-09-29 17:58:26 -0700995 phys_start = pall[i].phys_addr;
996 phys_end = phys_start + pall[i].reg_size;
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800997
998 mark_kpte_bitmap(phys_start, phys_end);
999
1000#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller56425302005-09-25 16:46:57 -07001001 mem_alloced += kernel_map_range(phys_start, phys_end,
1002 PAGE_KERNEL);
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001003#endif
David S. Miller56425302005-09-25 16:46:57 -07001004 }
1005
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001006#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller56425302005-09-25 16:46:57 -07001007 printk("Allocated %ld bytes for kernel page tables.\n",
1008 mem_alloced);
1009
1010 kvmap_linear_patch[0] = 0x01000000; /* nop */
1011 flushi(&kvmap_linear_patch[0]);
1012
1013 __flush_tlb_all();
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001014#endif
David S. Miller56425302005-09-25 16:46:57 -07001015}
1016
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001017#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller56425302005-09-25 16:46:57 -07001018void kernel_map_pages(struct page *page, int numpages, int enable)
1019{
1020 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1021 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1022
1023 kernel_map_range(phys_start, phys_end,
1024 (enable ? PAGE_KERNEL : __pgprot(0)));
1025
David S. Miller74bf4312006-01-31 18:29:18 -08001026 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1027 PAGE_OFFSET + phys_end);
1028
David S. Miller56425302005-09-25 16:46:57 -07001029 /* we should perform an IPI and flush all tlbs,
1030 * but that can deadlock->flush only current cpu.
1031 */
1032 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1033 PAGE_OFFSET + phys_end);
1034}
1035#endif
1036
David S. Miller10147572005-09-28 21:46:43 -07001037unsigned long __init find_ecache_flush_span(unsigned long size)
1038{
David S. Miller13edad72005-09-29 17:58:26 -07001039 int i;
David S. Miller10147572005-09-28 21:46:43 -07001040
David S. Miller13edad72005-09-29 17:58:26 -07001041 for (i = 0; i < pavail_ents; i++) {
1042 if (pavail[i].reg_size >= size)
1043 return pavail[i].phys_addr;
David S. Miller10147572005-09-28 21:46:43 -07001044 }
1045
1046 return ~0UL;
1047}
1048
David S. Miller517af332006-02-01 15:55:21 -08001049static void __init tsb_phys_patch(void)
1050{
David S. Millerd257d5d2006-02-06 23:44:37 -08001051 struct tsb_ldquad_phys_patch_entry *pquad;
David S. Miller517af332006-02-01 15:55:21 -08001052 struct tsb_phys_patch_entry *p;
1053
David S. Millerd257d5d2006-02-06 23:44:37 -08001054 pquad = &__tsb_ldquad_phys_patch;
1055 while (pquad < &__tsb_ldquad_phys_patch_end) {
1056 unsigned long addr = pquad->addr;
1057
1058 if (tlb_type == hypervisor)
1059 *(unsigned int *) addr = pquad->sun4v_insn;
1060 else
1061 *(unsigned int *) addr = pquad->sun4u_insn;
1062 wmb();
1063 __asm__ __volatile__("flush %0"
1064 : /* no outputs */
1065 : "r" (addr));
1066
1067 pquad++;
1068 }
1069
David S. Miller517af332006-02-01 15:55:21 -08001070 p = &__tsb_phys_patch;
1071 while (p < &__tsb_phys_patch_end) {
1072 unsigned long addr = p->addr;
1073
1074 *(unsigned int *) addr = p->insn;
1075 wmb();
1076 __asm__ __volatile__("flush %0"
1077 : /* no outputs */
1078 : "r" (addr));
1079
1080 p++;
1081 }
1082}
1083
David S. Miller490384e2006-02-11 14:41:18 -08001084/* Don't mark as init, we give this to the Hypervisor. */
1085static struct hv_tsb_descr ktsb_descr[2];
1086extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
1087
1088static void __init sun4v_ktsb_init(void)
1089{
1090 unsigned long ktsb_pa;
1091
David S. Millerd7744a02006-02-21 22:31:11 -08001092 /* First KTSB for PAGE_SIZE mappings. */
David S. Miller490384e2006-02-11 14:41:18 -08001093 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
1094
1095 switch (PAGE_SIZE) {
1096 case 8 * 1024:
1097 default:
1098 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
1099 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
1100 break;
1101
1102 case 64 * 1024:
1103 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
1104 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
1105 break;
1106
1107 case 512 * 1024:
1108 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
1109 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
1110 break;
1111
1112 case 4 * 1024 * 1024:
1113 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
1114 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
1115 break;
1116 };
1117
David S. Miller3f19a842006-02-17 12:03:20 -08001118 ktsb_descr[0].assoc = 1;
David S. Miller490384e2006-02-11 14:41:18 -08001119 ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
1120 ktsb_descr[0].ctx_idx = 0;
1121 ktsb_descr[0].tsb_base = ktsb_pa;
1122 ktsb_descr[0].resv = 0;
1123
David S. Millerd7744a02006-02-21 22:31:11 -08001124 /* Second KTSB for 4MB/256MB mappings. */
1125 ktsb_pa = (kern_base +
1126 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
1127
1128 ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
1129 ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
1130 HV_PGSZ_MASK_256MB);
1131 ktsb_descr[1].assoc = 1;
1132 ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
1133 ktsb_descr[1].ctx_idx = 0;
1134 ktsb_descr[1].tsb_base = ktsb_pa;
1135 ktsb_descr[1].resv = 0;
David S. Miller490384e2006-02-11 14:41:18 -08001136}
1137
1138void __cpuinit sun4v_ktsb_register(void)
1139{
1140 register unsigned long func asm("%o5");
1141 register unsigned long arg0 asm("%o0");
1142 register unsigned long arg1 asm("%o1");
1143 unsigned long pa;
1144
1145 pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
1146
1147 func = HV_FAST_MMU_TSB_CTX0;
David S. Millerd7744a02006-02-21 22:31:11 -08001148 arg0 = 2;
David S. Miller490384e2006-02-11 14:41:18 -08001149 arg1 = pa;
1150 __asm__ __volatile__("ta %6"
1151 : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
1152 : "0" (func), "1" (arg0), "2" (arg1),
1153 "i" (HV_FAST_TRAP));
1154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/* paging_init() sets up the page tables */
1157
1158extern void cheetah_ecache_flush_init(void);
David S. Millerd257d5d2006-02-06 23:44:37 -08001159extern void sun4v_patch_tlb_handlers(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161static unsigned long last_valid_pfn;
David S. Miller56425302005-09-25 16:46:57 -07001162pgd_t swapper_pg_dir[2048];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
David S. Millerc4bce902006-02-11 21:57:54 -08001164static void sun4u_pgprot_init(void);
1165static void sun4v_pgprot_init(void);
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167void __init paging_init(void)
1168{
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001169 unsigned long end_pfn, pages_avail, shift;
David S. Miller0836a0e2005-09-28 21:38:08 -07001170 unsigned long real_end, i;
1171
David S. Miller481295f2006-02-07 21:51:08 -08001172 kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
1173 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
1174
David S. Millerd7744a02006-02-21 22:31:11 -08001175 /* Invalidate both kernel TSBs. */
David S. Miller8b234272006-02-17 18:01:02 -08001176 memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
David S. Millerd7744a02006-02-21 22:31:11 -08001177 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
David S. Miller8b234272006-02-17 18:01:02 -08001178
David S. Millerc4bce902006-02-11 21:57:54 -08001179 if (tlb_type == hypervisor)
1180 sun4v_pgprot_init();
1181 else
1182 sun4u_pgprot_init();
1183
David S. Millerd257d5d2006-02-06 23:44:37 -08001184 if (tlb_type == cheetah_plus ||
1185 tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -08001186 tsb_phys_patch();
1187
David S. Miller490384e2006-02-11 14:41:18 -08001188 if (tlb_type == hypervisor) {
David S. Millerd257d5d2006-02-06 23:44:37 -08001189 sun4v_patch_tlb_handlers();
David S. Miller490384e2006-02-11 14:41:18 -08001190 sun4v_ktsb_init();
1191 }
David S. Millerd257d5d2006-02-06 23:44:37 -08001192
David S. Miller13edad72005-09-29 17:58:26 -07001193 /* Find available physical memory... */
1194 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller0836a0e2005-09-28 21:38:08 -07001195
1196 phys_base = 0xffffffffffffffffUL;
David S. Miller13edad72005-09-29 17:58:26 -07001197 for (i = 0; i < pavail_ents; i++)
1198 phys_base = min(phys_base, pavail[i].phys_addr);
David S. Miller0836a0e2005-09-28 21:38:08 -07001199
David S. Miller0836a0e2005-09-28 21:38:08 -07001200 pfn_base = phys_base >> PAGE_SHIFT;
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 set_bit(0, mmu_context_bmap);
1203
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001204 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 real_end = (unsigned long)_end;
1207 if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1208 bigkernel = 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001209 if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
1210 prom_printf("paging_init: Kernel > 8MB, too large.\n");
1211 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001213
1214 /* Set kernel pgd to upper alias so physical page computations
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 * work.
1216 */
1217 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1218
David S. Miller56425302005-09-25 16:46:57 -07001219 memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /* Now can init the kernel/bad page tables. */
1222 pud_set(pud_offset(&swapper_pg_dir[0], 0),
David S. Miller56425302005-09-25 16:46:57 -07001223 swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
David S. Millerc9c10832005-10-12 12:22:46 -07001225 inherit_prom_mappings();
David S. Miller5085b4a2005-09-22 00:45:41 -07001226
David S. Millera8b900d2006-01-31 18:33:37 -08001227 /* Ok, we can use our TLB miss and window trap handlers safely. */
1228 setup_tba();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
David S. Millerc9c10832005-10-12 12:22:46 -07001230 __flush_tlb_all();
David S. Miller9ad98c52005-10-05 15:12:00 -07001231
David S. Miller490384e2006-02-11 14:41:18 -08001232 if (tlb_type == hypervisor)
1233 sun4v_ktsb_register();
1234
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001235 /* Setup bootmem... */
1236 pages_avail = 0;
1237 last_valid_pfn = end_pfn = bootmem_init(&pages_avail);
1238
David S. Miller56425302005-09-25 16:46:57 -07001239 kernel_physical_mapping_init();
David S. Miller56425302005-09-25 16:46:57 -07001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 {
1242 unsigned long zones_size[MAX_NR_ZONES];
1243 unsigned long zholes_size[MAX_NR_ZONES];
1244 unsigned long npages;
1245 int znum;
1246
1247 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1248 zones_size[znum] = zholes_size[znum] = 0;
1249
1250 npages = end_pfn - pfn_base;
1251 zones_size[ZONE_DMA] = npages;
1252 zholes_size[ZONE_DMA] = npages - pages_avail;
1253
1254 free_area_init_node(0, &contig_page_data, zones_size,
1255 phys_base >> PAGE_SHIFT, zholes_size);
1256 }
1257
1258 device_scan();
1259}
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261static void __init taint_real_pages(void)
1262{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 int i;
1264
David S. Miller13edad72005-09-29 17:58:26 -07001265 read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
David S. Miller13edad72005-09-29 17:58:26 -07001267 /* Find changes discovered in the physmem available rescan and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 * reserve the lost portions in the bootmem maps.
1269 */
David S. Miller13edad72005-09-29 17:58:26 -07001270 for (i = 0; i < pavail_ents; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 unsigned long old_start, old_end;
1272
David S. Miller13edad72005-09-29 17:58:26 -07001273 old_start = pavail[i].phys_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 old_end = old_start +
David S. Miller13edad72005-09-29 17:58:26 -07001275 pavail[i].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 while (old_start < old_end) {
1277 int n;
1278
David S. Miller13edad72005-09-29 17:58:26 -07001279 for (n = 0; pavail_rescan_ents; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 unsigned long new_start, new_end;
1281
David S. Miller13edad72005-09-29 17:58:26 -07001282 new_start = pavail_rescan[n].phys_addr;
1283 new_end = new_start +
1284 pavail_rescan[n].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (new_start <= old_start &&
1287 new_end >= (old_start + PAGE_SIZE)) {
David S. Miller13edad72005-09-29 17:58:26 -07001288 set_bit(old_start >> 22,
1289 sparc64_valid_addr_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 goto do_next_page;
1291 }
1292 }
1293 reserve_bootmem(old_start, PAGE_SIZE);
1294
1295 do_next_page:
1296 old_start += PAGE_SIZE;
1297 }
1298 }
1299}
1300
1301void __init mem_init(void)
1302{
1303 unsigned long codepages, datapages, initpages;
1304 unsigned long addr, last;
1305 int i;
1306
1307 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1308 i += 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001309 sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (sparc64_valid_addr_bitmap == NULL) {
1311 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1312 prom_halt();
1313 }
1314 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1315
1316 addr = PAGE_OFFSET + kern_base;
1317 last = PAGE_ALIGN(kern_size) + addr;
1318 while (addr < last) {
1319 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1320 addr += PAGE_SIZE;
1321 }
1322
1323 taint_real_pages();
1324
1325 max_mapnr = last_valid_pfn - pfn_base;
1326 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1327
1328#ifdef CONFIG_DEBUG_BOOTMEM
1329 prom_printf("mem_init: Calling free_all_bootmem().\n");
1330#endif
1331 totalram_pages = num_physpages = free_all_bootmem() - 1;
1332
1333 /*
1334 * Set up the zero page, mark it reserved, so that page count
1335 * is not manipulated when freeing the page from user ptes.
1336 */
1337 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
1338 if (mem_map_zero == NULL) {
1339 prom_printf("paging_init: Cannot alloc zero page.\n");
1340 prom_halt();
1341 }
1342 SetPageReserved(mem_map_zero);
1343
1344 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1345 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1346 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1347 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1348 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1349 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1350
1351 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1352 nr_free_pages() << (PAGE_SHIFT-10),
1353 codepages << (PAGE_SHIFT-10),
1354 datapages << (PAGE_SHIFT-10),
1355 initpages << (PAGE_SHIFT-10),
1356 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1357
1358 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1359 cheetah_ecache_flush_init();
1360}
1361
David S. Miller898cf0e2005-09-23 11:59:44 -07001362void free_initmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 unsigned long addr, initend;
1365
1366 /*
1367 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1368 */
1369 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1370 initend = (unsigned long)(__init_end) & PAGE_MASK;
1371 for (; addr < initend; addr += PAGE_SIZE) {
1372 unsigned long page;
1373 struct page *p;
1374
1375 page = (addr +
1376 ((unsigned long) __va(kern_base)) -
1377 ((unsigned long) KERNBASE));
1378 memset((void *)addr, 0xcc, PAGE_SIZE);
1379 p = virt_to_page(page);
1380
1381 ClearPageReserved(p);
1382 set_page_count(p, 1);
1383 __free_page(p);
1384 num_physpages++;
1385 totalram_pages++;
1386 }
1387}
1388
1389#ifdef CONFIG_BLK_DEV_INITRD
1390void free_initrd_mem(unsigned long start, unsigned long end)
1391{
1392 if (start < end)
1393 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1394 for (; start < end; start += PAGE_SIZE) {
1395 struct page *p = virt_to_page(start);
1396
1397 ClearPageReserved(p);
1398 set_page_count(p, 1);
1399 __free_page(p);
1400 num_physpages++;
1401 totalram_pages++;
1402 }
1403}
1404#endif
David S. Millerc4bce902006-02-11 21:57:54 -08001405
David S. Millerc4bce902006-02-11 21:57:54 -08001406#define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
1407#define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
1408#define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
1409#define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
1410#define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
1411#define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
1412
1413pgprot_t PAGE_KERNEL __read_mostly;
1414EXPORT_SYMBOL(PAGE_KERNEL);
1415
1416pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
1417pgprot_t PAGE_COPY __read_mostly;
David S. Miller0f159522006-02-18 12:43:16 -08001418
1419pgprot_t PAGE_SHARED __read_mostly;
1420EXPORT_SYMBOL(PAGE_SHARED);
1421
David S. Millerc4bce902006-02-11 21:57:54 -08001422pgprot_t PAGE_EXEC __read_mostly;
1423unsigned long pg_iobits __read_mostly;
1424
1425unsigned long _PAGE_IE __read_mostly;
1426unsigned long _PAGE_E __read_mostly;
1427unsigned long _PAGE_CACHE __read_mostly;
1428
1429static void prot_init_common(unsigned long page_none,
1430 unsigned long page_shared,
1431 unsigned long page_copy,
1432 unsigned long page_readonly,
1433 unsigned long page_exec_bit)
1434{
1435 PAGE_COPY = __pgprot(page_copy);
David S. Miller0f159522006-02-18 12:43:16 -08001436 PAGE_SHARED = __pgprot(page_shared);
David S. Millerc4bce902006-02-11 21:57:54 -08001437
1438 protection_map[0x0] = __pgprot(page_none);
1439 protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
1440 protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
1441 protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
1442 protection_map[0x4] = __pgprot(page_readonly);
1443 protection_map[0x5] = __pgprot(page_readonly);
1444 protection_map[0x6] = __pgprot(page_copy);
1445 protection_map[0x7] = __pgprot(page_copy);
1446 protection_map[0x8] = __pgprot(page_none);
1447 protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
1448 protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
1449 protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
1450 protection_map[0xc] = __pgprot(page_readonly);
1451 protection_map[0xd] = __pgprot(page_readonly);
1452 protection_map[0xe] = __pgprot(page_shared);
1453 protection_map[0xf] = __pgprot(page_shared);
1454}
1455
1456static void __init sun4u_pgprot_init(void)
1457{
1458 unsigned long page_none, page_shared, page_copy, page_readonly;
1459 unsigned long page_exec_bit;
1460
1461 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
1462 _PAGE_CACHE_4U | _PAGE_P_4U |
1463 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
1464 _PAGE_EXEC_4U);
1465 PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
1466 _PAGE_CACHE_4U | _PAGE_P_4U |
1467 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
1468 _PAGE_EXEC_4U | _PAGE_L_4U);
1469 PAGE_EXEC = __pgprot(_PAGE_EXEC_4U);
1470
1471 _PAGE_IE = _PAGE_IE_4U;
1472 _PAGE_E = _PAGE_E_4U;
1473 _PAGE_CACHE = _PAGE_CACHE_4U;
1474
1475 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
1476 __ACCESS_BITS_4U | _PAGE_E_4U);
1477
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001478 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
David S. Millerc4bce902006-02-11 21:57:54 -08001479 0xfffff80000000000;
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001480 kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
1481 _PAGE_P_4U | _PAGE_W_4U);
1482
1483 /* XXX Should use 256MB on Panther. XXX */
1484 kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
David S. Millerc4bce902006-02-11 21:57:54 -08001485
1486 _PAGE_SZBITS = _PAGE_SZBITS_4U;
1487 _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
1488 _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
1489 _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
1490
1491
1492 page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
1493 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1494 __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
1495 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1496 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
1497 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1498 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
1499
1500 page_exec_bit = _PAGE_EXEC_4U;
1501
1502 prot_init_common(page_none, page_shared, page_copy, page_readonly,
1503 page_exec_bit);
1504}
1505
1506static void __init sun4v_pgprot_init(void)
1507{
1508 unsigned long page_none, page_shared, page_copy, page_readonly;
1509 unsigned long page_exec_bit;
1510
1511 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
1512 _PAGE_CACHE_4V | _PAGE_P_4V |
1513 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
1514 _PAGE_EXEC_4V);
1515 PAGE_KERNEL_LOCKED = PAGE_KERNEL;
1516 PAGE_EXEC = __pgprot(_PAGE_EXEC_4V);
1517
1518 _PAGE_IE = _PAGE_IE_4V;
1519 _PAGE_E = _PAGE_E_4V;
1520 _PAGE_CACHE = _PAGE_CACHE_4V;
1521
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001522 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
David S. Millerc4bce902006-02-11 21:57:54 -08001523 0xfffff80000000000;
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001524 kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
1525 _PAGE_P_4V | _PAGE_W_4V);
1526
1527 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
1528 0xfffff80000000000;
1529 kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
1530 _PAGE_P_4V | _PAGE_W_4V);
David S. Millerc4bce902006-02-11 21:57:54 -08001531
1532 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
1533 __ACCESS_BITS_4V | _PAGE_E_4V);
1534
1535 _PAGE_SZBITS = _PAGE_SZBITS_4V;
1536 _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
1537 _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
1538 _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
1539 _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
1540
1541 page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
1542 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1543 __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
1544 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1545 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
1546 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1547 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
1548
1549 page_exec_bit = _PAGE_EXEC_4V;
1550
1551 prot_init_common(page_none, page_shared, page_copy, page_readonly,
1552 page_exec_bit);
1553}
1554
1555unsigned long pte_sz_bits(unsigned long sz)
1556{
1557 if (tlb_type == hypervisor) {
1558 switch (sz) {
1559 case 8 * 1024:
1560 default:
1561 return _PAGE_SZ8K_4V;
1562 case 64 * 1024:
1563 return _PAGE_SZ64K_4V;
1564 case 512 * 1024:
1565 return _PAGE_SZ512K_4V;
1566 case 4 * 1024 * 1024:
1567 return _PAGE_SZ4MB_4V;
1568 };
1569 } else {
1570 switch (sz) {
1571 case 8 * 1024:
1572 default:
1573 return _PAGE_SZ8K_4U;
1574 case 64 * 1024:
1575 return _PAGE_SZ64K_4U;
1576 case 512 * 1024:
1577 return _PAGE_SZ512K_4U;
1578 case 4 * 1024 * 1024:
1579 return _PAGE_SZ4MB_4U;
1580 };
1581 }
1582}
1583
1584pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
1585{
1586 pte_t pte;
David S. Millercf627152006-02-12 21:10:07 -08001587
1588 pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
David S. Millerc4bce902006-02-11 21:57:54 -08001589 pte_val(pte) |= (((unsigned long)space) << 32);
1590 pte_val(pte) |= pte_sz_bits(page_size);
David S. Millercf627152006-02-12 21:10:07 -08001591
David S. Millerc4bce902006-02-11 21:57:54 -08001592 return pte;
1593}
1594
David S. Millerc4bce902006-02-11 21:57:54 -08001595static unsigned long kern_large_tte(unsigned long paddr)
1596{
1597 unsigned long val;
1598
1599 val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
1600 _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
1601 _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
1602 if (tlb_type == hypervisor)
1603 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
1604 _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
1605 _PAGE_EXEC_4V | _PAGE_W_4V);
1606
1607 return val | paddr;
1608}
1609
1610/*
1611 * Translate PROM's mapping we capture at boot time into physical address.
1612 * The second parameter is only set from prom_callback() invocations.
1613 */
1614unsigned long prom_virt_to_phys(unsigned long promva, int *error)
1615{
1616 unsigned long mask;
1617 int i;
1618
1619 mask = _PAGE_PADDR_4U;
1620 if (tlb_type == hypervisor)
1621 mask = _PAGE_PADDR_4V;
1622
1623 for (i = 0; i < prom_trans_ents; i++) {
1624 struct linux_prom_translation *p = &prom_trans[i];
1625
1626 if (promva >= p->virt &&
1627 promva < (p->virt + p->size)) {
1628 unsigned long base = p->data & mask;
1629
1630 if (error)
1631 *error = 0;
1632 return base + (promva & (8192 - 1));
1633 }
1634 }
1635 if (error)
1636 *error = 1;
1637 return 0UL;
1638}
1639
1640/* XXX We should kill off this ugly thing at so me point. XXX */
1641unsigned long sun4u_get_pte(unsigned long addr)
1642{
1643 pgd_t *pgdp;
1644 pud_t *pudp;
1645 pmd_t *pmdp;
1646 pte_t *ptep;
1647 unsigned long mask = _PAGE_PADDR_4U;
1648
1649 if (tlb_type == hypervisor)
1650 mask = _PAGE_PADDR_4V;
1651
1652 if (addr >= PAGE_OFFSET)
1653 return addr & mask;
1654
1655 if ((addr >= LOW_OBP_ADDRESS) && (addr < HI_OBP_ADDRESS))
1656 return prom_virt_to_phys(addr, NULL);
1657
1658 pgdp = pgd_offset_k(addr);
1659 pudp = pud_offset(pgdp, addr);
1660 pmdp = pmd_offset(pudp, addr);
1661 ptep = pte_offset_kernel(pmdp, addr);
1662
1663 return pte_val(*ptep) & mask;
1664}
1665
1666/* If not locked, zap it. */
1667void __flush_tlb_all(void)
1668{
1669 unsigned long pstate;
1670 int i;
1671
1672 __asm__ __volatile__("flushw\n\t"
1673 "rdpr %%pstate, %0\n\t"
1674 "wrpr %0, %1, %%pstate"
1675 : "=r" (pstate)
1676 : "i" (PSTATE_IE));
1677 if (tlb_type == spitfire) {
1678 for (i = 0; i < 64; i++) {
1679 /* Spitfire Errata #32 workaround */
1680 /* NOTE: Always runs on spitfire, so no
1681 * cheetah+ page size encodings.
1682 */
1683 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1684 "flush %%g6"
1685 : /* No outputs */
1686 : "r" (0),
1687 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1688
1689 if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
1690 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1691 "membar #Sync"
1692 : /* no outputs */
1693 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
1694 spitfire_put_dtlb_data(i, 0x0UL);
1695 }
1696
1697 /* Spitfire Errata #32 workaround */
1698 /* NOTE: Always runs on spitfire, so no
1699 * cheetah+ page size encodings.
1700 */
1701 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1702 "flush %%g6"
1703 : /* No outputs */
1704 : "r" (0),
1705 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1706
1707 if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
1708 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1709 "membar #Sync"
1710 : /* no outputs */
1711 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1712 spitfire_put_itlb_data(i, 0x0UL);
1713 }
1714 }
1715 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1716 cheetah_flush_dtlb_all();
1717 cheetah_flush_itlb_all();
1718 }
1719 __asm__ __volatile__("wrpr %0, 0, %%pstate"
1720 : : "r" (pstate));
1721}