leon_mm.c 8.2 KB

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