1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2008-2011 Freescale Semiconductor, Inc.
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 #include <asm/processor.h>
12 #ifdef CONFIG_ADDR_MAP
16 #include <linux/log2.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 void invalidate_tlb(u8 tlb)
28 __weak void init_tlbs(void)
32 for (i = 0; i < num_tlb_entries; i++) {
33 write_tlb(tlb_table[i].mas0,
43 #if !defined(CONFIG_NAND_SPL) && \
44 (!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SPL_INIT_MINIMAL))
45 void read_tlbcam_entry(int idx, u32 *valid, u32 *tsize, unsigned long *epn,
50 mtspr(MAS0, FSL_BOOKE_MAS0(1, idx, 0));
51 asm volatile("tlbre;isync");
54 *valid = (_mas1 & MAS1_VALID);
55 *tsize = (_mas1 >> 7) & 0x1f;
56 *epn = mfspr(MAS2) & MAS2_EPN;
57 *rpn = mfspr(MAS3) & MAS3_RPN;
58 #ifdef CONFIG_ENABLE_36BIT_PHYS
59 *rpn |= ((u64)mfspr(MAS7)) << 32;
63 void print_tlbcam(void)
66 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
68 /* walk all the entries */
69 printf("TLBCAM entries\n");
70 for (i = 0; i < num_cam; i++) {
75 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
76 printf("entry %02d: V: %d EPN 0x%08x RPN 0x%08llx size:",
77 i, (valid == 0) ? 0 : 1, (unsigned int)epn,
78 (unsigned long long)rpn);
79 print_size(TSIZE_TO_BYTES(tsize), "\n");
83 static inline void use_tlb_cam(u8 idx)
88 gd->arch.used_tlb_cams[i] |= (1 << bit);
91 static inline void free_tlb_cam(u8 idx)
96 gd->arch.used_tlb_cams[i] &= ~(1 << bit);
99 void init_used_tlb_cams(void)
102 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
104 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++)
105 gd->arch.used_tlb_cams[i] = 0;
107 /* walk all the entries */
108 for (i = 0; i < num_cam; i++) {
109 mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
110 asm volatile("tlbre;isync");
111 if (mfspr(MAS1) & MAS1_VALID)
116 int find_free_tlbcam(void)
121 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) {
122 idx = ffz(gd->arch.used_tlb_cams[i]);
130 if (idx >= CONFIG_SYS_NUM_TLBCAMS)
136 void set_tlb(u8 tlb, u32 epn, u64 rpn,
138 u8 ts, u8 esel, u8 tsize, u8 iprot)
140 u32 _mas0, _mas1, _mas2, _mas3, _mas7;
145 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1 &&
147 printf("%s: bad tsize %d on entry %d at 0x%08x\n",
148 __func__, tsize, tlb, epn);
152 _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0);
153 _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize);
154 _mas2 = FSL_BOOKE_MAS2(epn, wimge);
155 _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms);
156 _mas7 = FSL_BOOKE_MAS7(rpn);
158 write_tlb(_mas0, _mas1, _mas2, _mas3, _mas7);
160 #ifdef CONFIG_ADDR_MAP
161 if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
162 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), esel);
166 void disable_tlb(u8 esel)
168 u32 _mas0, _mas1, _mas2, _mas3;
172 _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
181 #ifdef CONFIG_ENABLE_36BIT_PHYS
184 asm volatile("isync;msync;tlbwe;isync");
186 #ifdef CONFIG_ADDR_MAP
187 if (gd->flags & GD_FLG_RELOC)
188 addrmap_set_entry(0, 0, 0, esel);
192 static void tlbsx (const volatile unsigned *addr)
194 __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr));
197 /* return -1 if we didn't find anything */
198 int find_tlb_idx(void *addr, u8 tlbsel)
202 /* zero out Search PID, AS */
210 /* we found something, and its in the TLB we expect */
211 if ((MAS1_VALID & _mas1) &&
212 (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) {
213 return ((_mas0 & MAS0_ESEL_MSK) >> 16);
219 #ifdef CONFIG_ADDR_MAP
220 void init_addr_map(void)
223 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
225 /* walk all the entries */
226 for (i = 0; i < num_cam; i++) {
231 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
232 if (valid & MAS1_VALID)
233 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), i);
240 uint64_t tlb_map_range(ulong v_addr, phys_addr_t p_addr, uint64_t size,
241 enum tlb_map_type map_type)
244 unsigned int tlb_size;
247 unsigned int max_cam, tsize_mask;
249 if (map_type == TLB_MAP_RAM) {
250 perm = MAS3_SX|MAS3_SW|MAS3_SR;
252 #ifdef CONFIG_SYS_PPC_DDR_WIMGE
253 wimge = CONFIG_SYS_PPC_DDR_WIMGE;
256 perm = MAS3_SW|MAS3_SR;
257 wimge = MAS2_I|MAS2_G;
260 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1) {
261 /* Convert (4^max) kB to (2^max) bytes */
262 max_cam = ((mfspr(SPRN_TLB1CFG) >> 16) & 0xf) * 2 + 10;
265 /* Convert (2^max) kB to (2^max) bytes */
266 max_cam = __ilog2(mfspr(SPRN_TLB1PS)) + 10;
270 for (i = 0; size && i < 8; i++) {
271 int tlb_index = find_free_tlbcam();
272 u32 camsize = __ilog2_u64(size) & tsize_mask;
273 u32 align = __ilog2(v_addr) & tsize_mask;
278 if (align == -2) align = max_cam;
282 if (camsize > max_cam)
285 tlb_size = camsize - 10;
287 set_tlb(1, v_addr, p_addr, perm, wimge,
288 0, tlb_index, tlb_size, 1);
290 size -= 1ULL << camsize;
291 v_addr += 1UL << camsize;
292 p_addr += 1UL << camsize;
298 unsigned int setup_ddr_tlbs_phys(phys_addr_t p_addr,
299 unsigned int memsize_in_meg)
301 unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
302 u64 memsize = (u64)memsize_in_meg << 20;
305 size = min(memsize, (u64)CONFIG_MAX_MEM_MAPPED);
306 size = tlb_map_range(ram_tlb_address, p_addr, size, TLB_MAP_RAM);
308 if (size || memsize > CONFIG_MAX_MEM_MAPPED) {
309 print_size(memsize > CONFIG_MAX_MEM_MAPPED ?
310 memsize - CONFIG_MAX_MEM_MAPPED + size : size,
314 return memsize_in_meg;
317 unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
320 setup_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);
323 /* Invalidate the DDR TLBs for the requested size */
324 void clear_ddr_tlbs_phys(phys_addr_t p_addr, unsigned int memsize_in_meg)
326 u32 vstart = CONFIG_SYS_DDR_SDRAM_BASE;
328 u32 tsize, valid, ptr;
331 u64 memsize = (u64)memsize_in_meg << 20;
335 while (ptr < (vstart + memsize)) {
336 ddr_esel = find_tlb_idx((void *)ptr, 1);
337 if (ddr_esel != -1) {
338 read_tlbcam_entry(ddr_esel, &valid, &tsize, &epn, &rpn);
339 disable_tlb(ddr_esel);
341 ptr += TSIZE_TO_BYTES(tsize);
345 void clear_ddr_tlbs(unsigned int memsize_in_meg)
347 clear_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);