tlb.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * AVR32 TLB operations
  3. *
  4. * Copyright (C) 2004-2006 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/mm.h>
  11. #include <asm/mmu_context.h>
  12. #define _TLBEHI_I 0x100
  13. void show_dtlb_entry(unsigned int index)
  14. {
  15. unsigned int tlbehi, tlbehi_save, tlbelo, mmucr, mmucr_save, flags;
  16. local_irq_save(flags);
  17. mmucr_save = sysreg_read(MMUCR);
  18. tlbehi_save = sysreg_read(TLBEHI);
  19. mmucr = mmucr_save & 0x13;
  20. mmucr |= index << 14;
  21. sysreg_write(MMUCR, mmucr);
  22. asm volatile("tlbr" : : : "memory");
  23. cpu_sync_pipeline();
  24. tlbehi = sysreg_read(TLBEHI);
  25. tlbelo = sysreg_read(TLBELO);
  26. printk("%2u: %c %c %02x %05x %05x %o %o %c %c %c %c\n",
  27. index,
  28. (tlbehi & 0x200)?'1':'0',
  29. (tlbelo & 0x100)?'1':'0',
  30. (tlbehi & 0xff),
  31. (tlbehi >> 12), (tlbelo >> 12),
  32. (tlbelo >> 4) & 7, (tlbelo >> 2) & 3,
  33. (tlbelo & 0x200)?'1':'0',
  34. (tlbelo & 0x080)?'1':'0',
  35. (tlbelo & 0x001)?'1':'0',
  36. (tlbelo & 0x002)?'1':'0');
  37. sysreg_write(MMUCR, mmucr_save);
  38. sysreg_write(TLBEHI, tlbehi_save);
  39. cpu_sync_pipeline();
  40. local_irq_restore(flags);
  41. }
  42. void dump_dtlb(void)
  43. {
  44. unsigned int i;
  45. printk("ID V G ASID VPN PFN AP SZ C B W D\n");
  46. for (i = 0; i < 32; i++)
  47. show_dtlb_entry(i);
  48. }
  49. static unsigned long last_mmucr;
  50. static inline void set_replacement_pointer(unsigned shift)
  51. {
  52. unsigned long mmucr, mmucr_save;
  53. mmucr = mmucr_save = sysreg_read(MMUCR);
  54. /* Does this mapping already exist? */
  55. __asm__ __volatile__(
  56. " tlbs\n"
  57. " mfsr %0, %1"
  58. : "=r"(mmucr)
  59. : "i"(SYSREG_MMUCR));
  60. if (mmucr & SYSREG_BIT(MMUCR_N)) {
  61. /* Not found -- pick a not-recently-accessed entry */
  62. unsigned long rp;
  63. unsigned long tlbar = sysreg_read(TLBARLO);
  64. rp = 32 - fls(tlbar);
  65. if (rp == 32) {
  66. rp = 0;
  67. sysreg_write(TLBARLO, -1L);
  68. }
  69. mmucr &= 0x13;
  70. mmucr |= (rp << shift);
  71. sysreg_write(MMUCR, mmucr);
  72. }
  73. last_mmucr = mmucr;
  74. }
  75. static void update_dtlb(unsigned long address, pte_t pte, unsigned long asid)
  76. {
  77. unsigned long vpn;
  78. vpn = (address & MMU_VPN_MASK) | _TLBEHI_VALID | asid;
  79. sysreg_write(TLBEHI, vpn);
  80. cpu_sync_pipeline();
  81. set_replacement_pointer(14);
  82. sysreg_write(TLBELO, pte_val(pte) & _PAGE_FLAGS_HARDWARE_MASK);
  83. /* Let's go */
  84. asm volatile("nop\n\ttlbw" : : : "memory");
  85. cpu_sync_pipeline();
  86. }
  87. void update_mmu_cache(struct vm_area_struct *vma,
  88. unsigned long address, pte_t pte)
  89. {
  90. unsigned long flags;
  91. /* ptrace may call this routine */
  92. if (vma && current->active_mm != vma->vm_mm)
  93. return;
  94. local_irq_save(flags);
  95. update_dtlb(address, pte, get_asid());
  96. local_irq_restore(flags);
  97. }
  98. void __flush_tlb_page(unsigned long asid, unsigned long page)
  99. {
  100. unsigned long mmucr, tlbehi;
  101. page |= asid;
  102. sysreg_write(TLBEHI, page);
  103. cpu_sync_pipeline();
  104. asm volatile("tlbs");
  105. mmucr = sysreg_read(MMUCR);
  106. if (!(mmucr & SYSREG_BIT(MMUCR_N))) {
  107. unsigned long tlbarlo;
  108. unsigned long entry;
  109. /* Clear the "valid" bit */
  110. tlbehi = sysreg_read(TLBEHI);
  111. tlbehi &= ~_TLBEHI_VALID;
  112. sysreg_write(TLBEHI, tlbehi);
  113. cpu_sync_pipeline();
  114. /* mark the entry as "not accessed" */
  115. entry = (mmucr >> 14) & 0x3f;
  116. tlbarlo = sysreg_read(TLBARLO);
  117. tlbarlo |= (0x80000000 >> entry);
  118. sysreg_write(TLBARLO, tlbarlo);
  119. /* update the entry with valid bit clear */
  120. asm volatile("tlbw");
  121. cpu_sync_pipeline();
  122. }
  123. }
  124. void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  125. {
  126. if (vma->vm_mm && vma->vm_mm->context != NO_CONTEXT) {
  127. unsigned long flags, asid;
  128. unsigned long saved_asid = MMU_NO_ASID;
  129. asid = vma->vm_mm->context & MMU_CONTEXT_ASID_MASK;
  130. page &= PAGE_MASK;
  131. local_irq_save(flags);
  132. if (vma->vm_mm != current->mm) {
  133. saved_asid = get_asid();
  134. set_asid(asid);
  135. }
  136. __flush_tlb_page(asid, page);
  137. if (saved_asid != MMU_NO_ASID)
  138. set_asid(saved_asid);
  139. local_irq_restore(flags);
  140. }
  141. }
  142. void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  143. unsigned long end)
  144. {
  145. struct mm_struct *mm = vma->vm_mm;
  146. if (mm->context != NO_CONTEXT) {
  147. unsigned long flags;
  148. int size;
  149. local_irq_save(flags);
  150. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  151. if (size > (MMU_DTLB_ENTRIES / 4)) { /* Too many entries to flush */
  152. mm->context = NO_CONTEXT;
  153. if (mm == current->mm)
  154. activate_context(mm);
  155. } else {
  156. unsigned long asid = mm->context & MMU_CONTEXT_ASID_MASK;
  157. unsigned long saved_asid = MMU_NO_ASID;
  158. start &= PAGE_MASK;
  159. end += (PAGE_SIZE - 1);
  160. end &= PAGE_MASK;
  161. if (mm != current->mm) {
  162. saved_asid = get_asid();
  163. set_asid(asid);
  164. }
  165. while (start < end) {
  166. __flush_tlb_page(asid, start);
  167. start += PAGE_SIZE;
  168. }
  169. if (saved_asid != MMU_NO_ASID)
  170. set_asid(saved_asid);
  171. }
  172. local_irq_restore(flags);
  173. }
  174. }
  175. /*
  176. * TODO: If this is only called for addresses > TASK_SIZE, we can probably
  177. * skip the ASID stuff and just use the Global bit...
  178. */
  179. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  180. {
  181. unsigned long flags;
  182. int size;
  183. local_irq_save(flags);
  184. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  185. if (size > (MMU_DTLB_ENTRIES / 4)) { /* Too many entries to flush */
  186. flush_tlb_all();
  187. } else {
  188. unsigned long asid = init_mm.context & MMU_CONTEXT_ASID_MASK;
  189. unsigned long saved_asid = get_asid();
  190. start &= PAGE_MASK;
  191. end += (PAGE_SIZE - 1);
  192. end &= PAGE_MASK;
  193. set_asid(asid);
  194. while (start < end) {
  195. __flush_tlb_page(asid, start);
  196. start += PAGE_SIZE;
  197. }
  198. set_asid(saved_asid);
  199. }
  200. local_irq_restore(flags);
  201. }
  202. void flush_tlb_mm(struct mm_struct *mm)
  203. {
  204. /* Invalidate all TLB entries of this process by getting a new ASID */
  205. if (mm->context != NO_CONTEXT) {
  206. unsigned long flags;
  207. local_irq_save(flags);
  208. mm->context = NO_CONTEXT;
  209. if (mm == current->mm)
  210. activate_context(mm);
  211. local_irq_restore(flags);
  212. }
  213. }
  214. void flush_tlb_all(void)
  215. {
  216. unsigned long flags;
  217. local_irq_save(flags);
  218. sysreg_write(MMUCR, sysreg_read(MMUCR) | SYSREG_BIT(MMUCR_I));
  219. local_irq_restore(flags);
  220. }
  221. #ifdef CONFIG_PROC_FS
  222. #include <linux/seq_file.h>
  223. #include <linux/proc_fs.h>
  224. #include <linux/init.h>
  225. static void *tlb_start(struct seq_file *tlb, loff_t *pos)
  226. {
  227. static unsigned long tlb_index;
  228. if (*pos >= 32)
  229. return NULL;
  230. tlb_index = 0;
  231. return &tlb_index;
  232. }
  233. static void *tlb_next(struct seq_file *tlb, void *v, loff_t *pos)
  234. {
  235. unsigned long *index = v;
  236. if (*index >= 31)
  237. return NULL;
  238. ++*pos;
  239. ++*index;
  240. return index;
  241. }
  242. static void tlb_stop(struct seq_file *tlb, void *v)
  243. {
  244. }
  245. static int tlb_show(struct seq_file *tlb, void *v)
  246. {
  247. unsigned int tlbehi, tlbehi_save, tlbelo, mmucr, mmucr_save, flags;
  248. unsigned long *index = v;
  249. if (*index == 0)
  250. seq_puts(tlb, "ID V G ASID VPN PFN AP SZ C B W D\n");
  251. BUG_ON(*index >= 32);
  252. local_irq_save(flags);
  253. mmucr_save = sysreg_read(MMUCR);
  254. tlbehi_save = sysreg_read(TLBEHI);
  255. mmucr = mmucr_save & 0x13;
  256. mmucr |= *index << 14;
  257. sysreg_write(MMUCR, mmucr);
  258. asm volatile("tlbr" : : : "memory");
  259. cpu_sync_pipeline();
  260. tlbehi = sysreg_read(TLBEHI);
  261. tlbelo = sysreg_read(TLBELO);
  262. sysreg_write(MMUCR, mmucr_save);
  263. sysreg_write(TLBEHI, tlbehi_save);
  264. cpu_sync_pipeline();
  265. local_irq_restore(flags);
  266. seq_printf(tlb, "%2lu: %c %c %02x %05x %05x %o %o %c %c %c %c\n",
  267. *index,
  268. (tlbehi & 0x200)?'1':'0',
  269. (tlbelo & 0x100)?'1':'0',
  270. (tlbehi & 0xff),
  271. (tlbehi >> 12), (tlbelo >> 12),
  272. (tlbelo >> 4) & 7, (tlbelo >> 2) & 3,
  273. (tlbelo & 0x200)?'1':'0',
  274. (tlbelo & 0x080)?'1':'0',
  275. (tlbelo & 0x001)?'1':'0',
  276. (tlbelo & 0x002)?'1':'0');
  277. return 0;
  278. }
  279. static struct seq_operations tlb_ops = {
  280. .start = tlb_start,
  281. .next = tlb_next,
  282. .stop = tlb_stop,
  283. .show = tlb_show,
  284. };
  285. static int tlb_open(struct inode *inode, struct file *file)
  286. {
  287. return seq_open(file, &tlb_ops);
  288. }
  289. static struct file_operations proc_tlb_operations = {
  290. .open = tlb_open,
  291. .read = seq_read,
  292. .llseek = seq_lseek,
  293. .release = seq_release,
  294. };
  295. static int __init proctlb_init(void)
  296. {
  297. struct proc_dir_entry *entry;
  298. entry = create_proc_entry("tlb", 0, NULL);
  299. if (entry)
  300. entry->proc_fops = &proc_tlb_operations;
  301. return 0;
  302. }
  303. late_initcall(proctlb_init);
  304. #endif /* CONFIG_PROC_FS */