blob: 3e6c4ecb8fee1dc5a022e89f2a31a7c63bfd8cf1 [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>
David S. Miller5cbc3072007-05-25 15:49:59 -070026#include <linux/percpu.h>
David S. Miller3b2a7e22008-02-13 18:13:20 -080027#include <linux/lmb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/head.h>
30#include <asm/system.h>
31#include <asm/page.h>
32#include <asm/pgalloc.h>
33#include <asm/pgtable.h>
34#include <asm/oplib.h>
35#include <asm/iommu.h>
36#include <asm/io.h>
37#include <asm/uaccess.h>
38#include <asm/mmu_context.h>
39#include <asm/tlbflush.h>
40#include <asm/dma.h>
41#include <asm/starfire.h>
42#include <asm/tlb.h>
43#include <asm/spitfire.h>
44#include <asm/sections.h>
David S. Miller517af332006-02-01 15:55:21 -080045#include <asm/tsb.h>
David S. Miller481295f2006-02-07 21:51:08 -080046#include <asm/hypervisor.h>
David S. Miller372b07b2006-06-21 15:35:28 -070047#include <asm/prom.h>
David S. Miller22d6a1c2007-05-25 00:37:12 -070048#include <asm/sstate.h>
David S. Miller5cbc3072007-05-25 15:49:59 -070049#include <asm/mdesc.h>
David S. Miller3d5ae6b2008-03-25 21:51:40 -070050#include <asm/cpudata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
David S. Miller9cc3a1a2006-02-21 20:51:13 -080052#define MAX_PHYS_ADDRESS (1UL << 42UL)
53#define KPTE_BITMAP_CHUNK_SZ (256UL * 1024UL * 1024UL)
54#define KPTE_BITMAP_BYTES \
55 ((MAX_PHYS_ADDRESS / KPTE_BITMAP_CHUNK_SZ) / 8)
56
57unsigned long kern_linear_pte_xor[2] __read_mostly;
58
59/* A bitmap, one bit for every 256MB of physical memory. If the bit
60 * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
61 * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
62 */
63unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
64
David S. Millerd1acb422007-03-16 17:20:28 -070065#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Miller2d9e2762007-05-29 01:58:31 -070066/* A special kernel TSB for 4MB and 256MB linear mappings.
67 * Space is allocated for this right after the trap table
68 * in arch/sparc64/kernel/head.S
69 */
70extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
David S. Millerd1acb422007-03-16 17:20:28 -070071#endif
David S. Millerd7744a02006-02-21 22:31:11 -080072
David S. Miller13edad72005-09-29 17:58:26 -070073#define MAX_BANKS 32
David S. Miller10147572005-09-28 21:46:43 -070074
David S. Miller13edad72005-09-29 17:58:26 -070075static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
76static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
77static int pavail_ents __initdata;
78static int pavail_rescan_ents __initdata;
David S. Miller10147572005-09-28 21:46:43 -070079
David S. Miller13edad72005-09-29 17:58:26 -070080static int cmp_p64(const void *a, const void *b)
81{
82 const struct linux_prom64_registers *x = a, *y = b;
83
84 if (x->phys_addr > y->phys_addr)
85 return 1;
86 if (x->phys_addr < y->phys_addr)
87 return -1;
88 return 0;
89}
90
91static void __init read_obp_memory(const char *property,
92 struct linux_prom64_registers *regs,
93 int *num_ents)
94{
95 int node = prom_finddevice("/memory");
96 int prop_size = prom_getproplen(node, property);
97 int ents, ret, i;
98
99 ents = prop_size / sizeof(struct linux_prom64_registers);
100 if (ents > MAX_BANKS) {
101 prom_printf("The machine has more %s property entries than "
102 "this kernel can support (%d).\n",
103 property, MAX_BANKS);
104 prom_halt();
105 }
106
107 ret = prom_getproperty(node, property, (char *) regs, prop_size);
108 if (ret == -1) {
109 prom_printf("Couldn't get %s property from /memory.\n");
110 prom_halt();
111 }
112
David S. Miller13edad72005-09-29 17:58:26 -0700113 /* Sanitize what we got from the firmware, by page aligning
114 * everything.
115 */
116 for (i = 0; i < ents; i++) {
117 unsigned long base, size;
118
119 base = regs[i].phys_addr;
120 size = regs[i].reg_size;
121
122 size &= PAGE_MASK;
123 if (base & ~PAGE_MASK) {
124 unsigned long new_base = PAGE_ALIGN(base);
125
126 size -= new_base - base;
127 if ((long) size < 0L)
128 size = 0UL;
129 base = new_base;
130 }
David S. Miller0015d3d2007-03-15 00:06:34 -0700131 if (size == 0UL) {
132 /* If it is empty, simply get rid of it.
133 * This simplifies the logic of the other
134 * functions that process these arrays.
135 */
136 memmove(&regs[i], &regs[i + 1],
137 (ents - i - 1) * sizeof(regs[0]));
138 i--;
139 ents--;
140 continue;
141 }
David S. Miller13edad72005-09-29 17:58:26 -0700142 regs[i].phys_addr = base;
143 regs[i].reg_size = size;
144 }
David S. Miller486ad102006-06-22 00:00:00 -0700145
David S. Miller486ad102006-06-22 00:00:00 -0700146 *num_ents = ents;
147
David S. Millerc9c10832005-10-12 12:22:46 -0700148 sort(regs, ents, sizeof(struct linux_prom64_registers),
David S. Miller13edad72005-09-29 17:58:26 -0700149 cmp_p64, NULL);
150}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
David S. Miller2bdb3cb2005-09-22 01:08:57 -0700152unsigned long *sparc64_valid_addr_bitmap __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
David S. Millerd1112012006-03-08 02:16:07 -0800154/* Kernel physical address base and size in bytes. */
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700155unsigned long kern_base __read_mostly;
156unsigned long kern_size __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* Initial ramdisk setup */
159extern unsigned long sparc_ramdisk_image64;
160extern unsigned int sparc_ramdisk_image;
161extern unsigned int sparc_ramdisk_size;
162
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700163struct page *mem_map_zero __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
David S. Miller0835ae02005-10-04 15:23:20 -0700165unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
166
167unsigned long sparc64_kern_pri_context __read_mostly;
168unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
169unsigned long sparc64_kern_sec_context __read_mostly;
170
David S. Miller64658742008-03-21 17:01:38 -0700171int num_kernel_image_mappings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#ifdef CONFIG_DEBUG_DCFLUSH
174atomic_t dcpage_flushes = ATOMIC_INIT(0);
175#ifdef CONFIG_SMP
176atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
177#endif
178#endif
179
David S. Miller7a591cf2006-02-26 19:44:50 -0800180inline void flush_dcache_page_impl(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
David S. Miller7a591cf2006-02-26 19:44:50 -0800182 BUG_ON(tlb_type == hypervisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#ifdef CONFIG_DEBUG_DCFLUSH
184 atomic_inc(&dcpage_flushes);
185#endif
186
187#ifdef DCACHE_ALIASING_POSSIBLE
188 __flush_dcache_page(page_address(page),
189 ((tlb_type == spitfire) &&
190 page_mapping(page) != NULL));
191#else
192 if (page_mapping(page) != NULL &&
193 tlb_type == spitfire)
194 __flush_icache_page(__pa(page_address(page)));
195#endif
196}
197
198#define PG_dcache_dirty PG_arch_1
David S. Miller22adb352007-05-26 01:14:43 -0700199#define PG_dcache_cpu_shift 32UL
200#define PG_dcache_cpu_mask \
201 ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203#define dcache_dirty_cpu(page) \
David S. Miller48b0e542005-07-27 16:08:44 -0700204 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
David S. Millerd979f172007-10-27 00:13:04 -0700206static inline void set_dcache_dirty(struct page *page, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 unsigned long mask = this_cpu;
David S. Miller48b0e542005-07-27 16:08:44 -0700209 unsigned long non_cpu_bits;
210
211 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
212 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 __asm__ __volatile__("1:\n\t"
215 "ldx [%2], %%g7\n\t"
216 "and %%g7, %1, %%g1\n\t"
217 "or %%g1, %0, %%g1\n\t"
218 "casx [%2], %%g7, %%g1\n\t"
219 "cmp %%g7, %%g1\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700220 "membar #StoreLoad | #StoreStore\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700222 " nop"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 : /* no outputs */
224 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
225 : "g1", "g7");
226}
227
David S. Millerd979f172007-10-27 00:13:04 -0700228static inline void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 unsigned long mask = (1UL << PG_dcache_dirty);
231
232 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
233 "1:\n\t"
234 "ldx [%2], %%g7\n\t"
David S. Miller48b0e542005-07-27 16:08:44 -0700235 "srlx %%g7, %4, %%g1\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 "and %%g1, %3, %%g1\n\t"
237 "cmp %%g1, %0\n\t"
238 "bne,pn %%icc, 2f\n\t"
239 " andn %%g7, %1, %%g1\n\t"
240 "casx [%2], %%g7, %%g1\n\t"
241 "cmp %%g7, %%g1\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700242 "membar #StoreLoad | #StoreStore\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700244 " nop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 "2:"
246 : /* no outputs */
247 : "r" (cpu), "r" (mask), "r" (&page->flags),
David S. Miller48b0e542005-07-27 16:08:44 -0700248 "i" (PG_dcache_cpu_mask),
249 "i" (PG_dcache_cpu_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 : "g1", "g7");
251}
252
David S. Miller517af332006-02-01 15:55:21 -0800253static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
254{
255 unsigned long tsb_addr = (unsigned long) ent;
256
David S. Miller3b3ab2e2006-02-17 09:54:42 -0800257 if (tlb_type == cheetah_plus || tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -0800258 tsb_addr = __pa(tsb_addr);
259
260 __tsb_insert(tsb_addr, tag, pte);
261}
262
David S. Millerc4bce902006-02-11 21:57:54 -0800263unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
264unsigned long _PAGE_SZBITS __read_mostly;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
267{
David S. Millerbd407912006-01-31 18:31:38 -0800268 struct mm_struct *mm;
David S. Miller74ae9982006-03-05 18:26:24 -0800269 struct tsb *tsb;
David S. Miller7a1ac522006-03-16 02:02:32 -0800270 unsigned long tag, flags;
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800271 unsigned long tsb_index, tsb_hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
David S. Miller7a591cf2006-02-26 19:44:50 -0800273 if (tlb_type != hypervisor) {
274 unsigned long pfn = pte_pfn(pte);
275 unsigned long pg_flags;
276 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
David S. Miller7a591cf2006-02-26 19:44:50 -0800278 if (pfn_valid(pfn) &&
279 (page = pfn_to_page(pfn), page_mapping(page)) &&
280 ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
281 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
282 PG_dcache_cpu_mask);
283 int this_cpu = get_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
David S. Miller7a591cf2006-02-26 19:44:50 -0800285 /* This is just to optimize away some function calls
286 * in the SMP case.
287 */
288 if (cpu == this_cpu)
289 flush_dcache_page_impl(page);
290 else
291 smp_flush_dcache_page_impl(page, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
David S. Miller7a591cf2006-02-26 19:44:50 -0800293 clear_dcache_dirty_cpu(page, cpu);
294
295 put_cpu();
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
David S. Millerbd407912006-01-31 18:31:38 -0800298
299 mm = vma->vm_mm;
David S. Miller7a1ac522006-03-16 02:02:32 -0800300
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800301 tsb_index = MM_TSB_BASE;
302 tsb_hash_shift = PAGE_SHIFT;
303
David S. Miller7a1ac522006-03-16 02:02:32 -0800304 spin_lock_irqsave(&mm->context.lock, flags);
305
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800306#ifdef CONFIG_HUGETLB_PAGE
307 if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL) {
308 if ((tlb_type == hypervisor &&
309 (pte_val(pte) & _PAGE_SZALL_4V) == _PAGE_SZHUGE_4V) ||
310 (tlb_type != hypervisor &&
311 (pte_val(pte) & _PAGE_SZALL_4U) == _PAGE_SZHUGE_4U)) {
312 tsb_index = MM_TSB_HUGE;
313 tsb_hash_shift = HPAGE_SHIFT;
314 }
315 }
316#endif
317
318 tsb = mm->context.tsb_block[tsb_index].tsb;
319 tsb += ((address >> tsb_hash_shift) &
320 (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
David S. Miller74ae9982006-03-05 18:26:24 -0800321 tag = (address >> 22UL);
322 tsb_insert(tsb, tag, pte_val(pte));
David S. Miller7a1ac522006-03-16 02:02:32 -0800323
324 spin_unlock_irqrestore(&mm->context.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327void flush_dcache_page(struct page *page)
328{
David S. Millera9546f52005-04-17 18:03:09 -0700329 struct address_space *mapping;
330 int this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
David S. Miller7a591cf2006-02-26 19:44:50 -0800332 if (tlb_type == hypervisor)
333 return;
334
David S. Millera9546f52005-04-17 18:03:09 -0700335 /* Do not bother with the expensive D-cache flush if it
336 * is merely the zero page. The 'bigcore' testcase in GDB
337 * causes this case to run millions of times.
338 */
339 if (page == ZERO_PAGE(0))
340 return;
341
342 this_cpu = get_cpu();
343
344 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (mapping && !mapping_mapped(mapping)) {
David S. Millera9546f52005-04-17 18:03:09 -0700346 int dirty = test_bit(PG_dcache_dirty, &page->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (dirty) {
David S. Millera9546f52005-04-17 18:03:09 -0700348 int dirty_cpu = dcache_dirty_cpu(page);
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (dirty_cpu == this_cpu)
351 goto out;
352 smp_flush_dcache_page_impl(page, dirty_cpu);
353 }
354 set_dcache_dirty(page, this_cpu);
355 } else {
356 /* We could delay the flush for the !page_mapping
357 * case too. But that case is for exec env/arg
358 * pages and those are %99 certainly going to get
359 * faulted into the tlb (and thus flushed) anyways.
360 */
361 flush_dcache_page_impl(page);
362 }
363
364out:
365 put_cpu();
366}
367
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700368void __kprobes flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
David S. Millera43fe0e2006-02-04 03:10:53 -0800370 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (tlb_type == spitfire) {
372 unsigned long kaddr;
373
David S. Millera94aa252007-03-15 15:50:11 -0700374 /* This code only runs on Spitfire cpus so this is
375 * why we can assume _PAGE_PADDR_4U.
376 */
377 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
378 unsigned long paddr, mask = _PAGE_PADDR_4U;
379
380 if (kaddr >= PAGE_OFFSET)
381 paddr = kaddr & mask;
382 else {
383 pgd_t *pgdp = pgd_offset_k(kaddr);
384 pud_t *pudp = pud_offset(pgdp, kaddr);
385 pmd_t *pmdp = pmd_offset(pudp, kaddr);
386 pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
387
388 paddr = pte_val(*ptep) & mask;
389 }
390 __flush_icache_page(paddr);
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395void show_mem(void)
396{
David S. Miller5be4a962007-03-15 16:00:29 -0700397 unsigned long total = 0, reserved = 0;
398 unsigned long shared = 0, cached = 0;
399 pg_data_t *pgdat;
400
David S. Miller28256ca2007-03-15 15:56:07 -0700401 printk(KERN_INFO "Mem-info:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 show_free_areas();
David S. Miller28256ca2007-03-15 15:56:07 -0700403 printk(KERN_INFO "Free swap: %6ldkB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 nr_swap_pages << (PAGE_SHIFT-10));
David S. Miller5be4a962007-03-15 16:00:29 -0700405 for_each_online_pgdat(pgdat) {
406 unsigned long i, flags;
407
408 pgdat_resize_lock(pgdat, &flags);
409 for (i = 0; i < pgdat->node_spanned_pages; i++) {
410 struct page *page = pgdat_page_nr(pgdat, i);
411 total++;
412 if (PageReserved(page))
413 reserved++;
414 else if (PageSwapCache(page))
415 cached++;
416 else if (page_count(page))
417 shared += page_count(page) - 1;
418 }
419 pgdat_resize_unlock(pgdat, &flags);
420 }
421
422 printk(KERN_INFO "%lu pages of RAM\n", total);
423 printk(KERN_INFO "%lu reserved pages\n", reserved);
424 printk(KERN_INFO "%lu pages shared\n", shared);
425 printk(KERN_INFO "%lu pages swap cached\n", cached);
426
427 printk(KERN_INFO "%lu pages dirty\n",
428 global_page_state(NR_FILE_DIRTY));
429 printk(KERN_INFO "%lu pages writeback\n",
430 global_page_state(NR_WRITEBACK));
431 printk(KERN_INFO "%lu pages mapped\n",
432 global_page_state(NR_FILE_MAPPED));
433 printk(KERN_INFO "%lu pages slab\n",
434 global_page_state(NR_SLAB_RECLAIMABLE) +
435 global_page_state(NR_SLAB_UNRECLAIMABLE));
436 printk(KERN_INFO "%lu pages pagetables\n",
437 global_page_state(NR_PAGETABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440void mmu_info(struct seq_file *m)
441{
442 if (tlb_type == cheetah)
443 seq_printf(m, "MMU Type\t: Cheetah\n");
444 else if (tlb_type == cheetah_plus)
445 seq_printf(m, "MMU Type\t: Cheetah+\n");
446 else if (tlb_type == spitfire)
447 seq_printf(m, "MMU Type\t: Spitfire\n");
David S. Millera43fe0e2006-02-04 03:10:53 -0800448 else if (tlb_type == hypervisor)
449 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 else
451 seq_printf(m, "MMU Type\t: ???\n");
452
453#ifdef CONFIG_DEBUG_DCFLUSH
454 seq_printf(m, "DCPageFlushes\t: %d\n",
455 atomic_read(&dcpage_flushes));
456#ifdef CONFIG_SMP
457 seq_printf(m, "DCPageFlushesXC\t: %d\n",
458 atomic_read(&dcpage_flushes_xcall));
459#endif /* CONFIG_SMP */
460#endif /* CONFIG_DEBUG_DCFLUSH */
461}
462
David S. Millera94aa252007-03-15 15:50:11 -0700463struct linux_prom_translation {
464 unsigned long virt;
465 unsigned long size;
466 unsigned long data;
467};
468
469/* Exported for kernel TLB miss handling in ktlb.S */
470struct linux_prom_translation prom_trans[512] __read_mostly;
471unsigned int prom_trans_ents __read_mostly;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/* Exported for SMP bootup purposes. */
474unsigned long kern_locked_tte_data;
475
David S. Miller405599b2005-09-22 00:12:35 -0700476/* The obp translations are saved based on 8k pagesize, since obp can
477 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
David S. Miller74bf4312006-01-31 18:29:18 -0800478 * HI_OBP_ADDRESS range are handled in ktlb.S.
David S. Miller405599b2005-09-22 00:12:35 -0700479 */
David S. Miller5085b4a2005-09-22 00:45:41 -0700480static inline int in_obp_range(unsigned long vaddr)
481{
482 return (vaddr >= LOW_OBP_ADDRESS &&
483 vaddr < HI_OBP_ADDRESS);
484}
485
David S. Millerc9c10832005-10-12 12:22:46 -0700486static int cmp_ptrans(const void *a, const void *b)
David S. Miller405599b2005-09-22 00:12:35 -0700487{
David S. Millerc9c10832005-10-12 12:22:46 -0700488 const struct linux_prom_translation *x = a, *y = b;
David S. Miller405599b2005-09-22 00:12:35 -0700489
David S. Millerc9c10832005-10-12 12:22:46 -0700490 if (x->virt > y->virt)
491 return 1;
492 if (x->virt < y->virt)
493 return -1;
494 return 0;
David S. Miller405599b2005-09-22 00:12:35 -0700495}
496
David S. Millerc9c10832005-10-12 12:22:46 -0700497/* Read OBP translations property into 'prom_trans[]'. */
David S. Miller9ad98c52005-10-05 15:12:00 -0700498static void __init read_obp_translations(void)
David S. Miller405599b2005-09-22 00:12:35 -0700499{
David S. Millerc9c10832005-10-12 12:22:46 -0700500 int n, node, ents, first, last, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 node = prom_finddevice("/virtual-memory");
503 n = prom_getproplen(node, "translations");
David S. Miller405599b2005-09-22 00:12:35 -0700504 if (unlikely(n == 0 || n == -1)) {
David S. Millerb206fc42005-09-21 22:31:13 -0700505 prom_printf("prom_mappings: Couldn't get size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 prom_halt();
507 }
David S. Miller405599b2005-09-22 00:12:35 -0700508 if (unlikely(n > sizeof(prom_trans))) {
509 prom_printf("prom_mappings: Size %Zd is too big.\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 prom_halt();
511 }
David S. Miller405599b2005-09-22 00:12:35 -0700512
David S. Millerb206fc42005-09-21 22:31:13 -0700513 if ((n = prom_getproperty(node, "translations",
David S. Miller405599b2005-09-22 00:12:35 -0700514 (char *)&prom_trans[0],
515 sizeof(prom_trans))) == -1) {
David S. Millerb206fc42005-09-21 22:31:13 -0700516 prom_printf("prom_mappings: Couldn't get property.\n");
517 prom_halt();
518 }
David S. Miller9ad98c52005-10-05 15:12:00 -0700519
David S. Millerb206fc42005-09-21 22:31:13 -0700520 n = n / sizeof(struct linux_prom_translation);
David S. Miller9ad98c52005-10-05 15:12:00 -0700521
David S. Millerc9c10832005-10-12 12:22:46 -0700522 ents = n;
523
524 sort(prom_trans, ents, sizeof(struct linux_prom_translation),
525 cmp_ptrans, NULL);
526
527 /* Now kick out all the non-OBP entries. */
528 for (i = 0; i < ents; i++) {
529 if (in_obp_range(prom_trans[i].virt))
530 break;
531 }
532 first = i;
533 for (; i < ents; i++) {
534 if (!in_obp_range(prom_trans[i].virt))
535 break;
536 }
537 last = i;
538
539 for (i = 0; i < (last - first); i++) {
540 struct linux_prom_translation *src = &prom_trans[i + first];
541 struct linux_prom_translation *dest = &prom_trans[i];
542
543 *dest = *src;
544 }
545 for (; i < ents; i++) {
546 struct linux_prom_translation *dest = &prom_trans[i];
547 dest->virt = dest->size = dest->data = 0x0UL;
548 }
549
550 prom_trans_ents = last - first;
551
552 if (tlb_type == spitfire) {
553 /* Clear diag TTE bits. */
554 for (i = 0; i < prom_trans_ents; i++)
555 prom_trans[i].data &= ~0x0003fe0000000000UL;
556 }
David S. Miller405599b2005-09-22 00:12:35 -0700557}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
David S. Millerd82ace72006-02-09 02:52:44 -0800559static void __init hypervisor_tlb_lock(unsigned long vaddr,
560 unsigned long pte,
561 unsigned long mmu)
562{
David S. Miller7db35f32007-05-29 02:22:14 -0700563 unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
David S. Millerd82ace72006-02-09 02:52:44 -0800564
David S. Miller7db35f32007-05-29 02:22:14 -0700565 if (ret != 0) {
David S. Miller12e126a2006-02-17 14:40:30 -0800566 prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
David S. Miller7db35f32007-05-29 02:22:14 -0700567 "errors with %lx\n", vaddr, 0, pte, mmu, ret);
David S. Miller12e126a2006-02-17 14:40:30 -0800568 prom_halt();
569 }
David S. Millerd82ace72006-02-09 02:52:44 -0800570}
571
David S. Millerc4bce902006-02-11 21:57:54 -0800572static unsigned long kern_large_tte(unsigned long paddr);
573
David S. Miller898cf0e2005-09-23 11:59:44 -0700574static void __init remap_kernel(void)
David S. Miller405599b2005-09-22 00:12:35 -0700575{
576 unsigned long phys_page, tte_vaddr, tte_data;
David S. Miller64658742008-03-21 17:01:38 -0700577 int i, tlb_ent = sparc64_highest_locked_tlbent();
David S. Miller405599b2005-09-22 00:12:35 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 tte_vaddr = (unsigned long) KERNBASE;
David S. Millerbff06d52005-09-22 20:11:33 -0700580 phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
David S. Millerc4bce902006-02-11 21:57:54 -0800581 tte_data = kern_large_tte(phys_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 kern_locked_tte_data = tte_data;
584
David S. Millerd82ace72006-02-09 02:52:44 -0800585 /* Now lock us into the TLBs via Hypervisor or OBP. */
586 if (tlb_type == hypervisor) {
David S. Miller64658742008-03-21 17:01:38 -0700587 for (i = 0; i < num_kernel_image_mappings; i++) {
David S. Millerd82ace72006-02-09 02:52:44 -0800588 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
589 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
David S. Miller64658742008-03-21 17:01:38 -0700590 tte_vaddr += 0x400000;
591 tte_data += 0x400000;
David S. Millerd82ace72006-02-09 02:52:44 -0800592 }
593 } else {
David S. Miller64658742008-03-21 17:01:38 -0700594 for (i = 0; i < num_kernel_image_mappings; i++) {
595 prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
596 prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
597 tte_vaddr += 0x400000;
598 tte_data += 0x400000;
David S. Millerd82ace72006-02-09 02:52:44 -0800599 }
David S. Miller64658742008-03-21 17:01:38 -0700600 sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
David S. Miller0835ae02005-10-04 15:23:20 -0700602 if (tlb_type == cheetah_plus) {
603 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
604 CTX_CHEETAH_PLUS_NUC);
605 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
606 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
607 }
David S. Miller405599b2005-09-22 00:12:35 -0700608}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
David S. Miller405599b2005-09-22 00:12:35 -0700610
David S. Millerc9c10832005-10-12 12:22:46 -0700611static void __init inherit_prom_mappings(void)
David S. Miller9ad98c52005-10-05 15:12:00 -0700612{
613 read_obp_translations();
David S. Miller405599b2005-09-22 00:12:35 -0700614
615 /* Now fixup OBP's idea about where we really are mapped. */
David S. Miller3c62a2d2008-02-17 23:22:50 -0800616 printk("Remapping the kernel... ");
David S. Miller405599b2005-09-22 00:12:35 -0700617 remap_kernel();
David S. Miller3c62a2d2008-02-17 23:22:50 -0800618 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621void prom_world(int enter)
622{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!enter)
624 set_fs((mm_segment_t) { get_thread_current_ds() });
625
David S. Miller3487d1d2006-01-31 18:33:25 -0800626 __asm__ __volatile__("flushw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629void __flush_dcache_range(unsigned long start, unsigned long end)
630{
631 unsigned long va;
632
633 if (tlb_type == spitfire) {
634 int n = 0;
635
636 for (va = start; va < end; va += 32) {
637 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
638 if (++n >= 512)
639 break;
640 }
David S. Millera43fe0e2006-02-04 03:10:53 -0800641 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 start = __pa(start);
643 end = __pa(end);
644 for (va = start; va < end; va += 32)
645 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
646 "membar #Sync"
647 : /* no outputs */
648 : "r" (va),
649 "i" (ASI_DCACHE_INVALIDATE));
650 }
651}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
David S. Miller85f1e1f2007-03-15 17:51:26 -0700653/* get_new_mmu_context() uses "cache + 1". */
654DEFINE_SPINLOCK(ctx_alloc_lock);
655unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
656#define MAX_CTX_NR (1UL << CTX_NR_BITS)
657#define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
658DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/* Caller does TLB context flushing on local CPU if necessary.
661 * The caller also ensures that CTX_VALID(mm->context) is false.
662 *
663 * We must be careful about boundary cases so that we never
664 * let the user have CTX 0 (nucleus) or we ever use a CTX
665 * version of zero (and thus NO_CONTEXT would not be caught
666 * by version mis-match tests in mmu_context.h).
David S. Millera0663a72006-02-23 14:19:28 -0800667 *
668 * Always invoked with interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
670void get_new_mmu_context(struct mm_struct *mm)
671{
672 unsigned long ctx, new_ctx;
673 unsigned long orig_pgsz_bits;
David S. Millera77754b2006-03-06 19:59:50 -0800674 unsigned long flags;
David S. Millera0663a72006-02-23 14:19:28 -0800675 int new_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
David S. Millera77754b2006-03-06 19:59:50 -0800677 spin_lock_irqsave(&ctx_alloc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
679 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
680 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
David S. Millera0663a72006-02-23 14:19:28 -0800681 new_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (new_ctx >= (1 << CTX_NR_BITS)) {
683 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
684 if (new_ctx >= ctx) {
685 int i;
686 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
687 CTX_FIRST_VERSION;
688 if (new_ctx == 1)
689 new_ctx = CTX_FIRST_VERSION;
690
691 /* Don't call memset, for 16 entries that's just
692 * plain silly...
693 */
694 mmu_context_bmap[0] = 3;
695 mmu_context_bmap[1] = 0;
696 mmu_context_bmap[2] = 0;
697 mmu_context_bmap[3] = 0;
698 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
699 mmu_context_bmap[i + 0] = 0;
700 mmu_context_bmap[i + 1] = 0;
701 mmu_context_bmap[i + 2] = 0;
702 mmu_context_bmap[i + 3] = 0;
703 }
David S. Millera0663a72006-02-23 14:19:28 -0800704 new_version = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto out;
706 }
707 }
708 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
709 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
710out:
711 tlb_context_cache = new_ctx;
712 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
David S. Millera77754b2006-03-06 19:59:50 -0800713 spin_unlock_irqrestore(&ctx_alloc_lock, flags);
David S. Millera0663a72006-02-23 14:19:28 -0800714
715 if (unlikely(new_version))
716 smp_new_mmu_context_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
David S. Millerd1112012006-03-08 02:16:07 -0800719/* Find a free area for the bootmem map, avoiding the kernel image
720 * and the initial ramdisk.
721 */
722static unsigned long __init choose_bootmap_pfn(unsigned long start_pfn,
723 unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
David S. Millerd1112012006-03-08 02:16:07 -0800725 unsigned long avoid_start, avoid_end, bootmap_size;
726 int i;
727
David S. Miller39964652007-03-15 19:36:53 -0700728 bootmap_size = bootmem_bootmap_pages(end_pfn - start_pfn);
729 bootmap_size <<= PAGE_SHIFT;
David S. Millerd1112012006-03-08 02:16:07 -0800730
731 avoid_start = avoid_end = 0;
732#ifdef CONFIG_BLK_DEV_INITRD
733 avoid_start = initrd_start;
734 avoid_end = PAGE_ALIGN(initrd_end);
735#endif
736
David S. Millerd1112012006-03-08 02:16:07 -0800737 for (i = 0; i < pavail_ents; i++) {
738 unsigned long start, end;
739
740 start = pavail[i].phys_addr;
741 end = start + pavail[i].reg_size;
742
743 while (start < end) {
744 if (start >= kern_base &&
745 start < PAGE_ALIGN(kern_base + kern_size)) {
746 start = PAGE_ALIGN(kern_base + kern_size);
747 continue;
748 }
749 if (start >= avoid_start && start < avoid_end) {
750 start = avoid_end;
751 continue;
752 }
753
754 if ((end - start) < bootmap_size)
755 break;
756
757 if (start < kern_base &&
758 (start + bootmap_size) > kern_base) {
759 start = PAGE_ALIGN(kern_base + kern_size);
760 continue;
761 }
762
763 if (start < avoid_start &&
764 (start + bootmap_size) > avoid_start) {
765 start = avoid_end;
766 continue;
767 }
768
769 /* OK, it doesn't overlap anything, use it. */
David S. Millerd1112012006-03-08 02:16:07 -0800770 return start >> PAGE_SHIFT;
771 }
772 }
773
774 prom_printf("Cannot find free area for bootmap, aborting.\n");
775 prom_halt();
776}
777
David S. Miller4e82c9a2008-02-13 18:00:03 -0800778static void __init find_ramdisk(unsigned long phys_base)
779{
780#ifdef CONFIG_BLK_DEV_INITRD
781 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
782 unsigned long ramdisk_image;
783
784 /* Older versions of the bootloader only supported a
785 * 32-bit physical address for the ramdisk image
786 * location, stored at sparc_ramdisk_image. Newer
787 * SILO versions set sparc_ramdisk_image to zero and
788 * provide a full 64-bit physical address at
789 * sparc_ramdisk_image64.
790 */
791 ramdisk_image = sparc_ramdisk_image;
792 if (!ramdisk_image)
793 ramdisk_image = sparc_ramdisk_image64;
794
795 /* Another bootloader quirk. The bootloader normalizes
796 * the physical address to KERNBASE, so we have to
797 * factor that back out and add in the lowest valid
798 * physical page address to get the true physical address.
799 */
800 ramdisk_image -= KERNBASE;
801 ramdisk_image += phys_base;
802
803 initrd_start = ramdisk_image;
804 initrd_end = ramdisk_image + sparc_ramdisk_size;
David S. Miller3b2a7e22008-02-13 18:13:20 -0800805
806 lmb_reserve(initrd_start, initrd_end);
David S. Miller4e82c9a2008-02-13 18:00:03 -0800807 }
808#endif
809}
810
David S. Millerf1cfdb52007-03-15 22:52:18 -0700811/* About pages_avail, this is the value we will use to calculate
812 * the zholes_size[] argument given to free_area_init_node(). The
813 * page allocator uses this to calculate nr_kernel_pages,
814 * nr_all_pages and zone->present_pages. On NUMA it is used
815 * to calculate zone->min_unmapped_pages and zone->min_slab_pages.
816 *
817 * So this number should really be set to what the page allocator
818 * actually ends up with. This means:
819 * 1) It should include bootmem map pages, we'll release those.
820 * 2) It should not include the kernel image, except for the
821 * __init sections which we will also release.
822 * 3) It should include the initrd image, since we'll release
823 * that too.
824 */
David S. Millerd1112012006-03-08 02:16:07 -0800825static unsigned long __init bootmem_init(unsigned long *pages_avail,
826 unsigned long phys_base)
827{
828 unsigned long bootmap_size, end_pfn;
David S. Miller25b0c652008-02-13 18:20:14 -0800829 unsigned long bootmap_pfn, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 int i;
831
David S. Miller25b0c652008-02-13 18:20:14 -0800832 *pages_avail = lmb_phys_mem_size() >> PAGE_SHIFT;
833 end_pfn = lmb_end_of_DRAM() >> PAGE_SHIFT;
David S. Miller6fc5bae2006-12-28 21:00:23 -0800834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* Initialize the boot-time allocator. */
836 max_pfn = max_low_pfn = end_pfn;
David S. Millerd1112012006-03-08 02:16:07 -0800837 min_low_pfn = (phys_base >> PAGE_SHIFT);
838
839 bootmap_pfn = choose_bootmap_pfn(min_low_pfn, end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
David S. Millerd1112012006-03-08 02:16:07 -0800841 bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn,
David S. Miller17b0e192006-03-08 15:57:03 -0800842 min_low_pfn, end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* Now register the available physical memory with the
845 * allocator.
846 */
David S. Miller0f78e752008-02-13 01:00:26 -0800847 for (i = 0; i < pavail_ents; i++)
David S. Miller13edad72005-09-29 17:58:26 -0700848 free_bootmem(pavail[i].phys_addr, pavail[i].reg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850#ifdef CONFIG_BLK_DEV_INITRD
851 if (initrd_start) {
852 size = initrd_end - initrd_start;
853
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700854 /* Reserve the initrd image area. */
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800855 reserve_bootmem(initrd_start, size, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 initrd_start += PAGE_OFFSET;
858 initrd_end += PAGE_OFFSET;
859 }
860#endif
861 /* Reserve the kernel text/data/bss. */
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800862 reserve_bootmem(kern_base, kern_size, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 *pages_avail -= PAGE_ALIGN(kern_size) >> PAGE_SHIFT;
864
David S. Millerf1cfdb52007-03-15 22:52:18 -0700865 /* Add back in the initmem pages. */
866 size = ((unsigned long)(__init_end) & PAGE_MASK) -
867 PAGE_ALIGN((unsigned long)__init_begin);
868 *pages_avail += size >> PAGE_SHIFT;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* Reserve the bootmem map. We do not account for it
871 * in pages_avail because we will release that memory
872 * in free_all_bootmem.
873 */
874 size = bootmap_size;
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800875 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
David S. Millerd1112012006-03-08 02:16:07 -0800877 for (i = 0; i < pavail_ents; i++) {
878 unsigned long start_pfn, end_pfn;
879
880 start_pfn = pavail[i].phys_addr >> PAGE_SHIFT;
881 end_pfn = (start_pfn + (pavail[i].reg_size >> PAGE_SHIFT));
David S. Millerd1112012006-03-08 02:16:07 -0800882 memory_present(0, start_pfn, end_pfn);
883 }
884
885 sparse_init();
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return end_pfn;
888}
889
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800890static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
891static int pall_ents __initdata;
892
David S. Miller56425302005-09-25 16:46:57 -0700893#ifdef CONFIG_DEBUG_PAGEALLOC
Sam Ravnborg896aef42008-02-24 19:49:52 -0800894static unsigned long __ref kernel_map_range(unsigned long pstart,
895 unsigned long pend, pgprot_t prot)
David S. Miller56425302005-09-25 16:46:57 -0700896{
897 unsigned long vstart = PAGE_OFFSET + pstart;
898 unsigned long vend = PAGE_OFFSET + pend;
899 unsigned long alloc_bytes = 0UL;
900
901 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
David S. Miller13edad72005-09-29 17:58:26 -0700902 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
David S. Miller56425302005-09-25 16:46:57 -0700903 vstart, vend);
904 prom_halt();
905 }
906
907 while (vstart < vend) {
908 unsigned long this_end, paddr = __pa(vstart);
909 pgd_t *pgd = pgd_offset_k(vstart);
910 pud_t *pud;
911 pmd_t *pmd;
912 pte_t *pte;
913
914 pud = pud_offset(pgd, vstart);
915 if (pud_none(*pud)) {
916 pmd_t *new;
917
918 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
919 alloc_bytes += PAGE_SIZE;
920 pud_populate(&init_mm, pud, new);
921 }
922
923 pmd = pmd_offset(pud, vstart);
924 if (!pmd_present(*pmd)) {
925 pte_t *new;
926
927 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
928 alloc_bytes += PAGE_SIZE;
929 pmd_populate_kernel(&init_mm, pmd, new);
930 }
931
932 pte = pte_offset_kernel(pmd, vstart);
933 this_end = (vstart + PMD_SIZE) & PMD_MASK;
934 if (this_end > vend)
935 this_end = vend;
936
937 while (vstart < this_end) {
938 pte_val(*pte) = (paddr | pgprot_val(prot));
939
940 vstart += PAGE_SIZE;
941 paddr += PAGE_SIZE;
942 pte++;
943 }
944 }
945
946 return alloc_bytes;
947}
948
David S. Miller56425302005-09-25 16:46:57 -0700949extern unsigned int kvmap_linear_patch[1];
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800950#endif /* CONFIG_DEBUG_PAGEALLOC */
951
952static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
953{
954 const unsigned long shift_256MB = 28;
955 const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
956 const unsigned long size_256MB = (1UL << shift_256MB);
957
958 while (start < end) {
959 long remains;
960
David S. Millerf7c00332006-03-05 22:18:50 -0800961 remains = end - start;
962 if (remains < size_256MB)
963 break;
964
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800965 if (start & mask_256MB) {
966 start = (start + size_256MB) & ~mask_256MB;
967 continue;
968 }
969
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800970 while (remains >= size_256MB) {
971 unsigned long index = start >> shift_256MB;
972
973 __set_bit(index, kpte_linear_bitmap);
974
975 start += size_256MB;
976 remains -= size_256MB;
977 }
978 }
979}
David S. Miller56425302005-09-25 16:46:57 -0700980
David S. Miller8f3614532007-12-13 06:13:38 -0800981static void __init init_kpte_bitmap(void)
David S. Miller56425302005-09-25 16:46:57 -0700982{
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800983 unsigned long i;
David S. Miller13edad72005-09-29 17:58:26 -0700984
985 for (i = 0; i < pall_ents; i++) {
David S. Miller56425302005-09-25 16:46:57 -0700986 unsigned long phys_start, phys_end;
987
David S. Miller13edad72005-09-29 17:58:26 -0700988 phys_start = pall[i].phys_addr;
989 phys_end = phys_start + pall[i].reg_size;
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800990
991 mark_kpte_bitmap(phys_start, phys_end);
David S. Miller8f3614532007-12-13 06:13:38 -0800992 }
993}
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800994
David S. Miller8f3614532007-12-13 06:13:38 -0800995static void __init kernel_physical_mapping_init(void)
996{
David S. Miller9cc3a1a2006-02-21 20:51:13 -0800997#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller8f3614532007-12-13 06:13:38 -0800998 unsigned long i, mem_alloced = 0UL;
999
1000 for (i = 0; i < pall_ents; i++) {
1001 unsigned long phys_start, phys_end;
1002
1003 phys_start = pall[i].phys_addr;
1004 phys_end = phys_start + pall[i].reg_size;
1005
David S. Miller56425302005-09-25 16:46:57 -07001006 mem_alloced += kernel_map_range(phys_start, phys_end,
1007 PAGE_KERNEL);
David S. Miller56425302005-09-25 16:46:57 -07001008 }
1009
1010 printk("Allocated %ld bytes for kernel page tables.\n",
1011 mem_alloced);
1012
1013 kvmap_linear_patch[0] = 0x01000000; /* nop */
1014 flushi(&kvmap_linear_patch[0]);
1015
1016 __flush_tlb_all();
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001017#endif
David S. Miller56425302005-09-25 16:46:57 -07001018}
1019
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001020#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller56425302005-09-25 16:46:57 -07001021void kernel_map_pages(struct page *page, int numpages, int enable)
1022{
1023 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1024 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1025
1026 kernel_map_range(phys_start, phys_end,
1027 (enable ? PAGE_KERNEL : __pgprot(0)));
1028
David S. Miller74bf4312006-01-31 18:29:18 -08001029 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1030 PAGE_OFFSET + phys_end);
1031
David S. Miller56425302005-09-25 16:46:57 -07001032 /* we should perform an IPI and flush all tlbs,
1033 * but that can deadlock->flush only current cpu.
1034 */
1035 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1036 PAGE_OFFSET + phys_end);
1037}
1038#endif
1039
David S. Miller10147572005-09-28 21:46:43 -07001040unsigned long __init find_ecache_flush_span(unsigned long size)
1041{
David S. Miller13edad72005-09-29 17:58:26 -07001042 int i;
David S. Miller10147572005-09-28 21:46:43 -07001043
David S. Miller13edad72005-09-29 17:58:26 -07001044 for (i = 0; i < pavail_ents; i++) {
1045 if (pavail[i].reg_size >= size)
1046 return pavail[i].phys_addr;
David S. Miller10147572005-09-28 21:46:43 -07001047 }
1048
1049 return ~0UL;
1050}
1051
David S. Miller517af332006-02-01 15:55:21 -08001052static void __init tsb_phys_patch(void)
1053{
David S. Millerd257d5d2006-02-06 23:44:37 -08001054 struct tsb_ldquad_phys_patch_entry *pquad;
David S. Miller517af332006-02-01 15:55:21 -08001055 struct tsb_phys_patch_entry *p;
1056
David S. Millerd257d5d2006-02-06 23:44:37 -08001057 pquad = &__tsb_ldquad_phys_patch;
1058 while (pquad < &__tsb_ldquad_phys_patch_end) {
1059 unsigned long addr = pquad->addr;
1060
1061 if (tlb_type == hypervisor)
1062 *(unsigned int *) addr = pquad->sun4v_insn;
1063 else
1064 *(unsigned int *) addr = pquad->sun4u_insn;
1065 wmb();
1066 __asm__ __volatile__("flush %0"
1067 : /* no outputs */
1068 : "r" (addr));
1069
1070 pquad++;
1071 }
1072
David S. Miller517af332006-02-01 15:55:21 -08001073 p = &__tsb_phys_patch;
1074 while (p < &__tsb_phys_patch_end) {
1075 unsigned long addr = p->addr;
1076
1077 *(unsigned int *) addr = p->insn;
1078 wmb();
1079 __asm__ __volatile__("flush %0"
1080 : /* no outputs */
1081 : "r" (addr));
1082
1083 p++;
1084 }
1085}
1086
David S. Miller490384e2006-02-11 14:41:18 -08001087/* Don't mark as init, we give this to the Hypervisor. */
David S. Millerd1acb422007-03-16 17:20:28 -07001088#ifndef CONFIG_DEBUG_PAGEALLOC
1089#define NUM_KTSB_DESCR 2
1090#else
1091#define NUM_KTSB_DESCR 1
1092#endif
1093static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
David S. Miller490384e2006-02-11 14:41:18 -08001094extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
1095
1096static void __init sun4v_ktsb_init(void)
1097{
1098 unsigned long ktsb_pa;
1099
David S. Millerd7744a02006-02-21 22:31:11 -08001100 /* First KTSB for PAGE_SIZE mappings. */
David S. Miller490384e2006-02-11 14:41:18 -08001101 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
1102
1103 switch (PAGE_SIZE) {
1104 case 8 * 1024:
1105 default:
1106 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
1107 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
1108 break;
1109
1110 case 64 * 1024:
1111 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
1112 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
1113 break;
1114
1115 case 512 * 1024:
1116 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
1117 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
1118 break;
1119
1120 case 4 * 1024 * 1024:
1121 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
1122 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
1123 break;
1124 };
1125
David S. Miller3f19a842006-02-17 12:03:20 -08001126 ktsb_descr[0].assoc = 1;
David S. Miller490384e2006-02-11 14:41:18 -08001127 ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
1128 ktsb_descr[0].ctx_idx = 0;
1129 ktsb_descr[0].tsb_base = ktsb_pa;
1130 ktsb_descr[0].resv = 0;
1131
David S. Millerd1acb422007-03-16 17:20:28 -07001132#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Millerd7744a02006-02-21 22:31:11 -08001133 /* Second KTSB for 4MB/256MB mappings. */
1134 ktsb_pa = (kern_base +
1135 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
1136
1137 ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
1138 ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
1139 HV_PGSZ_MASK_256MB);
1140 ktsb_descr[1].assoc = 1;
1141 ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
1142 ktsb_descr[1].ctx_idx = 0;
1143 ktsb_descr[1].tsb_base = ktsb_pa;
1144 ktsb_descr[1].resv = 0;
David S. Millerd1acb422007-03-16 17:20:28 -07001145#endif
David S. Miller490384e2006-02-11 14:41:18 -08001146}
1147
1148void __cpuinit sun4v_ktsb_register(void)
1149{
David S. Miller7db35f32007-05-29 02:22:14 -07001150 unsigned long pa, ret;
David S. Miller490384e2006-02-11 14:41:18 -08001151
1152 pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
1153
David S. Miller7db35f32007-05-29 02:22:14 -07001154 ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
1155 if (ret != 0) {
1156 prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
1157 "errors with %lx\n", pa, ret);
1158 prom_halt();
1159 }
David S. Miller490384e2006-02-11 14:41:18 -08001160}
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162/* paging_init() sets up the page tables */
1163
David S. Miller5cbc3072007-05-25 15:49:59 -07001164extern void central_probe(void);
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166static unsigned long last_valid_pfn;
David S. Miller56425302005-09-25 16:46:57 -07001167pgd_t swapper_pg_dir[2048];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
David S. Millerc4bce902006-02-11 21:57:54 -08001169static void sun4u_pgprot_init(void);
1170static void sun4v_pgprot_init(void);
1171
travis@sgi.com3afc6202008-01-30 23:27:58 +01001172/* Dummy function */
1173void __init setup_per_cpu_areas(void)
1174{
1175}
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177void __init paging_init(void)
1178{
David S. Millerd1112012006-03-08 02:16:07 -08001179 unsigned long end_pfn, pages_avail, shift, phys_base;
David S. Miller0836a0e2005-09-28 21:38:08 -07001180 unsigned long real_end, i;
1181
David S. Miller22adb352007-05-26 01:14:43 -07001182 /* These build time checkes make sure that the dcache_dirty_cpu()
1183 * page->flags usage will work.
1184 *
1185 * When a page gets marked as dcache-dirty, we store the
1186 * cpu number starting at bit 32 in the page->flags. Also,
1187 * functions like clear_dcache_dirty_cpu use the cpu mask
1188 * in 13-bit signed-immediate instruction fields.
1189 */
1190 BUILD_BUG_ON(FLAGS_RESERVED != 32);
1191 BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
1192 ilog2(roundup_pow_of_two(NR_CPUS)) > FLAGS_RESERVED);
1193 BUILD_BUG_ON(NR_CPUS > 4096);
1194
David S. Miller481295f2006-02-07 21:51:08 -08001195 kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
1196 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
1197
David S. Miller22d6a1c2007-05-25 00:37:12 -07001198 sstate_booting();
1199
David S. Millerd7744a02006-02-21 22:31:11 -08001200 /* Invalidate both kernel TSBs. */
David S. Miller8b234272006-02-17 18:01:02 -08001201 memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
David S. Millerd1acb422007-03-16 17:20:28 -07001202#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Millerd7744a02006-02-21 22:31:11 -08001203 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
David S. Millerd1acb422007-03-16 17:20:28 -07001204#endif
David S. Miller8b234272006-02-17 18:01:02 -08001205
David S. Millerc4bce902006-02-11 21:57:54 -08001206 if (tlb_type == hypervisor)
1207 sun4v_pgprot_init();
1208 else
1209 sun4u_pgprot_init();
1210
David S. Millerd257d5d2006-02-06 23:44:37 -08001211 if (tlb_type == cheetah_plus ||
1212 tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -08001213 tsb_phys_patch();
1214
David S. Miller490384e2006-02-11 14:41:18 -08001215 if (tlb_type == hypervisor) {
David S. Millerd257d5d2006-02-06 23:44:37 -08001216 sun4v_patch_tlb_handlers();
David S. Miller490384e2006-02-11 14:41:18 -08001217 sun4v_ktsb_init();
1218 }
David S. Millerd257d5d2006-02-06 23:44:37 -08001219
David S. Miller3b2a7e22008-02-13 18:13:20 -08001220 lmb_init();
1221
David S. Miller13edad72005-09-29 17:58:26 -07001222 /* Find available physical memory... */
1223 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller0836a0e2005-09-28 21:38:08 -07001224
1225 phys_base = 0xffffffffffffffffUL;
David S. Miller3b2a7e22008-02-13 18:13:20 -08001226 for (i = 0; i < pavail_ents; i++) {
David S. Miller13edad72005-09-29 17:58:26 -07001227 phys_base = min(phys_base, pavail[i].phys_addr);
David S. Miller3b2a7e22008-02-13 18:13:20 -08001228 lmb_add(pavail[i].phys_addr, pavail[i].reg_size);
1229 }
1230
1231 lmb_reserve(kern_base, kern_size);
David S. Miller0836a0e2005-09-28 21:38:08 -07001232
David S. Miller4e82c9a2008-02-13 18:00:03 -08001233 find_ramdisk(phys_base);
1234
David S. Miller25b0c652008-02-13 18:20:14 -08001235 if (cmdline_memory_size)
1236 lmb_enforce_memory_limit(phys_base + cmdline_memory_size);
1237
David S. Miller3b2a7e22008-02-13 18:13:20 -08001238 lmb_analyze();
1239 lmb_dump_all();
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 set_bit(0, mmu_context_bmap);
1242
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001243 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 real_end = (unsigned long)_end;
David S. Miller64658742008-03-21 17:01:38 -07001246 num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << 22);
1247 printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
1248 num_kernel_image_mappings);
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001249
1250 /* Set kernel pgd to upper alias so physical page computations
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 * work.
1252 */
1253 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1254
David S. Miller56425302005-09-25 16:46:57 -07001255 memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /* Now can init the kernel/bad page tables. */
1258 pud_set(pud_offset(&swapper_pg_dir[0], 0),
David S. Miller56425302005-09-25 16:46:57 -07001259 swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
David S. Millerc9c10832005-10-12 12:22:46 -07001261 inherit_prom_mappings();
David S. Miller5085b4a2005-09-22 00:45:41 -07001262
David S. Miller8f3614532007-12-13 06:13:38 -08001263 read_obp_memory("reg", &pall[0], &pall_ents);
1264
1265 init_kpte_bitmap();
1266
David S. Millera8b900d2006-01-31 18:33:37 -08001267 /* Ok, we can use our TLB miss and window trap handlers safely. */
1268 setup_tba();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
David S. Millerc9c10832005-10-12 12:22:46 -07001270 __flush_tlb_all();
David S. Miller9ad98c52005-10-05 15:12:00 -07001271
David S. Miller490384e2006-02-11 14:41:18 -08001272 if (tlb_type == hypervisor)
1273 sun4v_ktsb_register();
1274
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001275 /* Setup bootmem... */
1276 pages_avail = 0;
David S. Millerd1112012006-03-08 02:16:07 -08001277 last_valid_pfn = end_pfn = bootmem_init(&pages_avail, phys_base);
1278
David S. Miller17b0e192006-03-08 15:57:03 -08001279 max_mapnr = last_valid_pfn;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001280
David S. Miller56425302005-09-25 16:46:57 -07001281 kernel_physical_mapping_init();
David S. Miller56425302005-09-25 16:46:57 -07001282
David S. Miller5cbc3072007-05-25 15:49:59 -07001283 real_setup_per_cpu_areas();
1284
David S. Miller372b07b2006-06-21 15:35:28 -07001285 prom_build_devicetree();
1286
David S. Miller5cbc3072007-05-25 15:49:59 -07001287 if (tlb_type == hypervisor)
1288 sun4v_mdesc_init();
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 {
1291 unsigned long zones_size[MAX_NR_ZONES];
1292 unsigned long zholes_size[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 int znum;
1294
1295 for (znum = 0; znum < MAX_NR_ZONES; znum++)
1296 zones_size[znum] = zholes_size[znum] = 0;
1297
David S. Miller1b51d3a2007-02-12 00:13:31 -08001298 zones_size[ZONE_NORMAL] = end_pfn;
1299 zholes_size[ZONE_NORMAL] = end_pfn - pages_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 free_area_init_node(0, &contig_page_data, zones_size,
David S. Miller17b0e192006-03-08 15:57:03 -08001302 __pa(PAGE_OFFSET) >> PAGE_SHIFT,
1303 zholes_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
David S. Miller3c62a2d2008-02-17 23:22:50 -08001306 printk("Booting Linux...\n");
David S. Miller5cbc3072007-05-25 15:49:59 -07001307
1308 central_probe();
1309 cpu_probe();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312static void __init taint_real_pages(void)
1313{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 int i;
1315
David S. Miller13edad72005-09-29 17:58:26 -07001316 read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
David S. Miller13edad72005-09-29 17:58:26 -07001318 /* Find changes discovered in the physmem available rescan and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 * reserve the lost portions in the bootmem maps.
1320 */
David S. Miller13edad72005-09-29 17:58:26 -07001321 for (i = 0; i < pavail_ents; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 unsigned long old_start, old_end;
1323
David S. Miller13edad72005-09-29 17:58:26 -07001324 old_start = pavail[i].phys_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 old_end = old_start +
David S. Miller13edad72005-09-29 17:58:26 -07001326 pavail[i].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 while (old_start < old_end) {
1328 int n;
1329
David S. Millerc2a5a462006-06-22 00:01:56 -07001330 for (n = 0; n < pavail_rescan_ents; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 unsigned long new_start, new_end;
1332
David S. Miller13edad72005-09-29 17:58:26 -07001333 new_start = pavail_rescan[n].phys_addr;
1334 new_end = new_start +
1335 pavail_rescan[n].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 if (new_start <= old_start &&
1338 new_end >= (old_start + PAGE_SIZE)) {
David S. Miller13edad72005-09-29 17:58:26 -07001339 set_bit(old_start >> 22,
1340 sparc64_valid_addr_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 goto do_next_page;
1342 }
1343 }
Bernhard Walle72a7fe32008-02-07 00:15:17 -08001344 reserve_bootmem(old_start, PAGE_SIZE, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 do_next_page:
1347 old_start += PAGE_SIZE;
1348 }
1349 }
1350}
1351
David S. Millerc2a5a462006-06-22 00:01:56 -07001352int __init page_in_phys_avail(unsigned long paddr)
1353{
1354 int i;
1355
1356 paddr &= PAGE_MASK;
1357
1358 for (i = 0; i < pavail_rescan_ents; i++) {
1359 unsigned long start, end;
1360
1361 start = pavail_rescan[i].phys_addr;
1362 end = start + pavail_rescan[i].reg_size;
1363
1364 if (paddr >= start && paddr < end)
1365 return 1;
1366 }
1367 if (paddr >= kern_base && paddr < (kern_base + kern_size))
1368 return 1;
1369#ifdef CONFIG_BLK_DEV_INITRD
1370 if (paddr >= __pa(initrd_start) &&
1371 paddr < __pa(PAGE_ALIGN(initrd_end)))
1372 return 1;
1373#endif
1374
1375 return 0;
1376}
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378void __init mem_init(void)
1379{
1380 unsigned long codepages, datapages, initpages;
1381 unsigned long addr, last;
1382 int i;
1383
1384 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1385 i += 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001386 sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (sparc64_valid_addr_bitmap == NULL) {
1388 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1389 prom_halt();
1390 }
1391 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1392
1393 addr = PAGE_OFFSET + kern_base;
1394 last = PAGE_ALIGN(kern_size) + addr;
1395 while (addr < last) {
1396 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1397 addr += PAGE_SIZE;
1398 }
1399
1400 taint_real_pages();
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1403
David S. Millerf1cfdb52007-03-15 22:52:18 -07001404 /* We subtract one to account for the mem_map_zero page
1405 * allocated below.
1406 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 totalram_pages = num_physpages = free_all_bootmem() - 1;
1408
1409 /*
1410 * Set up the zero page, mark it reserved, so that page count
1411 * is not manipulated when freeing the page from user ptes.
1412 */
1413 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
1414 if (mem_map_zero == NULL) {
1415 prom_printf("paging_init: Cannot alloc zero page.\n");
1416 prom_halt();
1417 }
1418 SetPageReserved(mem_map_zero);
1419
1420 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1421 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1422 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1423 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1424 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1425 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1426
Christoph Lameter96177292007-02-10 01:43:03 -08001427 printk("Memory: %luk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 nr_free_pages() << (PAGE_SHIFT-10),
1429 codepages << (PAGE_SHIFT-10),
1430 datapages << (PAGE_SHIFT-10),
1431 initpages << (PAGE_SHIFT-10),
1432 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1433
1434 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1435 cheetah_ecache_flush_init();
1436}
1437
David S. Miller898cf0e2005-09-23 11:59:44 -07001438void free_initmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 unsigned long addr, initend;
1441
1442 /*
1443 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1444 */
1445 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1446 initend = (unsigned long)(__init_end) & PAGE_MASK;
1447 for (; addr < initend; addr += PAGE_SIZE) {
1448 unsigned long page;
1449 struct page *p;
1450
1451 page = (addr +
1452 ((unsigned long) __va(kern_base)) -
1453 ((unsigned long) KERNBASE));
Randy Dunlapc9cf5522006-06-27 02:53:52 -07001454 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 p = virt_to_page(page);
1456
1457 ClearPageReserved(p);
Nick Piggin7835e982006-03-22 00:08:40 -08001458 init_page_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 __free_page(p);
1460 num_physpages++;
1461 totalram_pages++;
1462 }
1463}
1464
1465#ifdef CONFIG_BLK_DEV_INITRD
1466void free_initrd_mem(unsigned long start, unsigned long end)
1467{
1468 if (start < end)
1469 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1470 for (; start < end; start += PAGE_SIZE) {
1471 struct page *p = virt_to_page(start);
1472
1473 ClearPageReserved(p);
Nick Piggin7835e982006-03-22 00:08:40 -08001474 init_page_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 __free_page(p);
1476 num_physpages++;
1477 totalram_pages++;
1478 }
1479}
1480#endif
David S. Millerc4bce902006-02-11 21:57:54 -08001481
David S. Millerc4bce902006-02-11 21:57:54 -08001482#define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
1483#define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
1484#define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
1485#define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
1486#define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
1487#define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
1488
1489pgprot_t PAGE_KERNEL __read_mostly;
1490EXPORT_SYMBOL(PAGE_KERNEL);
1491
1492pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
1493pgprot_t PAGE_COPY __read_mostly;
David S. Miller0f159522006-02-18 12:43:16 -08001494
1495pgprot_t PAGE_SHARED __read_mostly;
1496EXPORT_SYMBOL(PAGE_SHARED);
1497
David S. Millerc4bce902006-02-11 21:57:54 -08001498pgprot_t PAGE_EXEC __read_mostly;
1499unsigned long pg_iobits __read_mostly;
1500
1501unsigned long _PAGE_IE __read_mostly;
David S. Miller987c74f2006-06-25 01:34:43 -07001502EXPORT_SYMBOL(_PAGE_IE);
David S. Millerb2bef442006-02-23 01:55:55 -08001503
David S. Millerc4bce902006-02-11 21:57:54 -08001504unsigned long _PAGE_E __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08001505EXPORT_SYMBOL(_PAGE_E);
1506
David S. Millerc4bce902006-02-11 21:57:54 -08001507unsigned long _PAGE_CACHE __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08001508EXPORT_SYMBOL(_PAGE_CACHE);
David S. Millerc4bce902006-02-11 21:57:54 -08001509
David Miller46644c22007-10-16 01:24:16 -07001510#ifdef CONFIG_SPARSEMEM_VMEMMAP
1511
1512#define VMEMMAP_CHUNK_SHIFT 22
1513#define VMEMMAP_CHUNK (1UL << VMEMMAP_CHUNK_SHIFT)
1514#define VMEMMAP_CHUNK_MASK ~(VMEMMAP_CHUNK - 1UL)
1515#define VMEMMAP_ALIGN(x) (((x)+VMEMMAP_CHUNK-1UL)&VMEMMAP_CHUNK_MASK)
1516
1517#define VMEMMAP_SIZE ((((1UL << MAX_PHYSADDR_BITS) >> PAGE_SHIFT) * \
1518 sizeof(struct page *)) >> VMEMMAP_CHUNK_SHIFT)
1519unsigned long vmemmap_table[VMEMMAP_SIZE];
1520
1521int __meminit vmemmap_populate(struct page *start, unsigned long nr, int node)
1522{
1523 unsigned long vstart = (unsigned long) start;
1524 unsigned long vend = (unsigned long) (start + nr);
1525 unsigned long phys_start = (vstart - VMEMMAP_BASE);
1526 unsigned long phys_end = (vend - VMEMMAP_BASE);
1527 unsigned long addr = phys_start & VMEMMAP_CHUNK_MASK;
1528 unsigned long end = VMEMMAP_ALIGN(phys_end);
1529 unsigned long pte_base;
1530
1531 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
1532 _PAGE_CP_4U | _PAGE_CV_4U |
1533 _PAGE_P_4U | _PAGE_W_4U);
1534 if (tlb_type == hypervisor)
1535 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
1536 _PAGE_CP_4V | _PAGE_CV_4V |
1537 _PAGE_P_4V | _PAGE_W_4V);
1538
1539 for (; addr < end; addr += VMEMMAP_CHUNK) {
1540 unsigned long *vmem_pp =
1541 vmemmap_table + (addr >> VMEMMAP_CHUNK_SHIFT);
1542 void *block;
1543
1544 if (!(*vmem_pp & _PAGE_VALID)) {
1545 block = vmemmap_alloc_block(1UL << 22, node);
1546 if (!block)
1547 return -ENOMEM;
1548
1549 *vmem_pp = pte_base | __pa(block);
1550
1551 printk(KERN_INFO "[%p-%p] page_structs=%lu "
1552 "node=%d entry=%lu/%lu\n", start, block, nr,
1553 node,
1554 addr >> VMEMMAP_CHUNK_SHIFT,
1555 VMEMMAP_SIZE >> VMEMMAP_CHUNK_SHIFT);
1556 }
1557 }
1558 return 0;
1559}
1560#endif /* CONFIG_SPARSEMEM_VMEMMAP */
1561
David S. Millerc4bce902006-02-11 21:57:54 -08001562static void prot_init_common(unsigned long page_none,
1563 unsigned long page_shared,
1564 unsigned long page_copy,
1565 unsigned long page_readonly,
1566 unsigned long page_exec_bit)
1567{
1568 PAGE_COPY = __pgprot(page_copy);
David S. Miller0f159522006-02-18 12:43:16 -08001569 PAGE_SHARED = __pgprot(page_shared);
David S. Millerc4bce902006-02-11 21:57:54 -08001570
1571 protection_map[0x0] = __pgprot(page_none);
1572 protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
1573 protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
1574 protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
1575 protection_map[0x4] = __pgprot(page_readonly);
1576 protection_map[0x5] = __pgprot(page_readonly);
1577 protection_map[0x6] = __pgprot(page_copy);
1578 protection_map[0x7] = __pgprot(page_copy);
1579 protection_map[0x8] = __pgprot(page_none);
1580 protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
1581 protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
1582 protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
1583 protection_map[0xc] = __pgprot(page_readonly);
1584 protection_map[0xd] = __pgprot(page_readonly);
1585 protection_map[0xe] = __pgprot(page_shared);
1586 protection_map[0xf] = __pgprot(page_shared);
1587}
1588
1589static void __init sun4u_pgprot_init(void)
1590{
1591 unsigned long page_none, page_shared, page_copy, page_readonly;
1592 unsigned long page_exec_bit;
1593
1594 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
1595 _PAGE_CACHE_4U | _PAGE_P_4U |
1596 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
1597 _PAGE_EXEC_4U);
1598 PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
1599 _PAGE_CACHE_4U | _PAGE_P_4U |
1600 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
1601 _PAGE_EXEC_4U | _PAGE_L_4U);
1602 PAGE_EXEC = __pgprot(_PAGE_EXEC_4U);
1603
1604 _PAGE_IE = _PAGE_IE_4U;
1605 _PAGE_E = _PAGE_E_4U;
1606 _PAGE_CACHE = _PAGE_CACHE_4U;
1607
1608 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
1609 __ACCESS_BITS_4U | _PAGE_E_4U);
1610
David S. Millerd1acb422007-03-16 17:20:28 -07001611#ifdef CONFIG_DEBUG_PAGEALLOC
1612 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4U) ^
1613 0xfffff80000000000;
1614#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001615 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
David S. Millerc4bce902006-02-11 21:57:54 -08001616 0xfffff80000000000;
David S. Millerd1acb422007-03-16 17:20:28 -07001617#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001618 kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
1619 _PAGE_P_4U | _PAGE_W_4U);
1620
1621 /* XXX Should use 256MB on Panther. XXX */
1622 kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
David S. Millerc4bce902006-02-11 21:57:54 -08001623
1624 _PAGE_SZBITS = _PAGE_SZBITS_4U;
1625 _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
1626 _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
1627 _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
1628
1629
1630 page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
1631 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1632 __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
1633 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1634 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
1635 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
1636 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
1637
1638 page_exec_bit = _PAGE_EXEC_4U;
1639
1640 prot_init_common(page_none, page_shared, page_copy, page_readonly,
1641 page_exec_bit);
1642}
1643
1644static void __init sun4v_pgprot_init(void)
1645{
1646 unsigned long page_none, page_shared, page_copy, page_readonly;
1647 unsigned long page_exec_bit;
1648
1649 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
1650 _PAGE_CACHE_4V | _PAGE_P_4V |
1651 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
1652 _PAGE_EXEC_4V);
1653 PAGE_KERNEL_LOCKED = PAGE_KERNEL;
1654 PAGE_EXEC = __pgprot(_PAGE_EXEC_4V);
1655
1656 _PAGE_IE = _PAGE_IE_4V;
1657 _PAGE_E = _PAGE_E_4V;
1658 _PAGE_CACHE = _PAGE_CACHE_4V;
1659
David S. Millerd1acb422007-03-16 17:20:28 -07001660#ifdef CONFIG_DEBUG_PAGEALLOC
1661 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
1662 0xfffff80000000000;
1663#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001664 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
David S. Millerc4bce902006-02-11 21:57:54 -08001665 0xfffff80000000000;
David S. Millerd1acb422007-03-16 17:20:28 -07001666#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001667 kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
1668 _PAGE_P_4V | _PAGE_W_4V);
1669
David S. Millerd1acb422007-03-16 17:20:28 -07001670#ifdef CONFIG_DEBUG_PAGEALLOC
1671 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
1672 0xfffff80000000000;
1673#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001674 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
1675 0xfffff80000000000;
David S. Millerd1acb422007-03-16 17:20:28 -07001676#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001677 kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
1678 _PAGE_P_4V | _PAGE_W_4V);
David S. Millerc4bce902006-02-11 21:57:54 -08001679
1680 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
1681 __ACCESS_BITS_4V | _PAGE_E_4V);
1682
1683 _PAGE_SZBITS = _PAGE_SZBITS_4V;
1684 _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
1685 _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
1686 _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
1687 _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
1688
1689 page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
1690 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1691 __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
1692 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1693 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
1694 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
1695 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
1696
1697 page_exec_bit = _PAGE_EXEC_4V;
1698
1699 prot_init_common(page_none, page_shared, page_copy, page_readonly,
1700 page_exec_bit);
1701}
1702
1703unsigned long pte_sz_bits(unsigned long sz)
1704{
1705 if (tlb_type == hypervisor) {
1706 switch (sz) {
1707 case 8 * 1024:
1708 default:
1709 return _PAGE_SZ8K_4V;
1710 case 64 * 1024:
1711 return _PAGE_SZ64K_4V;
1712 case 512 * 1024:
1713 return _PAGE_SZ512K_4V;
1714 case 4 * 1024 * 1024:
1715 return _PAGE_SZ4MB_4V;
1716 };
1717 } else {
1718 switch (sz) {
1719 case 8 * 1024:
1720 default:
1721 return _PAGE_SZ8K_4U;
1722 case 64 * 1024:
1723 return _PAGE_SZ64K_4U;
1724 case 512 * 1024:
1725 return _PAGE_SZ512K_4U;
1726 case 4 * 1024 * 1024:
1727 return _PAGE_SZ4MB_4U;
1728 };
1729 }
1730}
1731
1732pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
1733{
1734 pte_t pte;
David S. Millercf627152006-02-12 21:10:07 -08001735
1736 pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
David S. Millerc4bce902006-02-11 21:57:54 -08001737 pte_val(pte) |= (((unsigned long)space) << 32);
1738 pte_val(pte) |= pte_sz_bits(page_size);
David S. Millercf627152006-02-12 21:10:07 -08001739
David S. Millerc4bce902006-02-11 21:57:54 -08001740 return pte;
1741}
1742
David S. Millerc4bce902006-02-11 21:57:54 -08001743static unsigned long kern_large_tte(unsigned long paddr)
1744{
1745 unsigned long val;
1746
1747 val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
1748 _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
1749 _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
1750 if (tlb_type == hypervisor)
1751 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
1752 _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
1753 _PAGE_EXEC_4V | _PAGE_W_4V);
1754
1755 return val | paddr;
1756}
1757
David S. Millerc4bce902006-02-11 21:57:54 -08001758/* If not locked, zap it. */
1759void __flush_tlb_all(void)
1760{
1761 unsigned long pstate;
1762 int i;
1763
1764 __asm__ __volatile__("flushw\n\t"
1765 "rdpr %%pstate, %0\n\t"
1766 "wrpr %0, %1, %%pstate"
1767 : "=r" (pstate)
1768 : "i" (PSTATE_IE));
David S. Miller8f3614532007-12-13 06:13:38 -08001769 if (tlb_type == hypervisor) {
1770 sun4v_mmu_demap_all();
1771 } else if (tlb_type == spitfire) {
David S. Millerc4bce902006-02-11 21:57:54 -08001772 for (i = 0; i < 64; i++) {
1773 /* Spitfire Errata #32 workaround */
1774 /* NOTE: Always runs on spitfire, so no
1775 * cheetah+ page size encodings.
1776 */
1777 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1778 "flush %%g6"
1779 : /* No outputs */
1780 : "r" (0),
1781 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1782
1783 if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
1784 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1785 "membar #Sync"
1786 : /* no outputs */
1787 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
1788 spitfire_put_dtlb_data(i, 0x0UL);
1789 }
1790
1791 /* Spitfire Errata #32 workaround */
1792 /* NOTE: Always runs on spitfire, so no
1793 * cheetah+ page size encodings.
1794 */
1795 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
1796 "flush %%g6"
1797 : /* No outputs */
1798 : "r" (0),
1799 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
1800
1801 if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
1802 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
1803 "membar #Sync"
1804 : /* no outputs */
1805 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
1806 spitfire_put_itlb_data(i, 0x0UL);
1807 }
1808 }
1809 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1810 cheetah_flush_dtlb_all();
1811 cheetah_flush_itlb_all();
1812 }
1813 __asm__ __volatile__("wrpr %0, 0, %%pstate"
1814 : : "r" (pstate));
1815}
David S. Miller88d70792006-03-18 19:16:23 -08001816
1817#ifdef CONFIG_MEMORY_HOTPLUG
1818
1819void online_page(struct page *page)
1820{
1821 ClearPageReserved(page);
Nick Pigginfcab1e52006-03-23 07:48:16 +01001822 init_page_count(page);
1823 __free_page(page);
David S. Miller88d70792006-03-18 19:16:23 -08001824 totalram_pages++;
1825 num_physpages++;
1826}
1827
David S. Miller88d70792006-03-18 19:16:23 -08001828#endif /* CONFIG_MEMORY_HOTPLUG */