tlb.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Nios2 TLB handling
  3. *
  4. * Copyright (C) 2009, Wind River Systems Inc
  5. * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <linux/pagemap.h>
  15. #include <asm/tlb.h>
  16. #include <asm/mmu_context.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/cpuinfo.h>
  19. #define TLB_INDEX_MASK \
  20. ((((1UL << (cpuinfo.tlb_ptr_sz - cpuinfo.tlb_num_ways_log2))) - 1) \
  21. << PAGE_SHIFT)
  22. /* Used as illegal PHYS_ADDR for TLB mappings
  23. */
  24. #define MAX_PHYS_ADDR 0
  25. static void get_misc_and_pid(unsigned long *misc, unsigned long *pid)
  26. {
  27. *misc = RDCTL(CTL_TLBMISC);
  28. *misc &= (TLBMISC_PID | TLBMISC_WAY);
  29. *pid = *misc & TLBMISC_PID;
  30. }
  31. /*
  32. * All entries common to a mm share an asid. To effectively flush these
  33. * entries, we just bump the asid.
  34. */
  35. void flush_tlb_mm(struct mm_struct *mm)
  36. {
  37. if (current->mm == mm)
  38. flush_tlb_all();
  39. else
  40. memset(&mm->context, 0, sizeof(mm_context_t));
  41. }
  42. /*
  43. * This one is only used for pages with the global bit set so we don't care
  44. * much about the ASID.
  45. */
  46. void flush_tlb_one_pid(unsigned long addr, unsigned long mmu_pid)
  47. {
  48. unsigned int way;
  49. unsigned long org_misc, pid_misc;
  50. pr_debug("Flush tlb-entry for vaddr=%#lx\n", addr);
  51. /* remember pid/way until we return. */
  52. get_misc_and_pid(&org_misc, &pid_misc);
  53. WRCTL(CTL_PTEADDR, (addr >> PAGE_SHIFT) << 2);
  54. for (way = 0; way < cpuinfo.tlb_num_ways; way++) {
  55. unsigned long pteaddr;
  56. unsigned long tlbmisc;
  57. unsigned long pid;
  58. tlbmisc = pid_misc | TLBMISC_RD | (way << TLBMISC_WAY_SHIFT);
  59. WRCTL(CTL_TLBMISC, tlbmisc);
  60. pteaddr = RDCTL(CTL_PTEADDR);
  61. tlbmisc = RDCTL(CTL_TLBMISC);
  62. pid = (tlbmisc >> TLBMISC_PID_SHIFT) & TLBMISC_PID_MASK;
  63. if (((((pteaddr >> 2) & 0xfffff)) == (addr >> PAGE_SHIFT)) &&
  64. pid == mmu_pid) {
  65. unsigned long vaddr = CONFIG_NIOS2_IO_REGION_BASE +
  66. ((PAGE_SIZE * cpuinfo.tlb_num_lines) * way) +
  67. (addr & TLB_INDEX_MASK);
  68. pr_debug("Flush entry by writing %#lx way=%dl pid=%ld\n",
  69. vaddr, way, (pid_misc >> TLBMISC_PID_SHIFT));
  70. WRCTL(CTL_PTEADDR, (vaddr >> 12) << 2);
  71. tlbmisc = pid_misc | TLBMISC_WE |
  72. (way << TLBMISC_WAY_SHIFT);
  73. WRCTL(CTL_TLBMISC, tlbmisc);
  74. WRCTL(CTL_TLBACC, (MAX_PHYS_ADDR >> PAGE_SHIFT));
  75. }
  76. }
  77. WRCTL(CTL_TLBMISC, org_misc);
  78. }
  79. void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  80. unsigned long end)
  81. {
  82. unsigned long mmu_pid = get_pid_from_context(&vma->vm_mm->context);
  83. while (start < end) {
  84. flush_tlb_one_pid(start, mmu_pid);
  85. start += PAGE_SIZE;
  86. }
  87. }
  88. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  89. {
  90. while (start < end) {
  91. flush_tlb_one(start);
  92. start += PAGE_SIZE;
  93. }
  94. }
  95. /*
  96. * This one is only used for pages with the global bit set so we don't care
  97. * much about the ASID.
  98. */
  99. void flush_tlb_one(unsigned long addr)
  100. {
  101. unsigned int way;
  102. unsigned long org_misc, pid_misc;
  103. pr_debug("Flush tlb-entry for vaddr=%#lx\n", addr);
  104. /* remember pid/way until we return. */
  105. get_misc_and_pid(&org_misc, &pid_misc);
  106. WRCTL(CTL_PTEADDR, (addr >> PAGE_SHIFT) << 2);
  107. for (way = 0; way < cpuinfo.tlb_num_ways; way++) {
  108. unsigned long pteaddr;
  109. unsigned long tlbmisc;
  110. tlbmisc = pid_misc | TLBMISC_RD | (way << TLBMISC_WAY_SHIFT);
  111. WRCTL(CTL_TLBMISC, tlbmisc);
  112. pteaddr = RDCTL(CTL_PTEADDR);
  113. tlbmisc = RDCTL(CTL_TLBMISC);
  114. if ((((pteaddr >> 2) & 0xfffff)) == (addr >> PAGE_SHIFT)) {
  115. unsigned long vaddr = CONFIG_NIOS2_IO_REGION_BASE +
  116. ((PAGE_SIZE * cpuinfo.tlb_num_lines) * way) +
  117. (addr & TLB_INDEX_MASK);
  118. pr_debug("Flush entry by writing %#lx way=%dl pid=%ld\n",
  119. vaddr, way, (pid_misc >> TLBMISC_PID_SHIFT));
  120. tlbmisc = pid_misc | TLBMISC_WE |
  121. (way << TLBMISC_WAY_SHIFT);
  122. WRCTL(CTL_PTEADDR, (vaddr >> 12) << 2);
  123. WRCTL(CTL_TLBMISC, tlbmisc);
  124. WRCTL(CTL_TLBACC, (MAX_PHYS_ADDR >> PAGE_SHIFT));
  125. }
  126. }
  127. WRCTL(CTL_TLBMISC, org_misc);
  128. }
  129. void dump_tlb_line(unsigned long line)
  130. {
  131. unsigned int way;
  132. unsigned long org_misc;
  133. pr_debug("dump tlb-entries for line=%#lx (addr %08lx)\n", line,
  134. line << (PAGE_SHIFT + cpuinfo.tlb_num_ways_log2));
  135. /* remember pid/way until we return */
  136. org_misc = (RDCTL(CTL_TLBMISC) & (TLBMISC_PID | TLBMISC_WAY));
  137. WRCTL(CTL_PTEADDR, line << 2);
  138. for (way = 0; way < cpuinfo.tlb_num_ways; way++) {
  139. unsigned long pteaddr;
  140. unsigned long tlbmisc;
  141. unsigned long tlbacc;
  142. WRCTL(CTL_TLBMISC, TLBMISC_RD | (way << TLBMISC_WAY_SHIFT));
  143. pteaddr = RDCTL(CTL_PTEADDR);
  144. tlbmisc = RDCTL(CTL_TLBMISC);
  145. tlbacc = RDCTL(CTL_TLBACC);
  146. if ((tlbacc << PAGE_SHIFT) != (MAX_PHYS_ADDR & PAGE_MASK)) {
  147. pr_debug("-- way:%02x vpn:0x%08lx phys:0x%08lx pid:0x%02lx flags:%c%c%c%c%c\n",
  148. way,
  149. (pteaddr << (PAGE_SHIFT-2)),
  150. (tlbacc << PAGE_SHIFT),
  151. ((tlbmisc >> TLBMISC_PID_SHIFT) &
  152. TLBMISC_PID_MASK),
  153. (tlbacc & _PAGE_READ ? 'r' : '-'),
  154. (tlbacc & _PAGE_WRITE ? 'w' : '-'),
  155. (tlbacc & _PAGE_EXEC ? 'x' : '-'),
  156. (tlbacc & _PAGE_GLOBAL ? 'g' : '-'),
  157. (tlbacc & _PAGE_CACHED ? 'c' : '-'));
  158. }
  159. }
  160. WRCTL(CTL_TLBMISC, org_misc);
  161. }
  162. void dump_tlb(void)
  163. {
  164. unsigned int i;
  165. for (i = 0; i < cpuinfo.tlb_num_lines; i++)
  166. dump_tlb_line(i);
  167. }
  168. void flush_tlb_pid(unsigned long pid)
  169. {
  170. unsigned int line;
  171. unsigned int way;
  172. unsigned long org_misc, pid_misc;
  173. /* remember pid/way until we return */
  174. get_misc_and_pid(&org_misc, &pid_misc);
  175. for (line = 0; line < cpuinfo.tlb_num_lines; line++) {
  176. WRCTL(CTL_PTEADDR, line << 2);
  177. for (way = 0; way < cpuinfo.tlb_num_ways; way++) {
  178. unsigned long pteaddr;
  179. unsigned long tlbmisc;
  180. unsigned long tlbacc;
  181. tlbmisc = pid_misc | TLBMISC_RD |
  182. (way << TLBMISC_WAY_SHIFT);
  183. WRCTL(CTL_TLBMISC, tlbmisc);
  184. pteaddr = RDCTL(CTL_PTEADDR);
  185. tlbmisc = RDCTL(CTL_TLBMISC);
  186. tlbacc = RDCTL(CTL_TLBACC);
  187. if (((tlbmisc>>TLBMISC_PID_SHIFT) & TLBMISC_PID_MASK)
  188. == pid) {
  189. tlbmisc = pid_misc | TLBMISC_WE |
  190. (way << TLBMISC_WAY_SHIFT);
  191. WRCTL(CTL_TLBMISC, tlbmisc);
  192. WRCTL(CTL_TLBACC,
  193. (MAX_PHYS_ADDR >> PAGE_SHIFT));
  194. }
  195. }
  196. WRCTL(CTL_TLBMISC, org_misc);
  197. }
  198. }
  199. void flush_tlb_all(void)
  200. {
  201. int i;
  202. unsigned long vaddr = CONFIG_NIOS2_IO_REGION_BASE;
  203. unsigned int way;
  204. unsigned long org_misc, pid_misc, tlbmisc;
  205. /* remember pid/way until we return */
  206. get_misc_and_pid(&org_misc, &pid_misc);
  207. pid_misc |= TLBMISC_WE;
  208. /* Map each TLB entry to physcal address 0 with no-access and a
  209. bad ptbase */
  210. for (way = 0; way < cpuinfo.tlb_num_ways; way++) {
  211. tlbmisc = pid_misc | (way << TLBMISC_WAY_SHIFT);
  212. for (i = 0; i < cpuinfo.tlb_num_lines; i++) {
  213. WRCTL(CTL_PTEADDR, ((vaddr) >> PAGE_SHIFT) << 2);
  214. WRCTL(CTL_TLBMISC, tlbmisc);
  215. WRCTL(CTL_TLBACC, (MAX_PHYS_ADDR >> PAGE_SHIFT));
  216. vaddr += 1UL << 12;
  217. }
  218. }
  219. /* restore pid/way */
  220. WRCTL(CTL_TLBMISC, org_misc);
  221. }
  222. void set_mmu_pid(unsigned long pid)
  223. {
  224. WRCTL(CTL_TLBMISC, (RDCTL(CTL_TLBMISC) & TLBMISC_WAY) |
  225. ((pid & TLBMISC_PID_MASK) << TLBMISC_PID_SHIFT));
  226. }