tlb.c 8.9 KB

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