tlb.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright 2008-2009 Freescale Semiconductor, Inc.
  3. *
  4. * (C) Copyright 2000
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <asm/processor.h>
  27. #include <asm/mmu.h>
  28. #ifdef CONFIG_ADDR_MAP
  29. #include <addr_map.h>
  30. #endif
  31. DECLARE_GLOBAL_DATA_PTR;
  32. void invalidate_tlb(u8 tlb)
  33. {
  34. if (tlb == 0)
  35. mtspr(MMUCSR0, 0x4);
  36. if (tlb == 1)
  37. mtspr(MMUCSR0, 0x2);
  38. }
  39. void init_tlbs(void)
  40. {
  41. int i;
  42. for (i = 0; i < num_tlb_entries; i++) {
  43. write_tlb(tlb_table[i].mas0,
  44. tlb_table[i].mas1,
  45. tlb_table[i].mas2,
  46. tlb_table[i].mas3,
  47. tlb_table[i].mas7);
  48. }
  49. return ;
  50. }
  51. void read_tlbcam_entry(int idx, u32 *valid, u32 *tsize, unsigned long *epn,
  52. phys_addr_t *rpn)
  53. {
  54. u32 _mas1;
  55. mtspr(MAS0, FSL_BOOKE_MAS0(1, idx, 0));
  56. asm volatile("tlbre;isync");
  57. _mas1 = mfspr(MAS1);
  58. *valid = (_mas1 & MAS1_VALID);
  59. *tsize = (_mas1 >> 8) & 0xf;
  60. *epn = mfspr(MAS2) & MAS2_EPN;
  61. *rpn = mfspr(MAS3) & MAS3_RPN;
  62. #ifdef CONFIG_ENABLE_36BIT_PHYS
  63. *rpn |= ((u64)mfspr(MAS7)) << 32;
  64. #endif
  65. }
  66. #ifndef CONFIG_NAND_SPL
  67. void print_tlbcam(void)
  68. {
  69. int i;
  70. 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++) {
  74. unsigned long epn;
  75. u32 tsize, valid;
  76. phys_addr_t rpn;
  77. read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
  78. printf("entry %02d: V: %d EPN 0x%08x RPN 0x%08llx size:",
  79. i, (valid == 0) ? 0 : 1, (unsigned int)epn,
  80. (unsigned long long)rpn);
  81. print_size(TSIZE_TO_BYTES(tsize), "\n");
  82. }
  83. }
  84. static inline void use_tlb_cam(u8 idx)
  85. {
  86. int i = idx / 32;
  87. int bit = idx % 32;
  88. gd->used_tlb_cams[i] |= (1 << bit);
  89. }
  90. static inline void free_tlb_cam(u8 idx)
  91. {
  92. int i = idx / 32;
  93. int bit = idx % 32;
  94. gd->used_tlb_cams[i] &= ~(1 << bit);
  95. }
  96. void init_used_tlb_cams(void)
  97. {
  98. int i;
  99. unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
  100. for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++)
  101. gd->used_tlb_cams[i] = 0;
  102. /* walk all the entries */
  103. for (i = 0; i < num_cam; i++) {
  104. mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
  105. asm volatile("tlbre;isync");
  106. if (mfspr(MAS1) & MAS1_VALID)
  107. use_tlb_cam(i);
  108. }
  109. }
  110. int find_free_tlbcam(void)
  111. {
  112. int i;
  113. u32 idx;
  114. for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) {
  115. idx = ffz(gd->used_tlb_cams[i]);
  116. if (idx != 32)
  117. break;
  118. }
  119. idx += i * 32;
  120. if (idx >= CONFIG_SYS_NUM_TLBCAMS)
  121. return -1;
  122. return idx;
  123. }
  124. void set_tlb(u8 tlb, u32 epn, u64 rpn,
  125. u8 perms, u8 wimge,
  126. u8 ts, u8 esel, u8 tsize, u8 iprot)
  127. {
  128. u32 _mas0, _mas1, _mas2, _mas3, _mas7;
  129. if (tlb == 1)
  130. use_tlb_cam(esel);
  131. _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0);
  132. _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize);
  133. _mas2 = FSL_BOOKE_MAS2(epn, wimge);
  134. _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms);
  135. _mas7 = FSL_BOOKE_MAS7(rpn);
  136. write_tlb(_mas0, _mas1, _mas2, _mas3, _mas7);
  137. #ifdef CONFIG_ADDR_MAP
  138. if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
  139. addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), esel);
  140. #endif
  141. }
  142. void disable_tlb(u8 esel)
  143. {
  144. u32 _mas0, _mas1, _mas2, _mas3, _mas7;
  145. free_tlb_cam(esel);
  146. _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
  147. _mas1 = 0;
  148. _mas2 = 0;
  149. _mas3 = 0;
  150. _mas7 = 0;
  151. mtspr(MAS0, _mas0);
  152. mtspr(MAS1, _mas1);
  153. mtspr(MAS2, _mas2);
  154. mtspr(MAS3, _mas3);
  155. #ifdef CONFIG_ENABLE_36BIT_PHYS
  156. mtspr(MAS7, _mas7);
  157. #endif
  158. asm volatile("isync;msync;tlbwe;isync");
  159. #ifdef CONFIG_ADDR_MAP
  160. if (gd->flags & GD_FLG_RELOC)
  161. addrmap_set_entry(0, 0, 0, esel);
  162. #endif
  163. }
  164. static void tlbsx (const volatile unsigned *addr)
  165. {
  166. __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr));
  167. }
  168. /* return -1 if we didn't find anything */
  169. int find_tlb_idx(void *addr, u8 tlbsel)
  170. {
  171. u32 _mas0, _mas1;
  172. /* zero out Search PID, AS */
  173. mtspr(MAS6, 0);
  174. tlbsx(addr);
  175. _mas0 = mfspr(MAS0);
  176. _mas1 = mfspr(MAS1);
  177. /* we found something, and its in the TLB we expect */
  178. if ((MAS1_VALID & _mas1) &&
  179. (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) {
  180. return ((_mas0 & MAS0_ESEL_MSK) >> 16);
  181. }
  182. return -1;
  183. }
  184. #ifdef CONFIG_ADDR_MAP
  185. void init_addr_map(void)
  186. {
  187. int i;
  188. unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
  189. /* walk all the entries */
  190. for (i = 0; i < num_cam; i++) {
  191. unsigned long epn;
  192. u32 tsize, valid;
  193. phys_addr_t rpn;
  194. read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
  195. if (valid & MAS1_VALID)
  196. addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), i);
  197. }
  198. return ;
  199. }
  200. #endif
  201. unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
  202. {
  203. int i;
  204. unsigned int tlb_size;
  205. unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
  206. unsigned int max_cam = (mfspr(SPRN_TLB1CFG) >> 16) & 0xf;
  207. u64 size, memsize = (u64)memsize_in_meg << 20;
  208. size = min(memsize, CONFIG_MAX_MEM_MAPPED);
  209. /* Convert (4^max) kB to (2^max) bytes */
  210. max_cam = max_cam * 2 + 10;
  211. for (i = 0; size && i < 8; i++) {
  212. int ram_tlb_index = find_free_tlbcam();
  213. u32 camsize = __ilog2_u64(size) & ~1U;
  214. u32 align = __ilog2(ram_tlb_address) & ~1U;
  215. if (ram_tlb_index == -1)
  216. break;
  217. if (align == -2) align = max_cam;
  218. if (camsize > align)
  219. camsize = align;
  220. if (camsize > max_cam)
  221. camsize = max_cam;
  222. tlb_size = (camsize - 10) / 2;
  223. set_tlb(1, ram_tlb_address, ram_tlb_address,
  224. MAS3_SX|MAS3_SW|MAS3_SR, 0,
  225. 0, ram_tlb_index, tlb_size, 1);
  226. size -= 1ULL << camsize;
  227. memsize -= 1ULL << camsize;
  228. ram_tlb_address += 1UL << camsize;
  229. }
  230. if (memsize)
  231. print_size(memsize, " left unmapped\n");
  232. /*
  233. * Confirm that the requested amount of memory was mapped.
  234. */
  235. return memsize_in_meg;
  236. }
  237. #endif /* !CONFIG_NAND_SPL */