blob: f60e006d90c3520c41905b2140fd522707214921 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
2 * Modifications by Matt Porter (mporter@mvista.com) to support
3 * PPC44x Book E processors.
4 *
5 * This file contains the routines for initializing the MMU
6 * on the 4xx series of chips.
7 * -- paulus
8 *
9 * Derived from arch/ppc/mm/init.c:
10 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
11 *
12 * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
13 * and Cort Dougan (PReP) (cort@cs.nmt.edu)
14 * Copyright (C) 1996 Paul Mackerras
Paul Mackerras14cf11a2005-09-26 16:04:21 +100015 *
16 * Derived from "arch/i386/mm/init.c"
17 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
18 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version
22 * 2 of the License, or (at your option) any later version.
23 *
24 */
25
Paul Mackerras14cf11a2005-09-26 16:04:21 +100026#include <linux/init.h>
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -070027#include <linux/memblock.h>
28
Paul Mackerras14cf11a2005-09-26 16:04:21 +100029#include <asm/mmu.h>
David Gibson57d79092007-04-30 14:06:25 +100030#include <asm/system.h>
31#include <asm/page.h>
Benjamin Herrenschmidt1bc54c02008-07-08 15:54:40 +100032#include <asm/cacheflush.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100033
34#include "mmu_decl.h"
35
Paul Mackerras14cf11a2005-09-26 16:04:21 +100036/* Used by the 44x TLB replacement exception handler.
37 * Just needed it declared someplace.
38 */
David Gibson57d79092007-04-30 14:06:25 +100039unsigned int tlb_44x_index; /* = 0 */
40unsigned int tlb_44x_hwater = PPC44x_TLB_SIZE - 1 - PPC44x_EARLY_TLBS;
Benjamin Herrenschmidtb98ac052007-10-31 16:42:19 +110041int icache_44x_need_flush;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100042
Dave Kleikampe7f75ad2010-03-05 10:43:12 +000043unsigned long tlb_47x_boltmap[1024/8];
44
45static void __cpuinit ppc44x_update_tlb_hwater(void)
Benjamin Herrenschmidt1bc54c02008-07-08 15:54:40 +100046{
47 extern unsigned int tlb_44x_patch_hwater_D[];
48 extern unsigned int tlb_44x_patch_hwater_I[];
49
50 /* The TLB miss handlers hard codes the watermark in a cmpli
51 * instruction to improve performances rather than loading it
52 * from the global variable. Thus, we patch the instructions
53 * in the 2 TLB miss handlers when updating the value
54 */
55 tlb_44x_patch_hwater_D[0] = (tlb_44x_patch_hwater_D[0] & 0xffff0000) |
56 tlb_44x_hwater;
57 flush_icache_range((unsigned long)&tlb_44x_patch_hwater_D[0],
58 (unsigned long)&tlb_44x_patch_hwater_D[1]);
59 tlb_44x_patch_hwater_I[0] = (tlb_44x_patch_hwater_I[0] & 0xffff0000) |
60 tlb_44x_hwater;
61 flush_icache_range((unsigned long)&tlb_44x_patch_hwater_I[0],
62 (unsigned long)&tlb_44x_patch_hwater_I[1]);
63}
64
Paul Mackerras14cf11a2005-09-26 16:04:21 +100065/*
Dave Kleikampe7f75ad2010-03-05 10:43:12 +000066 * "Pins" a 256MB TLB entry in AS0 for kernel lowmem for 44x type MMU
Paul Mackerras14cf11a2005-09-26 16:04:21 +100067 */
David Gibson57d79092007-04-30 14:06:25 +100068static void __init ppc44x_pin_tlb(unsigned int virt, unsigned int phys)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100069{
Benjamin Herrenschmidt1bc54c02008-07-08 15:54:40 +100070 unsigned int entry = tlb_44x_hwater--;
71
72 ppc44x_update_tlb_hwater();
73
Dave Kleikampe7f75ad2010-03-05 10:43:12 +000074 mtspr(SPRN_MMUCR, 0);
75
David Gibson57d79092007-04-30 14:06:25 +100076 __asm__ __volatile__(
77 "tlbwe %2,%3,%4\n"
78 "tlbwe %1,%3,%5\n"
79 "tlbwe %0,%3,%6\n"
Paul Mackerras14cf11a2005-09-26 16:04:21 +100080 :
Dave Kleikampe7f75ad2010-03-05 10:43:12 +000081#ifdef CONFIG_PPC47x
82 : "r" (PPC47x_TLB2_S_RWX),
83#else
David Gibson57d79092007-04-30 14:06:25 +100084 : "r" (PPC44x_TLB_SW | PPC44x_TLB_SR | PPC44x_TLB_SX | PPC44x_TLB_G),
Dave Kleikampe7f75ad2010-03-05 10:43:12 +000085#endif
David Gibson57d79092007-04-30 14:06:25 +100086 "r" (phys),
87 "r" (virt | PPC44x_TLB_VALID | PPC44x_TLB_256M),
Benjamin Herrenschmidt1bc54c02008-07-08 15:54:40 +100088 "r" (entry),
Paul Mackerras14cf11a2005-09-26 16:04:21 +100089 "i" (PPC44x_TLB_PAGEID),
90 "i" (PPC44x_TLB_XLAT),
91 "i" (PPC44x_TLB_ATTRIB));
92}
93
Dave Kleikampe7f75ad2010-03-05 10:43:12 +000094static int __init ppc47x_find_free_bolted(void)
95{
96 unsigned int mmube0 = mfspr(SPRN_MMUBE0);
97 unsigned int mmube1 = mfspr(SPRN_MMUBE1);
98
99 if (!(mmube0 & MMUBE0_VBE0))
100 return 0;
101 if (!(mmube0 & MMUBE0_VBE1))
102 return 1;
103 if (!(mmube0 & MMUBE0_VBE2))
104 return 2;
105 if (!(mmube1 & MMUBE1_VBE3))
106 return 3;
107 if (!(mmube1 & MMUBE1_VBE4))
108 return 4;
109 if (!(mmube1 & MMUBE1_VBE5))
110 return 5;
111 return -1;
112}
113
114static void __init ppc47x_update_boltmap(void)
115{
116 unsigned int mmube0 = mfspr(SPRN_MMUBE0);
117 unsigned int mmube1 = mfspr(SPRN_MMUBE1);
118
119 if (mmube0 & MMUBE0_VBE0)
120 __set_bit((mmube0 >> MMUBE0_IBE0_SHIFT) & 0xff,
121 tlb_47x_boltmap);
122 if (mmube0 & MMUBE0_VBE1)
123 __set_bit((mmube0 >> MMUBE0_IBE1_SHIFT) & 0xff,
124 tlb_47x_boltmap);
125 if (mmube0 & MMUBE0_VBE2)
126 __set_bit((mmube0 >> MMUBE0_IBE2_SHIFT) & 0xff,
127 tlb_47x_boltmap);
128 if (mmube1 & MMUBE1_VBE3)
129 __set_bit((mmube1 >> MMUBE1_IBE3_SHIFT) & 0xff,
130 tlb_47x_boltmap);
131 if (mmube1 & MMUBE1_VBE4)
132 __set_bit((mmube1 >> MMUBE1_IBE4_SHIFT) & 0xff,
133 tlb_47x_boltmap);
134 if (mmube1 & MMUBE1_VBE5)
135 __set_bit((mmube1 >> MMUBE1_IBE5_SHIFT) & 0xff,
136 tlb_47x_boltmap);
137}
138
139/*
140 * "Pins" a 256MB TLB entry in AS0 for kernel lowmem for 47x type MMU
141 */
142static void __cpuinit ppc47x_pin_tlb(unsigned int virt, unsigned int phys)
143{
144 unsigned int rA;
145 int bolted;
146
147 /* Base rA is HW way select, way 0, bolted bit set */
148 rA = 0x88000000;
149
150 /* Look for a bolted entry slot */
151 bolted = ppc47x_find_free_bolted();
152 BUG_ON(bolted < 0);
153
154 /* Insert bolted slot number */
155 rA |= bolted << 24;
156
157 pr_debug("256M TLB entry for 0x%08x->0x%08x in bolt slot %d\n",
158 virt, phys, bolted);
159
160 mtspr(SPRN_MMUCR, 0);
161
162 __asm__ __volatile__(
163 "tlbwe %2,%3,0\n"
164 "tlbwe %1,%3,1\n"
165 "tlbwe %0,%3,2\n"
166 :
167 : "r" (PPC47x_TLB2_SW | PPC47x_TLB2_SR |
168 PPC47x_TLB2_SX
169#ifdef CONFIG_SMP
170 | PPC47x_TLB2_M
171#endif
172 ),
173 "r" (phys),
174 "r" (virt | PPC47x_TLB0_VALID | PPC47x_TLB0_256M),
175 "r" (rA));
176}
177
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000178void __init MMU_init_hw(void)
179{
Dave Kleikampe7f75ad2010-03-05 10:43:12 +0000180 /* This is not useful on 47x but won't hurt either */
Benjamin Herrenschmidt1bc54c02008-07-08 15:54:40 +1000181 ppc44x_update_tlb_hwater();
182
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000183 flush_instruction_cache();
184}
185
Stephen Rothwellae4cec42009-12-14 09:04:24 -0700186unsigned long __init mmu_mapin_ram(unsigned long top)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000187{
David Gibson57d79092007-04-30 14:06:25 +1000188 unsigned long addr;
Dave Kleikamp96615342011-07-04 18:36:57 +0000189 unsigned long memstart = memstart_addr & ~(PPC_PIN_SIZE - 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000190
David Gibson57d79092007-04-30 14:06:25 +1000191 /* Pin in enough TLBs to cover any lowmem not covered by the
192 * initial 256M mapping established in head_44x.S */
Dave Kleikamp96615342011-07-04 18:36:57 +0000193 for (addr = memstart + PPC_PIN_SIZE; addr < lowmem_end_addr;
Dave Kleikampe7f75ad2010-03-05 10:43:12 +0000194 addr += PPC_PIN_SIZE) {
195 if (mmu_has_feature(MMU_FTR_TYPE_47x))
196 ppc47x_pin_tlb(addr + PAGE_OFFSET, addr);
197 else
198 ppc44x_pin_tlb(addr + PAGE_OFFSET, addr);
199 }
200 if (mmu_has_feature(MMU_FTR_TYPE_47x)) {
201 ppc47x_update_boltmap();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000202
Dave Kleikampe7f75ad2010-03-05 10:43:12 +0000203#ifdef DEBUG
204 {
205 int i;
206
207 printk(KERN_DEBUG "bolted entries: ");
208 for (i = 0; i < 255; i++) {
209 if (test_bit(i, tlb_47x_boltmap))
210 printk("%d ", i);
211 }
212 printk("\n");
213 }
214#endif /* DEBUG */
215 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000216 return total_lowmem;
217}
Dave Kleikampe7f75ad2010-03-05 10:43:12 +0000218
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700219void setup_initial_memory_limit(phys_addr_t first_memblock_base,
220 phys_addr_t first_memblock_size)
221{
Dave Kleikamp96615342011-07-04 18:36:57 +0000222 u64 size;
223
224#ifndef CONFIG_RELOCATABLE
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700225 /* We don't currently support the first MEMBLOCK not mapping 0
226 * physical on those processors
227 */
228 BUG_ON(first_memblock_base != 0);
Dave Kleikamp96615342011-07-04 18:36:57 +0000229#endif
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700230
231 /* 44x has a 256M TLB entry pinned at boot */
Dave Kleikamp96615342011-07-04 18:36:57 +0000232 size = (min_t(u64, first_memblock_size, PPC_PIN_SIZE));
233 memblock_set_current_limit(first_memblock_base + size);
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700234}
235
Dave Kleikampe7f75ad2010-03-05 10:43:12 +0000236#ifdef CONFIG_SMP
237void __cpuinit mmu_init_secondary(int cpu)
238{
239 unsigned long addr;
Dave Kleikamp96615342011-07-04 18:36:57 +0000240 unsigned long memstart = memstart_addr & ~(PPC_PIN_SIZE - 1);
Dave Kleikampe7f75ad2010-03-05 10:43:12 +0000241
242 /* Pin in enough TLBs to cover any lowmem not covered by the
243 * initial 256M mapping established in head_44x.S
244 *
245 * WARNING: This is called with only the first 256M of the
246 * linear mapping in the TLB and we can't take faults yet
247 * so beware of what this code uses. It runs off a temporary
248 * stack. current (r2) isn't initialized, smp_processor_id()
249 * will not work, current thread info isn't accessible, ...
250 */
Dave Kleikamp96615342011-07-04 18:36:57 +0000251 for (addr = memstart + PPC_PIN_SIZE; addr < lowmem_end_addr;
Dave Kleikampe7f75ad2010-03-05 10:43:12 +0000252 addr += PPC_PIN_SIZE) {
253 if (mmu_has_feature(MMU_FTR_TYPE_47x))
254 ppc47x_pin_tlb(addr + PAGE_OFFSET, addr);
255 else
256 ppc44x_pin_tlb(addr + PAGE_OFFSET, addr);
257 }
258}
259#endif /* CONFIG_SMP */