blob: 83a76a2eb71ea157da2a6dc345d3308a985021bb [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
David S. Millerc4bce902006-02-11 21:57:54 -08008#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/sched.h>
11#include <linux/string.h>
12#include <linux/init.h>
13#include <linux/bootmem.h>
14#include <linux/mm.h>
15#include <linux/hugetlb.h>
16#include <linux/slab.h>
17#include <linux/initrd.h>
18#include <linux/swap.h>
19#include <linux/pagemap.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070020#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
David S. Miller372b07b2006-06-21 15:35:28 -070045#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47extern void device_scan(void);
48
David S. Miller9cc3a1a2006-02-21 20:51:13 -080049#define MAX_PHYS_ADDRESS (1UL << 42UL)
50#define KPTE_BITMAP_CHUNK_SZ (256UL * 1024UL * 1024UL)
51#define KPTE_BITMAP_BYTES \
52 ((MAX_PHYS_ADDRESS / KPTE_BITMAP_CHUNK_SZ) / 8)
53
54unsigned long kern_linear_pte_xor[2] __read_mostly;
55
56/* A bitmap, one bit for every 256MB of physical memory. If the bit
57 * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
58 * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
59 */
60unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
61
David S. Millerd1acb422007-03-16 17:20:28 -070062#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Millerd7744a02006-02-21 22:31:11 -080063/* A special kernel TSB for 4MB and 256MB linear mappings. */
64struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
David S. Millerd1acb422007-03-16 17:20:28 -070065#endif
David S. Millerd7744a02006-02-21 22:31:11 -080066
David S. Miller13edad72005-09-29 17:58:26 -070067#define MAX_BANKS 32
David S. Miller10147572005-09-28 21:46:43 -070068
David S. Miller13edad72005-09-29 17:58:26 -070069static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
70static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
71static int pavail_ents __initdata;
72static int pavail_rescan_ents __initdata;
David S. Miller10147572005-09-28 21:46:43 -070073
David S. Miller13edad72005-09-29 17:58:26 -070074static int cmp_p64(const void *a, const void *b)
75{
76 const struct linux_prom64_registers *x = a, *y = b;
77
78 if (x->phys_addr > y->phys_addr)
79 return 1;
80 if (x->phys_addr < y->phys_addr)
81 return -1;
82 return 0;
83}
84
85static void __init read_obp_memory(const char *property,
86 struct linux_prom64_registers *regs,
87 int *num_ents)
88{
89 int node = prom_finddevice("/memory");
90 int prop_size = prom_getproplen(node, property);
91 int ents, ret, i;
92
93 ents = prop_size / sizeof(struct linux_prom64_registers);
94 if (ents > MAX_BANKS) {
95 prom_printf("The machine has more %s property entries than "
96 "this kernel can support (%d).\n",
97 property, MAX_BANKS);
98 prom_halt();
99 }
100
101 ret = prom_getproperty(node, property, (char *) regs, prop_size);
102 if (ret == -1) {
103 prom_printf("Couldn't get %s property from /memory.\n");
104 prom_halt();
105 }
106
David S. Miller13edad72005-09-29 17:58:26 -0700107 /* Sanitize what we got from the firmware, by page aligning
108 * everything.
109 */
110 for (i = 0; i < ents; i++) {
111 unsigned long base, size;
112
113 base = regs[i].phys_addr;
114 size = regs[i].reg_size;
115
116 size &= PAGE_MASK;
117 if (base & ~PAGE_MASK) {
118 unsigned long new_base = PAGE_ALIGN(base);
119
120 size -= new_base - base;
121 if ((long) size < 0L)
122 size = 0UL;
123 base = new_base;
124 }
David S. Miller0015d3d2007-03-15 00:06:34 -0700125 if (size == 0UL) {
126 /* If it is empty, simply get rid of it.
127 * This simplifies the logic of the other
128 * functions that process these arrays.
129 */
130 memmove(&regs[i], &regs[i + 1],
131 (ents - i - 1) * sizeof(regs[0]));
132 i--;
133 ents--;
134 continue;
135 }
David S. Miller13edad72005-09-29 17:58:26 -0700136 regs[i].phys_addr = base;
137 regs[i].reg_size = size;
138 }
David S. Miller486ad102006-06-22 00:00:00 -0700139
David S. Miller486ad102006-06-22 00:00:00 -0700140 *num_ents = ents;
141
David S. Millerc9c10832005-10-12 12:22:46 -0700142 sort(regs, ents, sizeof(struct linux_prom64_registers),
David S. Miller13edad72005-09-29 17:58:26 -0700143 cmp_p64, NULL);
144}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
David S. Miller2bdb3cb2005-09-22 01:08:57 -0700146unsigned long *sparc64_valid_addr_bitmap __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
David S. Millerd1112012006-03-08 02:16:07 -0800148/* Kernel physical address base and size in bytes. */
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700149unsigned long kern_base __read_mostly;
150unsigned long kern_size __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Initial ramdisk setup */
153extern unsigned long sparc_ramdisk_image64;
154extern unsigned int sparc_ramdisk_image;
155extern unsigned int sparc_ramdisk_size;
156
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700157struct page *mem_map_zero __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
David S. Miller0835ae02005-10-04 15:23:20 -0700159unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
160
161unsigned long sparc64_kern_pri_context __read_mostly;
162unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
163unsigned long sparc64_kern_sec_context __read_mostly;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165int bigkernel = 0;
166
Christoph Lametere18b8902006-12-06 20:33:20 -0800167struct kmem_cache *pgtable_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Christoph Lametere18b8902006-12-06 20:33:20 -0800169static void zero_ctor(void *addr, struct kmem_cache *cache, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
David S. Miller3c936462006-01-31 18:30:27 -0800171 clear_page(addr);
172}
173
David S. Miller9b4006d2006-03-18 18:12:42 -0800174extern void tsb_cache_init(void);
175
David S. Miller3c936462006-01-31 18:30:27 -0800176void pgtable_cache_init(void)
177{
178 pgtable_cache = kmem_cache_create("pgtable_cache",
179 PAGE_SIZE, PAGE_SIZE,
180 SLAB_HWCACHE_ALIGN |
181 SLAB_MUST_HWCACHE_ALIGN,
182 zero_ctor,
183 NULL);
184 if (!pgtable_cache) {
David S. Miller9b4006d2006-03-18 18:12:42 -0800185 prom_printf("Could not create pgtable_cache\n");
David S. Miller3c936462006-01-31 18:30:27 -0800186 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
David S. Miller9b4006d2006-03-18 18:12:42 -0800188 tsb_cache_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191#ifdef CONFIG_DEBUG_DCFLUSH
192atomic_t dcpage_flushes = ATOMIC_INIT(0);
193#ifdef CONFIG_SMP
194atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
195#endif
196#endif
197
David S. Miller7a591cf2006-02-26 19:44:50 -0800198inline void flush_dcache_page_impl(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David S. Miller7a591cf2006-02-26 19:44:50 -0800200 BUG_ON(tlb_type == hypervisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#ifdef CONFIG_DEBUG_DCFLUSH
202 atomic_inc(&dcpage_flushes);
203#endif
204
205#ifdef DCACHE_ALIASING_POSSIBLE
206 __flush_dcache_page(page_address(page),
207 ((tlb_type == spitfire) &&
208 page_mapping(page) != NULL));
209#else
210 if (page_mapping(page) != NULL &&
211 tlb_type == spitfire)
212 __flush_icache_page(__pa(page_address(page)));
213#endif
214}
215
216#define PG_dcache_dirty PG_arch_1
David S. Miller17b0e192006-03-08 15:57:03 -0800217#define PG_dcache_cpu_shift 24UL
218#define PG_dcache_cpu_mask (256UL - 1UL)
David S. Miller48b0e542005-07-27 16:08:44 -0700219
220#if NR_CPUS > 256
221#error D-cache dirty tracking and thread_info->cpu need fixing for > 256 cpus
222#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224#define dcache_dirty_cpu(page) \
David S. Miller48b0e542005-07-27 16:08:44 -0700225 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227static __inline__ void set_dcache_dirty(struct page *page, int this_cpu)
228{
229 unsigned long mask = this_cpu;
David S. Miller48b0e542005-07-27 16:08:44 -0700230 unsigned long non_cpu_bits;
231
232 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
233 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 __asm__ __volatile__("1:\n\t"
236 "ldx [%2], %%g7\n\t"
237 "and %%g7, %1, %%g1\n\t"
238 "or %%g1, %0, %%g1\n\t"
239 "casx [%2], %%g7, %%g1\n\t"
240 "cmp %%g7, %%g1\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700241 "membar #StoreLoad | #StoreStore\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700243 " nop"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 : /* no outputs */
245 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
246 : "g1", "g7");
247}
248
249static __inline__ void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
250{
251 unsigned long mask = (1UL << PG_dcache_dirty);
252
253 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
254 "1:\n\t"
255 "ldx [%2], %%g7\n\t"
David S. Miller48b0e542005-07-27 16:08:44 -0700256 "srlx %%g7, %4, %%g1\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 "and %%g1, %3, %%g1\n\t"
258 "cmp %%g1, %0\n\t"
259 "bne,pn %%icc, 2f\n\t"
260 " andn %%g7, %1, %%g1\n\t"
261 "casx [%2], %%g7, %%g1\n\t"
262 "cmp %%g7, %%g1\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700263 "membar #StoreLoad | #StoreStore\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700265 " nop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 "2:"
267 : /* no outputs */
268 : "r" (cpu), "r" (mask), "r" (&page->flags),
David S. Miller48b0e542005-07-27 16:08:44 -0700269 "i" (PG_dcache_cpu_mask),
270 "i" (PG_dcache_cpu_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 : "g1", "g7");
272}
273
David S. Miller517af332006-02-01 15:55:21 -0800274static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
275{
276 unsigned long tsb_addr = (unsigned long) ent;
277
David S. Miller3b3ab2e2006-02-17 09:54:42 -0800278 if (tlb_type == cheetah_plus || tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -0800279 tsb_addr = __pa(tsb_addr);
280
281 __tsb_insert(tsb_addr, tag, pte);
282}
283
David S. Millerc4bce902006-02-11 21:57:54 -0800284unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
285unsigned long _PAGE_SZBITS __read_mostly;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
288{
David S. Millerbd407912006-01-31 18:31:38 -0800289 struct mm_struct *mm;
David S. Miller74ae9982006-03-05 18:26:24 -0800290 struct tsb *tsb;
David S. Miller7a1ac522006-03-16 02:02:32 -0800291 unsigned long tag, flags;
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800292 unsigned long tsb_index, tsb_hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
David S. Miller7a591cf2006-02-26 19:44:50 -0800294 if (tlb_type != hypervisor) {
295 unsigned long pfn = pte_pfn(pte);
296 unsigned long pg_flags;
297 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
David S. Miller7a591cf2006-02-26 19:44:50 -0800299 if (pfn_valid(pfn) &&
300 (page = pfn_to_page(pfn), page_mapping(page)) &&
301 ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
302 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
303 PG_dcache_cpu_mask);
304 int this_cpu = get_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
David S. Miller7a591cf2006-02-26 19:44:50 -0800306 /* This is just to optimize away some function calls
307 * in the SMP case.
308 */
309 if (cpu == this_cpu)
310 flush_dcache_page_impl(page);
311 else
312 smp_flush_dcache_page_impl(page, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
David S. Miller7a591cf2006-02-26 19:44:50 -0800314 clear_dcache_dirty_cpu(page, cpu);
315
316 put_cpu();
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
David S. Millerbd407912006-01-31 18:31:38 -0800319
320 mm = vma->vm_mm;
David S. Miller7a1ac522006-03-16 02:02:32 -0800321
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800322 tsb_index = MM_TSB_BASE;
323 tsb_hash_shift = PAGE_SHIFT;
324
David S. Miller7a1ac522006-03-16 02:02:32 -0800325 spin_lock_irqsave(&mm->context.lock, flags);
326
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800327#ifdef CONFIG_HUGETLB_PAGE
328 if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL) {
329 if ((tlb_type == hypervisor &&
330 (pte_val(pte) & _PAGE_SZALL_4V) == _PAGE_SZHUGE_4V) ||
331 (tlb_type != hypervisor &&
332 (pte_val(pte) & _PAGE_SZALL_4U) == _PAGE_SZHUGE_4U)) {
333 tsb_index = MM_TSB_HUGE;
334 tsb_hash_shift = HPAGE_SHIFT;
335 }
336 }
337#endif
338
339 tsb = mm->context.tsb_block[tsb_index].tsb;
340 tsb += ((address >> tsb_hash_shift) &
341 (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
David S. Miller74ae9982006-03-05 18:26:24 -0800342 tag = (address >> 22UL);
343 tsb_insert(tsb, tag, pte_val(pte));
David S. Miller7a1ac522006-03-16 02:02:32 -0800344
345 spin_unlock_irqrestore(&mm->context.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348void flush_dcache_page(struct page *page)
349{
David S. Millera9546f52005-04-17 18:03:09 -0700350 struct address_space *mapping;
351 int this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
David S. Miller7a591cf2006-02-26 19:44:50 -0800353 if (tlb_type == hypervisor)
354 return;
355
David S. Millera9546f52005-04-17 18:03:09 -0700356 /* Do not bother with the expensive D-cache flush if it
357 * is merely the zero page. The 'bigcore' testcase in GDB
358 * causes this case to run millions of times.
359 */
360 if (page == ZERO_PAGE(0))
361 return;
362
363 this_cpu = get_cpu();
364
365 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (mapping && !mapping_mapped(mapping)) {
David S. Millera9546f52005-04-17 18:03:09 -0700367 int dirty = test_bit(PG_dcache_dirty, &page->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (dirty) {
David S. Millera9546f52005-04-17 18:03:09 -0700369 int dirty_cpu = dcache_dirty_cpu(page);
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (dirty_cpu == this_cpu)
372 goto out;
373 smp_flush_dcache_page_impl(page, dirty_cpu);
374 }
375 set_dcache_dirty(page, this_cpu);
376 } else {
377 /* We could delay the flush for the !page_mapping
378 * case too. But that case is for exec env/arg
379 * pages and those are %99 certainly going to get
380 * faulted into the tlb (and thus flushed) anyways.
381 */
382 flush_dcache_page_impl(page);
383 }
384
385out:
386 put_cpu();
387}
388
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700389void __kprobes flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
David S. Millera43fe0e2006-02-04 03:10:53 -0800391 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (tlb_type == spitfire) {
393 unsigned long kaddr;
394
David S. Millera94aa252007-03-15 15:50:11 -0700395 /* This code only runs on Spitfire cpus so this is
396 * why we can assume _PAGE_PADDR_4U.
397 */
398 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
399 unsigned long paddr, mask = _PAGE_PADDR_4U;
400
401 if (kaddr >= PAGE_OFFSET)
402 paddr = kaddr & mask;
403 else {
404 pgd_t *pgdp = pgd_offset_k(kaddr);
405 pud_t *pudp = pud_offset(pgdp, kaddr);
406 pmd_t *pmdp = pmd_offset(pudp, kaddr);
407 pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
408
409 paddr = pte_val(*ptep) & mask;
410 }
411 __flush_icache_page(paddr);
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416void show_mem(void)
417{
David S. Miller5be4a962007-03-15 16:00:29 -0700418 unsigned long total = 0, reserved = 0;
419 unsigned long shared = 0, cached = 0;
420 pg_data_t *pgdat;
421
David S. Miller28256ca2007-03-15 15:56:07 -0700422 printk(KERN_INFO "Mem-info:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 show_free_areas();
David S. Miller28256ca2007-03-15 15:56:07 -0700424 printk(KERN_INFO "Free swap: %6ldkB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 nr_swap_pages << (PAGE_SHIFT-10));
David S. Miller5be4a962007-03-15 16:00:29 -0700426 for_each_online_pgdat(pgdat) {
427 unsigned long i, flags;
428
429 pgdat_resize_lock(pgdat, &flags);
430 for (i = 0; i < pgdat->node_spanned_pages; i++) {
431 struct page *page = pgdat_page_nr(pgdat, i);
432 total++;
433 if (PageReserved(page))
434 reserved++;
435 else if (PageSwapCache(page))
436 cached++;
437 else if (page_count(page))
438 shared += page_count(page) - 1;
439 }
440 pgdat_resize_unlock(pgdat, &flags);
441 }
442
443 printk(KERN_INFO "%lu pages of RAM\n", total);
444 printk(KERN_INFO "%lu reserved pages\n", reserved);
445 printk(KERN_INFO "%lu pages shared\n", shared);
446 printk(KERN_INFO "%lu pages swap cached\n", cached);
447
448 printk(KERN_INFO "%lu pages dirty\n",
449 global_page_state(NR_FILE_DIRTY));
450 printk(KERN_INFO "%lu pages writeback\n",
451 global_page_state(NR_WRITEBACK));
452 printk(KERN_INFO "%lu pages mapped\n",
453 global_page_state(NR_FILE_MAPPED));
454 printk(KERN_INFO "%lu pages slab\n",
455 global_page_state(NR_SLAB_RECLAIMABLE) +
456 global_page_state(NR_SLAB_UNRECLAIMABLE));
457 printk(KERN_INFO "%lu pages pagetables\n",
458 global_page_state(NR_PAGETABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461void mmu_info(struct seq_file *m)
462{
463 if (tlb_type == cheetah)
464 seq_printf(m, "MMU Type\t: Cheetah\n");
465 else if (tlb_type == cheetah_plus)
466 seq_printf(m, "MMU Type\t: Cheetah+\n");
467 else if (tlb_type == spitfire)
468 seq_printf(m, "MMU Type\t: Spitfire\n");
David S. Millera43fe0e2006-02-04 03:10:53 -0800469 else if (tlb_type == hypervisor)
470 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 else
472 seq_printf(m, "MMU Type\t: ???\n");
473
474#ifdef CONFIG_DEBUG_DCFLUSH
475 seq_printf(m, "DCPageFlushes\t: %d\n",
476 atomic_read(&dcpage_flushes));
477#ifdef CONFIG_SMP
478 seq_printf(m, "DCPageFlushesXC\t: %d\n",
479 atomic_read(&dcpage_flushes_xcall));
480#endif /* CONFIG_SMP */
481#endif /* CONFIG_DEBUG_DCFLUSH */
482}
483
David S. Millera94aa252007-03-15 15:50:11 -0700484struct linux_prom_translation {
485 unsigned long virt;
486 unsigned long size;
487 unsigned long data;
488};
489
490/* Exported for kernel TLB miss handling in ktlb.S */
491struct linux_prom_translation prom_trans[512] __read_mostly;
492unsigned int prom_trans_ents __read_mostly;
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/* Exported for SMP bootup purposes. */
495unsigned long kern_locked_tte_data;
496
David S. Miller405599b2005-09-22 00:12:35 -0700497/* The obp translations are saved based on 8k pagesize, since obp can
498 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
David S. Miller74bf4312006-01-31 18:29:18 -0800499 * HI_OBP_ADDRESS range are handled in ktlb.S.
David S. Miller405599b2005-09-22 00:12:35 -0700500 */
David S. Miller5085b4a2005-09-22 00:45:41 -0700501static inline int in_obp_range(unsigned long vaddr)
502{
503 return (vaddr >= LOW_OBP_ADDRESS &&
504 vaddr < HI_OBP_ADDRESS);
505}
506
David S. Millerc9c10832005-10-12 12:22:46 -0700507static int cmp_ptrans(const void *a, const void *b)
David S. Miller405599b2005-09-22 00:12:35 -0700508{
David S. Millerc9c10832005-10-12 12:22:46 -0700509 const struct linux_prom_translation *x = a, *y = b;
David S. Miller405599b2005-09-22 00:12:35 -0700510
David S. Millerc9c10832005-10-12 12:22:46 -0700511 if (x->virt > y->virt)
512 return 1;
513 if (x->virt < y->virt)
514 return -1;
515 return 0;
David S. Miller405599b2005-09-22 00:12:35 -0700516}
517
David S. Millerc9c10832005-10-12 12:22:46 -0700518/* Read OBP translations property into 'prom_trans[]'. */
David S. Miller9ad98c52005-10-05 15:12:00 -0700519static void __init read_obp_translations(void)
David S. Miller405599b2005-09-22 00:12:35 -0700520{
David S. Millerc9c10832005-10-12 12:22:46 -0700521 int n, node, ents, first, last, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 node = prom_finddevice("/virtual-memory");
524 n = prom_getproplen(node, "translations");
David S. Miller405599b2005-09-22 00:12:35 -0700525 if (unlikely(n == 0 || n == -1)) {
David S. Millerb206fc42005-09-21 22:31:13 -0700526 prom_printf("prom_mappings: Couldn't get size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 prom_halt();
528 }
David S. Miller405599b2005-09-22 00:12:35 -0700529 if (unlikely(n > sizeof(prom_trans))) {
530 prom_printf("prom_mappings: Size %Zd is too big.\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 prom_halt();
532 }
David S. Miller405599b2005-09-22 00:12:35 -0700533
David S. Millerb206fc42005-09-21 22:31:13 -0700534 if ((n = prom_getproperty(node, "translations",
David S. Miller405599b2005-09-22 00:12:35 -0700535 (char *)&prom_trans[0],
536 sizeof(prom_trans))) == -1) {
David S. Millerb206fc42005-09-21 22:31:13 -0700537 prom_printf("prom_mappings: Couldn't get property.\n");
538 prom_halt();
539 }
David S. Miller9ad98c52005-10-05 15:12:00 -0700540
David S. Millerb206fc42005-09-21 22:31:13 -0700541 n = n / sizeof(struct linux_prom_translation);
David S. Miller9ad98c52005-10-05 15:12:00 -0700542
David S. Millerc9c10832005-10-12 12:22:46 -0700543 ents = n;
544
545 sort(prom_trans, ents, sizeof(struct linux_prom_translation),
546 cmp_ptrans, NULL);
547
548 /* Now kick out all the non-OBP entries. */
549 for (i = 0; i < ents; i++) {
550 if (in_obp_range(prom_trans[i].virt))
551 break;
552 }
553 first = i;
554 for (; i < ents; i++) {
555 if (!in_obp_range(prom_trans[i].virt))
556 break;
557 }
558 last = i;
559
560 for (i = 0; i < (last - first); i++) {
561 struct linux_prom_translation *src = &prom_trans[i + first];
562 struct linux_prom_translation *dest = &prom_trans[i];
563
564 *dest = *src;
565 }
566 for (; i < ents; i++) {
567 struct linux_prom_translation *dest = &prom_trans[i];
568 dest->virt = dest->size = dest->data = 0x0UL;
569 }
570
571 prom_trans_ents = last - first;
572
573 if (tlb_type == spitfire) {
574 /* Clear diag TTE bits. */
575 for (i = 0; i < prom_trans_ents; i++)
576 prom_trans[i].data &= ~0x0003fe0000000000UL;
577 }
David S. Miller405599b2005-09-22 00:12:35 -0700578}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
David S. Millerd82ace72006-02-09 02:52:44 -0800580static void __init hypervisor_tlb_lock(unsigned long vaddr,
581 unsigned long pte,
582 unsigned long mmu)
583{
David S. Miller164c2202006-02-09 22:57:21 -0800584 register unsigned long func asm("%o5");
585 register unsigned long arg0 asm("%o0");
586 register unsigned long arg1 asm("%o1");
587 register unsigned long arg2 asm("%o2");
588 register unsigned long arg3 asm("%o3");
David S. Millerd82ace72006-02-09 02:52:44 -0800589
590 func = HV_FAST_MMU_MAP_PERM_ADDR;
591 arg0 = vaddr;
592 arg1 = 0;
593 arg2 = pte;
594 arg3 = mmu;
595 __asm__ __volatile__("ta 0x80"
596 : "=&r" (func), "=&r" (arg0),
597 "=&r" (arg1), "=&r" (arg2),
598 "=&r" (arg3)
599 : "0" (func), "1" (arg0), "2" (arg1),
600 "3" (arg2), "4" (arg3));
David S. Miller12e126a2006-02-17 14:40:30 -0800601 if (arg0 != 0) {
602 prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
603 "errors with %lx\n", vaddr, 0, pte, mmu, arg0);
604 prom_halt();
605 }
David S. Millerd82ace72006-02-09 02:52:44 -0800606}
607
David S. Millerc4bce902006-02-11 21:57:54 -0800608static unsigned long kern_large_tte(unsigned long paddr);
609
David S. Miller898cf0e2005-09-23 11:59:44 -0700610static void __init remap_kernel(void)
David S. Miller405599b2005-09-22 00:12:35 -0700611{
612 unsigned long phys_page, tte_vaddr, tte_data;
David S. Miller405599b2005-09-22 00:12:35 -0700613 int tlb_ent = sparc64_highest_locked_tlbent();
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 tte_vaddr = (unsigned long) KERNBASE;
David S. Millerbff06d52005-09-22 20:11:33 -0700616 phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
David S. Millerc4bce902006-02-11 21:57:54 -0800617 tte_data = kern_large_tte(phys_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 kern_locked_tte_data = tte_data;
620
David S. Millerd82ace72006-02-09 02:52:44 -0800621 /* Now lock us into the TLBs via Hypervisor or OBP. */
622 if (tlb_type == hypervisor) {
623 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
624 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
625 if (bigkernel) {
626 tte_vaddr += 0x400000;
627 tte_data += 0x400000;
628 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
629 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
630 }
631 } else {
632 prom_dtlb_load(tlb_ent, tte_data, tte_vaddr);
633 prom_itlb_load(tlb_ent, tte_data, tte_vaddr);
634 if (bigkernel) {
635 tlb_ent -= 1;
636 prom_dtlb_load(tlb_ent,
637 tte_data + 0x400000,
638 tte_vaddr + 0x400000);
639 prom_itlb_load(tlb_ent,
640 tte_data + 0x400000,
641 tte_vaddr + 0x400000);
642 }
643 sparc64_highest_unlocked_tlb_ent = tlb_ent - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
David S. Miller0835ae02005-10-04 15:23:20 -0700645 if (tlb_type == cheetah_plus) {
646 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
647 CTX_CHEETAH_PLUS_NUC);
648 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
649 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
650 }
David S. Miller405599b2005-09-22 00:12:35 -0700651}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
David S. Miller405599b2005-09-22 00:12:35 -0700653
David S. Millerc9c10832005-10-12 12:22:46 -0700654static void __init inherit_prom_mappings(void)
David S. Miller9ad98c52005-10-05 15:12:00 -0700655{
656 read_obp_translations();
David S. Miller405599b2005-09-22 00:12:35 -0700657
658 /* Now fixup OBP's idea about where we really are mapped. */
659 prom_printf("Remapping the kernel... ");
660 remap_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 prom_printf("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664void prom_world(int enter)
665{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (!enter)
667 set_fs((mm_segment_t) { get_thread_current_ds() });
668
David S. Miller3487d1d2006-01-31 18:33:25 -0800669 __asm__ __volatile__("flushw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672#ifdef DCACHE_ALIASING_POSSIBLE
673void __flush_dcache_range(unsigned long start, unsigned long end)
674{
675 unsigned long va;
676
677 if (tlb_type == spitfire) {
678 int n = 0;
679
680 for (va = start; va < end; va += 32) {
681 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
682 if (++n >= 512)
683 break;
684 }
David S. Millera43fe0e2006-02-04 03:10:53 -0800685 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 start = __pa(start);
687 end = __pa(end);
688 for (va = start; va < end; va += 32)
689 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
690 "membar #Sync"
691 : /* no outputs */
692 : "r" (va),
693 "i" (ASI_DCACHE_INVALIDATE));
694 }
695}
696#endif /* DCACHE_ALIASING_POSSIBLE */
697
David S. Miller85f1e1f2007-03-15 17:51:26 -0700698/* get_new_mmu_context() uses "cache + 1". */
699DEFINE_SPINLOCK(ctx_alloc_lock);
700unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
701#define MAX_CTX_NR (1UL << CTX_NR_BITS)
702#define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
703DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/* Caller does TLB context flushing on local CPU if necessary.
706 * The caller also ensures that CTX_VALID(mm->context) is false.
707 *
708 * We must be careful about boundary cases so that we never
709 * let the user have CTX 0 (nucleus) or we ever use a CTX
710 * version of zero (and thus NO_CONTEXT would not be caught
711 * by version mis-match tests in mmu_context.h).
David S. Millera0663a72006-02-23 14:19:28 -0800712 *
713 * Always invoked with interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
715void get_new_mmu_context(struct mm_struct *mm)
716{
717 unsigned long ctx, new_ctx;
718 unsigned long orig_pgsz_bits;
David S. Millera77754b2006-03-06 19:59:50 -0800719 unsigned long flags;
David S. Millera0663a72006-02-23 14:19:28 -0800720 int new_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
David S. Millera77754b2006-03-06 19:59:50 -0800722 spin_lock_irqsave(&ctx_alloc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
724 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
725 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
David S. Millera0663a72006-02-23 14:19:28 -0800726 new_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (new_ctx >= (1 << CTX_NR_BITS)) {
728 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
729 if (new_ctx >= ctx) {
730 int i;
731 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
732 CTX_FIRST_VERSION;
733 if (new_ctx == 1)
734 new_ctx = CTX_FIRST_VERSION;
735
736 /* Don't call memset, for 16 entries that's just
737 * plain silly...
738 */
739 mmu_context_bmap[0] = 3;
740 mmu_context_bmap[1] = 0;
741 mmu_context_bmap[2] = 0;
742 mmu_context_bmap[3] = 0;
743 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
744 mmu_context_bmap[i + 0] = 0;
745 mmu_context_bmap[i + 1] = 0;
746 mmu_context_bmap[i + 2] = 0;
747 mmu_context_bmap[i + 3] = 0;
748 }
David S. Millera0663a72006-02-23 14:19:28 -0800749 new_version = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 goto out;
751 }
752 }
753 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
754 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
755out:
756 tlb_context_cache = new_ctx;
757 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
David S. Millera77754b2006-03-06 19:59:50 -0800758 spin_unlock_irqrestore(&ctx_alloc_lock, flags);
David S. Millera0663a72006-02-23 14:19:28 -0800759
760 if (unlikely(new_version))
761 smp_new_mmu_context_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764void sparc_ultra_dump_itlb(void)
765{
766 int slot;
767
768 if (tlb_type == spitfire) {
769 printk ("Contents of itlb: ");
770 for (slot = 0; slot < 14; slot++) printk (" ");
771 printk ("%2x:%016lx,%016lx\n",
772 0,
773 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
774 for (slot = 1; slot < 64; slot+=3) {
775 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
776 slot,
777 spitfire_get_itlb_tag(slot), spitfire_get_itlb_data(slot),
778 slot+1,
779 spitfire_get_itlb_tag(slot+1), spitfire_get_itlb_data(slot+1),
780 slot+2,
781 spitfire_get_itlb_tag(slot+2), spitfire_get_itlb_data(slot+2));
782 }
783 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
784 printk ("Contents of itlb0:\n");
785 for (slot = 0; slot < 16; slot+=2) {
786 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
787 slot,
788 cheetah_get_litlb_tag(slot), cheetah_get_litlb_data(slot),
789 slot+1,
790 cheetah_get_litlb_tag(slot+1), cheetah_get_litlb_data(slot+1));
791 }
792 printk ("Contents of itlb2:\n");
793 for (slot = 0; slot < 128; slot+=2) {
794 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
795 slot,
796 cheetah_get_itlb_tag(slot), cheetah_get_itlb_data(slot),
797 slot+1,
798 cheetah_get_itlb_tag(slot+1), cheetah_get_itlb_data(slot+1));
799 }
800 }
801}
802
803void sparc_ultra_dump_dtlb(void)
804{
805 int slot;
806
807 if (tlb_type == spitfire) {
808 printk ("Contents of dtlb: ");
809 for (slot = 0; slot < 14; slot++) printk (" ");
810 printk ("%2x:%016lx,%016lx\n", 0,
811 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
812 for (slot = 1; slot < 64; slot+=3) {
813 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
814 slot,
815 spitfire_get_dtlb_tag(slot), spitfire_get_dtlb_data(slot),
816 slot+1,
817 spitfire_get_dtlb_tag(slot+1), spitfire_get_dtlb_data(slot+1),
818 slot+2,
819 spitfire_get_dtlb_tag(slot+2), spitfire_get_dtlb_data(slot+2));
820 }
821 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
822 printk ("Contents of dtlb0:\n");
823 for (slot = 0; slot < 16; slot+=2) {
824 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
825 slot,
826 cheetah_get_ldtlb_tag(slot), cheetah_get_ldtlb_data(slot),
827 slot+1,
828 cheetah_get_ldtlb_tag(slot+1), cheetah_get_ldtlb_data(slot+1));
829 }
830 printk ("Contents of dtlb2:\n");
831 for (slot = 0; slot < 512; slot+=2) {
832 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
833 slot,
834 cheetah_get_dtlb_tag(slot, 2), cheetah_get_dtlb_data(slot, 2),
835 slot+1,
836 cheetah_get_dtlb_tag(slot+1, 2), cheetah_get_dtlb_data(slot+1, 2));
837 }
838 if (tlb_type == cheetah_plus) {
839 printk ("Contents of dtlb3:\n");
840 for (slot = 0; slot < 512; slot+=2) {
841 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
842 slot,
843 cheetah_get_dtlb_tag(slot, 3), cheetah_get_dtlb_data(slot, 3),
844 slot+1,
845 cheetah_get_dtlb_tag(slot+1, 3), cheetah_get_dtlb_data(slot+1, 3));
846 }
847 }
848 }
849}
850
851extern unsigned long cmdline_memory_size;
852
David S. Millerd1112012006-03-08 02:16:07 -0800853/* Find a free area for the bootmem map, avoiding the kernel image
854 * and the initial ramdisk.
855 */
856static unsigned long __init choose_bootmap_pfn(unsigned long start_pfn,
857 unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
David S. Millerd1112012006-03-08 02:16:07 -0800859 unsigned long avoid_start, avoid_end, bootmap_size;
860 int i;
861
862 bootmap_size = ((end_pfn - start_pfn) + 7) / 8;
863 bootmap_size = ALIGN(bootmap_size, sizeof(long));
864
865 avoid_start = avoid_end = 0;
866#ifdef CONFIG_BLK_DEV_INITRD
867 avoid_start = initrd_start;
868 avoid_end = PAGE_ALIGN(initrd_end);
869#endif
870
871#ifdef CONFIG_DEBUG_BOOTMEM
872 prom_printf("choose_bootmap_pfn: kern[%lx:%lx] avoid[%lx:%lx]\n",
873 kern_base, PAGE_ALIGN(kern_base + kern_size),
874 avoid_start, avoid_end);
875#endif
876 for (i = 0; i < pavail_ents; i++) {
877 unsigned long start, end;
878
879 start = pavail[i].phys_addr;
880 end = start + pavail[i].reg_size;
881
882 while (start < end) {
883 if (start >= kern_base &&
884 start < PAGE_ALIGN(kern_base + kern_size)) {
885 start = PAGE_ALIGN(kern_base + kern_size);
886 continue;
887 }
888 if (start >= avoid_start && start < avoid_end) {
889 start = avoid_end;
890 continue;
891 }
892
893 if ((end - start) < bootmap_size)
894 break;
895
896 if (start < kern_base &&
897 (start + bootmap_size) > kern_base) {
898 start = PAGE_ALIGN(kern_base + kern_size);
899 continue;
900 }
901
902 if (start < avoid_start &&
903 (start + bootmap_size) > avoid_start) {
904 start = avoid_end;
905 continue;
906 }
907
908 /* OK, it doesn't overlap anything, use it. */
909#ifdef CONFIG_DEBUG_BOOTMEM
910 prom_printf("choose_bootmap_pfn: Using %lx [%lx]\n",
911 start >> PAGE_SHIFT, start);
912#endif
913 return start >> PAGE_SHIFT;
914 }
915 }
916
917 prom_printf("Cannot find free area for bootmap, aborting.\n");
918 prom_halt();
919}
920
David S. Miller6fc5bae2006-12-28 21:00:23 -0800921static void __init trim_pavail(unsigned long *cur_size_p,
922 unsigned long *end_of_phys_p)
923{
924 unsigned long to_trim = *cur_size_p - cmdline_memory_size;
925 unsigned long avoid_start, avoid_end;
926 int i;
927
928 to_trim = PAGE_ALIGN(to_trim);
929
930 avoid_start = avoid_end = 0;
931#ifdef CONFIG_BLK_DEV_INITRD
932 avoid_start = initrd_start;
933 avoid_end = PAGE_ALIGN(initrd_end);
934#endif
935
936 /* Trim some pavail[] entries in order to satisfy the
937 * requested "mem=xxx" kernel command line specification.
938 *
939 * We must not trim off the kernel image area nor the
940 * initial ramdisk range (if any). Also, we must not trim
941 * any pavail[] entry down to zero in order to preserve
942 * the invariant that all pavail[] entries have a non-zero
943 * size which is assumed by all of the code in here.
944 */
945 for (i = 0; i < pavail_ents; i++) {
946 unsigned long start, end, kern_end;
947 unsigned long trim_low, trim_high, n;
948
949 kern_end = PAGE_ALIGN(kern_base + kern_size);
950
951 trim_low = start = pavail[i].phys_addr;
952 trim_high = end = start + pavail[i].reg_size;
953
954 if (kern_base >= start &&
955 kern_base < end) {
956 trim_low = kern_base;
957 if (kern_end >= end)
958 continue;
959 }
960 if (kern_end >= start &&
961 kern_end < end) {
962 trim_high = kern_end;
963 }
964 if (avoid_start &&
965 avoid_start >= start &&
966 avoid_start < end) {
967 if (trim_low > avoid_start)
968 trim_low = avoid_start;
969 if (avoid_end >= end)
970 continue;
971 }
972 if (avoid_end &&
973 avoid_end >= start &&
974 avoid_end < end) {
975 if (trim_high < avoid_end)
976 trim_high = avoid_end;
977 }
978
979 if (trim_high <= trim_low)
980 continue;
981
982 if (trim_low == start && trim_high == end) {
983 /* Whole chunk is available for trimming.
984 * Trim all except one page, in order to keep
985 * entry non-empty.
986 */
987 n = (end - start) - PAGE_SIZE;
988 if (n > to_trim)
989 n = to_trim;
990
991 if (n) {
992 pavail[i].phys_addr += n;
993 pavail[i].reg_size -= n;
994 to_trim -= n;
995 }
996 } else {
997 n = (trim_low - start);
998 if (n > to_trim)
999 n = to_trim;
1000
1001 if (n) {
1002 pavail[i].phys_addr += n;
1003 pavail[i].reg_size -= n;
1004 to_trim -= n;
1005 }
1006 if (to_trim) {
1007 n = end - trim_high;
1008 if (n > to_trim)
1009 n = to_trim;
1010 if (n) {
1011 pavail[i].reg_size -= n;
1012 to_trim -= n;
1013 }
1014 }
1015 }
1016
1017 if (!to_trim)
1018 break;
1019 }
1020
1021 /* Recalculate. */
1022 *cur_size_p = 0UL;
1023 for (i = 0; i < pavail_ents; i++) {
1024 *end_of_phys_p = pavail[i].phys_addr +
1025 pavail[i].reg_size;
1026 *cur_size_p += pavail[i].reg_size;
1027 }
1028}
1029
David S. Millerd1112012006-03-08 02:16:07 -08001030static unsigned long __init bootmem_init(unsigned long *pages_avail,
1031 unsigned long phys_base)
1032{
1033 unsigned long bootmap_size, end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 unsigned long end_of_phys_memory = 0UL;
1035 unsigned long bootmap_pfn, bytes_avail, size;
1036 int i;
1037
1038#ifdef CONFIG_DEBUG_BOOTMEM
David S. Miller13edad72005-09-29 17:58:26 -07001039 prom_printf("bootmem_init: Scan pavail, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040#endif
1041
1042 bytes_avail = 0UL;
David S. Miller13edad72005-09-29 17:58:26 -07001043 for (i = 0; i < pavail_ents; i++) {
1044 end_of_phys_memory = pavail[i].phys_addr +
1045 pavail[i].reg_size;
1046 bytes_avail += pavail[i].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
David S. Miller6fc5bae2006-12-28 21:00:23 -08001049 /* Determine the location of the initial ramdisk before trying
1050 * to honor the "mem=xxx" command line argument. We must know
1051 * where the kernel image and the ramdisk image are so that we
1052 * do not trim those two areas from the physical memory map.
1053 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055#ifdef CONFIG_BLK_DEV_INITRD
1056 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
1057 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
1058 unsigned long ramdisk_image = sparc_ramdisk_image ?
1059 sparc_ramdisk_image : sparc_ramdisk_image64;
David S. Miller715a0ec2006-09-26 23:14:21 -07001060 ramdisk_image -= KERNBASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 initrd_start = ramdisk_image + phys_base;
1062 initrd_end = initrd_start + sparc_ramdisk_size;
1063 if (initrd_end > end_of_phys_memory) {
1064 printk(KERN_CRIT "initrd extends beyond end of memory "
1065 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
1066 initrd_end, end_of_phys_memory);
1067 initrd_start = 0;
David S. Millerd1112012006-03-08 02:16:07 -08001068 initrd_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070 }
1071#endif
David S. Miller6fc5bae2006-12-28 21:00:23 -08001072
1073 if (cmdline_memory_size &&
1074 bytes_avail > cmdline_memory_size)
1075 trim_pavail(&bytes_avail,
1076 &end_of_phys_memory);
1077
1078 *pages_avail = bytes_avail >> PAGE_SHIFT;
1079
1080 end_pfn = end_of_phys_memory >> PAGE_SHIFT;
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /* Initialize the boot-time allocator. */
1083 max_pfn = max_low_pfn = end_pfn;
David S. Millerd1112012006-03-08 02:16:07 -08001084 min_low_pfn = (phys_base >> PAGE_SHIFT);
1085
1086 bootmap_pfn = choose_bootmap_pfn(min_low_pfn, end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088#ifdef CONFIG_DEBUG_BOOTMEM
1089 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
1090 min_low_pfn, bootmap_pfn, max_low_pfn);
1091#endif
David S. Millerd1112012006-03-08 02:16:07 -08001092 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn,
David S. Miller17b0e192006-03-08 15:57:03 -08001093 min_low_pfn, end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 /* Now register the available physical memory with the
1096 * allocator.
1097 */
David S. Miller13edad72005-09-29 17:58:26 -07001098 for (i = 0; i < pavail_ents; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099#ifdef CONFIG_DEBUG_BOOTMEM
David S. Miller13edad72005-09-29 17:58:26 -07001100 prom_printf("free_bootmem(pavail:%d): base[%lx] size[%lx]\n",
1101 i, pavail[i].phys_addr, pavail[i].reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102#endif
David S. Miller13edad72005-09-29 17:58:26 -07001103 free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105
1106#ifdef CONFIG_BLK_DEV_INITRD
1107 if (initrd_start) {
1108 size = initrd_end - initrd_start;
1109
1110 /* Resert the initrd image area. */
1111#ifdef CONFIG_DEBUG_BOOTMEM
1112 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
1113 initrd_start, initrd_end);
1114#endif
1115 reserve_bootmem(initrd_start, size);
1116 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1117
1118 initrd_start += PAGE_OFFSET;
1119 initrd_end += PAGE_OFFSET;
1120 }
1121#endif
1122 /* Reserve the kernel text/data/bss. */
1123#ifdef CONFIG_DEBUG_BOOTMEM
1124 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base, kern_size);
1125#endif
1126 reserve_bootmem(kern_base, kern_size);
1127 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
1128
1129 /* Reserve the bootmem map. We do not account for it
1130 * in pages_avail because we will release that memory
1131 * in free_all_bootmem.
1132 */
1133 size = bootmap_size;
1134#ifdef CONFIG_DEBUG_BOOTMEM
1135 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
1136 (bootmap_pfn << PAGE_SHIFT), size);
1137#endif
1138 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size);
1139 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
1140
David S. Millerd1112012006-03-08 02:16:07 -08001141 for (i = 0; i < pavail_ents; i++) {
1142 unsigned long start_pfn, end_pfn;
1143
1144 start_pfn = pavail[i].phys_addr >> PAGE_SHIFT;
1145 end_pfn = (start_pfn + (pavail[i].reg_size >> PAGE_SHIFT));
1146#ifdef CONFIG_DEBUG_BOOTMEM
1147 prom_printf("memory_present(0, %lx, %lx)\n",
1148 start_pfn, end_pfn);
1149#endif
1150 memory_present(0, start_pfn, end_pfn);
1151 }
1152
1153 sparse_init();
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return end_pfn;
1156}
1157
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001158static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
1159static int pall_ents __initdata;
1160
David S. Miller56425302005-09-25 16:46:57 -07001161#ifdef CONFIG_DEBUG_PAGEALLOC
1162static unsigned long kernel_map_range(unsigned long pstart, unsigned long pend, pgprot_t prot)
1163{
1164 unsigned long vstart = PAGE_OFFSET + pstart;
1165 unsigned long vend = PAGE_OFFSET + pend;
1166 unsigned long alloc_bytes = 0UL;
1167
1168 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
David S. Miller13edad72005-09-29 17:58:26 -07001169 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
David S. Miller56425302005-09-25 16:46:57 -07001170 vstart, vend);
1171 prom_halt();
1172 }
1173
1174 while (vstart < vend) {
1175 unsigned long this_end, paddr = __pa(vstart);
1176 pgd_t *pgd = pgd_offset_k(vstart);
1177 pud_t *pud;
1178 pmd_t *pmd;
1179 pte_t *pte;
1180
1181 pud = pud_offset(pgd, vstart);
1182 if (pud_none(*pud)) {
1183 pmd_t *new;
1184
1185 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1186 alloc_bytes += PAGE_SIZE;
1187 pud_populate(&init_mm, pud, new);
1188 }
1189
1190 pmd = pmd_offset(pud, vstart);
1191 if (!pmd_present(*pmd)) {
1192 pte_t *new;
1193
1194 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1195 alloc_bytes += PAGE_SIZE;
1196 pmd_populate_kernel(&init_mm, pmd, new);
1197 }
1198
1199 pte = pte_offset_kernel(pmd, vstart);
1200 this_end = (vstart + PMD_SIZE) & PMD_MASK;
1201 if (this_end > vend)
1202 this_end = vend;
1203
1204 while (vstart < this_end) {
1205 pte_val(*pte) = (paddr | pgprot_val(prot));
1206
1207 vstart += PAGE_SIZE;
1208 paddr += PAGE_SIZE;
1209 pte++;
1210 }
1211 }
1212
1213 return alloc_bytes;
1214}
1215
David S. Miller56425302005-09-25 16:46:57 -07001216extern unsigned int kvmap_linear_patch[1];
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001217#endif /* CONFIG_DEBUG_PAGEALLOC */
1218
1219static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
1220{
1221 const unsigned long shift_256MB = 28;
1222 const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
1223 const unsigned long size_256MB = (1UL << shift_256MB);
1224
1225 while (start < end) {
1226 long remains;
1227
David S. Millerf7c00332006-03-05 22:18:50 -08001228 remains = end - start;
1229 if (remains < size_256MB)
1230 break;
1231
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001232 if (start & mask_256MB) {
1233 start = (start + size_256MB) & ~mask_256MB;
1234 continue;
1235 }
1236
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001237 while (remains >= size_256MB) {
1238 unsigned long index = start >> shift_256MB;
1239
1240 __set_bit(index, kpte_linear_bitmap);
1241
1242 start += size_256MB;
1243 remains -= size_256MB;
1244 }
1245 }
1246}
David S. Miller56425302005-09-25 16:46:57 -07001247
1248static void __init kernel_physical_mapping_init(void)
1249{
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001250 unsigned long i;
1251#ifdef CONFIG_DEBUG_PAGEALLOC
1252 unsigned long mem_alloced = 0UL;
1253#endif
David S. Miller56425302005-09-25 16:46:57 -07001254
David S. Miller13edad72005-09-29 17:58:26 -07001255 read_obp_memory("reg", &pall[0], &pall_ents);
1256
1257 for (i = 0; i < pall_ents; i++) {
David S. Miller56425302005-09-25 16:46:57 -07001258 unsigned long phys_start, phys_end;
1259
David S. Miller13edad72005-09-29 17:58:26 -07001260 phys_start = pall[i].phys_addr;
1261 phys_end = phys_start + pall[i].reg_size;
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001262
1263 mark_kpte_bitmap(phys_start, phys_end);
1264
1265#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller56425302005-09-25 16:46:57 -07001266 mem_alloced += kernel_map_range(phys_start, phys_end,
1267 PAGE_KERNEL);
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001268#endif
David S. Miller56425302005-09-25 16:46:57 -07001269 }
1270
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001271#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller56425302005-09-25 16:46:57 -07001272 printk("Allocated %ld bytes for kernel page tables.\n",
1273 mem_alloced);
1274
1275 kvmap_linear_patch[0] = 0x01000000; /* nop */
1276 flushi(&kvmap_linear_patch[0]);
1277
1278 __flush_tlb_all();
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001279#endif
David S. Miller56425302005-09-25 16:46:57 -07001280}
1281
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001282#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller56425302005-09-25 16:46:57 -07001283void kernel_map_pages(struct page *page, int numpages, int enable)
1284{
1285 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1286 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1287
1288 kernel_map_range(phys_start, phys_end,
1289 (enable ? PAGE_KERNEL : __pgprot(0)));
1290
David S. Miller74bf4312006-01-31 18:29:18 -08001291 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1292 PAGE_OFFSET + phys_end);
1293
David S. Miller56425302005-09-25 16:46:57 -07001294 /* we should perform an IPI and flush all tlbs,
1295 * but that can deadlock->flush only current cpu.
1296 */
1297 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1298 PAGE_OFFSET + phys_end);
1299}
1300#endif
1301
David S. Miller10147572005-09-28 21:46:43 -07001302unsigned long __init find_ecache_flush_span(unsigned long size)
1303{
David S. Miller13edad72005-09-29 17:58:26 -07001304 int i;
David S. Miller10147572005-09-28 21:46:43 -07001305
David S. Miller13edad72005-09-29 17:58:26 -07001306 for (i = 0; i < pavail_ents; i++) {
1307 if (pavail[i].reg_size >= size)
1308 return pavail[i].phys_addr;
David S. Miller10147572005-09-28 21:46:43 -07001309 }
1310
1311 return ~0UL;
1312}
1313
David S. Miller517af332006-02-01 15:55:21 -08001314static void __init tsb_phys_patch(void)
1315{
David S. Millerd257d5d2006-02-06 23:44:37 -08001316 struct tsb_ldquad_phys_patch_entry *pquad;
David S. Miller517af332006-02-01 15:55:21 -08001317 struct tsb_phys_patch_entry *p;
1318
David S. Millerd257d5d2006-02-06 23:44:37 -08001319 pquad = &__tsb_ldquad_phys_patch;
1320 while (pquad < &__tsb_ldquad_phys_patch_end) {
1321 unsigned long addr = pquad->addr;
1322
1323 if (tlb_type == hypervisor)
1324 *(unsigned int *) addr = pquad->sun4v_insn;
1325 else
1326 *(unsigned int *) addr = pquad->sun4u_insn;
1327 wmb();
1328 __asm__ __volatile__("flush %0"
1329 : /* no outputs */
1330 : "r" (addr));
1331
1332 pquad++;
1333 }
1334
David S. Miller517af332006-02-01 15:55:21 -08001335 p = &__tsb_phys_patch;
1336 while (p < &__tsb_phys_patch_end) {
1337 unsigned long addr = p->addr;
1338
1339 *(unsigned int *) addr = p->insn;
1340 wmb();
1341 __asm__ __volatile__("flush %0"
1342 : /* no outputs */
1343 : "r" (addr));
1344
1345 p++;
1346 }
1347}
1348
David S. Miller490384e2006-02-11 14:41:18 -08001349/* Don't mark as init, we give this to the Hypervisor. */
David S. Millerd1acb422007-03-16 17:20:28 -07001350#ifndef CONFIG_DEBUG_PAGEALLOC
1351#define NUM_KTSB_DESCR 2
1352#else
1353#define NUM_KTSB_DESCR 1
1354#endif
1355static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
David S. Miller490384e2006-02-11 14:41:18 -08001356extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
1357
1358static void __init sun4v_ktsb_init(void)
1359{
1360 unsigned long ktsb_pa;
1361
David S. Millerd7744a02006-02-21 22:31:11 -08001362 /* First KTSB for PAGE_SIZE mappings. */
David S. Miller490384e2006-02-11 14:41:18 -08001363 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
1364
1365 switch (PAGE_SIZE) {
1366 case 8 * 1024:
1367 default:
1368 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
1369 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
1370 break;
1371
1372 case 64 * 1024:
1373 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
1374 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
1375 break;
1376
1377 case 512 * 1024:
1378 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
1379 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
1380 break;
1381
1382 case 4 * 1024 * 1024:
1383 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
1384 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
1385 break;
1386 };
1387
David S. Miller3f19a842006-02-17 12:03:20 -08001388 ktsb_descr[0].assoc = 1;
David S. Miller490384e2006-02-11 14:41:18 -08001389 ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
1390 ktsb_descr[0].ctx_idx = 0;
1391 ktsb_descr[0].tsb_base = ktsb_pa;
1392 ktsb_descr[0].resv = 0;
1393
David S. Millerd1acb422007-03-16 17:20:28 -07001394#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Millerd7744a02006-02-21 22:31:11 -08001395 /* Second KTSB for 4MB/256MB mappings. */
1396 ktsb_pa = (kern_base +
1397 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
1398
1399 ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
1400 ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
1401 HV_PGSZ_MASK_256MB);
1402 ktsb_descr[1].assoc = 1;
1403 ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
1404 ktsb_descr[1].ctx_idx = 0;
1405 ktsb_descr[1].tsb_base = ktsb_pa;
1406 ktsb_descr[1].resv = 0;
David S. Millerd1acb422007-03-16 17:20:28 -07001407#endif
David S. Miller490384e2006-02-11 14:41:18 -08001408}
1409
1410void __cpuinit sun4v_ktsb_register(void)
1411{
1412 register unsigned long func asm("%o5");
1413 register unsigned long arg0 asm("%o0");
1414 register unsigned long arg1 asm("%o1");
1415 unsigned long pa;
1416
1417 pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
1418
1419 func = HV_FAST_MMU_TSB_CTX0;
David S. Millerd1acb422007-03-16 17:20:28 -07001420 arg0 = NUM_KTSB_DESCR;
David S. Miller490384e2006-02-11 14:41:18 -08001421 arg1 = pa;
1422 __asm__ __volatile__("ta %6"
1423 : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
1424 : "0" (func), "1" (arg0), "2" (arg1),
1425 "i" (HV_FAST_TRAP));
1426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428/* paging_init() sets up the page tables */
1429
1430extern void cheetah_ecache_flush_init(void);
David S. Millerd257d5d2006-02-06 23:44:37 -08001431extern void sun4v_patch_tlb_handlers(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433static unsigned long last_valid_pfn;
David S. Miller56425302005-09-25 16:46:57 -07001434pgd_t swapper_pg_dir[2048];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
David S. Millerc4bce902006-02-11 21:57:54 -08001436static void sun4u_pgprot_init(void);
1437static void sun4v_pgprot_init(void);
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439void __init paging_init(void)
1440{
David S. Millerd1112012006-03-08 02:16:07 -08001441 unsigned long end_pfn, pages_avail, shift, phys_base;
David S. Miller0836a0e2005-09-28 21:38:08 -07001442 unsigned long real_end, i;
1443
David S. Miller481295f2006-02-07 21:51:08 -08001444 kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
1445 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
1446
David S. Millerd7744a02006-02-21 22:31:11 -08001447 /* Invalidate both kernel TSBs. */
David S. Miller8b234272006-02-17 18:01:02 -08001448 memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
David S. Millerd1acb422007-03-16 17:20:28 -07001449#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Millerd7744a02006-02-21 22:31:11 -08001450 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
David S. Millerd1acb422007-03-16 17:20:28 -07001451#endif
David S. Miller8b234272006-02-17 18:01:02 -08001452
David S. Millerc4bce902006-02-11 21:57:54 -08001453 if (tlb_type == hypervisor)
1454 sun4v_pgprot_init();
1455 else
1456 sun4u_pgprot_init();
1457
David S. Millerd257d5d2006-02-06 23:44:37 -08001458 if (tlb_type == cheetah_plus ||
1459 tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -08001460 tsb_phys_patch();
1461
David S. Miller490384e2006-02-11 14:41:18 -08001462 if (tlb_type == hypervisor) {
David S. Millerd257d5d2006-02-06 23:44:37 -08001463 sun4v_patch_tlb_handlers();
David S. Miller490384e2006-02-11 14:41:18 -08001464 sun4v_ktsb_init();
1465 }
David S. Millerd257d5d2006-02-06 23:44:37 -08001466
David S. Miller13edad72005-09-29 17:58:26 -07001467 /* Find available physical memory... */
1468 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller0836a0e2005-09-28 21:38:08 -07001469
1470 phys_base = 0xffffffffffffffffUL;
David S. Miller13edad72005-09-29 17:58:26 -07001471 for (i = 0; i < pavail_ents; i++)
1472 phys_base = min(phys_base, pavail[i].phys_addr);
David S. Miller0836a0e2005-09-28 21:38:08 -07001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 set_bit(0, mmu_context_bmap);
1475
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001476 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 real_end = (unsigned long)_end;
1479 if ((real_end > ((unsigned long)KERNBASE + 0x400000)))
1480 bigkernel = 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001481 if ((real_end > ((unsigned long)KERNBASE + 0x800000))) {
1482 prom_printf("paging_init: Kernel > 8MB, too large.\n");
1483 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001485
1486 /* Set kernel pgd to upper alias so physical page computations
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 * work.
1488 */
1489 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1490
David S. Miller56425302005-09-25 16:46:57 -07001491 memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 /* Now can init the kernel/bad page tables. */
1494 pud_set(pud_offset(&swapper_pg_dir[0], 0),
David S. Miller56425302005-09-25 16:46:57 -07001495 swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
David S. Millerc9c10832005-10-12 12:22:46 -07001497 inherit_prom_mappings();
David S. Miller5085b4a2005-09-22 00:45:41 -07001498
David S. Millera8b900d2006-01-31 18:33:37 -08001499 /* Ok, we can use our TLB miss and window trap handlers safely. */
1500 setup_tba();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
David S. Millerc9c10832005-10-12 12:22:46 -07001502 __flush_tlb_all();
David S. Miller9ad98c52005-10-05 15:12:00 -07001503
David S. Miller490384e2006-02-11 14:41:18 -08001504 if (tlb_type == hypervisor)
1505 sun4v_ktsb_register();
1506
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001507 /* Setup bootmem... */
1508 pages_avail = 0;
David S. Millerd1112012006-03-08 02:16:07 -08001509 last_valid_pfn = end_pfn = bootmem_init(&pages_avail, phys_base);
1510
David S. Miller17b0e192006-03-08 15:57:03 -08001511 max_mapnr = last_valid_pfn;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001512
David S. Miller56425302005-09-25 16:46:57 -07001513 kernel_physical_mapping_init();
David S. Miller56425302005-09-25 16:46:57 -07001514
David S. Miller372b07b2006-06-21 15:35:28 -07001515 prom_build_devicetree();
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 {
1518 unsigned long zones_size[MAX_NR_ZONES];
1519 unsigned long zholes_size[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int znum;
1521
1522 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1523 zones_size[znum] = zholes_size[znum] = 0;
1524
David S. Miller1b51d3a2007-02-12 00:13:31 -08001525 zones_size[ZONE_NORMAL] = end_pfn;
1526 zholes_size[ZONE_NORMAL] = end_pfn - pages_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 free_area_init_node(0, &contig_page_data, zones_size,
David S. Miller17b0e192006-03-08 15:57:03 -08001529 __pa(PAGE_OFFSET) >> PAGE_SHIFT,
1530 zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532
1533 device_scan();
1534}
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536static void __init taint_real_pages(void)
1537{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 int i;
1539
David S. Miller13edad72005-09-29 17:58:26 -07001540 read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
David S. Miller13edad72005-09-29 17:58:26 -07001542 /* Find changes discovered in the physmem available rescan and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 * reserve the lost portions in the bootmem maps.
1544 */
David S. Miller13edad72005-09-29 17:58:26 -07001545 for (i = 0; i < pavail_ents; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 unsigned long old_start, old_end;
1547
David S. Miller13edad72005-09-29 17:58:26 -07001548 old_start = pavail[i].phys_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 old_end = old_start +
David S. Miller13edad72005-09-29 17:58:26 -07001550 pavail[i].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 while (old_start < old_end) {
1552 int n;
1553
David S. Millerc2a5a462006-06-22 00:01:56 -07001554 for (n = 0; n < pavail_rescan_ents; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 unsigned long new_start, new_end;
1556
David S. Miller13edad72005-09-29 17:58:26 -07001557 new_start = pavail_rescan[n].phys_addr;
1558 new_end = new_start +
1559 pavail_rescan[n].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 if (new_start <= old_start &&
1562 new_end >= (old_start + PAGE_SIZE)) {
David S. Miller13edad72005-09-29 17:58:26 -07001563 set_bit(old_start >> 22,
1564 sparc64_valid_addr_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 goto do_next_page;
1566 }
1567 }
1568 reserve_bootmem(old_start, PAGE_SIZE);
1569
1570 do_next_page:
1571 old_start += PAGE_SIZE;
1572 }
1573 }
1574}
1575
David S. Millerc2a5a462006-06-22 00:01:56 -07001576int __init page_in_phys_avail(unsigned long paddr)
1577{
1578 int i;
1579
1580 paddr &= PAGE_MASK;
1581
1582 for (i = 0; i < pavail_rescan_ents; i++) {
1583 unsigned long start, end;
1584
1585 start = pavail_rescan[i].phys_addr;
1586 end = start + pavail_rescan[i].reg_size;
1587
1588 if (paddr >= start && paddr < end)
1589 return 1;
1590 }
1591 if (paddr >= kern_base && paddr < (kern_base + kern_size))
1592 return 1;
1593#ifdef CONFIG_BLK_DEV_INITRD
1594 if (paddr >= __pa(initrd_start) &&
1595 paddr < __pa(PAGE_ALIGN(initrd_end)))
1596 return 1;
1597#endif
1598
1599 return 0;
1600}
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602void __init mem_init(void)
1603{
1604 unsigned long codepages, datapages, initpages;
1605 unsigned long addr, last;
1606 int i;
1607
1608 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1609 i += 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001610 sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (sparc64_valid_addr_bitmap == NULL) {
1612 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1613 prom_halt();
1614 }
1615 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1616
1617 addr = PAGE_OFFSET + kern_base;
1618 last = PAGE_ALIGN(kern_size) + addr;
1619 while (addr < last) {
1620 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1621 addr += PAGE_SIZE;
1622 }
1623
1624 taint_real_pages();
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1627
1628#ifdef CONFIG_DEBUG_BOOTMEM
1629 prom_printf("mem_init: Calling free_all_bootmem().\n");
1630#endif
1631 totalram_pages = num_physpages = free_all_bootmem() - 1;
1632
1633 /*
1634 * Set up the zero page, mark it reserved, so that page count
1635 * is not manipulated when freeing the page from user ptes.
1636 */
1637 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
1638 if (mem_map_zero == NULL) {
1639 prom_printf("paging_init: Cannot alloc zero page.\n");
1640 prom_halt();
1641 }
1642 SetPageReserved(mem_map_zero);
1643
1644 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1645 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1646 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1647 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1648 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1649 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1650
Christoph Lameter96177292007-02-10 01:43:03 -08001651 printk("Memory: %luk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 nr_free_pages() << (PAGE_SHIFT-10),
1653 codepages << (PAGE_SHIFT-10),
1654 datapages << (PAGE_SHIFT-10),
1655 initpages << (PAGE_SHIFT-10),
1656 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1657
1658 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1659 cheetah_ecache_flush_init();
1660}
1661
David S. Miller898cf0e2005-09-23 11:59:44 -07001662void free_initmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 unsigned long addr, initend;
1665
1666 /*
1667 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1668 */
1669 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1670 initend = (unsigned long)(__init_end) & PAGE_MASK;
1671 for (; addr < initend; addr += PAGE_SIZE) {
1672 unsigned long page;
1673 struct page *p;
1674
1675 page = (addr +
1676 ((unsigned long) __va(kern_base)) -
1677 ((unsigned long) KERNBASE));
Randy Dunlapc9cf5522006-06-27 02:53:52 -07001678 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 p = virt_to_page(page);
1680
1681 ClearPageReserved(p);
Nick Piggin7835e982006-03-22 00:08:40 -08001682 init_page_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 __free_page(p);
1684 num_physpages++;
1685 totalram_pages++;
1686 }
1687}
1688
1689#ifdef CONFIG_BLK_DEV_INITRD
1690void free_initrd_mem(unsigned long start, unsigned long end)
1691{
1692 if (start < end)
1693 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1694 for (; start < end; start += PAGE_SIZE) {
1695 struct page *p = virt_to_page(start);
1696
1697 ClearPageReserved(p);
Nick Piggin7835e982006-03-22 00:08:40 -08001698 init_page_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 __free_page(p);
1700 num_physpages++;
1701 totalram_pages++;
1702 }
1703}
1704#endif
David S. Millerc4bce902006-02-11 21:57:54 -08001705
David S. Millerc4bce902006-02-11 21:57:54 -08001706#define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
1707#define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
1708#define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
1709#define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
1710#define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
1711#define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
1712
1713pgprot_t PAGE_KERNEL __read_mostly;
1714EXPORT_SYMBOL(PAGE_KERNEL);
1715
1716pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
1717pgprot_t PAGE_COPY __read_mostly;
David S. Miller0f159522006-02-18 12:43:16 -08001718
1719pgprot_t PAGE_SHARED __read_mostly;
1720EXPORT_SYMBOL(PAGE_SHARED);
1721
David S. Millerc4bce902006-02-11 21:57:54 -08001722pgprot_t PAGE_EXEC __read_mostly;
1723unsigned long pg_iobits __read_mostly;
1724
1725unsigned long _PAGE_IE __read_mostly;
David S. Miller987c74f2006-06-25 01:34:43 -07001726EXPORT_SYMBOL(_PAGE_IE);
David S. Millerb2bef442006-02-23 01:55:55 -08001727
David S. Millerc4bce902006-02-11 21:57:54 -08001728unsigned long _PAGE_E __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08001729EXPORT_SYMBOL(_PAGE_E);
1730
David S. Millerc4bce902006-02-11 21:57:54 -08001731unsigned long _PAGE_CACHE __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08001732EXPORT_SYMBOL(_PAGE_CACHE);
David S. Millerc4bce902006-02-11 21:57:54 -08001733
1734static void prot_init_common(unsigned long page_none,
1735 unsigned long page_shared,
1736 unsigned long page_copy,
1737 unsigned long page_readonly,
1738 unsigned long page_exec_bit)
1739{
1740 PAGE_COPY = __pgprot(page_copy);
David S. Miller0f159522006-02-18 12:43:16 -08001741 PAGE_SHARED = __pgprot(page_shared);
David S. Millerc4bce902006-02-11 21:57:54 -08001742
1743 protection_map[0x0] = __pgprot(page_none);
1744 protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
1745 protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
1746 protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
1747 protection_map[0x4] = __pgprot(page_readonly);
1748 protection_map[0x5] = __pgprot(page_readonly);
1749 protection_map[0x6] = __pgprot(page_copy);
1750 protection_map[0x7] = __pgprot(page_copy);
1751 protection_map[0x8] = __pgprot(page_none);
1752 protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
1753 protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
1754 protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
1755 protection_map[0xc] = __pgprot(page_readonly);
1756 protection_map[0xd] = __pgprot(page_readonly);
1757 protection_map[0xe] = __pgprot(page_shared);
1758 protection_map[0xf] = __pgprot(page_shared);
1759}
1760
1761static void __init sun4u_pgprot_init(void)
1762{
1763 unsigned long page_none, page_shared, page_copy, page_readonly;
1764 unsigned long page_exec_bit;
1765
1766 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
1767 _PAGE_CACHE_4U | _PAGE_P_4U |
1768 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
1769 _PAGE_EXEC_4U);
1770 PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
1771 _PAGE_CACHE_4U | _PAGE_P_4U |
1772 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
1773 _PAGE_EXEC_4U | _PAGE_L_4U);
1774 PAGE_EXEC = __pgprot(_PAGE_EXEC_4U);
1775
1776 _PAGE_IE = _PAGE_IE_4U;
1777 _PAGE_E = _PAGE_E_4U;
1778 _PAGE_CACHE = _PAGE_CACHE_4U;
1779
1780 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
1781 __ACCESS_BITS_4U | _PAGE_E_4U);
1782
David S. Millerd1acb422007-03-16 17:20:28 -07001783#ifdef CONFIG_DEBUG_PAGEALLOC
1784 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4U) ^
1785 0xfffff80000000000;
1786#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001787 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
David S. Millerc4bce902006-02-11 21:57:54 -08001788 0xfffff80000000000;
David S. Millerd1acb422007-03-16 17:20:28 -07001789#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001790 kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
1791 _PAGE_P_4U | _PAGE_W_4U);
1792
1793 /* XXX Should use 256MB on Panther. XXX */
1794 kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
David S. Millerc4bce902006-02-11 21:57:54 -08001795
1796 _PAGE_SZBITS = _PAGE_SZBITS_4U;
1797 _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
1798 _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
1799 _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
1800
1801
1802 page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
1803 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1804 __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
1805 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1806 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
1807 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1808 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
1809
1810 page_exec_bit = _PAGE_EXEC_4U;
1811
1812 prot_init_common(page_none, page_shared, page_copy, page_readonly,
1813 page_exec_bit);
1814}
1815
1816static void __init sun4v_pgprot_init(void)
1817{
1818 unsigned long page_none, page_shared, page_copy, page_readonly;
1819 unsigned long page_exec_bit;
1820
1821 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
1822 _PAGE_CACHE_4V | _PAGE_P_4V |
1823 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
1824 _PAGE_EXEC_4V);
1825 PAGE_KERNEL_LOCKED = PAGE_KERNEL;
1826 PAGE_EXEC = __pgprot(_PAGE_EXEC_4V);
1827
1828 _PAGE_IE = _PAGE_IE_4V;
1829 _PAGE_E = _PAGE_E_4V;
1830 _PAGE_CACHE = _PAGE_CACHE_4V;
1831
David S. Millerd1acb422007-03-16 17:20:28 -07001832#ifdef CONFIG_DEBUG_PAGEALLOC
1833 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
1834 0xfffff80000000000;
1835#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001836 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
David S. Millerc4bce902006-02-11 21:57:54 -08001837 0xfffff80000000000;
David S. Millerd1acb422007-03-16 17:20:28 -07001838#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001839 kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
1840 _PAGE_P_4V | _PAGE_W_4V);
1841
David S. Millerd1acb422007-03-16 17:20:28 -07001842#ifdef CONFIG_DEBUG_PAGEALLOC
1843 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
1844 0xfffff80000000000;
1845#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001846 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
1847 0xfffff80000000000;
David S. Millerd1acb422007-03-16 17:20:28 -07001848#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001849 kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
1850 _PAGE_P_4V | _PAGE_W_4V);
David S. Millerc4bce902006-02-11 21:57:54 -08001851
1852 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
1853 __ACCESS_BITS_4V | _PAGE_E_4V);
1854
1855 _PAGE_SZBITS = _PAGE_SZBITS_4V;
1856 _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
1857 _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
1858 _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
1859 _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
1860
1861 page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
1862 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1863 __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
1864 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1865 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
1866 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1867 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
1868
1869 page_exec_bit = _PAGE_EXEC_4V;
1870
1871 prot_init_common(page_none, page_shared, page_copy, page_readonly,
1872 page_exec_bit);
1873}
1874
1875unsigned long pte_sz_bits(unsigned long sz)
1876{
1877 if (tlb_type == hypervisor) {
1878 switch (sz) {
1879 case 8 * 1024:
1880 default:
1881 return _PAGE_SZ8K_4V;
1882 case 64 * 1024:
1883 return _PAGE_SZ64K_4V;
1884 case 512 * 1024:
1885 return _PAGE_SZ512K_4V;
1886 case 4 * 1024 * 1024:
1887 return _PAGE_SZ4MB_4V;
1888 };
1889 } else {
1890 switch (sz) {
1891 case 8 * 1024:
1892 default:
1893 return _PAGE_SZ8K_4U;
1894 case 64 * 1024:
1895 return _PAGE_SZ64K_4U;
1896 case 512 * 1024:
1897 return _PAGE_SZ512K_4U;
1898 case 4 * 1024 * 1024:
1899 return _PAGE_SZ4MB_4U;
1900 };
1901 }
1902}
1903
1904pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
1905{
1906 pte_t pte;
David S. Millercf627152006-02-12 21:10:07 -08001907
1908 pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
David S. Millerc4bce902006-02-11 21:57:54 -08001909 pte_val(pte) |= (((unsigned long)space) << 32);
1910 pte_val(pte) |= pte_sz_bits(page_size);
David S. Millercf627152006-02-12 21:10:07 -08001911
David S. Millerc4bce902006-02-11 21:57:54 -08001912 return pte;
1913}
1914
David S. Millerc4bce902006-02-11 21:57:54 -08001915static unsigned long kern_large_tte(unsigned long paddr)
1916{
1917 unsigned long val;
1918
1919 val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
1920 _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
1921 _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
1922 if (tlb_type == hypervisor)
1923 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
1924 _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
1925 _PAGE_EXEC_4V | _PAGE_W_4V);
1926
1927 return val | paddr;
1928}
1929
David S. Millerc4bce902006-02-11 21:57:54 -08001930/* If not locked, zap it. */
1931void __flush_tlb_all(void)
1932{
1933 unsigned long pstate;
1934 int i;
1935
1936 __asm__ __volatile__("flushw\n\t"
1937 "rdpr %%pstate, %0\n\t"
1938 "wrpr %0, %1, %%pstate"
1939 : "=r" (pstate)
1940 : "i" (PSTATE_IE));
1941 if (tlb_type == spitfire) {
1942 for (i = 0; i < 64; i++) {
1943 /* Spitfire Errata #32 workaround */
1944 /* NOTE: Always runs on spitfire, so no
1945 * cheetah+ page size encodings.
1946 */
1947 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1948 "flush %%g6"
1949 : /* No outputs */
1950 : "r" (0),
1951 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1952
1953 if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
1954 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1955 "membar #Sync"
1956 : /* no outputs */
1957 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
1958 spitfire_put_dtlb_data(i, 0x0UL);
1959 }
1960
1961 /* Spitfire Errata #32 workaround */
1962 /* NOTE: Always runs on spitfire, so no
1963 * cheetah+ page size encodings.
1964 */
1965 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1966 "flush %%g6"
1967 : /* No outputs */
1968 : "r" (0),
1969 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1970
1971 if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
1972 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1973 "membar #Sync"
1974 : /* no outputs */
1975 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1976 spitfire_put_itlb_data(i, 0x0UL);
1977 }
1978 }
1979 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1980 cheetah_flush_dtlb_all();
1981 cheetah_flush_itlb_all();
1982 }
1983 __asm__ __volatile__("wrpr %0, 0, %%pstate"
1984 : : "r" (pstate));
1985}
David S. Miller88d70792006-03-18 19:16:23 -08001986
1987#ifdef CONFIG_MEMORY_HOTPLUG
1988
1989void online_page(struct page *page)
1990{
1991 ClearPageReserved(page);
Nick Pigginfcab1e52006-03-23 07:48:16 +01001992 init_page_count(page);
1993 __free_page(page);
David S. Miller88d70792006-03-18 19:16:23 -08001994 totalram_pages++;
1995 num_physpages++;
1996}
1997
1998int remove_memory(u64 start, u64 size)
1999{
2000 return -EINVAL;
2001}
2002
2003#endif /* CONFIG_MEMORY_HOTPLUG */