tlb.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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/cache.h>
  27. #include <asm/io.h>
  28. #include <asm/mmu.h>
  29. typedef struct region {
  30. unsigned long base;
  31. unsigned long size;
  32. unsigned long tlb_word2_i_value;
  33. } region_t;
  34. void remove_tlb(u32 vaddr, u32 size)
  35. {
  36. int i;
  37. u32 tlb_word0_value;
  38. u32 tlb_vaddr;
  39. u32 tlb_size = 0;
  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. /*
  91. * Change the I attribute (cache inhibited) of a TLB or multiple TLB's.
  92. * This function is used to either turn cache on or off in a specific
  93. * memory area.
  94. */
  95. void change_tlb(u32 vaddr, u32 size, u32 tlb_word2_i_value)
  96. {
  97. int i;
  98. u32 tlb_word0_value;
  99. u32 tlb_word2_value;
  100. u32 tlb_vaddr;
  101. u32 tlb_size = 0;
  102. for (i=0; i<PPC4XX_TLB_SIZE; i++) {
  103. tlb_word0_value = mftlb1(i);
  104. tlb_vaddr = TLB_WORD0_EPN_DECODE(tlb_word0_value);
  105. if (((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_ENABLE) &&
  106. (tlb_vaddr >= vaddr)) {
  107. /*
  108. * TLB is enabled and start address is lower or equal
  109. * than the area we are looking for. Now we only have
  110. * to check the size/end address for a match.
  111. */
  112. switch (tlb_word0_value & TLB_WORD0_SIZE_MASK) {
  113. case TLB_WORD0_SIZE_1KB:
  114. tlb_size = 1 << 10;
  115. break;
  116. case TLB_WORD0_SIZE_4KB:
  117. tlb_size = 4 << 10;
  118. break;
  119. case TLB_WORD0_SIZE_16KB:
  120. tlb_size = 16 << 10;
  121. break;
  122. case TLB_WORD0_SIZE_64KB:
  123. tlb_size = 64 << 10;
  124. break;
  125. case TLB_WORD0_SIZE_256KB:
  126. tlb_size = 256 << 10;
  127. break;
  128. case TLB_WORD0_SIZE_1MB:
  129. tlb_size = 1 << 20;
  130. break;
  131. case TLB_WORD0_SIZE_16MB:
  132. tlb_size = 16 << 20;
  133. break;
  134. case TLB_WORD0_SIZE_256MB:
  135. tlb_size = 256 << 20;
  136. break;
  137. }
  138. /*
  139. * Now check the end-address if it's in the range
  140. */
  141. if ((tlb_vaddr + tlb_size - 1) <= (vaddr + size - 1)) {
  142. /*
  143. * Found a TLB in the range.
  144. * Change cache attribute in tlb2 word.
  145. */
  146. tlb_word2_value =
  147. TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE |
  148. TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE |
  149. TLB_WORD2_W_DISABLE | tlb_word2_i_value |
  150. TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE |
  151. TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE |
  152. TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE |
  153. TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE |
  154. TLB_WORD2_SR_ENABLE;
  155. /*
  156. * Now either flush or invalidate the dcache
  157. */
  158. if (tlb_word2_i_value)
  159. flush_dcache();
  160. else
  161. invalidate_dcache();
  162. mttlb3(i, tlb_word2_value);
  163. asm("iccci 0,0");
  164. }
  165. }
  166. }
  167. /* Execute an ISYNC instruction so that the new TLB entry takes effect */
  168. asm("isync");
  169. }
  170. static int add_tlb_entry(unsigned long phys_addr,
  171. unsigned long virt_addr,
  172. unsigned long tlb_word0_size_value,
  173. unsigned long tlb_word2_i_value)
  174. {
  175. int i;
  176. unsigned long tlb_word0_value;
  177. unsigned long tlb_word1_value;
  178. unsigned long tlb_word2_value;
  179. /* First, find the index of a TLB entry not being used */
  180. for (i=0; i<PPC4XX_TLB_SIZE; i++) {
  181. tlb_word0_value = mftlb1(i);
  182. if ((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_DISABLE)
  183. break;
  184. }
  185. if (i >= PPC4XX_TLB_SIZE)
  186. return -1;
  187. /* Second, create the TLB entry */
  188. tlb_word0_value = TLB_WORD0_EPN_ENCODE(virt_addr) | TLB_WORD0_V_ENABLE |
  189. TLB_WORD0_TS_0 | tlb_word0_size_value;
  190. tlb_word1_value = TLB_WORD1_RPN_ENCODE(phys_addr) | TLB_WORD1_ERPN_ENCODE(0);
  191. tlb_word2_value = TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE |
  192. TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE |
  193. TLB_WORD2_W_DISABLE | tlb_word2_i_value |
  194. TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE |
  195. TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE |
  196. TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE |
  197. TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE |
  198. TLB_WORD2_SR_ENABLE;
  199. /* Wait for all memory accesses to complete */
  200. sync();
  201. /* Third, add the TLB entries */
  202. mttlb1(i, tlb_word0_value);
  203. mttlb2(i, tlb_word1_value);
  204. mttlb3(i, tlb_word2_value);
  205. /* Execute an ISYNC instruction so that the new TLB entry takes effect */
  206. asm("isync");
  207. return 0;
  208. }
  209. static void program_tlb_addr(unsigned long phys_addr,
  210. unsigned long virt_addr,
  211. unsigned long mem_size,
  212. unsigned long tlb_word2_i_value)
  213. {
  214. int rc;
  215. int tlb_i;
  216. tlb_i = tlb_word2_i_value;
  217. while (mem_size != 0) {
  218. rc = 0;
  219. /* Add the TLB entries in to map the region. */
  220. if (((phys_addr & TLB_256MB_ALIGN_MASK) == phys_addr) &&
  221. (mem_size >= TLB_256MB_SIZE)) {
  222. /* Add a 256MB TLB entry */
  223. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  224. TLB_WORD0_SIZE_256MB, tlb_i)) == 0) {
  225. mem_size -= TLB_256MB_SIZE;
  226. phys_addr += TLB_256MB_SIZE;
  227. virt_addr += TLB_256MB_SIZE;
  228. }
  229. } else if (((phys_addr & TLB_16MB_ALIGN_MASK) == phys_addr) &&
  230. (mem_size >= TLB_16MB_SIZE)) {
  231. /* Add a 16MB TLB entry */
  232. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  233. TLB_WORD0_SIZE_16MB, tlb_i)) == 0) {
  234. mem_size -= TLB_16MB_SIZE;
  235. phys_addr += TLB_16MB_SIZE;
  236. virt_addr += TLB_16MB_SIZE;
  237. }
  238. } else if (((phys_addr & TLB_1MB_ALIGN_MASK) == phys_addr) &&
  239. (mem_size >= TLB_1MB_SIZE)) {
  240. /* Add a 1MB TLB entry */
  241. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  242. TLB_WORD0_SIZE_1MB, tlb_i)) == 0) {
  243. mem_size -= TLB_1MB_SIZE;
  244. phys_addr += TLB_1MB_SIZE;
  245. virt_addr += TLB_1MB_SIZE;
  246. }
  247. } else if (((phys_addr & TLB_256KB_ALIGN_MASK) == phys_addr) &&
  248. (mem_size >= TLB_256KB_SIZE)) {
  249. /* Add a 256KB TLB entry */
  250. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  251. TLB_WORD0_SIZE_256KB, tlb_i)) == 0) {
  252. mem_size -= TLB_256KB_SIZE;
  253. phys_addr += TLB_256KB_SIZE;
  254. virt_addr += TLB_256KB_SIZE;
  255. }
  256. } else if (((phys_addr & TLB_64KB_ALIGN_MASK) == phys_addr) &&
  257. (mem_size >= TLB_64KB_SIZE)) {
  258. /* Add a 64KB TLB entry */
  259. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  260. TLB_WORD0_SIZE_64KB, tlb_i)) == 0) {
  261. mem_size -= TLB_64KB_SIZE;
  262. phys_addr += TLB_64KB_SIZE;
  263. virt_addr += TLB_64KB_SIZE;
  264. }
  265. } else if (((phys_addr & TLB_16KB_ALIGN_MASK) == phys_addr) &&
  266. (mem_size >= TLB_16KB_SIZE)) {
  267. /* Add a 16KB TLB entry */
  268. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  269. TLB_WORD0_SIZE_16KB, tlb_i)) == 0) {
  270. mem_size -= TLB_16KB_SIZE;
  271. phys_addr += TLB_16KB_SIZE;
  272. virt_addr += TLB_16KB_SIZE;
  273. }
  274. } else if (((phys_addr & TLB_4KB_ALIGN_MASK) == phys_addr) &&
  275. (mem_size >= TLB_4KB_SIZE)) {
  276. /* Add a 4KB TLB entry */
  277. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  278. TLB_WORD0_SIZE_4KB, tlb_i)) == 0) {
  279. mem_size -= TLB_4KB_SIZE;
  280. phys_addr += TLB_4KB_SIZE;
  281. virt_addr += TLB_4KB_SIZE;
  282. }
  283. } else if (((phys_addr & TLB_1KB_ALIGN_MASK) == phys_addr) &&
  284. (mem_size >= TLB_1KB_SIZE)) {
  285. /* Add a 1KB TLB entry */
  286. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  287. TLB_WORD0_SIZE_1KB, tlb_i)) == 0) {
  288. mem_size -= TLB_1KB_SIZE;
  289. phys_addr += TLB_1KB_SIZE;
  290. virt_addr += TLB_1KB_SIZE;
  291. }
  292. } else {
  293. printf("ERROR: no TLB size exists for the base address 0x%0X.\n",
  294. phys_addr);
  295. }
  296. if (rc != 0)
  297. printf("ERROR: no TLB entries available for the base addr 0x%0X.\n",
  298. phys_addr);
  299. }
  300. return;
  301. }
  302. /*
  303. * Program one (or multiple) TLB entries for one memory region
  304. *
  305. * Common usage for boards with SDRAM DIMM modules to dynamically
  306. * configure the TLB's for the SDRAM
  307. */
  308. void program_tlb(u32 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value)
  309. {
  310. region_t region_array;
  311. region_array.base = phys_addr;
  312. region_array.size = size;
  313. region_array.tlb_word2_i_value = tlb_word2_i_value; /* en-/disable cache */
  314. /* Call the routine to add in the tlb entries for the memory regions */
  315. program_tlb_addr(region_array.base, virt_addr, region_array.size,
  316. region_array.tlb_word2_i_value);
  317. return;
  318. }
  319. #endif /* CONFIG_440 */