tlb.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * (C) Copyright 2007
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  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 as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #if defined(CONFIG_440)
  25. #include <ppc440.h>
  26. #include <asm/io.h>
  27. #include <asm/mmu.h>
  28. typedef struct region {
  29. unsigned long base;
  30. unsigned long size;
  31. unsigned long tlb_word2_i_value;
  32. } region_t;
  33. void remove_tlb(u32 vaddr, u32 size)
  34. {
  35. int i;
  36. u32 tlb_word0_value;
  37. u32 tlb_vaddr;
  38. u32 tlb_size = 0;
  39. /* First, find the index of a TLB entry not being used */
  40. for (i=0; i<PPC4XX_TLB_SIZE; i++) {
  41. tlb_word0_value = mftlb1(i);
  42. tlb_vaddr = TLB_WORD0_EPN_DECODE(tlb_word0_value);
  43. if (((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_ENABLE) &&
  44. (tlb_vaddr >= vaddr)) {
  45. /*
  46. * TLB is enabled and start address is lower or equal
  47. * than the area we are looking for. Now we only have
  48. * to check the size/end address for a match.
  49. */
  50. switch (tlb_word0_value & TLB_WORD0_SIZE_MASK) {
  51. case TLB_WORD0_SIZE_1KB:
  52. tlb_size = 1 << 10;
  53. break;
  54. case TLB_WORD0_SIZE_4KB:
  55. tlb_size = 4 << 10;
  56. break;
  57. case TLB_WORD0_SIZE_16KB:
  58. tlb_size = 16 << 10;
  59. break;
  60. case TLB_WORD0_SIZE_64KB:
  61. tlb_size = 64 << 10;
  62. break;
  63. case TLB_WORD0_SIZE_256KB:
  64. tlb_size = 256 << 10;
  65. break;
  66. case TLB_WORD0_SIZE_1MB:
  67. tlb_size = 1 << 20;
  68. break;
  69. case TLB_WORD0_SIZE_16MB:
  70. tlb_size = 16 << 20;
  71. break;
  72. case TLB_WORD0_SIZE_256MB:
  73. tlb_size = 256 << 20;
  74. break;
  75. }
  76. /*
  77. * Now check the end-address if it's in the range
  78. */
  79. if ((tlb_vaddr + tlb_size - 1) <= (vaddr + size - 1))
  80. /*
  81. * Found a TLB in the range.
  82. * Disable it by writing 0 to tlb0 word.
  83. */
  84. mttlb1(i, 0);
  85. }
  86. }
  87. /* Execute an ISYNC instruction so that the new TLB entry takes effect */
  88. asm("isync");
  89. }
  90. static int add_tlb_entry(unsigned long phys_addr,
  91. unsigned long virt_addr,
  92. unsigned long tlb_word0_size_value,
  93. unsigned long tlb_word2_i_value)
  94. {
  95. int i;
  96. unsigned long tlb_word0_value;
  97. unsigned long tlb_word1_value;
  98. unsigned long tlb_word2_value;
  99. /* First, find the index of a TLB entry not being used */
  100. for (i=0; i<PPC4XX_TLB_SIZE; i++) {
  101. tlb_word0_value = mftlb1(i);
  102. if ((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_DISABLE)
  103. break;
  104. }
  105. if (i >= PPC4XX_TLB_SIZE)
  106. return -1;
  107. /* Second, create the TLB entry */
  108. tlb_word0_value = TLB_WORD0_EPN_ENCODE(virt_addr) | TLB_WORD0_V_ENABLE |
  109. TLB_WORD0_TS_0 | tlb_word0_size_value;
  110. tlb_word1_value = TLB_WORD1_RPN_ENCODE(phys_addr) | TLB_WORD1_ERPN_ENCODE(0);
  111. tlb_word2_value = TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE |
  112. TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE |
  113. TLB_WORD2_W_DISABLE | tlb_word2_i_value |
  114. TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE |
  115. TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE |
  116. TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE |
  117. TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE |
  118. TLB_WORD2_SR_ENABLE;
  119. /* Wait for all memory accesses to complete */
  120. sync();
  121. /* Third, add the TLB entries */
  122. mttlb1(i, tlb_word0_value);
  123. mttlb2(i, tlb_word1_value);
  124. mttlb3(i, tlb_word2_value);
  125. /* Execute an ISYNC instruction so that the new TLB entry takes effect */
  126. asm("isync");
  127. return 0;
  128. }
  129. static void program_tlb_addr(unsigned long phys_addr,
  130. unsigned long virt_addr,
  131. unsigned long mem_size,
  132. unsigned long tlb_word2_i_value)
  133. {
  134. int rc;
  135. int tlb_i;
  136. tlb_i = tlb_word2_i_value;
  137. while (mem_size != 0) {
  138. rc = 0;
  139. /* Add the TLB entries in to map the region. */
  140. if (((phys_addr & TLB_256MB_ALIGN_MASK) == phys_addr) &&
  141. (mem_size >= TLB_256MB_SIZE)) {
  142. /* Add a 256MB TLB entry */
  143. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  144. TLB_WORD0_SIZE_256MB, tlb_i)) == 0) {
  145. mem_size -= TLB_256MB_SIZE;
  146. phys_addr += TLB_256MB_SIZE;
  147. virt_addr += TLB_256MB_SIZE;
  148. }
  149. } else if (((phys_addr & TLB_16MB_ALIGN_MASK) == phys_addr) &&
  150. (mem_size >= TLB_16MB_SIZE)) {
  151. /* Add a 16MB TLB entry */
  152. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  153. TLB_WORD0_SIZE_16MB, tlb_i)) == 0) {
  154. mem_size -= TLB_16MB_SIZE;
  155. phys_addr += TLB_16MB_SIZE;
  156. virt_addr += TLB_16MB_SIZE;
  157. }
  158. } else if (((phys_addr & TLB_1MB_ALIGN_MASK) == phys_addr) &&
  159. (mem_size >= TLB_1MB_SIZE)) {
  160. /* Add a 1MB TLB entry */
  161. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  162. TLB_WORD0_SIZE_1MB, tlb_i)) == 0) {
  163. mem_size -= TLB_1MB_SIZE;
  164. phys_addr += TLB_1MB_SIZE;
  165. virt_addr += TLB_1MB_SIZE;
  166. }
  167. } else if (((phys_addr & TLB_256KB_ALIGN_MASK) == phys_addr) &&
  168. (mem_size >= TLB_256KB_SIZE)) {
  169. /* Add a 256KB TLB entry */
  170. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  171. TLB_WORD0_SIZE_256KB, tlb_i)) == 0) {
  172. mem_size -= TLB_256KB_SIZE;
  173. phys_addr += TLB_256KB_SIZE;
  174. virt_addr += TLB_256KB_SIZE;
  175. }
  176. } else if (((phys_addr & TLB_64KB_ALIGN_MASK) == phys_addr) &&
  177. (mem_size >= TLB_64KB_SIZE)) {
  178. /* Add a 64KB TLB entry */
  179. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  180. TLB_WORD0_SIZE_64KB, tlb_i)) == 0) {
  181. mem_size -= TLB_64KB_SIZE;
  182. phys_addr += TLB_64KB_SIZE;
  183. virt_addr += TLB_64KB_SIZE;
  184. }
  185. } else if (((phys_addr & TLB_16KB_ALIGN_MASK) == phys_addr) &&
  186. (mem_size >= TLB_16KB_SIZE)) {
  187. /* Add a 16KB TLB entry */
  188. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  189. TLB_WORD0_SIZE_16KB, tlb_i)) == 0) {
  190. mem_size -= TLB_16KB_SIZE;
  191. phys_addr += TLB_16KB_SIZE;
  192. virt_addr += TLB_16KB_SIZE;
  193. }
  194. } else if (((phys_addr & TLB_4KB_ALIGN_MASK) == phys_addr) &&
  195. (mem_size >= TLB_4KB_SIZE)) {
  196. /* Add a 4KB TLB entry */
  197. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  198. TLB_WORD0_SIZE_4KB, tlb_i)) == 0) {
  199. mem_size -= TLB_4KB_SIZE;
  200. phys_addr += TLB_4KB_SIZE;
  201. virt_addr += TLB_4KB_SIZE;
  202. }
  203. } else if (((phys_addr & TLB_1KB_ALIGN_MASK) == phys_addr) &&
  204. (mem_size >= TLB_1KB_SIZE)) {
  205. /* Add a 1KB TLB entry */
  206. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  207. TLB_WORD0_SIZE_1KB, tlb_i)) == 0) {
  208. mem_size -= TLB_1KB_SIZE;
  209. phys_addr += TLB_1KB_SIZE;
  210. virt_addr += TLB_1KB_SIZE;
  211. }
  212. } else {
  213. printf("ERROR: no TLB size exists for the base address 0x%0X.\n",
  214. phys_addr);
  215. }
  216. if (rc != 0)
  217. printf("ERROR: no TLB entries available for the base addr 0x%0X.\n",
  218. phys_addr);
  219. }
  220. return;
  221. }
  222. /*
  223. * Program one (or multiple) TLB entries for one memory region
  224. *
  225. * Common usage for boards with SDRAM DIMM modules to dynamically
  226. * configure the TLB's for the SDRAM
  227. */
  228. void program_tlb(u32 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value)
  229. {
  230. region_t region_array;
  231. region_array.base = phys_addr;
  232. region_array.size = size;
  233. region_array.tlb_word2_i_value = tlb_word2_i_value; /* en-/disable cache */
  234. /* Call the routine to add in the tlb entries for the memory regions */
  235. program_tlb_addr(region_array.base, virt_addr, region_array.size,
  236. region_array.tlb_word2_i_value);
  237. return;
  238. }
  239. #endif /* CONFIG_440 */