tlb.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. u64 base;
  31. u32 size;
  32. u32 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. ((tlb_vaddr < (vaddr + size - 1)) &&
  143. ((tlb_vaddr + tlb_size - 1) > (vaddr + size - 1)))) {
  144. /*
  145. * Found a TLB in the range.
  146. * Change cache attribute in tlb2 word.
  147. */
  148. tlb_word2_value =
  149. TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE |
  150. TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE |
  151. TLB_WORD2_W_DISABLE | tlb_word2_i_value |
  152. TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE |
  153. TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE |
  154. TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE |
  155. TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE |
  156. TLB_WORD2_SR_ENABLE;
  157. /*
  158. * Now either flush or invalidate the dcache
  159. */
  160. if (tlb_word2_i_value)
  161. flush_dcache();
  162. else
  163. invalidate_dcache();
  164. mttlb3(i, tlb_word2_value);
  165. asm("iccci 0,0");
  166. }
  167. }
  168. }
  169. /* Execute an ISYNC instruction so that the new TLB entry takes effect */
  170. asm("isync");
  171. }
  172. static int add_tlb_entry(u64 phys_addr,
  173. u32 virt_addr,
  174. u32 tlb_word0_size_value,
  175. u32 tlb_word2_i_value)
  176. {
  177. int i;
  178. unsigned long tlb_word0_value;
  179. unsigned long tlb_word1_value;
  180. unsigned long tlb_word2_value;
  181. /* First, find the index of a TLB entry not being used */
  182. for (i=0; i<PPC4XX_TLB_SIZE; i++) {
  183. tlb_word0_value = mftlb1(i);
  184. if ((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_DISABLE)
  185. break;
  186. }
  187. if (i >= PPC4XX_TLB_SIZE)
  188. return -1;
  189. /* Second, create the TLB entry */
  190. tlb_word0_value = TLB_WORD0_EPN_ENCODE(virt_addr) | TLB_WORD0_V_ENABLE |
  191. TLB_WORD0_TS_0 | tlb_word0_size_value;
  192. tlb_word1_value = TLB_WORD1_RPN_ENCODE((u32)phys_addr) |
  193. TLB_WORD1_ERPN_ENCODE(phys_addr >> 32);
  194. tlb_word2_value = TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE |
  195. TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE |
  196. TLB_WORD2_W_DISABLE | tlb_word2_i_value |
  197. TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE |
  198. TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE |
  199. TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE |
  200. TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE |
  201. TLB_WORD2_SR_ENABLE;
  202. /* Wait for all memory accesses to complete */
  203. sync();
  204. /* Third, add the TLB entries */
  205. mttlb1(i, tlb_word0_value);
  206. mttlb2(i, tlb_word1_value);
  207. mttlb3(i, tlb_word2_value);
  208. /* Execute an ISYNC instruction so that the new TLB entry takes effect */
  209. asm("isync");
  210. return 0;
  211. }
  212. static void program_tlb_addr(u64 phys_addr,
  213. u32 virt_addr,
  214. u32 mem_size,
  215. u32 tlb_word2_i_value)
  216. {
  217. int rc;
  218. int tlb_i;
  219. tlb_i = tlb_word2_i_value;
  220. while (mem_size != 0) {
  221. rc = 0;
  222. /* Add the TLB entries in to map the region. */
  223. if (((phys_addr & TLB_256MB_ALIGN_MASK) == phys_addr) &&
  224. (mem_size >= TLB_256MB_SIZE)) {
  225. /* Add a 256MB TLB entry */
  226. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  227. TLB_WORD0_SIZE_256MB, tlb_i)) == 0) {
  228. mem_size -= TLB_256MB_SIZE;
  229. phys_addr += TLB_256MB_SIZE;
  230. virt_addr += TLB_256MB_SIZE;
  231. }
  232. } else if (((phys_addr & TLB_16MB_ALIGN_MASK) == phys_addr) &&
  233. (mem_size >= TLB_16MB_SIZE)) {
  234. /* Add a 16MB TLB entry */
  235. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  236. TLB_WORD0_SIZE_16MB, tlb_i)) == 0) {
  237. mem_size -= TLB_16MB_SIZE;
  238. phys_addr += TLB_16MB_SIZE;
  239. virt_addr += TLB_16MB_SIZE;
  240. }
  241. } else if (((phys_addr & TLB_1MB_ALIGN_MASK) == phys_addr) &&
  242. (mem_size >= TLB_1MB_SIZE)) {
  243. /* Add a 1MB TLB entry */
  244. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  245. TLB_WORD0_SIZE_1MB, tlb_i)) == 0) {
  246. mem_size -= TLB_1MB_SIZE;
  247. phys_addr += TLB_1MB_SIZE;
  248. virt_addr += TLB_1MB_SIZE;
  249. }
  250. } else if (((phys_addr & TLB_256KB_ALIGN_MASK) == phys_addr) &&
  251. (mem_size >= TLB_256KB_SIZE)) {
  252. /* Add a 256KB TLB entry */
  253. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  254. TLB_WORD0_SIZE_256KB, tlb_i)) == 0) {
  255. mem_size -= TLB_256KB_SIZE;
  256. phys_addr += TLB_256KB_SIZE;
  257. virt_addr += TLB_256KB_SIZE;
  258. }
  259. } else if (((phys_addr & TLB_64KB_ALIGN_MASK) == phys_addr) &&
  260. (mem_size >= TLB_64KB_SIZE)) {
  261. /* Add a 64KB TLB entry */
  262. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  263. TLB_WORD0_SIZE_64KB, tlb_i)) == 0) {
  264. mem_size -= TLB_64KB_SIZE;
  265. phys_addr += TLB_64KB_SIZE;
  266. virt_addr += TLB_64KB_SIZE;
  267. }
  268. } else if (((phys_addr & TLB_16KB_ALIGN_MASK) == phys_addr) &&
  269. (mem_size >= TLB_16KB_SIZE)) {
  270. /* Add a 16KB TLB entry */
  271. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  272. TLB_WORD0_SIZE_16KB, tlb_i)) == 0) {
  273. mem_size -= TLB_16KB_SIZE;
  274. phys_addr += TLB_16KB_SIZE;
  275. virt_addr += TLB_16KB_SIZE;
  276. }
  277. } else if (((phys_addr & TLB_4KB_ALIGN_MASK) == phys_addr) &&
  278. (mem_size >= TLB_4KB_SIZE)) {
  279. /* Add a 4KB TLB entry */
  280. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  281. TLB_WORD0_SIZE_4KB, tlb_i)) == 0) {
  282. mem_size -= TLB_4KB_SIZE;
  283. phys_addr += TLB_4KB_SIZE;
  284. virt_addr += TLB_4KB_SIZE;
  285. }
  286. } else if (((phys_addr & TLB_1KB_ALIGN_MASK) == phys_addr) &&
  287. (mem_size >= TLB_1KB_SIZE)) {
  288. /* Add a 1KB TLB entry */
  289. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  290. TLB_WORD0_SIZE_1KB, tlb_i)) == 0) {
  291. mem_size -= TLB_1KB_SIZE;
  292. phys_addr += TLB_1KB_SIZE;
  293. virt_addr += TLB_1KB_SIZE;
  294. }
  295. } else {
  296. printf("ERROR: no TLB size exists for the base address 0x%llx.\n",
  297. phys_addr);
  298. }
  299. if (rc != 0)
  300. printf("ERROR: no TLB entries available for the base addr 0x%llx.\n",
  301. phys_addr);
  302. }
  303. return;
  304. }
  305. /*
  306. * Program one (or multiple) TLB entries for one memory region
  307. *
  308. * Common usage for boards with SDRAM DIMM modules to dynamically
  309. * configure the TLB's for the SDRAM
  310. */
  311. void program_tlb(u64 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value)
  312. {
  313. region_t region_array;
  314. region_array.base = phys_addr;
  315. region_array.size = size;
  316. region_array.tlb_word2_i_value = tlb_word2_i_value; /* en-/disable cache */
  317. /* Call the routine to add in the tlb entries for the memory regions */
  318. program_tlb_addr(region_array.base, virt_addr, region_array.size,
  319. region_array.tlb_word2_i_value);
  320. return;
  321. }
  322. #endif /* CONFIG_440 */