leon_mm.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include "mm_32.h"
  17. int leon_flush_during_switch = 1;
  18. static int srmmu_swprobe_trace;
  19. static inline unsigned long leon_get_ctable_ptr(void)
  20. {
  21. unsigned int retval;
  22. __asm__ __volatile__("lda [%1] %2, %0\n\t" :
  23. "=r" (retval) :
  24. "r" (SRMMU_CTXTBL_PTR),
  25. "i" (ASI_LEON_MMUREGS));
  26. return (retval & SRMMU_CTX_PMASK) << 4;
  27. }
  28. unsigned long leon_swprobe(unsigned long vaddr, unsigned long *paddr)
  29. {
  30. unsigned int ctxtbl;
  31. unsigned int pgd, pmd, ped;
  32. unsigned int ptr;
  33. unsigned int lvl, pte, paddrbase;
  34. unsigned int ctx;
  35. unsigned int paddr_calc;
  36. paddrbase = 0;
  37. if (srmmu_swprobe_trace)
  38. printk(KERN_INFO "swprobe: trace on\n");
  39. ctxtbl = leon_get_ctable_ptr();
  40. if (!(ctxtbl)) {
  41. if (srmmu_swprobe_trace)
  42. printk(KERN_INFO "swprobe: leon_get_ctable_ptr returned 0=>0\n");
  43. return 0;
  44. }
  45. if (!_pfn_valid(PFN(ctxtbl))) {
  46. if (srmmu_swprobe_trace)
  47. printk(KERN_INFO
  48. "swprobe: !_pfn_valid(%x)=>0\n",
  49. PFN(ctxtbl));
  50. return 0;
  51. }
  52. ctx = srmmu_get_context();
  53. if (srmmu_swprobe_trace)
  54. printk(KERN_INFO "swprobe: --- ctx (%x) ---\n", ctx);
  55. pgd = LEON_BYPASS_LOAD_PA(ctxtbl + (ctx * 4));
  56. if (((pgd & SRMMU_ET_MASK) == SRMMU_ET_PTE)) {
  57. if (srmmu_swprobe_trace)
  58. printk(KERN_INFO "swprobe: pgd is entry level 3\n");
  59. lvl = 3;
  60. pte = pgd;
  61. paddrbase = pgd & _SRMMU_PTE_PMASK_LEON;
  62. goto ready;
  63. }
  64. if (((pgd & SRMMU_ET_MASK) != SRMMU_ET_PTD)) {
  65. if (srmmu_swprobe_trace)
  66. printk(KERN_INFO "swprobe: pgd is invalid => 0\n");
  67. return 0;
  68. }
  69. if (srmmu_swprobe_trace)
  70. printk(KERN_INFO "swprobe: --- pgd (%x) ---\n", pgd);
  71. ptr = (pgd & SRMMU_PTD_PMASK) << 4;
  72. ptr += ((((vaddr) >> LEON_PGD_SH) & LEON_PGD_M) * 4);
  73. if (!_pfn_valid(PFN(ptr)))
  74. return 0;
  75. pmd = LEON_BYPASS_LOAD_PA(ptr);
  76. if (((pmd & SRMMU_ET_MASK) == SRMMU_ET_PTE)) {
  77. if (srmmu_swprobe_trace)
  78. printk(KERN_INFO "swprobe: pmd is entry level 2\n");
  79. lvl = 2;
  80. pte = pmd;
  81. paddrbase = pmd & _SRMMU_PTE_PMASK_LEON;
  82. goto ready;
  83. }
  84. if (((pmd & SRMMU_ET_MASK) != SRMMU_ET_PTD)) {
  85. if (srmmu_swprobe_trace)
  86. printk(KERN_INFO "swprobe: pmd is invalid => 0\n");
  87. return 0;
  88. }
  89. if (srmmu_swprobe_trace)
  90. printk(KERN_INFO "swprobe: --- pmd (%x) ---\n", pmd);
  91. ptr = (pmd & SRMMU_PTD_PMASK) << 4;
  92. ptr += (((vaddr >> LEON_PMD_SH) & LEON_PMD_M) * 4);
  93. if (!_pfn_valid(PFN(ptr))) {
  94. if (srmmu_swprobe_trace)
  95. printk(KERN_INFO "swprobe: !_pfn_valid(%x)=>0\n",
  96. PFN(ptr));
  97. return 0;
  98. }
  99. ped = LEON_BYPASS_LOAD_PA(ptr);
  100. if (((ped & SRMMU_ET_MASK) == SRMMU_ET_PTE)) {
  101. if (srmmu_swprobe_trace)
  102. printk(KERN_INFO "swprobe: ped is entry level 1\n");
  103. lvl = 1;
  104. pte = ped;
  105. paddrbase = ped & _SRMMU_PTE_PMASK_LEON;
  106. goto ready;
  107. }
  108. if (((ped & SRMMU_ET_MASK) != SRMMU_ET_PTD)) {
  109. if (srmmu_swprobe_trace)
  110. printk(KERN_INFO "swprobe: ped is invalid => 0\n");
  111. return 0;
  112. }
  113. if (srmmu_swprobe_trace)
  114. printk(KERN_INFO "swprobe: --- ped (%x) ---\n", ped);
  115. ptr = (ped & SRMMU_PTD_PMASK) << 4;
  116. ptr += (((vaddr >> LEON_PTE_SH) & LEON_PTE_M) * 4);
  117. if (!_pfn_valid(PFN(ptr)))
  118. return 0;
  119. ptr = LEON_BYPASS_LOAD_PA(ptr);
  120. if (((ptr & SRMMU_ET_MASK) == SRMMU_ET_PTE)) {
  121. if (srmmu_swprobe_trace)
  122. printk(KERN_INFO "swprobe: ptr is entry level 0\n");
  123. lvl = 0;
  124. pte = ptr;
  125. paddrbase = ptr & _SRMMU_PTE_PMASK_LEON;
  126. goto ready;
  127. }
  128. if (srmmu_swprobe_trace)
  129. printk(KERN_INFO "swprobe: ptr is invalid => 0\n");
  130. return 0;
  131. ready:
  132. switch (lvl) {
  133. case 0:
  134. paddr_calc =
  135. (vaddr & ~(-1 << LEON_PTE_SH)) | ((pte & ~0xff) << 4);
  136. break;
  137. case 1:
  138. paddr_calc =
  139. (vaddr & ~(-1 << LEON_PMD_SH)) | ((pte & ~0xff) << 4);
  140. break;
  141. case 2:
  142. paddr_calc =
  143. (vaddr & ~(-1 << LEON_PGD_SH)) | ((pte & ~0xff) << 4);
  144. break;
  145. default:
  146. case 3:
  147. paddr_calc = vaddr;
  148. break;
  149. }
  150. if (srmmu_swprobe_trace)
  151. printk(KERN_INFO "swprobe: padde %x\n", paddr_calc);
  152. if (paddr)
  153. *paddr = paddr_calc;
  154. return pte;
  155. }
  156. void leon_flush_icache_all(void)
  157. {
  158. __asm__ __volatile__(" flush "); /*iflush*/
  159. }
  160. void leon_flush_dcache_all(void)
  161. {
  162. __asm__ __volatile__("sta %%g0, [%%g0] %0\n\t" : :
  163. "i"(ASI_LEON_DFLUSH) : "memory");
  164. }
  165. void leon_flush_pcache_all(struct vm_area_struct *vma, unsigned long page)
  166. {
  167. if (vma->vm_flags & VM_EXEC)
  168. leon_flush_icache_all();
  169. leon_flush_dcache_all();
  170. }
  171. void leon_flush_cache_all(void)
  172. {
  173. __asm__ __volatile__(" flush "); /*iflush*/
  174. __asm__ __volatile__("sta %%g0, [%%g0] %0\n\t" : :
  175. "i"(ASI_LEON_DFLUSH) : "memory");
  176. }
  177. void leon_flush_tlb_all(void)
  178. {
  179. leon_flush_cache_all();
  180. __asm__ __volatile__("sta %%g0, [%0] %1\n\t" : : "r"(0x400),
  181. "i"(ASI_LEON_MMUFLUSH) : "memory");
  182. }
  183. /* get all cache regs */
  184. void leon3_getCacheRegs(struct leon3_cacheregs *regs)
  185. {
  186. unsigned long ccr, iccr, dccr;
  187. if (!regs)
  188. return;
  189. /* Get Cache regs from "Cache ASI" address 0x0, 0x8 and 0xC */
  190. __asm__ __volatile__("lda [%%g0] %3, %0\n\t"
  191. "mov 0x08, %%g1\n\t"
  192. "lda [%%g1] %3, %1\n\t"
  193. "mov 0x0c, %%g1\n\t"
  194. "lda [%%g1] %3, %2\n\t"
  195. : "=r"(ccr), "=r"(iccr), "=r"(dccr)
  196. /* output */
  197. : "i"(ASI_LEON_CACHEREGS) /* input */
  198. : "g1" /* clobber list */
  199. );
  200. regs->ccr = ccr;
  201. regs->iccr = iccr;
  202. regs->dccr = dccr;
  203. }
  204. /* Due to virtual cache we need to check cache configuration if
  205. * it is possible to skip flushing in some cases.
  206. *
  207. * Leon2 and Leon3 differ in their way of telling cache information
  208. *
  209. */
  210. int __init leon_flush_needed(void)
  211. {
  212. int flush_needed = -1;
  213. unsigned int ssize, sets;
  214. char *setStr[4] =
  215. { "direct mapped", "2-way associative", "3-way associative",
  216. "4-way associative"
  217. };
  218. /* leon 3 */
  219. struct leon3_cacheregs cregs;
  220. leon3_getCacheRegs(&cregs);
  221. sets = (cregs.dccr & LEON3_XCCR_SETS_MASK) >> 24;
  222. /* (ssize=>realsize) 0=>1k, 1=>2k, 2=>4k, 3=>8k ... */
  223. ssize = 1 << ((cregs.dccr & LEON3_XCCR_SSIZE_MASK) >> 20);
  224. printk(KERN_INFO "CACHE: %s cache, set size %dk\n",
  225. sets > 3 ? "unknown" : setStr[sets], ssize);
  226. if ((ssize <= (PAGE_SIZE / 1024)) && (sets == 0)) {
  227. /* Set Size <= Page size ==>
  228. flush on every context switch not needed. */
  229. flush_needed = 0;
  230. printk(KERN_INFO "CACHE: not flushing on every context switch\n");
  231. }
  232. return flush_needed;
  233. }
  234. void leon_switch_mm(void)
  235. {
  236. flush_tlb_mm((void *)0);
  237. if (leon_flush_during_switch)
  238. leon_flush_cache_all();
  239. }
  240. static void leon_flush_cache_mm(struct mm_struct *mm)
  241. {
  242. leon_flush_cache_all();
  243. }
  244. static void leon_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
  245. {
  246. leon_flush_pcache_all(vma, page);
  247. }
  248. static void leon_flush_cache_range(struct vm_area_struct *vma,
  249. unsigned long start,
  250. unsigned long end)
  251. {
  252. leon_flush_cache_all();
  253. }
  254. static void leon_flush_tlb_mm(struct mm_struct *mm)
  255. {
  256. leon_flush_tlb_all();
  257. }
  258. static void leon_flush_tlb_page(struct vm_area_struct *vma,
  259. unsigned long page)
  260. {
  261. leon_flush_tlb_all();
  262. }
  263. static void leon_flush_tlb_range(struct vm_area_struct *vma,
  264. unsigned long start,
  265. unsigned long end)
  266. {
  267. leon_flush_tlb_all();
  268. }
  269. static void leon_flush_page_to_ram(unsigned long page)
  270. {
  271. leon_flush_cache_all();
  272. }
  273. static void leon_flush_sig_insns(struct mm_struct *mm, unsigned long page)
  274. {
  275. leon_flush_cache_all();
  276. }
  277. static void leon_flush_page_for_dma(unsigned long page)
  278. {
  279. leon_flush_dcache_all();
  280. }
  281. void __init poke_leonsparc(void)
  282. {
  283. }
  284. static const struct sparc32_cachetlb_ops leon_ops = {
  285. .cache_all = leon_flush_cache_all,
  286. .cache_mm = leon_flush_cache_mm,
  287. .cache_page = leon_flush_cache_page,
  288. .cache_range = leon_flush_cache_range,
  289. .tlb_all = leon_flush_tlb_all,
  290. .tlb_mm = leon_flush_tlb_mm,
  291. .tlb_page = leon_flush_tlb_page,
  292. .tlb_range = leon_flush_tlb_range,
  293. .page_to_ram = leon_flush_page_to_ram,
  294. .sig_insns = leon_flush_sig_insns,
  295. .page_for_dma = leon_flush_page_for_dma,
  296. };
  297. void __init init_leon(void)
  298. {
  299. srmmu_name = "LEON";
  300. sparc32_cachetlb_ops = &leon_ops;
  301. poke_srmmu = poke_leonsparc;
  302. leon_flush_during_switch = leon_flush_needed();
  303. }