tlb.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2008 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 set_tlb(u8 tlb, u32 epn, u64 rpn,
  33. u8 perms, u8 wimge,
  34. u8 ts, u8 esel, u8 tsize, u8 iprot)
  35. {
  36. u32 _mas0, _mas1, _mas2, _mas3, _mas7;
  37. _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0);
  38. _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize);
  39. _mas2 = FSL_BOOKE_MAS2(epn, wimge);
  40. _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms);
  41. _mas7 = rpn >> 32;
  42. mtspr(MAS0, _mas0);
  43. mtspr(MAS1, _mas1);
  44. mtspr(MAS2, _mas2);
  45. mtspr(MAS3, _mas3);
  46. #ifdef CONFIG_ENABLE_36BIT_PHYS
  47. mtspr(MAS7, _mas7);
  48. #endif
  49. #ifdef CONFIG_SYS_BOOK3E_HV
  50. mtspr(MAS8, 0);
  51. #endif
  52. asm volatile("isync;msync;tlbwe;isync");
  53. #ifdef CONFIG_ADDR_MAP
  54. if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
  55. addrmap_set_entry(epn, rpn, (1UL << ((tsize * 2) + 10)), esel);
  56. #endif
  57. }
  58. void disable_tlb(u8 esel)
  59. {
  60. u32 _mas0, _mas1, _mas2, _mas3, _mas7;
  61. _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
  62. _mas1 = 0;
  63. _mas2 = 0;
  64. _mas3 = 0;
  65. _mas7 = 0;
  66. mtspr(MAS0, _mas0);
  67. mtspr(MAS1, _mas1);
  68. mtspr(MAS2, _mas2);
  69. mtspr(MAS3, _mas3);
  70. #ifdef CONFIG_ENABLE_36BIT_PHYS
  71. mtspr(MAS7, _mas7);
  72. #endif
  73. asm volatile("isync;msync;tlbwe;isync");
  74. #ifdef CONFIG_ADDR_MAP
  75. if (gd->flags & GD_FLG_RELOC)
  76. addrmap_set_entry(0, 0, 0, esel);
  77. #endif
  78. }
  79. void invalidate_tlb(u8 tlb)
  80. {
  81. if (tlb == 0)
  82. mtspr(MMUCSR0, 0x4);
  83. if (tlb == 1)
  84. mtspr(MMUCSR0, 0x2);
  85. }
  86. void init_tlbs(void)
  87. {
  88. int i;
  89. for (i = 0; i < num_tlb_entries; i++) {
  90. set_tlb(tlb_table[i].tlb, tlb_table[i].epn, tlb_table[i].rpn,
  91. tlb_table[i].perms, tlb_table[i].wimge,
  92. tlb_table[i].ts, tlb_table[i].esel, tlb_table[i].tsize,
  93. tlb_table[i].iprot);
  94. }
  95. return ;
  96. }
  97. static void tlbsx (const volatile unsigned *addr)
  98. {
  99. __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr));
  100. }
  101. /* return -1 if we didn't find anything */
  102. int find_tlb_idx(void *addr, u8 tlbsel)
  103. {
  104. u32 _mas0, _mas1;
  105. /* zero out Search PID, AS */
  106. mtspr(MAS6, 0);
  107. tlbsx(addr);
  108. _mas0 = mfspr(MAS0);
  109. _mas1 = mfspr(MAS1);
  110. /* we found something, and its in the TLB we expect */
  111. if ((MAS1_VALID & _mas1) &&
  112. (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) {
  113. return ((_mas0 & MAS0_ESEL_MSK) >> 16);
  114. }
  115. return -1;
  116. }
  117. #ifdef CONFIG_ADDR_MAP
  118. void init_addr_map(void)
  119. {
  120. int i;
  121. unsigned int max_cam = (mfspr(SPRN_TLB1CFG) >> 16) & 0xff;
  122. /* walk all the entries */
  123. for (i = 0; i < max_cam; i++) {
  124. unsigned long epn;
  125. u32 tsize, _mas1;
  126. phys_addr_t rpn;
  127. mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
  128. asm volatile("tlbre;isync");
  129. _mas1 = mfspr(MAS1);
  130. /* if the entry isn't valid skip it */
  131. if (!(_mas1 & MAS1_VALID))
  132. continue;
  133. tsize = (_mas1 >> 8) & 0xf;
  134. epn = mfspr(MAS2) & MAS2_EPN;
  135. rpn = mfspr(MAS3) & MAS3_RPN;
  136. #ifdef CONFIG_ENABLE_36BIT_PHYS
  137. rpn |= ((phys_addr_t)mfspr(MAS7)) << 32;
  138. #endif
  139. addrmap_set_entry(epn, rpn, (1UL << ((tsize * 2) + 10)), i);
  140. }
  141. return ;
  142. }
  143. #endif
  144. #ifndef CONFIG_SYS_DDR_TLB_START
  145. #define CONFIG_SYS_DDR_TLB_START 8
  146. #endif
  147. unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
  148. {
  149. unsigned int tlb_size;
  150. unsigned int ram_tlb_index = CONFIG_SYS_DDR_TLB_START;
  151. unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
  152. unsigned int max_cam = (mfspr(SPRN_TLB1CFG) >> 16) & 0xf;
  153. u64 size, memsize = (u64)memsize_in_meg << 20;
  154. size = min(memsize, CONFIG_MAX_MEM_MAPPED);
  155. /* Convert (4^max) kB to (2^max) bytes */
  156. max_cam = max_cam * 2 + 10;
  157. for (; size && ram_tlb_index < 16; ram_tlb_index++) {
  158. u32 camsize = __ilog2_u64(size) & ~1U;
  159. u32 align = __ilog2(ram_tlb_address) & ~1U;
  160. if (align == -2) align = max_cam;
  161. if (camsize > align)
  162. camsize = align;
  163. if (camsize > max_cam)
  164. camsize = max_cam;
  165. tlb_size = (camsize - 10) / 2;
  166. set_tlb(1, ram_tlb_address, ram_tlb_address,
  167. MAS3_SX|MAS3_SW|MAS3_SR, 0,
  168. 0, ram_tlb_index, tlb_size, 1);
  169. size -= 1ULL << camsize;
  170. memsize -= 1ULL << camsize;
  171. ram_tlb_address += 1UL << camsize;
  172. }
  173. if (memsize)
  174. print_size(memsize, " left unmapped\n");
  175. /*
  176. * Confirm that the requested amount of memory was mapped.
  177. */
  178. return memsize_in_meg;
  179. }