tlb.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. /*
  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(u64 phys_addr,
  171. u32 virt_addr,
  172. u32 tlb_word0_size_value,
  173. u32 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((u32)phys_addr) |
  191. TLB_WORD1_ERPN_ENCODE(phys_addr >> 32);
  192. tlb_word2_value = TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE |
  193. TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE |
  194. TLB_WORD2_W_DISABLE | tlb_word2_i_value |
  195. TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE |
  196. TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE |
  197. TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE |
  198. TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE |
  199. TLB_WORD2_SR_ENABLE;
  200. /* Wait for all memory accesses to complete */
  201. sync();
  202. /* Third, add the TLB entries */
  203. mttlb1(i, tlb_word0_value);
  204. mttlb2(i, tlb_word1_value);
  205. mttlb3(i, tlb_word2_value);
  206. /* Execute an ISYNC instruction so that the new TLB entry takes effect */
  207. asm("isync");
  208. return 0;
  209. }
  210. static void program_tlb_addr(u64 phys_addr,
  211. u32 virt_addr,
  212. u32 mem_size,
  213. u32 tlb_word2_i_value)
  214. {
  215. int rc;
  216. int tlb_i;
  217. tlb_i = tlb_word2_i_value;
  218. while (mem_size != 0) {
  219. rc = 0;
  220. /* Add the TLB entries in to map the region. */
  221. if (((phys_addr & TLB_256MB_ALIGN_MASK) == phys_addr) &&
  222. (mem_size >= TLB_256MB_SIZE)) {
  223. /* Add a 256MB TLB entry */
  224. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  225. TLB_WORD0_SIZE_256MB, tlb_i)) == 0) {
  226. mem_size -= TLB_256MB_SIZE;
  227. phys_addr += TLB_256MB_SIZE;
  228. virt_addr += TLB_256MB_SIZE;
  229. }
  230. } else if (((phys_addr & TLB_16MB_ALIGN_MASK) == phys_addr) &&
  231. (mem_size >= TLB_16MB_SIZE)) {
  232. /* Add a 16MB TLB entry */
  233. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  234. TLB_WORD0_SIZE_16MB, tlb_i)) == 0) {
  235. mem_size -= TLB_16MB_SIZE;
  236. phys_addr += TLB_16MB_SIZE;
  237. virt_addr += TLB_16MB_SIZE;
  238. }
  239. } else if (((phys_addr & TLB_1MB_ALIGN_MASK) == phys_addr) &&
  240. (mem_size >= TLB_1MB_SIZE)) {
  241. /* Add a 1MB TLB entry */
  242. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  243. TLB_WORD0_SIZE_1MB, tlb_i)) == 0) {
  244. mem_size -= TLB_1MB_SIZE;
  245. phys_addr += TLB_1MB_SIZE;
  246. virt_addr += TLB_1MB_SIZE;
  247. }
  248. } else if (((phys_addr & TLB_256KB_ALIGN_MASK) == phys_addr) &&
  249. (mem_size >= TLB_256KB_SIZE)) {
  250. /* Add a 256KB TLB entry */
  251. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  252. TLB_WORD0_SIZE_256KB, tlb_i)) == 0) {
  253. mem_size -= TLB_256KB_SIZE;
  254. phys_addr += TLB_256KB_SIZE;
  255. virt_addr += TLB_256KB_SIZE;
  256. }
  257. } else if (((phys_addr & TLB_64KB_ALIGN_MASK) == phys_addr) &&
  258. (mem_size >= TLB_64KB_SIZE)) {
  259. /* Add a 64KB TLB entry */
  260. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  261. TLB_WORD0_SIZE_64KB, tlb_i)) == 0) {
  262. mem_size -= TLB_64KB_SIZE;
  263. phys_addr += TLB_64KB_SIZE;
  264. virt_addr += TLB_64KB_SIZE;
  265. }
  266. } else if (((phys_addr & TLB_16KB_ALIGN_MASK) == phys_addr) &&
  267. (mem_size >= TLB_16KB_SIZE)) {
  268. /* Add a 16KB TLB entry */
  269. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  270. TLB_WORD0_SIZE_16KB, tlb_i)) == 0) {
  271. mem_size -= TLB_16KB_SIZE;
  272. phys_addr += TLB_16KB_SIZE;
  273. virt_addr += TLB_16KB_SIZE;
  274. }
  275. } else if (((phys_addr & TLB_4KB_ALIGN_MASK) == phys_addr) &&
  276. (mem_size >= TLB_4KB_SIZE)) {
  277. /* Add a 4KB TLB entry */
  278. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  279. TLB_WORD0_SIZE_4KB, tlb_i)) == 0) {
  280. mem_size -= TLB_4KB_SIZE;
  281. phys_addr += TLB_4KB_SIZE;
  282. virt_addr += TLB_4KB_SIZE;
  283. }
  284. } else if (((phys_addr & TLB_1KB_ALIGN_MASK) == phys_addr) &&
  285. (mem_size >= TLB_1KB_SIZE)) {
  286. /* Add a 1KB TLB entry */
  287. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  288. TLB_WORD0_SIZE_1KB, tlb_i)) == 0) {
  289. mem_size -= TLB_1KB_SIZE;
  290. phys_addr += TLB_1KB_SIZE;
  291. virt_addr += TLB_1KB_SIZE;
  292. }
  293. } else {
  294. printf("ERROR: no TLB size exists for the base address 0x%0X.\n",
  295. phys_addr);
  296. }
  297. if (rc != 0)
  298. printf("ERROR: no TLB entries available for the base addr 0x%0X.\n",
  299. phys_addr);
  300. }
  301. return;
  302. }
  303. /*
  304. * Program one (or multiple) TLB entries for one memory region
  305. *
  306. * Common usage for boards with SDRAM DIMM modules to dynamically
  307. * configure the TLB's for the SDRAM
  308. */
  309. void program_tlb(u64 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value)
  310. {
  311. region_t region_array;
  312. region_array.base = phys_addr;
  313. region_array.size = size;
  314. region_array.tlb_word2_i_value = tlb_word2_i_value; /* en-/disable cache */
  315. /* Call the routine to add in the tlb entries for the memory regions */
  316. program_tlb_addr(region_array.base, virt_addr, region_array.size,
  317. region_array.tlb_word2_i_value);
  318. return;
  319. }
  320. #endif /* CONFIG_440 */