blob: ab3b10746f36410cfe7dbcc546fe9c6428da6b8c [file] [log] [blame]
Christophe Leroya372acf2016-02-09 17:07:50 +01001/*
2 * This file contains the routines for initializing the MMU
3 * on the 8xx series of chips.
4 * -- christophe
5 *
6 * Derived from arch/powerpc/mm/40x_mmu.c:
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 */
14
15#include <linux/memblock.h>
Christophe Leroy4badd432016-05-17 09:02:45 +020016#include <asm/fixmap.h>
17#include <asm/code-patching.h>
Christophe Leroya372acf2016-02-09 17:07:50 +010018
19#include "mmu_decl.h"
20
Christophe Leroy4badd432016-05-17 09:02:45 +020021#define IMMR_SIZE (FIX_IMMR_SIZE << PAGE_SHIFT)
22
Christophe Leroya372acf2016-02-09 17:07:50 +010023extern int __map_without_ltlbs;
Christophe Leroy4badd432016-05-17 09:02:45 +020024
Christophe Leroyeef784b2017-07-12 12:08:45 +020025static unsigned long block_mapped_ram;
26
Christophe Leroy4badd432016-05-17 09:02:45 +020027/*
Christophe Leroyeef784b2017-07-12 12:08:45 +020028 * Return PA for this VA if it is in an area mapped with LTLBs.
29 * Otherwise, returns 0
Christophe Leroy4badd432016-05-17 09:02:45 +020030 */
31phys_addr_t v_block_mapped(unsigned long va)
32{
33 unsigned long p = PHYS_IMMR_BASE;
34
35 if (__map_without_ltlbs)
36 return 0;
37 if (va >= VIRT_IMMR_BASE && va < VIRT_IMMR_BASE + IMMR_SIZE)
38 return p + va - VIRT_IMMR_BASE;
Christophe Leroyeef784b2017-07-12 12:08:45 +020039 if (va >= PAGE_OFFSET && va < PAGE_OFFSET + block_mapped_ram)
40 return __pa(va);
Christophe Leroy4badd432016-05-17 09:02:45 +020041 return 0;
42}
43
44/*
Christophe Leroyeef784b2017-07-12 12:08:45 +020045 * Return VA for a given PA mapped with LTLBs or 0 if not mapped
Christophe Leroy4badd432016-05-17 09:02:45 +020046 */
47unsigned long p_block_mapped(phys_addr_t pa)
48{
49 unsigned long p = PHYS_IMMR_BASE;
50
51 if (__map_without_ltlbs)
52 return 0;
53 if (pa >= p && pa < p + IMMR_SIZE)
54 return VIRT_IMMR_BASE + pa - p;
Christophe Leroyeef784b2017-07-12 12:08:45 +020055 if (pa < block_mapped_ram)
56 return (unsigned long)__va(pa);
Christophe Leroy4badd432016-05-17 09:02:45 +020057 return 0;
58}
59
Christophe Leroy4ad27452016-05-17 09:02:54 +020060#define LARGE_PAGE_SIZE_8M (1<<23)
61
Christophe Leroya372acf2016-02-09 17:07:50 +010062/*
63 * MMU_init_hw does the chip-specific initialization of the MMU hardware.
64 */
65void __init MMU_init_hw(void)
66{
Christophe Leroy4ad27452016-05-17 09:02:54 +020067 /* PIN up to the 3 first 8Mb after IMMR in DTLB table */
Christophe Leroya3059b02017-07-12 12:08:51 +020068#ifdef CONFIG_PIN_TLB_DATA
Christophe Leroy4ad27452016-05-17 09:02:54 +020069 unsigned long ctr = mfspr(SPRN_MD_CTR) & 0xfe000000;
70 unsigned long flags = 0xf0 | MD_SPS16K | _PAGE_SHARED | _PAGE_DIRTY;
Christophe Leroy62f64b42016-05-17 09:02:56 +020071#ifdef CONFIG_PIN_TLB_IMMR
72 int i = 29;
73#else
74 int i = 28;
75#endif
Christophe Leroy4ad27452016-05-17 09:02:54 +020076 unsigned long addr = 0;
77 unsigned long mem = total_lowmem;
Christophe Leroya372acf2016-02-09 17:07:50 +010078
Christophe Leroy62f64b42016-05-17 09:02:56 +020079 for (; i < 32 && mem >= LARGE_PAGE_SIZE_8M; i++) {
Christophe Leroy4ad27452016-05-17 09:02:54 +020080 mtspr(SPRN_MD_CTR, ctr | (i << 8));
81 mtspr(SPRN_MD_EPN, (unsigned long)__va(addr) | MD_EVALID);
82 mtspr(SPRN_MD_TWC, MD_PS8MEG | MD_SVALID);
83 mtspr(SPRN_MD_RPN, addr | flags | _PAGE_PRESENT);
84 addr += LARGE_PAGE_SIZE_8M;
85 mem -= LARGE_PAGE_SIZE_8M;
86 }
87#endif
88}
Christophe Leroya372acf2016-02-09 17:07:50 +010089
Christophe Leroy4badd432016-05-17 09:02:45 +020090static void mmu_mapin_immr(void)
91{
92 unsigned long p = PHYS_IMMR_BASE;
93 unsigned long v = VIRT_IMMR_BASE;
94 unsigned long f = pgprot_val(PAGE_KERNEL_NCG);
95 int offset;
96
97 for (offset = 0; offset < IMMR_SIZE; offset += PAGE_SIZE)
Christophe Leroy4386c092017-05-29 17:31:56 +020098 map_kernel_page(v + offset, p + offset, f);
Christophe Leroy4badd432016-05-17 09:02:45 +020099}
100
101/* Address of instructions to patch */
Christophe Leroy62f64b42016-05-17 09:02:56 +0200102#ifndef CONFIG_PIN_TLB_IMMR
Christophe Leroy4badd432016-05-17 09:02:45 +0200103extern unsigned int DTLBMiss_jmp;
104#endif
Christophe Leroybb7f3802016-05-17 09:02:51 +0200105extern unsigned int DTLBMiss_cmp, FixupDAR_cmp;
Christophe Leroya3059b02017-07-12 12:08:51 +0200106#ifndef CONFIG_PIN_TLB_TEXT
107extern unsigned int ITLBMiss_cmp;
108#endif
Christophe Leroybb7f3802016-05-17 09:02:51 +0200109
110void mmu_patch_cmp_limit(unsigned int *addr, unsigned long mapped)
111{
112 unsigned int instr = *addr;
113
114 instr &= 0xffff0000;
115 instr |= (unsigned long)__va(mapped) >> 16;
116 patch_instruction(addr, instr);
117}
Christophe Leroy4badd432016-05-17 09:02:45 +0200118
Christophe Leroya372acf2016-02-09 17:07:50 +0100119unsigned long __init mmu_mapin_ram(unsigned long top)
120{
Christophe Leroybb7f3802016-05-17 09:02:51 +0200121 unsigned long mapped;
Christophe Leroya372acf2016-02-09 17:07:50 +0100122
Christophe Leroy4badd432016-05-17 09:02:45 +0200123 if (__map_without_ltlbs) {
Christophe Leroybb7f3802016-05-17 09:02:51 +0200124 mapped = 0;
Christophe Leroy4badd432016-05-17 09:02:45 +0200125 mmu_mapin_immr();
Christophe Leroy62f64b42016-05-17 09:02:56 +0200126#ifndef CONFIG_PIN_TLB_IMMR
Christophe Leroy4badd432016-05-17 09:02:45 +0200127 patch_instruction(&DTLBMiss_jmp, PPC_INST_NOP);
128#endif
Christophe Leroya3059b02017-07-12 12:08:51 +0200129#ifndef CONFIG_PIN_TLB_TEXT
130 mmu_patch_cmp_limit(&ITLBMiss_cmp, 0);
131#endif
Christophe Leroybb7f3802016-05-17 09:02:51 +0200132 } else {
133 mapped = top & ~(LARGE_PAGE_SIZE_8M - 1);
Christophe Leroy4badd432016-05-17 09:02:45 +0200134 }
Christophe Leroya372acf2016-02-09 17:07:50 +0100135
Christophe Leroybb7f3802016-05-17 09:02:51 +0200136 mmu_patch_cmp_limit(&DTLBMiss_cmp, mapped);
137 mmu_patch_cmp_limit(&FixupDAR_cmp, mapped);
Christophe Leroya372acf2016-02-09 17:07:50 +0100138
139 /* If the size of RAM is not an exact power of two, we may not
140 * have covered RAM in its entirety with 8 MiB
141 * pages. Consequently, restrict the top end of RAM currently
142 * allocable so that calls to the MEMBLOCK to allocate PTEs for "tail"
143 * coverage with normal-sized pages (or other reasons) do not
144 * attempt to allocate outside the allowed range.
145 */
Christophe Leroybb7f3802016-05-17 09:02:51 +0200146 if (mapped)
147 memblock_set_current_limit(mapped);
Christophe Leroya372acf2016-02-09 17:07:50 +0100148
Christophe Leroyeef784b2017-07-12 12:08:45 +0200149 block_mapped_ram = mapped;
150
Christophe Leroya372acf2016-02-09 17:07:50 +0100151 return mapped;
152}
Christophe Leroy516d9182016-02-09 17:07:54 +0100153
154void setup_initial_memory_limit(phys_addr_t first_memblock_base,
155 phys_addr_t first_memblock_size)
156{
157 /* We don't currently support the first MEMBLOCK not mapping 0
158 * physical on those processors
159 */
160 BUG_ON(first_memblock_base != 0);
161
Christophe Leroy516d9182016-02-09 17:07:54 +0100162 /* 8xx can only access 24MB at the moment */
163 memblock_set_current_limit(min_t(u64, first_memblock_size, 0x01800000));
Christophe Leroy516d9182016-02-09 17:07:54 +0100164}
Christophe Leroya7761fe2016-02-09 17:08:18 +0100165
166/*
167 * Set up to use a given MMU context.
168 * id is context number, pgd is PGD pointer.
169 *
170 * We place the physical address of the new task page directory loaded
171 * into the MMU base register, and set the ASID compare register with
172 * the new "context."
173 */
174void set_context(unsigned long id, pgd_t *pgd)
175{
176 s16 offset = (s16)(__pa(swapper_pg_dir));
177
178#ifdef CONFIG_BDI_SWITCH
179 pgd_t **ptr = *(pgd_t ***)(KERNELBASE + 0xf0);
180
181 /* Context switch the PTE pointer for the Abatron BDI2000.
182 * The PGDIR is passed as second argument.
183 */
184 *(ptr + 1) = pgd;
185#endif
186
187 /* Register M_TW will contain base address of level 1 table minus the
188 * lower part of the kernel PGDIR base address, so that all accesses to
189 * level 1 table are done relative to lower part of kernel PGDIR base
190 * address.
191 */
192 mtspr(SPRN_M_TW, __pa(pgd) - offset);
193
194 /* Update context */
195 mtspr(SPRN_M_CASID, id);
196 /* sync */
197 mb();
198}
Christophe Leroy766d45c2016-02-09 17:08:21 +0100199
200void flush_instruction_cache(void)
201{
202 isync();
203 mtspr(SPRN_IC_CST, IDC_INVALL);
204 isync();
205}