1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2008-2011 Freescale Semiconductor, Inc.
11 #include <asm/bitops.h>
12 #include <asm/global_data.h>
13 #include <asm/processor.h>
15 #ifdef CONFIG_ADDR_MAP
19 #include <linux/log2.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 void invalidate_tlb(u8 tlb)
31 __weak void init_tlbs(void)
35 for (i = 0; i < num_tlb_entries; i++) {
36 write_tlb(tlb_table[i].mas0,
46 #if !defined(CONFIG_NAND_SPL) && \
47 (!defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(INIT_MINIMAL))
48 void read_tlbcam_entry(int idx, u32 *valid, u32 *tsize, unsigned long *epn,
53 mtspr(MAS0, FSL_BOOKE_MAS0(1, idx, 0));
54 asm volatile("tlbre;isync");
57 *valid = (_mas1 & MAS1_VALID);
58 *tsize = (_mas1 >> 7) & 0x1f;
59 *epn = mfspr(MAS2) & MAS2_EPN;
60 *rpn = mfspr(MAS3) & MAS3_RPN;
61 #ifdef CONFIG_ENABLE_36BIT_PHYS
62 *rpn |= ((u64)mfspr(MAS7)) << 32;
66 void print_tlbcam(void)
69 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
71 /* walk all the entries */
72 printf("TLBCAM entries\n");
73 for (i = 0; i < num_cam; i++) {
78 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
79 printf("entry %02d: V: %d EPN 0x%08x RPN 0x%08llx size:",
80 i, (valid == 0) ? 0 : 1, (unsigned int)epn,
81 (unsigned long long)rpn);
82 print_size(TSIZE_TO_BYTES(tsize), "\n");
86 static inline void use_tlb_cam(u8 idx)
91 gd->arch.used_tlb_cams[i] |= (1 << bit);
94 static inline void free_tlb_cam(u8 idx)
99 gd->arch.used_tlb_cams[i] &= ~(1 << bit);
102 void init_used_tlb_cams(void)
105 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
107 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++)
108 gd->arch.used_tlb_cams[i] = 0;
110 /* walk all the entries */
111 for (i = 0; i < num_cam; i++) {
112 mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
113 asm volatile("tlbre;isync");
114 if (mfspr(MAS1) & MAS1_VALID)
119 int find_free_tlbcam(void)
124 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) {
125 idx = ffz(gd->arch.used_tlb_cams[i]);
133 if (idx >= CONFIG_SYS_NUM_TLBCAMS)
139 void set_tlb(u8 tlb, u32 epn, u64 rpn,
141 u8 ts, u8 esel, u8 tsize, u8 iprot)
143 u32 _mas0, _mas1, _mas2, _mas3, _mas7;
148 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1 &&
150 printf("%s: bad tsize %d on entry %d at 0x%08x\n",
151 __func__, tsize, tlb, epn);
155 _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0);
156 _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize);
157 _mas2 = FSL_BOOKE_MAS2(epn, wimge);
158 _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms);
159 _mas7 = FSL_BOOKE_MAS7(rpn);
161 write_tlb(_mas0, _mas1, _mas2, _mas3, _mas7);
163 #ifdef CONFIG_ADDR_MAP
164 if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
165 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), esel);
169 void disable_tlb(u8 esel)
171 u32 _mas0, _mas1, _mas2, _mas3;
175 _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
184 #ifdef CONFIG_ENABLE_36BIT_PHYS
187 asm volatile("isync;msync;tlbwe;isync");
189 #ifdef CONFIG_ADDR_MAP
190 if (gd->flags & GD_FLG_RELOC)
191 addrmap_set_entry(0, 0, 0, esel);
195 static void tlbsx (const volatile unsigned *addr)
197 __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr));
200 /* return -1 if we didn't find anything */
201 int find_tlb_idx(void *addr, u8 tlbsel)
205 /* zero out Search PID, AS */
213 /* we found something, and its in the TLB we expect */
214 if ((MAS1_VALID & _mas1) &&
215 (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) {
216 return ((_mas0 & MAS0_ESEL_MSK) >> 16);
222 #ifdef CONFIG_ADDR_MAP
223 int init_addr_map(void)
226 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
228 /* walk all the entries */
229 for (i = 0; i < num_cam; i++) {
234 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
235 if (valid & MAS1_VALID)
236 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), i);
243 uint64_t tlb_map_range(ulong v_addr, phys_addr_t p_addr, uint64_t size,
244 enum tlb_map_type map_type)
247 unsigned int tlb_size;
250 unsigned int max_cam, tsize_mask;
252 if (map_type == TLB_MAP_RAM) {
253 perm = MAS3_SX|MAS3_SW|MAS3_SR;
255 #ifdef CONFIG_SYS_PPC_DDR_WIMGE
256 wimge = CONFIG_SYS_PPC_DDR_WIMGE;
259 perm = MAS3_SW|MAS3_SR;
260 wimge = MAS2_I|MAS2_G;
263 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1) {
264 /* Convert (4^max) kB to (2^max) bytes */
265 max_cam = ((mfspr(SPRN_TLB1CFG) >> 16) & 0xf) * 2 + 10;
268 /* Convert (2^max) kB to (2^max) bytes */
269 max_cam = __ilog2(mfspr(SPRN_TLB1PS)) + 10;
273 for (i = 0; size && i < 8; i++) {
274 int tlb_index = find_free_tlbcam();
275 u32 camsize = __ilog2_u64(size) & tsize_mask;
276 u32 align = __ilog2(v_addr) & tsize_mask;
281 if (align == -2) align = max_cam;
285 if (camsize > max_cam)
288 tlb_size = camsize - 10;
290 set_tlb(1, v_addr, p_addr, perm, wimge,
291 0, tlb_index, tlb_size, 1);
293 size -= 1ULL << camsize;
294 v_addr += 1UL << camsize;
295 p_addr += 1UL << camsize;
301 unsigned int setup_ddr_tlbs_phys(phys_addr_t p_addr,
302 unsigned int memsize_in_meg)
304 unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
305 u64 memsize = (u64)memsize_in_meg << 20;
308 size = min(memsize, (u64)CONFIG_MAX_MEM_MAPPED);
309 size = tlb_map_range(ram_tlb_address, p_addr, size, TLB_MAP_RAM);
311 if (size || memsize > CONFIG_MAX_MEM_MAPPED) {
312 print_size(memsize > CONFIG_MAX_MEM_MAPPED ?
313 memsize - CONFIG_MAX_MEM_MAPPED + size : size,
317 return memsize_in_meg;
320 unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
323 setup_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);
326 /* Invalidate the DDR TLBs for the requested size */
327 void clear_ddr_tlbs_phys(phys_addr_t p_addr, unsigned int memsize_in_meg)
329 u32 vstart = CONFIG_SYS_DDR_SDRAM_BASE;
331 u32 tsize, valid, ptr;
334 u64 memsize = (u64)memsize_in_meg << 20;
338 while (ptr < (vstart + memsize)) {
339 ddr_esel = find_tlb_idx((void *)ptr, 1);
340 if (ddr_esel != -1) {
341 read_tlbcam_entry(ddr_esel, &valid, &tsize, &epn, &rpn);
342 disable_tlb(ddr_esel);
344 ptr += TSIZE_TO_BYTES(tsize);
348 void clear_ddr_tlbs(unsigned int memsize_in_meg)
350 clear_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);