blob: 217de3ea29e842cff4c0eff6be2b82c5a13f25fe [file] [log] [blame]
Adrian Bunkb00dc832008-05-19 16:52:27 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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>
David S. Miller919ee672008-04-23 05:40:25 -070028#include <linux/mmzone.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/head.h>
31#include <asm/system.h>
32#include <asm/page.h>
33#include <asm/pgalloc.h>
34#include <asm/pgtable.h>
35#include <asm/oplib.h>
36#include <asm/iommu.h>
37#include <asm/io.h>
38#include <asm/uaccess.h>
39#include <asm/mmu_context.h>
40#include <asm/tlbflush.h>
41#include <asm/dma.h>
42#include <asm/starfire.h>
43#include <asm/tlb.h>
44#include <asm/spitfire.h>
45#include <asm/sections.h>
David S. Miller517af332006-02-01 15:55:21 -080046#include <asm/tsb.h>
David S. Miller481295f2006-02-07 21:51:08 -080047#include <asm/hypervisor.h>
David S. Miller372b07b2006-06-21 15:35:28 -070048#include <asm/prom.h>
David S. Miller22d6a1c2007-05-25 00:37:12 -070049#include <asm/sstate.h>
David S. Miller5cbc3072007-05-25 15:49:59 -070050#include <asm/mdesc.h>
David S. Miller3d5ae6b2008-03-25 21:51:40 -070051#include <asm/cpudata.h>
David S. Miller4f70f7a2008-08-12 18:33:56 -070052#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
David S. Miller9cc3a1a2006-02-21 20:51:13 -080054#define MAX_PHYS_ADDRESS (1UL << 42UL)
55#define KPTE_BITMAP_CHUNK_SZ (256UL * 1024UL * 1024UL)
56#define KPTE_BITMAP_BYTES \
57 ((MAX_PHYS_ADDRESS / KPTE_BITMAP_CHUNK_SZ) / 8)
58
59unsigned long kern_linear_pte_xor[2] __read_mostly;
60
61/* A bitmap, one bit for every 256MB of physical memory. If the bit
62 * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
63 * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
64 */
65unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
66
David S. Millerd1acb422007-03-16 17:20:28 -070067#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Miller2d9e2762007-05-29 01:58:31 -070068/* A special kernel TSB for 4MB and 256MB linear mappings.
69 * Space is allocated for this right after the trap table
70 * in arch/sparc64/kernel/head.S
71 */
72extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
David S. Millerd1acb422007-03-16 17:20:28 -070073#endif
David S. Millerd7744a02006-02-21 22:31:11 -080074
David S. Miller13edad72005-09-29 17:58:26 -070075#define MAX_BANKS 32
David S. Miller10147572005-09-28 21:46:43 -070076
David S. Miller13edad72005-09-29 17:58:26 -070077static struct linux_prom64_registers pavail[MAX_BANKS] __initdata;
David S. Miller13edad72005-09-29 17:58:26 -070078static int pavail_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;
Aneesh Kumar K.V35802c02008-04-29 08:11:12 -0400164EXPORT_SYMBOL(mem_map_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
David S. Miller0835ae02005-10-04 15:23:20 -0700166unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
167
168unsigned long sparc64_kern_pri_context __read_mostly;
169unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
170unsigned long sparc64_kern_sec_context __read_mostly;
171
David S. Miller64658742008-03-21 17:01:38 -0700172int num_kernel_image_mappings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#ifdef CONFIG_DEBUG_DCFLUSH
175atomic_t dcpage_flushes = ATOMIC_INIT(0);
176#ifdef CONFIG_SMP
177atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
178#endif
179#endif
180
David S. Miller7a591cf2006-02-26 19:44:50 -0800181inline void flush_dcache_page_impl(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
David S. Miller7a591cf2006-02-26 19:44:50 -0800183 BUG_ON(tlb_type == hypervisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#ifdef CONFIG_DEBUG_DCFLUSH
185 atomic_inc(&dcpage_flushes);
186#endif
187
188#ifdef DCACHE_ALIASING_POSSIBLE
189 __flush_dcache_page(page_address(page),
190 ((tlb_type == spitfire) &&
191 page_mapping(page) != NULL));
192#else
193 if (page_mapping(page) != NULL &&
194 tlb_type == spitfire)
195 __flush_icache_page(__pa(page_address(page)));
196#endif
197}
198
199#define PG_dcache_dirty PG_arch_1
David S. Miller22adb352007-05-26 01:14:43 -0700200#define PG_dcache_cpu_shift 32UL
201#define PG_dcache_cpu_mask \
202 ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204#define dcache_dirty_cpu(page) \
David S. Miller48b0e542005-07-27 16:08:44 -0700205 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
David S. Millerd979f172007-10-27 00:13:04 -0700207static inline void set_dcache_dirty(struct page *page, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 unsigned long mask = this_cpu;
David S. Miller48b0e542005-07-27 16:08:44 -0700210 unsigned long non_cpu_bits;
211
212 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
213 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 __asm__ __volatile__("1:\n\t"
216 "ldx [%2], %%g7\n\t"
217 "and %%g7, %1, %%g1\n\t"
218 "or %%g1, %0, %%g1\n\t"
219 "casx [%2], %%g7, %%g1\n\t"
220 "cmp %%g7, %%g1\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700221 "membar #StoreLoad | #StoreStore\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700223 " nop"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 : /* no outputs */
225 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
226 : "g1", "g7");
227}
228
David S. Millerd979f172007-10-27 00:13:04 -0700229static inline void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 unsigned long mask = (1UL << PG_dcache_dirty);
232
233 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
234 "1:\n\t"
235 "ldx [%2], %%g7\n\t"
David S. Miller48b0e542005-07-27 16:08:44 -0700236 "srlx %%g7, %4, %%g1\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 "and %%g1, %3, %%g1\n\t"
238 "cmp %%g1, %0\n\t"
239 "bne,pn %%icc, 2f\n\t"
240 " andn %%g7, %1, %%g1\n\t"
241 "casx [%2], %%g7, %%g1\n\t"
242 "cmp %%g7, %%g1\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700243 "membar #StoreLoad | #StoreStore\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700245 " nop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 "2:"
247 : /* no outputs */
248 : "r" (cpu), "r" (mask), "r" (&page->flags),
David S. Miller48b0e542005-07-27 16:08:44 -0700249 "i" (PG_dcache_cpu_mask),
250 "i" (PG_dcache_cpu_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 : "g1", "g7");
252}
253
David S. Miller517af332006-02-01 15:55:21 -0800254static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
255{
256 unsigned long tsb_addr = (unsigned long) ent;
257
David S. Miller3b3ab2e2006-02-17 09:54:42 -0800258 if (tlb_type == cheetah_plus || tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -0800259 tsb_addr = __pa(tsb_addr);
260
261 __tsb_insert(tsb_addr, tag, pte);
262}
263
David S. Millerc4bce902006-02-11 21:57:54 -0800264unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
265unsigned long _PAGE_SZBITS __read_mostly;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
268{
David S. Millerbd407912006-01-31 18:31:38 -0800269 struct mm_struct *mm;
David S. Miller74ae9982006-03-05 18:26:24 -0800270 struct tsb *tsb;
David S. Miller7a1ac522006-03-16 02:02:32 -0800271 unsigned long tag, flags;
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800272 unsigned long tsb_index, tsb_hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David S. Miller7a591cf2006-02-26 19:44:50 -0800274 if (tlb_type != hypervisor) {
275 unsigned long pfn = pte_pfn(pte);
276 unsigned long pg_flags;
277 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
David S. Miller7a591cf2006-02-26 19:44:50 -0800279 if (pfn_valid(pfn) &&
280 (page = pfn_to_page(pfn), page_mapping(page)) &&
281 ((pg_flags = page->flags) & (1UL << PG_dcache_dirty))) {
282 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
283 PG_dcache_cpu_mask);
284 int this_cpu = get_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
David S. Miller7a591cf2006-02-26 19:44:50 -0800286 /* This is just to optimize away some function calls
287 * in the SMP case.
288 */
289 if (cpu == this_cpu)
290 flush_dcache_page_impl(page);
291 else
292 smp_flush_dcache_page_impl(page, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
David S. Miller7a591cf2006-02-26 19:44:50 -0800294 clear_dcache_dirty_cpu(page, cpu);
295
296 put_cpu();
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
David S. Millerbd407912006-01-31 18:31:38 -0800299
300 mm = vma->vm_mm;
David S. Miller7a1ac522006-03-16 02:02:32 -0800301
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800302 tsb_index = MM_TSB_BASE;
303 tsb_hash_shift = PAGE_SHIFT;
304
David S. Miller7a1ac522006-03-16 02:02:32 -0800305 spin_lock_irqsave(&mm->context.lock, flags);
306
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800307#ifdef CONFIG_HUGETLB_PAGE
308 if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL) {
309 if ((tlb_type == hypervisor &&
310 (pte_val(pte) & _PAGE_SZALL_4V) == _PAGE_SZHUGE_4V) ||
311 (tlb_type != hypervisor &&
312 (pte_val(pte) & _PAGE_SZALL_4U) == _PAGE_SZHUGE_4U)) {
313 tsb_index = MM_TSB_HUGE;
314 tsb_hash_shift = HPAGE_SHIFT;
315 }
316 }
317#endif
318
319 tsb = mm->context.tsb_block[tsb_index].tsb;
320 tsb += ((address >> tsb_hash_shift) &
321 (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
David S. Miller74ae9982006-03-05 18:26:24 -0800322 tag = (address >> 22UL);
323 tsb_insert(tsb, tag, pte_val(pte));
David S. Miller7a1ac522006-03-16 02:02:32 -0800324
325 spin_unlock_irqrestore(&mm->context.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328void flush_dcache_page(struct page *page)
329{
David S. Millera9546f52005-04-17 18:03:09 -0700330 struct address_space *mapping;
331 int this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
David S. Miller7a591cf2006-02-26 19:44:50 -0800333 if (tlb_type == hypervisor)
334 return;
335
David S. Millera9546f52005-04-17 18:03:09 -0700336 /* Do not bother with the expensive D-cache flush if it
337 * is merely the zero page. The 'bigcore' testcase in GDB
338 * causes this case to run millions of times.
339 */
340 if (page == ZERO_PAGE(0))
341 return;
342
343 this_cpu = get_cpu();
344
345 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (mapping && !mapping_mapped(mapping)) {
David S. Millera9546f52005-04-17 18:03:09 -0700347 int dirty = test_bit(PG_dcache_dirty, &page->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (dirty) {
David S. Millera9546f52005-04-17 18:03:09 -0700349 int dirty_cpu = dcache_dirty_cpu(page);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (dirty_cpu == this_cpu)
352 goto out;
353 smp_flush_dcache_page_impl(page, dirty_cpu);
354 }
355 set_dcache_dirty(page, this_cpu);
356 } else {
357 /* We could delay the flush for the !page_mapping
358 * case too. But that case is for exec env/arg
359 * pages and those are %99 certainly going to get
360 * faulted into the tlb (and thus flushed) anyways.
361 */
362 flush_dcache_page_impl(page);
363 }
364
365out:
366 put_cpu();
367}
368
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700369void __kprobes flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
David S. Millera43fe0e2006-02-04 03:10:53 -0800371 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (tlb_type == spitfire) {
373 unsigned long kaddr;
374
David S. Millera94aa252007-03-15 15:50:11 -0700375 /* This code only runs on Spitfire cpus so this is
376 * why we can assume _PAGE_PADDR_4U.
377 */
378 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
379 unsigned long paddr, mask = _PAGE_PADDR_4U;
380
381 if (kaddr >= PAGE_OFFSET)
382 paddr = kaddr & mask;
383 else {
384 pgd_t *pgdp = pgd_offset_k(kaddr);
385 pud_t *pudp = pud_offset(pgdp, kaddr);
386 pmd_t *pmdp = pmd_offset(pudp, kaddr);
387 pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
388
389 paddr = pte_val(*ptep) & mask;
390 }
391 __flush_icache_page(paddr);
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396void mmu_info(struct seq_file *m)
397{
398 if (tlb_type == cheetah)
399 seq_printf(m, "MMU Type\t: Cheetah\n");
400 else if (tlb_type == cheetah_plus)
401 seq_printf(m, "MMU Type\t: Cheetah+\n");
402 else if (tlb_type == spitfire)
403 seq_printf(m, "MMU Type\t: Spitfire\n");
David S. Millera43fe0e2006-02-04 03:10:53 -0800404 else if (tlb_type == hypervisor)
405 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 else
407 seq_printf(m, "MMU Type\t: ???\n");
408
409#ifdef CONFIG_DEBUG_DCFLUSH
410 seq_printf(m, "DCPageFlushes\t: %d\n",
411 atomic_read(&dcpage_flushes));
412#ifdef CONFIG_SMP
413 seq_printf(m, "DCPageFlushesXC\t: %d\n",
414 atomic_read(&dcpage_flushes_xcall));
415#endif /* CONFIG_SMP */
416#endif /* CONFIG_DEBUG_DCFLUSH */
417}
418
David S. Millera94aa252007-03-15 15:50:11 -0700419struct linux_prom_translation {
420 unsigned long virt;
421 unsigned long size;
422 unsigned long data;
423};
424
425/* Exported for kernel TLB miss handling in ktlb.S */
426struct linux_prom_translation prom_trans[512] __read_mostly;
427unsigned int prom_trans_ents __read_mostly;
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/* Exported for SMP bootup purposes. */
430unsigned long kern_locked_tte_data;
431
David S. Miller405599b2005-09-22 00:12:35 -0700432/* The obp translations are saved based on 8k pagesize, since obp can
433 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
David S. Miller74bf4312006-01-31 18:29:18 -0800434 * HI_OBP_ADDRESS range are handled in ktlb.S.
David S. Miller405599b2005-09-22 00:12:35 -0700435 */
David S. Miller5085b4a2005-09-22 00:45:41 -0700436static inline int in_obp_range(unsigned long vaddr)
437{
438 return (vaddr >= LOW_OBP_ADDRESS &&
439 vaddr < HI_OBP_ADDRESS);
440}
441
David S. Millerc9c10832005-10-12 12:22:46 -0700442static int cmp_ptrans(const void *a, const void *b)
David S. Miller405599b2005-09-22 00:12:35 -0700443{
David S. Millerc9c10832005-10-12 12:22:46 -0700444 const struct linux_prom_translation *x = a, *y = b;
David S. Miller405599b2005-09-22 00:12:35 -0700445
David S. Millerc9c10832005-10-12 12:22:46 -0700446 if (x->virt > y->virt)
447 return 1;
448 if (x->virt < y->virt)
449 return -1;
450 return 0;
David S. Miller405599b2005-09-22 00:12:35 -0700451}
452
David S. Millerc9c10832005-10-12 12:22:46 -0700453/* Read OBP translations property into 'prom_trans[]'. */
David S. Miller9ad98c52005-10-05 15:12:00 -0700454static void __init read_obp_translations(void)
David S. Miller405599b2005-09-22 00:12:35 -0700455{
David S. Millerc9c10832005-10-12 12:22:46 -0700456 int n, node, ents, first, last, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 node = prom_finddevice("/virtual-memory");
459 n = prom_getproplen(node, "translations");
David S. Miller405599b2005-09-22 00:12:35 -0700460 if (unlikely(n == 0 || n == -1)) {
David S. Millerb206fc42005-09-21 22:31:13 -0700461 prom_printf("prom_mappings: Couldn't get size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 prom_halt();
463 }
David S. Miller405599b2005-09-22 00:12:35 -0700464 if (unlikely(n > sizeof(prom_trans))) {
465 prom_printf("prom_mappings: Size %Zd is too big.\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 prom_halt();
467 }
David S. Miller405599b2005-09-22 00:12:35 -0700468
David S. Millerb206fc42005-09-21 22:31:13 -0700469 if ((n = prom_getproperty(node, "translations",
David S. Miller405599b2005-09-22 00:12:35 -0700470 (char *)&prom_trans[0],
471 sizeof(prom_trans))) == -1) {
David S. Millerb206fc42005-09-21 22:31:13 -0700472 prom_printf("prom_mappings: Couldn't get property.\n");
473 prom_halt();
474 }
David S. Miller9ad98c52005-10-05 15:12:00 -0700475
David S. Millerb206fc42005-09-21 22:31:13 -0700476 n = n / sizeof(struct linux_prom_translation);
David S. Miller9ad98c52005-10-05 15:12:00 -0700477
David S. Millerc9c10832005-10-12 12:22:46 -0700478 ents = n;
479
480 sort(prom_trans, ents, sizeof(struct linux_prom_translation),
481 cmp_ptrans, NULL);
482
483 /* Now kick out all the non-OBP entries. */
484 for (i = 0; i < ents; i++) {
485 if (in_obp_range(prom_trans[i].virt))
486 break;
487 }
488 first = i;
489 for (; i < ents; i++) {
490 if (!in_obp_range(prom_trans[i].virt))
491 break;
492 }
493 last = i;
494
495 for (i = 0; i < (last - first); i++) {
496 struct linux_prom_translation *src = &prom_trans[i + first];
497 struct linux_prom_translation *dest = &prom_trans[i];
498
499 *dest = *src;
500 }
501 for (; i < ents; i++) {
502 struct linux_prom_translation *dest = &prom_trans[i];
503 dest->virt = dest->size = dest->data = 0x0UL;
504 }
505
506 prom_trans_ents = last - first;
507
508 if (tlb_type == spitfire) {
509 /* Clear diag TTE bits. */
510 for (i = 0; i < prom_trans_ents; i++)
511 prom_trans[i].data &= ~0x0003fe0000000000UL;
512 }
David S. Miller405599b2005-09-22 00:12:35 -0700513}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
David S. Millerd82ace72006-02-09 02:52:44 -0800515static void __init hypervisor_tlb_lock(unsigned long vaddr,
516 unsigned long pte,
517 unsigned long mmu)
518{
David S. Miller7db35f32007-05-29 02:22:14 -0700519 unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
David S. Millerd82ace72006-02-09 02:52:44 -0800520
David S. Miller7db35f32007-05-29 02:22:14 -0700521 if (ret != 0) {
David S. Miller12e126a2006-02-17 14:40:30 -0800522 prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
David S. Miller7db35f32007-05-29 02:22:14 -0700523 "errors with %lx\n", vaddr, 0, pte, mmu, ret);
David S. Miller12e126a2006-02-17 14:40:30 -0800524 prom_halt();
525 }
David S. Millerd82ace72006-02-09 02:52:44 -0800526}
527
David S. Millerc4bce902006-02-11 21:57:54 -0800528static unsigned long kern_large_tte(unsigned long paddr);
529
David S. Miller898cf0e2005-09-23 11:59:44 -0700530static void __init remap_kernel(void)
David S. Miller405599b2005-09-22 00:12:35 -0700531{
532 unsigned long phys_page, tte_vaddr, tte_data;
David S. Miller64658742008-03-21 17:01:38 -0700533 int i, tlb_ent = sparc64_highest_locked_tlbent();
David S. Miller405599b2005-09-22 00:12:35 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 tte_vaddr = (unsigned long) KERNBASE;
David S. Millerbff06d52005-09-22 20:11:33 -0700536 phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
David S. Millerc4bce902006-02-11 21:57:54 -0800537 tte_data = kern_large_tte(phys_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 kern_locked_tte_data = tte_data;
540
David S. Millerd82ace72006-02-09 02:52:44 -0800541 /* Now lock us into the TLBs via Hypervisor or OBP. */
542 if (tlb_type == hypervisor) {
David S. Miller64658742008-03-21 17:01:38 -0700543 for (i = 0; i < num_kernel_image_mappings; i++) {
David S. Millerd82ace72006-02-09 02:52:44 -0800544 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
545 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
David S. Miller64658742008-03-21 17:01:38 -0700546 tte_vaddr += 0x400000;
547 tte_data += 0x400000;
David S. Millerd82ace72006-02-09 02:52:44 -0800548 }
549 } else {
David S. Miller64658742008-03-21 17:01:38 -0700550 for (i = 0; i < num_kernel_image_mappings; i++) {
551 prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
552 prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
553 tte_vaddr += 0x400000;
554 tte_data += 0x400000;
David S. Millerd82ace72006-02-09 02:52:44 -0800555 }
David S. Miller64658742008-03-21 17:01:38 -0700556 sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
David S. Miller0835ae02005-10-04 15:23:20 -0700558 if (tlb_type == cheetah_plus) {
559 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
560 CTX_CHEETAH_PLUS_NUC);
561 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
562 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
563 }
David S. Miller405599b2005-09-22 00:12:35 -0700564}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
David S. Miller405599b2005-09-22 00:12:35 -0700566
David S. Millerc9c10832005-10-12 12:22:46 -0700567static void __init inherit_prom_mappings(void)
David S. Miller9ad98c52005-10-05 15:12:00 -0700568{
David S. Miller405599b2005-09-22 00:12:35 -0700569 /* Now fixup OBP's idea about where we really are mapped. */
David S. Miller3c62a2d2008-02-17 23:22:50 -0800570 printk("Remapping the kernel... ");
David S. Miller405599b2005-09-22 00:12:35 -0700571 remap_kernel();
David S. Miller3c62a2d2008-02-17 23:22:50 -0800572 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575void prom_world(int enter)
576{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (!enter)
578 set_fs((mm_segment_t) { get_thread_current_ds() });
579
David S. Miller3487d1d2006-01-31 18:33:25 -0800580 __asm__ __volatile__("flushw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583void __flush_dcache_range(unsigned long start, unsigned long end)
584{
585 unsigned long va;
586
587 if (tlb_type == spitfire) {
588 int n = 0;
589
590 for (va = start; va < end; va += 32) {
591 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
592 if (++n >= 512)
593 break;
594 }
David S. Millera43fe0e2006-02-04 03:10:53 -0800595 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 start = __pa(start);
597 end = __pa(end);
598 for (va = start; va < end; va += 32)
599 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
600 "membar #Sync"
601 : /* no outputs */
602 : "r" (va),
603 "i" (ASI_DCACHE_INVALIDATE));
604 }
605}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
David S. Miller85f1e1f2007-03-15 17:51:26 -0700607/* get_new_mmu_context() uses "cache + 1". */
608DEFINE_SPINLOCK(ctx_alloc_lock);
609unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
610#define MAX_CTX_NR (1UL << CTX_NR_BITS)
611#define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
612DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614/* Caller does TLB context flushing on local CPU if necessary.
615 * The caller also ensures that CTX_VALID(mm->context) is false.
616 *
617 * We must be careful about boundary cases so that we never
618 * let the user have CTX 0 (nucleus) or we ever use a CTX
619 * version of zero (and thus NO_CONTEXT would not be caught
620 * by version mis-match tests in mmu_context.h).
David S. Millera0663a72006-02-23 14:19:28 -0800621 *
622 * Always invoked with interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
624void get_new_mmu_context(struct mm_struct *mm)
625{
626 unsigned long ctx, new_ctx;
627 unsigned long orig_pgsz_bits;
David S. Millera77754b2006-03-06 19:59:50 -0800628 unsigned long flags;
David S. Millera0663a72006-02-23 14:19:28 -0800629 int new_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
David S. Millera77754b2006-03-06 19:59:50 -0800631 spin_lock_irqsave(&ctx_alloc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
633 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
634 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
David S. Millera0663a72006-02-23 14:19:28 -0800635 new_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (new_ctx >= (1 << CTX_NR_BITS)) {
637 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
638 if (new_ctx >= ctx) {
639 int i;
640 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
641 CTX_FIRST_VERSION;
642 if (new_ctx == 1)
643 new_ctx = CTX_FIRST_VERSION;
644
645 /* Don't call memset, for 16 entries that's just
646 * plain silly...
647 */
648 mmu_context_bmap[0] = 3;
649 mmu_context_bmap[1] = 0;
650 mmu_context_bmap[2] = 0;
651 mmu_context_bmap[3] = 0;
652 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
653 mmu_context_bmap[i + 0] = 0;
654 mmu_context_bmap[i + 1] = 0;
655 mmu_context_bmap[i + 2] = 0;
656 mmu_context_bmap[i + 3] = 0;
657 }
David S. Millera0663a72006-02-23 14:19:28 -0800658 new_version = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 goto out;
660 }
661 }
662 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
663 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
664out:
665 tlb_context_cache = new_ctx;
666 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
David S. Millera77754b2006-03-06 19:59:50 -0800667 spin_unlock_irqrestore(&ctx_alloc_lock, flags);
David S. Millera0663a72006-02-23 14:19:28 -0800668
669 if (unlikely(new_version))
670 smp_new_mmu_context_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
David S. Miller919ee672008-04-23 05:40:25 -0700673static int numa_enabled = 1;
674static int numa_debug;
675
676static int __init early_numa(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
David S. Miller919ee672008-04-23 05:40:25 -0700678 if (!p)
679 return 0;
David S. Millerd1112012006-03-08 02:16:07 -0800680
David S. Miller919ee672008-04-23 05:40:25 -0700681 if (strstr(p, "off"))
682 numa_enabled = 0;
David S. Millerd1112012006-03-08 02:16:07 -0800683
David S. Miller919ee672008-04-23 05:40:25 -0700684 if (strstr(p, "debug"))
685 numa_debug = 1;
686
687 return 0;
David S. Millerd1112012006-03-08 02:16:07 -0800688}
David S. Miller919ee672008-04-23 05:40:25 -0700689early_param("numa", early_numa);
690
691#define numadbg(f, a...) \
692do { if (numa_debug) \
693 printk(KERN_INFO f, ## a); \
694} while (0)
David S. Millerd1112012006-03-08 02:16:07 -0800695
David S. Miller4e82c9a2008-02-13 18:00:03 -0800696static void __init find_ramdisk(unsigned long phys_base)
697{
698#ifdef CONFIG_BLK_DEV_INITRD
699 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
700 unsigned long ramdisk_image;
701
702 /* Older versions of the bootloader only supported a
703 * 32-bit physical address for the ramdisk image
704 * location, stored at sparc_ramdisk_image. Newer
705 * SILO versions set sparc_ramdisk_image to zero and
706 * provide a full 64-bit physical address at
707 * sparc_ramdisk_image64.
708 */
709 ramdisk_image = sparc_ramdisk_image;
710 if (!ramdisk_image)
711 ramdisk_image = sparc_ramdisk_image64;
712
713 /* Another bootloader quirk. The bootloader normalizes
714 * the physical address to KERNBASE, so we have to
715 * factor that back out and add in the lowest valid
716 * physical page address to get the true physical address.
717 */
718 ramdisk_image -= KERNBASE;
719 ramdisk_image += phys_base;
720
David S. Miller919ee672008-04-23 05:40:25 -0700721 numadbg("Found ramdisk at physical address 0x%lx, size %u\n",
722 ramdisk_image, sparc_ramdisk_size);
723
David S. Miller4e82c9a2008-02-13 18:00:03 -0800724 initrd_start = ramdisk_image;
725 initrd_end = ramdisk_image + sparc_ramdisk_size;
David S. Miller3b2a7e22008-02-13 18:13:20 -0800726
David S. Miller70479012008-05-14 23:10:33 -0700727 lmb_reserve(initrd_start, sparc_ramdisk_size);
David S. Millerd45100f2008-05-06 15:19:54 -0700728
729 initrd_start += PAGE_OFFSET;
730 initrd_end += PAGE_OFFSET;
David S. Miller4e82c9a2008-02-13 18:00:03 -0800731 }
732#endif
733}
734
David S. Miller919ee672008-04-23 05:40:25 -0700735struct node_mem_mask {
736 unsigned long mask;
737 unsigned long val;
738 unsigned long bootmem_paddr;
739};
740static struct node_mem_mask node_masks[MAX_NUMNODES];
741static int num_node_masks;
742
743int numa_cpu_lookup_table[NR_CPUS];
744cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
745
746#ifdef CONFIG_NEED_MULTIPLE_NODES
David S. Miller919ee672008-04-23 05:40:25 -0700747
748struct mdesc_mblock {
749 u64 base;
750 u64 size;
751 u64 offset; /* RA-to-PA */
752};
753static struct mdesc_mblock *mblocks;
754static int num_mblocks;
755
756static unsigned long ra_to_pa(unsigned long addr)
David S. Millerd1112012006-03-08 02:16:07 -0800757{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 int i;
759
David S. Miller919ee672008-04-23 05:40:25 -0700760 for (i = 0; i < num_mblocks; i++) {
761 struct mdesc_mblock *m = &mblocks[i];
David S. Miller6fc5bae2006-12-28 21:00:23 -0800762
David S. Miller919ee672008-04-23 05:40:25 -0700763 if (addr >= m->base &&
764 addr < (m->base + m->size)) {
765 addr += m->offset;
766 break;
767 }
768 }
769 return addr;
770}
771
772static int find_node(unsigned long addr)
773{
774 int i;
775
776 addr = ra_to_pa(addr);
777 for (i = 0; i < num_node_masks; i++) {
778 struct node_mem_mask *p = &node_masks[i];
779
780 if ((addr & p->mask) == p->val)
781 return i;
782 }
783 return -1;
784}
785
786static unsigned long nid_range(unsigned long start, unsigned long end,
787 int *nid)
788{
789 *nid = find_node(start);
790 start += PAGE_SIZE;
791 while (start < end) {
792 int n = find_node(start);
793
794 if (n != *nid)
795 break;
796 start += PAGE_SIZE;
797 }
798
799 return start;
800}
801#else
802static unsigned long nid_range(unsigned long start, unsigned long end,
803 int *nid)
804{
805 *nid = 0;
806 return end;
807}
808#endif
809
810/* This must be invoked after performing all of the necessary
811 * add_active_range() calls for 'nid'. We need to be able to get
812 * correct data from get_pfn_range_for_nid().
813 */
814static void __init allocate_node_data(int nid)
815{
816 unsigned long paddr, num_pages, start_pfn, end_pfn;
817 struct pglist_data *p;
818
819#ifdef CONFIG_NEED_MULTIPLE_NODES
820 paddr = lmb_alloc_nid(sizeof(struct pglist_data),
821 SMP_CACHE_BYTES, nid, nid_range);
822 if (!paddr) {
823 prom_printf("Cannot allocate pglist_data for nid[%d]\n", nid);
824 prom_halt();
825 }
826 NODE_DATA(nid) = __va(paddr);
827 memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
828
Johannes Weinerb61bfa32008-07-23 21:26:55 -0700829 NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
David S. Miller919ee672008-04-23 05:40:25 -0700830#endif
831
832 p = NODE_DATA(nid);
833
834 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
835 p->node_start_pfn = start_pfn;
836 p->node_spanned_pages = end_pfn - start_pfn;
837
838 if (p->node_spanned_pages) {
839 num_pages = bootmem_bootmap_pages(p->node_spanned_pages);
840
841 paddr = lmb_alloc_nid(num_pages << PAGE_SHIFT, PAGE_SIZE, nid,
842 nid_range);
843 if (!paddr) {
844 prom_printf("Cannot allocate bootmap for nid[%d]\n",
845 nid);
846 prom_halt();
847 }
848 node_masks[nid].bootmem_paddr = paddr;
849 }
850}
851
852static void init_node_masks_nonnuma(void)
853{
854 int i;
855
856 numadbg("Initializing tables for non-numa.\n");
857
858 node_masks[0].mask = node_masks[0].val = 0;
859 num_node_masks = 1;
860
861 for (i = 0; i < NR_CPUS; i++)
862 numa_cpu_lookup_table[i] = 0;
863
864 numa_cpumask_lookup_table[0] = CPU_MASK_ALL;
865}
866
867#ifdef CONFIG_NEED_MULTIPLE_NODES
868struct pglist_data *node_data[MAX_NUMNODES];
869
870EXPORT_SYMBOL(numa_cpu_lookup_table);
871EXPORT_SYMBOL(numa_cpumask_lookup_table);
872EXPORT_SYMBOL(node_data);
873
874struct mdesc_mlgroup {
875 u64 node;
876 u64 latency;
877 u64 match;
878 u64 mask;
879};
880static struct mdesc_mlgroup *mlgroups;
881static int num_mlgroups;
882
883static int scan_pio_for_cfg_handle(struct mdesc_handle *md, u64 pio,
884 u32 cfg_handle)
885{
886 u64 arc;
887
888 mdesc_for_each_arc(arc, md, pio, MDESC_ARC_TYPE_FWD) {
889 u64 target = mdesc_arc_target(md, arc);
890 const u64 *val;
891
892 val = mdesc_get_property(md, target,
893 "cfg-handle", NULL);
894 if (val && *val == cfg_handle)
895 return 0;
896 }
897 return -ENODEV;
898}
899
900static int scan_arcs_for_cfg_handle(struct mdesc_handle *md, u64 grp,
901 u32 cfg_handle)
902{
903 u64 arc, candidate, best_latency = ~(u64)0;
904
905 candidate = MDESC_NODE_NULL;
906 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
907 u64 target = mdesc_arc_target(md, arc);
908 const char *name = mdesc_node_name(md, target);
909 const u64 *val;
910
911 if (strcmp(name, "pio-latency-group"))
912 continue;
913
914 val = mdesc_get_property(md, target, "latency", NULL);
915 if (!val)
916 continue;
917
918 if (*val < best_latency) {
919 candidate = target;
920 best_latency = *val;
921 }
922 }
923
924 if (candidate == MDESC_NODE_NULL)
925 return -ENODEV;
926
927 return scan_pio_for_cfg_handle(md, candidate, cfg_handle);
928}
929
930int of_node_to_nid(struct device_node *dp)
931{
932 const struct linux_prom64_registers *regs;
933 struct mdesc_handle *md;
934 u32 cfg_handle;
935 int count, nid;
936 u64 grp;
937
938 if (!mlgroups)
939 return -1;
940
941 regs = of_get_property(dp, "reg", NULL);
942 if (!regs)
943 return -1;
944
945 cfg_handle = (regs->phys_addr >> 32UL) & 0x0fffffff;
946
947 md = mdesc_grab();
948
949 count = 0;
950 nid = -1;
951 mdesc_for_each_node_by_name(md, grp, "group") {
952 if (!scan_arcs_for_cfg_handle(md, grp, cfg_handle)) {
953 nid = count;
954 break;
955 }
956 count++;
957 }
958
959 mdesc_release(md);
960
961 return nid;
962}
963
964static void add_node_ranges(void)
965{
966 int i;
967
968 for (i = 0; i < lmb.memory.cnt; i++) {
969 unsigned long size = lmb_size_bytes(&lmb.memory, i);
970 unsigned long start, end;
971
972 start = lmb.memory.region[i].base;
973 end = start + size;
974 while (start < end) {
975 unsigned long this_end;
976 int nid;
977
978 this_end = nid_range(start, end, &nid);
979
980 numadbg("Adding active range nid[%d] "
981 "start[%lx] end[%lx]\n",
982 nid, start, this_end);
983
984 add_active_range(nid,
985 start >> PAGE_SHIFT,
986 this_end >> PAGE_SHIFT);
987
988 start = this_end;
989 }
990 }
991}
992
993static int __init grab_mlgroups(struct mdesc_handle *md)
994{
995 unsigned long paddr;
996 int count = 0;
997 u64 node;
998
999 mdesc_for_each_node_by_name(md, node, "memory-latency-group")
1000 count++;
1001 if (!count)
1002 return -ENOENT;
1003
1004 paddr = lmb_alloc(count * sizeof(struct mdesc_mlgroup),
1005 SMP_CACHE_BYTES);
1006 if (!paddr)
1007 return -ENOMEM;
1008
1009 mlgroups = __va(paddr);
1010 num_mlgroups = count;
1011
1012 count = 0;
1013 mdesc_for_each_node_by_name(md, node, "memory-latency-group") {
1014 struct mdesc_mlgroup *m = &mlgroups[count++];
1015 const u64 *val;
1016
1017 m->node = node;
1018
1019 val = mdesc_get_property(md, node, "latency", NULL);
1020 m->latency = *val;
1021 val = mdesc_get_property(md, node, "address-match", NULL);
1022 m->match = *val;
1023 val = mdesc_get_property(md, node, "address-mask", NULL);
1024 m->mask = *val;
1025
1026 numadbg("MLGROUP[%d]: node[%lx] latency[%lx] "
1027 "match[%lx] mask[%lx]\n",
1028 count - 1, m->node, m->latency, m->match, m->mask);
1029 }
1030
1031 return 0;
1032}
1033
1034static int __init grab_mblocks(struct mdesc_handle *md)
1035{
1036 unsigned long paddr;
1037 int count = 0;
1038 u64 node;
1039
1040 mdesc_for_each_node_by_name(md, node, "mblock")
1041 count++;
1042 if (!count)
1043 return -ENOENT;
1044
1045 paddr = lmb_alloc(count * sizeof(struct mdesc_mblock),
1046 SMP_CACHE_BYTES);
1047 if (!paddr)
1048 return -ENOMEM;
1049
1050 mblocks = __va(paddr);
1051 num_mblocks = count;
1052
1053 count = 0;
1054 mdesc_for_each_node_by_name(md, node, "mblock") {
1055 struct mdesc_mblock *m = &mblocks[count++];
1056 const u64 *val;
1057
1058 val = mdesc_get_property(md, node, "base", NULL);
1059 m->base = *val;
1060 val = mdesc_get_property(md, node, "size", NULL);
1061 m->size = *val;
1062 val = mdesc_get_property(md, node,
1063 "address-congruence-offset", NULL);
1064 m->offset = *val;
1065
1066 numadbg("MBLOCK[%d]: base[%lx] size[%lx] offset[%lx]\n",
1067 count - 1, m->base, m->size, m->offset);
1068 }
1069
1070 return 0;
1071}
1072
1073static void __init numa_parse_mdesc_group_cpus(struct mdesc_handle *md,
1074 u64 grp, cpumask_t *mask)
1075{
1076 u64 arc;
1077
1078 cpus_clear(*mask);
1079
1080 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_BACK) {
1081 u64 target = mdesc_arc_target(md, arc);
1082 const char *name = mdesc_node_name(md, target);
1083 const u64 *id;
1084
1085 if (strcmp(name, "cpu"))
1086 continue;
1087 id = mdesc_get_property(md, target, "id", NULL);
1088 if (*id < NR_CPUS)
1089 cpu_set(*id, *mask);
1090 }
1091}
1092
1093static struct mdesc_mlgroup * __init find_mlgroup(u64 node)
1094{
1095 int i;
1096
1097 for (i = 0; i < num_mlgroups; i++) {
1098 struct mdesc_mlgroup *m = &mlgroups[i];
1099 if (m->node == node)
1100 return m;
1101 }
1102 return NULL;
1103}
1104
1105static int __init numa_attach_mlgroup(struct mdesc_handle *md, u64 grp,
1106 int index)
1107{
1108 struct mdesc_mlgroup *candidate = NULL;
1109 u64 arc, best_latency = ~(u64)0;
1110 struct node_mem_mask *n;
1111
1112 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1113 u64 target = mdesc_arc_target(md, arc);
1114 struct mdesc_mlgroup *m = find_mlgroup(target);
1115 if (!m)
1116 continue;
1117 if (m->latency < best_latency) {
1118 candidate = m;
1119 best_latency = m->latency;
1120 }
1121 }
1122 if (!candidate)
1123 return -ENOENT;
1124
1125 if (num_node_masks != index) {
1126 printk(KERN_ERR "Inconsistent NUMA state, "
1127 "index[%d] != num_node_masks[%d]\n",
1128 index, num_node_masks);
1129 return -EINVAL;
1130 }
1131
1132 n = &node_masks[num_node_masks++];
1133
1134 n->mask = candidate->mask;
1135 n->val = candidate->match;
1136
1137 numadbg("NUMA NODE[%d]: mask[%lx] val[%lx] (latency[%lx])\n",
1138 index, n->mask, n->val, candidate->latency);
1139
1140 return 0;
1141}
1142
1143static int __init numa_parse_mdesc_group(struct mdesc_handle *md, u64 grp,
1144 int index)
1145{
1146 cpumask_t mask;
1147 int cpu;
1148
1149 numa_parse_mdesc_group_cpus(md, grp, &mask);
1150
1151 for_each_cpu_mask(cpu, mask)
1152 numa_cpu_lookup_table[cpu] = index;
1153 numa_cpumask_lookup_table[index] = mask;
1154
1155 if (numa_debug) {
1156 printk(KERN_INFO "NUMA GROUP[%d]: cpus [ ", index);
1157 for_each_cpu_mask(cpu, mask)
1158 printk("%d ", cpu);
1159 printk("]\n");
1160 }
1161
1162 return numa_attach_mlgroup(md, grp, index);
1163}
1164
1165static int __init numa_parse_mdesc(void)
1166{
1167 struct mdesc_handle *md = mdesc_grab();
1168 int i, err, count;
1169 u64 node;
1170
1171 node = mdesc_node_by_name(md, MDESC_NODE_NULL, "latency-groups");
1172 if (node == MDESC_NODE_NULL) {
1173 mdesc_release(md);
1174 return -ENOENT;
1175 }
1176
1177 err = grab_mblocks(md);
1178 if (err < 0)
1179 goto out;
1180
1181 err = grab_mlgroups(md);
1182 if (err < 0)
1183 goto out;
1184
1185 count = 0;
1186 mdesc_for_each_node_by_name(md, node, "group") {
1187 err = numa_parse_mdesc_group(md, node, count);
1188 if (err < 0)
1189 break;
1190 count++;
1191 }
1192
1193 add_node_ranges();
1194
1195 for (i = 0; i < num_node_masks; i++) {
1196 allocate_node_data(i);
1197 node_set_online(i);
1198 }
1199
1200 err = 0;
1201out:
1202 mdesc_release(md);
1203 return err;
1204}
1205
1206static int __init numa_parse_sun4u(void)
1207{
1208 return -1;
1209}
1210
1211static int __init bootmem_init_numa(void)
1212{
1213 int err = -1;
1214
1215 numadbg("bootmem_init_numa()\n");
1216
1217 if (numa_enabled) {
1218 if (tlb_type == hypervisor)
1219 err = numa_parse_mdesc();
1220 else
1221 err = numa_parse_sun4u();
1222 }
1223 return err;
1224}
1225
1226#else
1227
1228static int bootmem_init_numa(void)
1229{
1230 return -1;
1231}
1232
1233#endif
1234
1235static void __init bootmem_init_nonnuma(void)
1236{
1237 unsigned long top_of_ram = lmb_end_of_DRAM();
1238 unsigned long total_ram = lmb_phys_mem_size();
1239 unsigned int i;
1240
1241 numadbg("bootmem_init_nonnuma()\n");
1242
1243 printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
1244 top_of_ram, total_ram);
1245 printk(KERN_INFO "Memory hole size: %ldMB\n",
1246 (top_of_ram - total_ram) >> 20);
1247
1248 init_node_masks_nonnuma();
1249
1250 for (i = 0; i < lmb.memory.cnt; i++) {
1251 unsigned long size = lmb_size_bytes(&lmb.memory, i);
1252 unsigned long start_pfn, end_pfn;
1253
1254 if (!size)
1255 continue;
1256
1257 start_pfn = lmb.memory.region[i].base >> PAGE_SHIFT;
1258 end_pfn = start_pfn + lmb_size_pages(&lmb.memory, i);
1259 add_active_range(0, start_pfn, end_pfn);
1260 }
1261
1262 allocate_node_data(0);
1263
1264 node_set_online(0);
1265}
1266
1267static void __init reserve_range_in_node(int nid, unsigned long start,
1268 unsigned long end)
1269{
1270 numadbg(" reserve_range_in_node(nid[%d],start[%lx],end[%lx]\n",
1271 nid, start, end);
1272 while (start < end) {
1273 unsigned long this_end;
1274 int n;
1275
1276 this_end = nid_range(start, end, &n);
1277 if (n == nid) {
1278 numadbg(" MATCH reserving range [%lx:%lx]\n",
1279 start, this_end);
1280 reserve_bootmem_node(NODE_DATA(nid), start,
1281 (this_end - start), BOOTMEM_DEFAULT);
1282 } else
1283 numadbg(" NO MATCH, advancing start to %lx\n",
1284 this_end);
1285
1286 start = this_end;
1287 }
1288}
1289
1290static void __init trim_reserved_in_node(int nid)
1291{
1292 int i;
1293
1294 numadbg(" trim_reserved_in_node(%d)\n", nid);
1295
1296 for (i = 0; i < lmb.reserved.cnt; i++) {
1297 unsigned long start = lmb.reserved.region[i].base;
1298 unsigned long size = lmb_size_bytes(&lmb.reserved, i);
1299 unsigned long end = start + size;
1300
1301 reserve_range_in_node(nid, start, end);
1302 }
1303}
1304
1305static void __init bootmem_init_one_node(int nid)
1306{
1307 struct pglist_data *p;
1308
1309 numadbg("bootmem_init_one_node(%d)\n", nid);
1310
1311 p = NODE_DATA(nid);
1312
1313 if (p->node_spanned_pages) {
1314 unsigned long paddr = node_masks[nid].bootmem_paddr;
1315 unsigned long end_pfn;
1316
1317 end_pfn = p->node_start_pfn + p->node_spanned_pages;
1318
1319 numadbg(" init_bootmem_node(%d, %lx, %lx, %lx)\n",
1320 nid, paddr >> PAGE_SHIFT, p->node_start_pfn, end_pfn);
1321
1322 init_bootmem_node(p, paddr >> PAGE_SHIFT,
1323 p->node_start_pfn, end_pfn);
1324
1325 numadbg(" free_bootmem_with_active_regions(%d, %lx)\n",
1326 nid, end_pfn);
1327 free_bootmem_with_active_regions(nid, end_pfn);
1328
1329 trim_reserved_in_node(nid);
1330
1331 numadbg(" sparse_memory_present_with_active_regions(%d)\n",
1332 nid);
1333 sparse_memory_present_with_active_regions(nid);
1334 }
1335}
1336
1337static unsigned long __init bootmem_init(unsigned long phys_base)
1338{
1339 unsigned long end_pfn;
1340 int nid;
1341
1342 end_pfn = lmb_end_of_DRAM() >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 max_pfn = max_low_pfn = end_pfn;
David S. Millerd1112012006-03-08 02:16:07 -08001344 min_low_pfn = (phys_base >> PAGE_SHIFT);
1345
David S. Miller919ee672008-04-23 05:40:25 -07001346 if (bootmem_init_numa() < 0)
1347 bootmem_init_nonnuma();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
David S. Miller919ee672008-04-23 05:40:25 -07001349 /* XXX cpu notifier XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
David S. Miller919ee672008-04-23 05:40:25 -07001351 for_each_online_node(nid)
1352 bootmem_init_one_node(nid);
David S. Millerd1112012006-03-08 02:16:07 -08001353
1354 sparse_init();
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return end_pfn;
1357}
1358
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001359static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
1360static int pall_ents __initdata;
1361
David S. Miller56425302005-09-25 16:46:57 -07001362#ifdef CONFIG_DEBUG_PAGEALLOC
Sam Ravnborg896aef42008-02-24 19:49:52 -08001363static unsigned long __ref kernel_map_range(unsigned long pstart,
1364 unsigned long pend, pgprot_t prot)
David S. Miller56425302005-09-25 16:46:57 -07001365{
1366 unsigned long vstart = PAGE_OFFSET + pstart;
1367 unsigned long vend = PAGE_OFFSET + pend;
1368 unsigned long alloc_bytes = 0UL;
1369
1370 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
David S. Miller13edad72005-09-29 17:58:26 -07001371 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
David S. Miller56425302005-09-25 16:46:57 -07001372 vstart, vend);
1373 prom_halt();
1374 }
1375
1376 while (vstart < vend) {
1377 unsigned long this_end, paddr = __pa(vstart);
1378 pgd_t *pgd = pgd_offset_k(vstart);
1379 pud_t *pud;
1380 pmd_t *pmd;
1381 pte_t *pte;
1382
1383 pud = pud_offset(pgd, vstart);
1384 if (pud_none(*pud)) {
1385 pmd_t *new;
1386
1387 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1388 alloc_bytes += PAGE_SIZE;
1389 pud_populate(&init_mm, pud, new);
1390 }
1391
1392 pmd = pmd_offset(pud, vstart);
1393 if (!pmd_present(*pmd)) {
1394 pte_t *new;
1395
1396 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1397 alloc_bytes += PAGE_SIZE;
1398 pmd_populate_kernel(&init_mm, pmd, new);
1399 }
1400
1401 pte = pte_offset_kernel(pmd, vstart);
1402 this_end = (vstart + PMD_SIZE) & PMD_MASK;
1403 if (this_end > vend)
1404 this_end = vend;
1405
1406 while (vstart < this_end) {
1407 pte_val(*pte) = (paddr | pgprot_val(prot));
1408
1409 vstart += PAGE_SIZE;
1410 paddr += PAGE_SIZE;
1411 pte++;
1412 }
1413 }
1414
1415 return alloc_bytes;
1416}
1417
David S. Miller56425302005-09-25 16:46:57 -07001418extern unsigned int kvmap_linear_patch[1];
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001419#endif /* CONFIG_DEBUG_PAGEALLOC */
1420
1421static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
1422{
1423 const unsigned long shift_256MB = 28;
1424 const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
1425 const unsigned long size_256MB = (1UL << shift_256MB);
1426
1427 while (start < end) {
1428 long remains;
1429
David S. Millerf7c00332006-03-05 22:18:50 -08001430 remains = end - start;
1431 if (remains < size_256MB)
1432 break;
1433
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001434 if (start & mask_256MB) {
1435 start = (start + size_256MB) & ~mask_256MB;
1436 continue;
1437 }
1438
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001439 while (remains >= size_256MB) {
1440 unsigned long index = start >> shift_256MB;
1441
1442 __set_bit(index, kpte_linear_bitmap);
1443
1444 start += size_256MB;
1445 remains -= size_256MB;
1446 }
1447 }
1448}
David S. Miller56425302005-09-25 16:46:57 -07001449
David S. Miller8f3614532007-12-13 06:13:38 -08001450static void __init init_kpte_bitmap(void)
David S. Miller56425302005-09-25 16:46:57 -07001451{
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001452 unsigned long i;
David S. Miller13edad72005-09-29 17:58:26 -07001453
1454 for (i = 0; i < pall_ents; i++) {
David S. Miller56425302005-09-25 16:46:57 -07001455 unsigned long phys_start, phys_end;
1456
David S. Miller13edad72005-09-29 17:58:26 -07001457 phys_start = pall[i].phys_addr;
1458 phys_end = phys_start + pall[i].reg_size;
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001459
1460 mark_kpte_bitmap(phys_start, phys_end);
David S. Miller8f3614532007-12-13 06:13:38 -08001461 }
1462}
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001463
David S. Miller8f3614532007-12-13 06:13:38 -08001464static void __init kernel_physical_mapping_init(void)
1465{
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001466#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller8f3614532007-12-13 06:13:38 -08001467 unsigned long i, mem_alloced = 0UL;
1468
1469 for (i = 0; i < pall_ents; i++) {
1470 unsigned long phys_start, phys_end;
1471
1472 phys_start = pall[i].phys_addr;
1473 phys_end = phys_start + pall[i].reg_size;
1474
David S. Miller56425302005-09-25 16:46:57 -07001475 mem_alloced += kernel_map_range(phys_start, phys_end,
1476 PAGE_KERNEL);
David S. Miller56425302005-09-25 16:46:57 -07001477 }
1478
1479 printk("Allocated %ld bytes for kernel page tables.\n",
1480 mem_alloced);
1481
1482 kvmap_linear_patch[0] = 0x01000000; /* nop */
1483 flushi(&kvmap_linear_patch[0]);
1484
1485 __flush_tlb_all();
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001486#endif
David S. Miller56425302005-09-25 16:46:57 -07001487}
1488
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001489#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller56425302005-09-25 16:46:57 -07001490void kernel_map_pages(struct page *page, int numpages, int enable)
1491{
1492 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1493 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1494
1495 kernel_map_range(phys_start, phys_end,
1496 (enable ? PAGE_KERNEL : __pgprot(0)));
1497
David S. Miller74bf4312006-01-31 18:29:18 -08001498 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1499 PAGE_OFFSET + phys_end);
1500
David S. Miller56425302005-09-25 16:46:57 -07001501 /* we should perform an IPI and flush all tlbs,
1502 * but that can deadlock->flush only current cpu.
1503 */
1504 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1505 PAGE_OFFSET + phys_end);
1506}
1507#endif
1508
David S. Miller10147572005-09-28 21:46:43 -07001509unsigned long __init find_ecache_flush_span(unsigned long size)
1510{
David S. Miller13edad72005-09-29 17:58:26 -07001511 int i;
David S. Miller10147572005-09-28 21:46:43 -07001512
David S. Miller13edad72005-09-29 17:58:26 -07001513 for (i = 0; i < pavail_ents; i++) {
1514 if (pavail[i].reg_size >= size)
1515 return pavail[i].phys_addr;
David S. Miller10147572005-09-28 21:46:43 -07001516 }
1517
1518 return ~0UL;
1519}
1520
David S. Miller517af332006-02-01 15:55:21 -08001521static void __init tsb_phys_patch(void)
1522{
David S. Millerd257d5d2006-02-06 23:44:37 -08001523 struct tsb_ldquad_phys_patch_entry *pquad;
David S. Miller517af332006-02-01 15:55:21 -08001524 struct tsb_phys_patch_entry *p;
1525
David S. Millerd257d5d2006-02-06 23:44:37 -08001526 pquad = &__tsb_ldquad_phys_patch;
1527 while (pquad < &__tsb_ldquad_phys_patch_end) {
1528 unsigned long addr = pquad->addr;
1529
1530 if (tlb_type == hypervisor)
1531 *(unsigned int *) addr = pquad->sun4v_insn;
1532 else
1533 *(unsigned int *) addr = pquad->sun4u_insn;
1534 wmb();
1535 __asm__ __volatile__("flush %0"
1536 : /* no outputs */
1537 : "r" (addr));
1538
1539 pquad++;
1540 }
1541
David S. Miller517af332006-02-01 15:55:21 -08001542 p = &__tsb_phys_patch;
1543 while (p < &__tsb_phys_patch_end) {
1544 unsigned long addr = p->addr;
1545
1546 *(unsigned int *) addr = p->insn;
1547 wmb();
1548 __asm__ __volatile__("flush %0"
1549 : /* no outputs */
1550 : "r" (addr));
1551
1552 p++;
1553 }
1554}
1555
David S. Miller490384e2006-02-11 14:41:18 -08001556/* Don't mark as init, we give this to the Hypervisor. */
David S. Millerd1acb422007-03-16 17:20:28 -07001557#ifndef CONFIG_DEBUG_PAGEALLOC
1558#define NUM_KTSB_DESCR 2
1559#else
1560#define NUM_KTSB_DESCR 1
1561#endif
1562static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
David S. Miller490384e2006-02-11 14:41:18 -08001563extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
1564
1565static void __init sun4v_ktsb_init(void)
1566{
1567 unsigned long ktsb_pa;
1568
David S. Millerd7744a02006-02-21 22:31:11 -08001569 /* First KTSB for PAGE_SIZE mappings. */
David S. Miller490384e2006-02-11 14:41:18 -08001570 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
1571
1572 switch (PAGE_SIZE) {
1573 case 8 * 1024:
1574 default:
1575 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
1576 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
1577 break;
1578
1579 case 64 * 1024:
1580 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
1581 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
1582 break;
1583
1584 case 512 * 1024:
1585 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
1586 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
1587 break;
1588
1589 case 4 * 1024 * 1024:
1590 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
1591 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
1592 break;
1593 };
1594
David S. Miller3f19a842006-02-17 12:03:20 -08001595 ktsb_descr[0].assoc = 1;
David S. Miller490384e2006-02-11 14:41:18 -08001596 ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
1597 ktsb_descr[0].ctx_idx = 0;
1598 ktsb_descr[0].tsb_base = ktsb_pa;
1599 ktsb_descr[0].resv = 0;
1600
David S. Millerd1acb422007-03-16 17:20:28 -07001601#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Millerd7744a02006-02-21 22:31:11 -08001602 /* Second KTSB for 4MB/256MB mappings. */
1603 ktsb_pa = (kern_base +
1604 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
1605
1606 ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
1607 ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
1608 HV_PGSZ_MASK_256MB);
1609 ktsb_descr[1].assoc = 1;
1610 ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
1611 ktsb_descr[1].ctx_idx = 0;
1612 ktsb_descr[1].tsb_base = ktsb_pa;
1613 ktsb_descr[1].resv = 0;
David S. Millerd1acb422007-03-16 17:20:28 -07001614#endif
David S. Miller490384e2006-02-11 14:41:18 -08001615}
1616
1617void __cpuinit sun4v_ktsb_register(void)
1618{
David S. Miller7db35f32007-05-29 02:22:14 -07001619 unsigned long pa, ret;
David S. Miller490384e2006-02-11 14:41:18 -08001620
1621 pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
1622
David S. Miller7db35f32007-05-29 02:22:14 -07001623 ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
1624 if (ret != 0) {
1625 prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
1626 "errors with %lx\n", pa, ret);
1627 prom_halt();
1628 }
David S. Miller490384e2006-02-11 14:41:18 -08001629}
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631/* paging_init() sets up the page tables */
1632
David S. Miller5cbc3072007-05-25 15:49:59 -07001633extern void central_probe(void);
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635static unsigned long last_valid_pfn;
David S. Miller56425302005-09-25 16:46:57 -07001636pgd_t swapper_pg_dir[2048];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
David S. Millerc4bce902006-02-11 21:57:54 -08001638static void sun4u_pgprot_init(void);
1639static void sun4v_pgprot_init(void);
1640
travis@sgi.com3afc6202008-01-30 23:27:58 +01001641/* Dummy function */
1642void __init setup_per_cpu_areas(void)
1643{
1644}
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646void __init paging_init(void)
1647{
David S. Miller919ee672008-04-23 05:40:25 -07001648 unsigned long end_pfn, shift, phys_base;
David S. Miller0836a0e2005-09-28 21:38:08 -07001649 unsigned long real_end, i;
1650
David S. Miller22adb352007-05-26 01:14:43 -07001651 /* These build time checkes make sure that the dcache_dirty_cpu()
1652 * page->flags usage will work.
1653 *
1654 * When a page gets marked as dcache-dirty, we store the
1655 * cpu number starting at bit 32 in the page->flags. Also,
1656 * functions like clear_dcache_dirty_cpu use the cpu mask
1657 * in 13-bit signed-immediate instruction fields.
1658 */
Christoph Lameter9223b412008-04-28 02:12:48 -07001659
1660 /*
1661 * Page flags must not reach into upper 32 bits that are used
1662 * for the cpu number
1663 */
1664 BUILD_BUG_ON(NR_PAGEFLAGS > 32);
1665
1666 /*
1667 * The bit fields placed in the high range must not reach below
1668 * the 32 bit boundary. Otherwise we cannot place the cpu field
1669 * at the 32 bit boundary.
1670 */
David S. Miller22adb352007-05-26 01:14:43 -07001671 BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
Christoph Lameter9223b412008-04-28 02:12:48 -07001672 ilog2(roundup_pow_of_two(NR_CPUS)) > 32);
1673
David S. Miller22adb352007-05-26 01:14:43 -07001674 BUILD_BUG_ON(NR_CPUS > 4096);
1675
David S. Miller481295f2006-02-07 21:51:08 -08001676 kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
1677 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
1678
David S. Miller22d6a1c2007-05-25 00:37:12 -07001679 sstate_booting();
1680
David S. Millerd7744a02006-02-21 22:31:11 -08001681 /* Invalidate both kernel TSBs. */
David S. Miller8b234272006-02-17 18:01:02 -08001682 memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
David S. Millerd1acb422007-03-16 17:20:28 -07001683#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Millerd7744a02006-02-21 22:31:11 -08001684 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
David S. Millerd1acb422007-03-16 17:20:28 -07001685#endif
David S. Miller8b234272006-02-17 18:01:02 -08001686
David S. Millerc4bce902006-02-11 21:57:54 -08001687 if (tlb_type == hypervisor)
1688 sun4v_pgprot_init();
1689 else
1690 sun4u_pgprot_init();
1691
David S. Millerd257d5d2006-02-06 23:44:37 -08001692 if (tlb_type == cheetah_plus ||
1693 tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -08001694 tsb_phys_patch();
1695
David S. Miller490384e2006-02-11 14:41:18 -08001696 if (tlb_type == hypervisor) {
David S. Millerd257d5d2006-02-06 23:44:37 -08001697 sun4v_patch_tlb_handlers();
David S. Miller490384e2006-02-11 14:41:18 -08001698 sun4v_ktsb_init();
1699 }
David S. Millerd257d5d2006-02-06 23:44:37 -08001700
David S. Miller3b2a7e22008-02-13 18:13:20 -08001701 lmb_init();
1702
David S. Millera94a1722008-05-11 21:04:48 -07001703 /* Find available physical memory...
1704 *
1705 * Read it twice in order to work around a bug in openfirmware.
1706 * The call to grab this table itself can cause openfirmware to
1707 * allocate memory, which in turn can take away some space from
1708 * the list of available memory. Reading it twice makes sure
1709 * we really do get the final value.
1710 */
1711 read_obp_translations();
1712 read_obp_memory("reg", &pall[0], &pall_ents);
1713 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller13edad72005-09-29 17:58:26 -07001714 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller0836a0e2005-09-28 21:38:08 -07001715
1716 phys_base = 0xffffffffffffffffUL;
David S. Miller3b2a7e22008-02-13 18:13:20 -08001717 for (i = 0; i < pavail_ents; i++) {
David S. Miller13edad72005-09-29 17:58:26 -07001718 phys_base = min(phys_base, pavail[i].phys_addr);
David S. Miller3b2a7e22008-02-13 18:13:20 -08001719 lmb_add(pavail[i].phys_addr, pavail[i].reg_size);
1720 }
1721
1722 lmb_reserve(kern_base, kern_size);
David S. Miller0836a0e2005-09-28 21:38:08 -07001723
David S. Miller4e82c9a2008-02-13 18:00:03 -08001724 find_ramdisk(phys_base);
1725
David S. Miller25b0c652008-02-13 18:20:14 -08001726 if (cmdline_memory_size)
1727 lmb_enforce_memory_limit(phys_base + cmdline_memory_size);
1728
David S. Miller3b2a7e22008-02-13 18:13:20 -08001729 lmb_analyze();
1730 lmb_dump_all();
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 set_bit(0, mmu_context_bmap);
1733
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001734 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 real_end = (unsigned long)_end;
David S. Miller64658742008-03-21 17:01:38 -07001737 num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << 22);
1738 printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
1739 num_kernel_image_mappings);
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001740
1741 /* Set kernel pgd to upper alias so physical page computations
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 * work.
1743 */
1744 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1745
David S. Miller56425302005-09-25 16:46:57 -07001746 memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 /* Now can init the kernel/bad page tables. */
1749 pud_set(pud_offset(&swapper_pg_dir[0], 0),
David S. Miller56425302005-09-25 16:46:57 -07001750 swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
David S. Millerc9c10832005-10-12 12:22:46 -07001752 inherit_prom_mappings();
David S. Miller5085b4a2005-09-22 00:45:41 -07001753
David S. Miller8f3614532007-12-13 06:13:38 -08001754 init_kpte_bitmap();
1755
David S. Millera8b900d2006-01-31 18:33:37 -08001756 /* Ok, we can use our TLB miss and window trap handlers safely. */
1757 setup_tba();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
David S. Millerc9c10832005-10-12 12:22:46 -07001759 __flush_tlb_all();
David S. Miller9ad98c52005-10-05 15:12:00 -07001760
David S. Miller490384e2006-02-11 14:41:18 -08001761 if (tlb_type == hypervisor)
1762 sun4v_ktsb_register();
1763
David S. Millerb9709452008-02-13 19:20:45 -08001764 /* We must setup the per-cpu areas before we pull in the
1765 * PROM and the MDESC. The code there fills in cpu and
1766 * other information into per-cpu data structures.
1767 */
1768 real_setup_per_cpu_areas();
1769
David S. Millerad072002008-02-13 19:21:51 -08001770 prom_build_devicetree();
1771
David S. Miller4a283332008-02-13 19:22:23 -08001772 if (tlb_type == hypervisor)
1773 sun4v_mdesc_init();
1774
David S. Miller4f70f7a2008-08-12 18:33:56 -07001775 /* Once the OF device tree and MDESC have been setup, we know
1776 * the list of possible cpus. Therefore we can allocate the
1777 * IRQ stacks.
1778 */
1779 for_each_possible_cpu(i) {
1780 /* XXX Use node local allocations... XXX */
1781 softirq_stack[i] = __va(lmb_alloc(THREAD_SIZE, THREAD_SIZE));
1782 hardirq_stack[i] = __va(lmb_alloc(THREAD_SIZE, THREAD_SIZE));
1783 }
1784
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001785 /* Setup bootmem... */
David S. Miller919ee672008-04-23 05:40:25 -07001786 last_valid_pfn = end_pfn = bootmem_init(phys_base);
David S. Millerd1112012006-03-08 02:16:07 -08001787
David S. Miller919ee672008-04-23 05:40:25 -07001788#ifndef CONFIG_NEED_MULTIPLE_NODES
David S. Miller17b0e192006-03-08 15:57:03 -08001789 max_mapnr = last_valid_pfn;
David S. Miller919ee672008-04-23 05:40:25 -07001790#endif
David S. Miller56425302005-09-25 16:46:57 -07001791 kernel_physical_mapping_init();
David S. Miller56425302005-09-25 16:46:57 -07001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 {
David S. Miller919ee672008-04-23 05:40:25 -07001794 unsigned long max_zone_pfns[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
David S. Miller919ee672008-04-23 05:40:25 -07001796 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
David S. Miller919ee672008-04-23 05:40:25 -07001798 max_zone_pfns[ZONE_NORMAL] = end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
David S. Miller919ee672008-04-23 05:40:25 -07001800 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802
David S. Miller3c62a2d2008-02-17 23:22:50 -08001803 printk("Booting Linux...\n");
David S. Miller5cbc3072007-05-25 15:49:59 -07001804
1805 central_probe();
1806 cpu_probe();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
David S. Miller919ee672008-04-23 05:40:25 -07001809int __init page_in_phys_avail(unsigned long paddr)
1810{
1811 int i;
1812
1813 paddr &= PAGE_MASK;
1814
1815 for (i = 0; i < pavail_ents; i++) {
1816 unsigned long start, end;
1817
1818 start = pavail[i].phys_addr;
1819 end = start + pavail[i].reg_size;
1820
1821 if (paddr >= start && paddr < end)
1822 return 1;
1823 }
1824 if (paddr >= kern_base && paddr < (kern_base + kern_size))
1825 return 1;
1826#ifdef CONFIG_BLK_DEV_INITRD
1827 if (paddr >= __pa(initrd_start) &&
1828 paddr < __pa(PAGE_ALIGN(initrd_end)))
1829 return 1;
1830#endif
1831
1832 return 0;
1833}
1834
1835static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
1836static int pavail_rescan_ents __initdata;
1837
1838/* Certain OBP calls, such as fetching "available" properties, can
1839 * claim physical memory. So, along with initializing the valid
1840 * address bitmap, what we do here is refetch the physical available
1841 * memory list again, and make sure it provides at least as much
1842 * memory as 'pavail' does.
1843 */
1844static void setup_valid_addr_bitmap_from_pavail(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 int i;
1847
David S. Miller13edad72005-09-29 17:58:26 -07001848 read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
David S. Miller13edad72005-09-29 17:58:26 -07001850 for (i = 0; i < pavail_ents; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 unsigned long old_start, old_end;
1852
David S. Miller13edad72005-09-29 17:58:26 -07001853 old_start = pavail[i].phys_addr;
David S. Miller919ee672008-04-23 05:40:25 -07001854 old_end = old_start + pavail[i].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 while (old_start < old_end) {
1856 int n;
1857
David S. Millerc2a5a462006-06-22 00:01:56 -07001858 for (n = 0; n < pavail_rescan_ents; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 unsigned long new_start, new_end;
1860
David S. Miller13edad72005-09-29 17:58:26 -07001861 new_start = pavail_rescan[n].phys_addr;
1862 new_end = new_start +
1863 pavail_rescan[n].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 if (new_start <= old_start &&
1866 new_end >= (old_start + PAGE_SIZE)) {
David S. Miller13edad72005-09-29 17:58:26 -07001867 set_bit(old_start >> 22,
1868 sparc64_valid_addr_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 goto do_next_page;
1870 }
1871 }
David S. Miller919ee672008-04-23 05:40:25 -07001872
1873 prom_printf("mem_init: Lost memory in pavail\n");
1874 prom_printf("mem_init: OLD start[%lx] size[%lx]\n",
1875 pavail[i].phys_addr,
1876 pavail[i].reg_size);
1877 prom_printf("mem_init: NEW start[%lx] size[%lx]\n",
1878 pavail_rescan[i].phys_addr,
1879 pavail_rescan[i].reg_size);
1880 prom_printf("mem_init: Cannot continue, aborting.\n");
1881 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 do_next_page:
1884 old_start += PAGE_SIZE;
1885 }
1886 }
1887}
1888
1889void __init mem_init(void)
1890{
1891 unsigned long codepages, datapages, initpages;
1892 unsigned long addr, last;
1893 int i;
1894
1895 i = last_valid_pfn >> ((22 - PAGE_SHIFT) + 6);
1896 i += 1;
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001897 sparc64_valid_addr_bitmap = (unsigned long *) alloc_bootmem(i << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (sparc64_valid_addr_bitmap == NULL) {
1899 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1900 prom_halt();
1901 }
1902 memset(sparc64_valid_addr_bitmap, 0, i << 3);
1903
1904 addr = PAGE_OFFSET + kern_base;
1905 last = PAGE_ALIGN(kern_size) + addr;
1906 while (addr < last) {
1907 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1908 addr += PAGE_SIZE;
1909 }
1910
David S. Miller919ee672008-04-23 05:40:25 -07001911 setup_valid_addr_bitmap_from_pavail();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1914
David S. Miller919ee672008-04-23 05:40:25 -07001915#ifdef CONFIG_NEED_MULTIPLE_NODES
1916 for_each_online_node(i) {
1917 if (NODE_DATA(i)->node_spanned_pages != 0) {
1918 totalram_pages +=
1919 free_all_bootmem_node(NODE_DATA(i));
1920 }
1921 }
1922#else
1923 totalram_pages = free_all_bootmem();
1924#endif
1925
David S. Millerf1cfdb52007-03-15 22:52:18 -07001926 /* We subtract one to account for the mem_map_zero page
1927 * allocated below.
1928 */
David S. Miller919ee672008-04-23 05:40:25 -07001929 totalram_pages -= 1;
1930 num_physpages = totalram_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /*
1933 * Set up the zero page, mark it reserved, so that page count
1934 * is not manipulated when freeing the page from user ptes.
1935 */
1936 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
1937 if (mem_map_zero == NULL) {
1938 prom_printf("paging_init: Cannot alloc zero page.\n");
1939 prom_halt();
1940 }
1941 SetPageReserved(mem_map_zero);
1942
1943 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
1944 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
1945 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
1946 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
1947 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
1948 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
1949
Christoph Lameter96177292007-02-10 01:43:03 -08001950 printk("Memory: %luk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 nr_free_pages() << (PAGE_SHIFT-10),
1952 codepages << (PAGE_SHIFT-10),
1953 datapages << (PAGE_SHIFT-10),
1954 initpages << (PAGE_SHIFT-10),
1955 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
1956
1957 if (tlb_type == cheetah || tlb_type == cheetah_plus)
1958 cheetah_ecache_flush_init();
1959}
1960
David S. Miller898cf0e2005-09-23 11:59:44 -07001961void free_initmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 unsigned long addr, initend;
1964
1965 /*
1966 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1967 */
1968 addr = PAGE_ALIGN((unsigned long)(__init_begin));
1969 initend = (unsigned long)(__init_end) & PAGE_MASK;
1970 for (; addr < initend; addr += PAGE_SIZE) {
1971 unsigned long page;
1972 struct page *p;
1973
1974 page = (addr +
1975 ((unsigned long) __va(kern_base)) -
1976 ((unsigned long) KERNBASE));
Randy Dunlapc9cf5522006-06-27 02:53:52 -07001977 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 p = virt_to_page(page);
1979
1980 ClearPageReserved(p);
Nick Piggin7835e982006-03-22 00:08:40 -08001981 init_page_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 __free_page(p);
1983 num_physpages++;
1984 totalram_pages++;
1985 }
1986}
1987
1988#ifdef CONFIG_BLK_DEV_INITRD
1989void free_initrd_mem(unsigned long start, unsigned long end)
1990{
1991 if (start < end)
1992 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
1993 for (; start < end; start += PAGE_SIZE) {
1994 struct page *p = virt_to_page(start);
1995
1996 ClearPageReserved(p);
Nick Piggin7835e982006-03-22 00:08:40 -08001997 init_page_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 __free_page(p);
1999 num_physpages++;
2000 totalram_pages++;
2001 }
2002}
2003#endif
David S. Millerc4bce902006-02-11 21:57:54 -08002004
David S. Millerc4bce902006-02-11 21:57:54 -08002005#define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
2006#define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
2007#define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
2008#define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
2009#define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
2010#define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
2011
2012pgprot_t PAGE_KERNEL __read_mostly;
2013EXPORT_SYMBOL(PAGE_KERNEL);
2014
2015pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
2016pgprot_t PAGE_COPY __read_mostly;
David S. Miller0f159522006-02-18 12:43:16 -08002017
2018pgprot_t PAGE_SHARED __read_mostly;
2019EXPORT_SYMBOL(PAGE_SHARED);
2020
David S. Millerc4bce902006-02-11 21:57:54 -08002021pgprot_t PAGE_EXEC __read_mostly;
2022unsigned long pg_iobits __read_mostly;
2023
2024unsigned long _PAGE_IE __read_mostly;
David S. Miller987c74f2006-06-25 01:34:43 -07002025EXPORT_SYMBOL(_PAGE_IE);
David S. Millerb2bef442006-02-23 01:55:55 -08002026
David S. Millerc4bce902006-02-11 21:57:54 -08002027unsigned long _PAGE_E __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08002028EXPORT_SYMBOL(_PAGE_E);
2029
David S. Millerc4bce902006-02-11 21:57:54 -08002030unsigned long _PAGE_CACHE __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08002031EXPORT_SYMBOL(_PAGE_CACHE);
David S. Millerc4bce902006-02-11 21:57:54 -08002032
David Miller46644c22007-10-16 01:24:16 -07002033#ifdef CONFIG_SPARSEMEM_VMEMMAP
2034
2035#define VMEMMAP_CHUNK_SHIFT 22
2036#define VMEMMAP_CHUNK (1UL << VMEMMAP_CHUNK_SHIFT)
2037#define VMEMMAP_CHUNK_MASK ~(VMEMMAP_CHUNK - 1UL)
2038#define VMEMMAP_ALIGN(x) (((x)+VMEMMAP_CHUNK-1UL)&VMEMMAP_CHUNK_MASK)
2039
2040#define VMEMMAP_SIZE ((((1UL << MAX_PHYSADDR_BITS) >> PAGE_SHIFT) * \
2041 sizeof(struct page *)) >> VMEMMAP_CHUNK_SHIFT)
2042unsigned long vmemmap_table[VMEMMAP_SIZE];
2043
2044int __meminit vmemmap_populate(struct page *start, unsigned long nr, int node)
2045{
2046 unsigned long vstart = (unsigned long) start;
2047 unsigned long vend = (unsigned long) (start + nr);
2048 unsigned long phys_start = (vstart - VMEMMAP_BASE);
2049 unsigned long phys_end = (vend - VMEMMAP_BASE);
2050 unsigned long addr = phys_start & VMEMMAP_CHUNK_MASK;
2051 unsigned long end = VMEMMAP_ALIGN(phys_end);
2052 unsigned long pte_base;
2053
2054 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2055 _PAGE_CP_4U | _PAGE_CV_4U |
2056 _PAGE_P_4U | _PAGE_W_4U);
2057 if (tlb_type == hypervisor)
2058 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
2059 _PAGE_CP_4V | _PAGE_CV_4V |
2060 _PAGE_P_4V | _PAGE_W_4V);
2061
2062 for (; addr < end; addr += VMEMMAP_CHUNK) {
2063 unsigned long *vmem_pp =
2064 vmemmap_table + (addr >> VMEMMAP_CHUNK_SHIFT);
2065 void *block;
2066
2067 if (!(*vmem_pp & _PAGE_VALID)) {
2068 block = vmemmap_alloc_block(1UL << 22, node);
2069 if (!block)
2070 return -ENOMEM;
2071
2072 *vmem_pp = pte_base | __pa(block);
2073
2074 printk(KERN_INFO "[%p-%p] page_structs=%lu "
2075 "node=%d entry=%lu/%lu\n", start, block, nr,
2076 node,
2077 addr >> VMEMMAP_CHUNK_SHIFT,
2078 VMEMMAP_SIZE >> VMEMMAP_CHUNK_SHIFT);
2079 }
2080 }
2081 return 0;
2082}
2083#endif /* CONFIG_SPARSEMEM_VMEMMAP */
2084
David S. Millerc4bce902006-02-11 21:57:54 -08002085static void prot_init_common(unsigned long page_none,
2086 unsigned long page_shared,
2087 unsigned long page_copy,
2088 unsigned long page_readonly,
2089 unsigned long page_exec_bit)
2090{
2091 PAGE_COPY = __pgprot(page_copy);
David S. Miller0f159522006-02-18 12:43:16 -08002092 PAGE_SHARED = __pgprot(page_shared);
David S. Millerc4bce902006-02-11 21:57:54 -08002093
2094 protection_map[0x0] = __pgprot(page_none);
2095 protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
2096 protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
2097 protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
2098 protection_map[0x4] = __pgprot(page_readonly);
2099 protection_map[0x5] = __pgprot(page_readonly);
2100 protection_map[0x6] = __pgprot(page_copy);
2101 protection_map[0x7] = __pgprot(page_copy);
2102 protection_map[0x8] = __pgprot(page_none);
2103 protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
2104 protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
2105 protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
2106 protection_map[0xc] = __pgprot(page_readonly);
2107 protection_map[0xd] = __pgprot(page_readonly);
2108 protection_map[0xe] = __pgprot(page_shared);
2109 protection_map[0xf] = __pgprot(page_shared);
2110}
2111
2112static void __init sun4u_pgprot_init(void)
2113{
2114 unsigned long page_none, page_shared, page_copy, page_readonly;
2115 unsigned long page_exec_bit;
2116
2117 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2118 _PAGE_CACHE_4U | _PAGE_P_4U |
2119 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2120 _PAGE_EXEC_4U);
2121 PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2122 _PAGE_CACHE_4U | _PAGE_P_4U |
2123 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2124 _PAGE_EXEC_4U | _PAGE_L_4U);
2125 PAGE_EXEC = __pgprot(_PAGE_EXEC_4U);
2126
2127 _PAGE_IE = _PAGE_IE_4U;
2128 _PAGE_E = _PAGE_E_4U;
2129 _PAGE_CACHE = _PAGE_CACHE_4U;
2130
2131 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
2132 __ACCESS_BITS_4U | _PAGE_E_4U);
2133
David S. Millerd1acb422007-03-16 17:20:28 -07002134#ifdef CONFIG_DEBUG_PAGEALLOC
2135 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4U) ^
2136 0xfffff80000000000;
2137#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002138 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
David S. Millerc4bce902006-02-11 21:57:54 -08002139 0xfffff80000000000;
David S. Millerd1acb422007-03-16 17:20:28 -07002140#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002141 kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
2142 _PAGE_P_4U | _PAGE_W_4U);
2143
2144 /* XXX Should use 256MB on Panther. XXX */
2145 kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
David S. Millerc4bce902006-02-11 21:57:54 -08002146
2147 _PAGE_SZBITS = _PAGE_SZBITS_4U;
2148 _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
2149 _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
2150 _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
2151
2152
2153 page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
2154 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2155 __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
2156 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2157 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2158 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2159 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2160
2161 page_exec_bit = _PAGE_EXEC_4U;
2162
2163 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2164 page_exec_bit);
2165}
2166
2167static void __init sun4v_pgprot_init(void)
2168{
2169 unsigned long page_none, page_shared, page_copy, page_readonly;
2170 unsigned long page_exec_bit;
2171
2172 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
2173 _PAGE_CACHE_4V | _PAGE_P_4V |
2174 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
2175 _PAGE_EXEC_4V);
2176 PAGE_KERNEL_LOCKED = PAGE_KERNEL;
2177 PAGE_EXEC = __pgprot(_PAGE_EXEC_4V);
2178
2179 _PAGE_IE = _PAGE_IE_4V;
2180 _PAGE_E = _PAGE_E_4V;
2181 _PAGE_CACHE = _PAGE_CACHE_4V;
2182
David S. Millerd1acb422007-03-16 17:20:28 -07002183#ifdef CONFIG_DEBUG_PAGEALLOC
2184 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
2185 0xfffff80000000000;
2186#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002187 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
David S. Millerc4bce902006-02-11 21:57:54 -08002188 0xfffff80000000000;
David S. Millerd1acb422007-03-16 17:20:28 -07002189#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002190 kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
2191 _PAGE_P_4V | _PAGE_W_4V);
2192
David S. Millerd1acb422007-03-16 17:20:28 -07002193#ifdef CONFIG_DEBUG_PAGEALLOC
2194 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
2195 0xfffff80000000000;
2196#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002197 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
2198 0xfffff80000000000;
David S. Millerd1acb422007-03-16 17:20:28 -07002199#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002200 kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
2201 _PAGE_P_4V | _PAGE_W_4V);
David S. Millerc4bce902006-02-11 21:57:54 -08002202
2203 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
2204 __ACCESS_BITS_4V | _PAGE_E_4V);
2205
2206 _PAGE_SZBITS = _PAGE_SZBITS_4V;
2207 _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
2208 _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
2209 _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
2210 _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
2211
2212 page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
2213 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
2214 __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
2215 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
2216 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2217 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
2218 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2219
2220 page_exec_bit = _PAGE_EXEC_4V;
2221
2222 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2223 page_exec_bit);
2224}
2225
2226unsigned long pte_sz_bits(unsigned long sz)
2227{
2228 if (tlb_type == hypervisor) {
2229 switch (sz) {
2230 case 8 * 1024:
2231 default:
2232 return _PAGE_SZ8K_4V;
2233 case 64 * 1024:
2234 return _PAGE_SZ64K_4V;
2235 case 512 * 1024:
2236 return _PAGE_SZ512K_4V;
2237 case 4 * 1024 * 1024:
2238 return _PAGE_SZ4MB_4V;
2239 };
2240 } else {
2241 switch (sz) {
2242 case 8 * 1024:
2243 default:
2244 return _PAGE_SZ8K_4U;
2245 case 64 * 1024:
2246 return _PAGE_SZ64K_4U;
2247 case 512 * 1024:
2248 return _PAGE_SZ512K_4U;
2249 case 4 * 1024 * 1024:
2250 return _PAGE_SZ4MB_4U;
2251 };
2252 }
2253}
2254
2255pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
2256{
2257 pte_t pte;
David S. Millercf627152006-02-12 21:10:07 -08002258
2259 pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
David S. Millerc4bce902006-02-11 21:57:54 -08002260 pte_val(pte) |= (((unsigned long)space) << 32);
2261 pte_val(pte) |= pte_sz_bits(page_size);
David S. Millercf627152006-02-12 21:10:07 -08002262
David S. Millerc4bce902006-02-11 21:57:54 -08002263 return pte;
2264}
2265
David S. Millerc4bce902006-02-11 21:57:54 -08002266static unsigned long kern_large_tte(unsigned long paddr)
2267{
2268 unsigned long val;
2269
2270 val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2271 _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
2272 _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
2273 if (tlb_type == hypervisor)
2274 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
2275 _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
2276 _PAGE_EXEC_4V | _PAGE_W_4V);
2277
2278 return val | paddr;
2279}
2280
David S. Millerc4bce902006-02-11 21:57:54 -08002281/* If not locked, zap it. */
2282void __flush_tlb_all(void)
2283{
2284 unsigned long pstate;
2285 int i;
2286
2287 __asm__ __volatile__("flushw\n\t"
2288 "rdpr %%pstate, %0\n\t"
2289 "wrpr %0, %1, %%pstate"
2290 : "=r" (pstate)
2291 : "i" (PSTATE_IE));
David S. Miller8f3614532007-12-13 06:13:38 -08002292 if (tlb_type == hypervisor) {
2293 sun4v_mmu_demap_all();
2294 } else if (tlb_type == spitfire) {
David S. Millerc4bce902006-02-11 21:57:54 -08002295 for (i = 0; i < 64; i++) {
2296 /* Spitfire Errata #32 workaround */
2297 /* NOTE: Always runs on spitfire, so no
2298 * cheetah+ page size encodings.
2299 */
2300 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2301 "flush %%g6"
2302 : /* No outputs */
2303 : "r" (0),
2304 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2305
2306 if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
2307 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2308 "membar #Sync"
2309 : /* no outputs */
2310 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
2311 spitfire_put_dtlb_data(i, 0x0UL);
2312 }
2313
2314 /* Spitfire Errata #32 workaround */
2315 /* NOTE: Always runs on spitfire, so no
2316 * cheetah+ page size encodings.
2317 */
2318 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2319 "flush %%g6"
2320 : /* No outputs */
2321 : "r" (0),
2322 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2323
2324 if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
2325 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2326 "membar #Sync"
2327 : /* no outputs */
2328 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
2329 spitfire_put_itlb_data(i, 0x0UL);
2330 }
2331 }
2332 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
2333 cheetah_flush_dtlb_all();
2334 cheetah_flush_itlb_all();
2335 }
2336 __asm__ __volatile__("wrpr %0, 0, %%pstate"
2337 : : "r" (pstate));
2338}