leon_mm.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * linux/arch/sparc/mm/leon_m.c
  3. *
  4. * Copyright (C) 2004 Konrad Eisele (eiselekd@web.de, konrad@gaisler.com) Gaisler Research
  5. * Copyright (C) 2009 Daniel Hellstrom (daniel@gaisler.com) Aeroflex Gaisler AB
  6. * Copyright (C) 2009 Konrad Eisele (konrad@gaisler.com) Aeroflex Gaisler AB
  7. *
  8. * do srmmu probe in software
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <asm/asi.h>
  14. #include <asm/leon.h>
  15. #include <asm/tlbflush.h>
  16. int leon_flush_during_switch = 1;
  17. int srmmu_swprobe_trace;
  18. unsigned long srmmu_swprobe(unsigned long vaddr, unsigned long *paddr)
  19. {
  20. unsigned int ctxtbl;
  21. unsigned int pgd, pmd, ped;
  22. unsigned int ptr;
  23. unsigned int lvl, pte, paddrbase;
  24. unsigned int ctx;
  25. unsigned int paddr_calc;
  26. paddrbase = 0;
  27. if (srmmu_swprobe_trace)
  28. printk(KERN_INFO "swprobe: trace on\n");
  29. ctxtbl = srmmu_get_ctable_ptr();
  30. if (!(ctxtbl)) {
  31. if (srmmu_swprobe_trace)
  32. printk(KERN_INFO "swprobe: srmmu_get_ctable_ptr returned 0=>0\n");
  33. return 0;
  34. }
  35. if (!_pfn_valid(PFN(ctxtbl))) {
  36. if (srmmu_swprobe_trace)
  37. printk(KERN_INFO
  38. "swprobe: !_pfn_valid(%x)=>0\n",
  39. PFN(ctxtbl));
  40. return 0;
  41. }
  42. ctx = srmmu_get_context();
  43. if (srmmu_swprobe_trace)
  44. printk(KERN_INFO "swprobe: --- ctx (%x) ---\n", ctx);
  45. pgd = LEON_BYPASS_LOAD_PA(ctxtbl + (ctx * 4));
  46. if (((pgd & SRMMU_ET_MASK) == SRMMU_ET_PTE)) {
  47. if (srmmu_swprobe_trace)
  48. printk(KERN_INFO "swprobe: pgd is entry level 3\n");
  49. lvl = 3;
  50. pte = pgd;
  51. paddrbase = pgd & _SRMMU_PTE_PMASK_LEON;
  52. goto ready;
  53. }
  54. if (((pgd & SRMMU_ET_MASK) != SRMMU_ET_PTD)) {
  55. if (srmmu_swprobe_trace)
  56. printk(KERN_INFO "swprobe: pgd is invalid => 0\n");
  57. return 0;
  58. }
  59. if (srmmu_swprobe_trace)
  60. printk(KERN_INFO "swprobe: --- pgd (%x) ---\n", pgd);
  61. ptr = (pgd & SRMMU_PTD_PMASK) << 4;
  62. ptr += ((((vaddr) >> LEON_PGD_SH) & LEON_PGD_M) * 4);
  63. if (!_pfn_valid(PFN(ptr)))
  64. return 0;
  65. pmd = LEON_BYPASS_LOAD_PA(ptr);
  66. if (((pmd & SRMMU_ET_MASK) == SRMMU_ET_PTE)) {
  67. if (srmmu_swprobe_trace)
  68. printk(KERN_INFO "swprobe: pmd is entry level 2\n");
  69. lvl = 2;
  70. pte = pmd;
  71. paddrbase = pmd & _SRMMU_PTE_PMASK_LEON;
  72. goto ready;
  73. }
  74. if (((pmd & SRMMU_ET_MASK) != SRMMU_ET_PTD)) {
  75. if (srmmu_swprobe_trace)
  76. printk(KERN_INFO "swprobe: pmd is invalid => 0\n");
  77. return 0;
  78. }
  79. if (srmmu_swprobe_trace)
  80. printk(KERN_INFO "swprobe: --- pmd (%x) ---\n", pmd);
  81. ptr = (pmd & SRMMU_PTD_PMASK) << 4;
  82. ptr += (((vaddr >> LEON_PMD_SH) & LEON_PMD_M) * 4);
  83. if (!_pfn_valid(PFN(ptr))) {
  84. if (srmmu_swprobe_trace)
  85. printk(KERN_INFO "swprobe: !_pfn_valid(%x)=>0\n",
  86. PFN(ptr));
  87. return 0;
  88. }
  89. ped = LEON_BYPASS_LOAD_PA(ptr);
  90. if (((ped & SRMMU_ET_MASK) == SRMMU_ET_PTE)) {
  91. if (srmmu_swprobe_trace)
  92. printk(KERN_INFO "swprobe: ped is entry level 1\n");
  93. lvl = 1;
  94. pte = ped;
  95. paddrbase = ped & _SRMMU_PTE_PMASK_LEON;
  96. goto ready;
  97. }
  98. if (((ped & SRMMU_ET_MASK) != SRMMU_ET_PTD)) {
  99. if (srmmu_swprobe_trace)
  100. printk(KERN_INFO "swprobe: ped is invalid => 0\n");
  101. return 0;
  102. }
  103. if (srmmu_swprobe_trace)
  104. printk(KERN_INFO "swprobe: --- ped (%x) ---\n", ped);
  105. ptr = (ped & SRMMU_PTD_PMASK) << 4;
  106. ptr += (((vaddr >> LEON_PTE_SH) & LEON_PTE_M) * 4);
  107. if (!_pfn_valid(PFN(ptr)))
  108. return 0;
  109. ptr = LEON_BYPASS_LOAD_PA(ptr);
  110. if (((ptr & SRMMU_ET_MASK) == SRMMU_ET_PTE)) {
  111. if (srmmu_swprobe_trace)
  112. printk(KERN_INFO "swprobe: ptr is entry level 0\n");
  113. lvl = 0;
  114. pte = ptr;
  115. paddrbase = ptr & _SRMMU_PTE_PMASK_LEON;
  116. goto ready;
  117. }
  118. if (srmmu_swprobe_trace)
  119. printk(KERN_INFO "swprobe: ptr is invalid => 0\n");
  120. return 0;
  121. ready:
  122. switch (lvl) {
  123. case 0:
  124. paddr_calc =
  125. (vaddr & ~(-1 << LEON_PTE_SH)) | ((pte & ~0xff) << 4);
  126. break;
  127. case 1:
  128. paddr_calc =
  129. (vaddr & ~(-1 << LEON_PMD_SH)) | ((pte & ~0xff) << 4);
  130. break;
  131. case 2:
  132. paddr_calc =
  133. (vaddr & ~(-1 << LEON_PGD_SH)) | ((pte & ~0xff) << 4);
  134. break;
  135. default:
  136. case 3:
  137. paddr_calc = vaddr;
  138. break;
  139. }
  140. if (srmmu_swprobe_trace)
  141. printk(KERN_INFO "swprobe: padde %x\n", paddr_calc);
  142. if (paddr)
  143. *paddr = paddr_calc;
  144. return paddrbase;
  145. }
  146. void leon_flush_icache_all(void)
  147. {
  148. __asm__ __volatile__(" flush "); /*iflush*/
  149. }
  150. void leon_flush_dcache_all(void)
  151. {
  152. __asm__ __volatile__("sta %%g0, [%%g0] %0\n\t" : :
  153. "i"(ASI_LEON_DFLUSH) : "memory");
  154. }
  155. void leon_flush_pcache_all(struct vm_area_struct *vma, unsigned long page)
  156. {
  157. if (vma->vm_flags & VM_EXEC)
  158. leon_flush_icache_all();
  159. leon_flush_dcache_all();
  160. }
  161. void leon_flush_cache_all(void)
  162. {
  163. __asm__ __volatile__(" flush "); /*iflush*/
  164. __asm__ __volatile__("sta %%g0, [%%g0] %0\n\t" : :
  165. "i"(ASI_LEON_DFLUSH) : "memory");
  166. }
  167. void leon_flush_tlb_all(void)
  168. {
  169. leon_flush_cache_all();
  170. __asm__ __volatile__("sta %%g0, [%0] %1\n\t" : : "r"(0x400),
  171. "i"(ASI_LEON_MMUFLUSH) : "memory");
  172. }
  173. /* get all cache regs */
  174. void leon3_getCacheRegs(struct leon3_cacheregs *regs)
  175. {
  176. unsigned long ccr, iccr, dccr;
  177. if (!regs)
  178. return;
  179. /* Get Cache regs from "Cache ASI" address 0x0, 0x8 and 0xC */
  180. __asm__ __volatile__("lda [%%g0] %3, %0\n\t"
  181. "mov 0x08, %%g1\n\t"
  182. "lda [%%g1] %3, %1\n\t"
  183. "mov 0x0c, %%g1\n\t"
  184. "lda [%%g1] %3, %2\n\t"
  185. : "=r"(ccr), "=r"(iccr), "=r"(dccr)
  186. /* output */
  187. : "i"(ASI_LEON_CACHEREGS) /* input */
  188. : "g1" /* clobber list */
  189. );
  190. regs->ccr = ccr;
  191. regs->iccr = iccr;
  192. regs->dccr = dccr;
  193. }
  194. /* Due to virtual cache we need to check cache configuration if
  195. * it is possible to skip flushing in some cases.
  196. *
  197. * Leon2 and Leon3 differ in their way of telling cache information
  198. *
  199. */
  200. int __init leon_flush_needed(void)
  201. {
  202. int flush_needed = -1;
  203. unsigned int ssize, sets;
  204. char *setStr[4] =
  205. { "direct mapped", "2-way associative", "3-way associative",
  206. "4-way associative"
  207. };
  208. /* leon 3 */
  209. struct leon3_cacheregs cregs;
  210. leon3_getCacheRegs(&cregs);
  211. sets = (cregs.dccr & LEON3_XCCR_SETS_MASK) >> 24;
  212. /* (ssize=>realsize) 0=>1k, 1=>2k, 2=>4k, 3=>8k ... */
  213. ssize = 1 << ((cregs.dccr & LEON3_XCCR_SSIZE_MASK) >> 20);
  214. printk(KERN_INFO "CACHE: %s cache, set size %dk\n",
  215. sets > 3 ? "unknown" : setStr[sets], ssize);
  216. if ((ssize <= (PAGE_SIZE / 1024)) && (sets == 0)) {
  217. /* Set Size <= Page size ==>
  218. flush on every context switch not needed. */
  219. flush_needed = 0;
  220. printk(KERN_INFO "CACHE: not flushing on every context switch\n");
  221. }
  222. return flush_needed;
  223. }
  224. void leon_switch_mm(void)
  225. {
  226. flush_tlb_mm((void *)0);
  227. if (leon_flush_during_switch)
  228. leon_flush_cache_all();
  229. }