c-r3k.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * r2300.c: R2000 and R3000 specific mmu/cache code.
  3. *
  4. * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
  5. *
  6. * with a lot of changes to make this thing work for R3000s
  7. * Tx39XX R4k style caches added. HK
  8. * Copyright (C) 1998, 1999, 2000 Harald Koerfgen
  9. * Copyright (C) 1998 Gleb Raiko & Vladimir Roganov
  10. * Copyright (C) 2001, 2004, 2007 Maciej W. Rozycki
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/smp.h>
  15. #include <linux/mm.h>
  16. #include <asm/page.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/mmu_context.h>
  19. #include <asm/isadep.h>
  20. #include <asm/io.h>
  21. #include <asm/bootinfo.h>
  22. #include <asm/cpu.h>
  23. static unsigned long icache_size, dcache_size; /* Size in bytes */
  24. static unsigned long icache_lsize, dcache_lsize; /* Size in bytes */
  25. unsigned long r3k_cache_size(unsigned long ca_flags)
  26. {
  27. unsigned long flags, status, dummy, size;
  28. volatile unsigned long *p;
  29. p = (volatile unsigned long *) KSEG0;
  30. flags = read_c0_status();
  31. /* isolate cache space */
  32. write_c0_status((ca_flags|flags)&~ST0_IEC);
  33. *p = 0xa5a55a5a;
  34. dummy = *p;
  35. status = read_c0_status();
  36. if (dummy != 0xa5a55a5a || (status & ST0_CM)) {
  37. size = 0;
  38. } else {
  39. for (size = 128; size <= 0x40000; size <<= 1)
  40. *(p + size) = 0;
  41. *p = -1;
  42. for (size = 128;
  43. (size <= 0x40000) && (*(p + size) == 0);
  44. size <<= 1)
  45. ;
  46. if (size > 0x40000)
  47. size = 0;
  48. }
  49. write_c0_status(flags);
  50. return size * sizeof(*p);
  51. }
  52. unsigned long r3k_cache_lsize(unsigned long ca_flags)
  53. {
  54. unsigned long flags, status, lsize, i;
  55. volatile unsigned long *p;
  56. p = (volatile unsigned long *) KSEG0;
  57. flags = read_c0_status();
  58. /* isolate cache space */
  59. write_c0_status((ca_flags|flags)&~ST0_IEC);
  60. for (i = 0; i < 128; i++)
  61. *(p + i) = 0;
  62. *(volatile unsigned char *)p = 0;
  63. for (lsize = 1; lsize < 128; lsize <<= 1) {
  64. *(p + lsize);
  65. status = read_c0_status();
  66. if (!(status & ST0_CM))
  67. break;
  68. }
  69. for (i = 0; i < 128; i += lsize)
  70. *(volatile unsigned char *)(p + i) = 0;
  71. write_c0_status(flags);
  72. return lsize * sizeof(*p);
  73. }
  74. static void r3k_probe_cache(void)
  75. {
  76. dcache_size = r3k_cache_size(ST0_ISC);
  77. if (dcache_size)
  78. dcache_lsize = r3k_cache_lsize(ST0_ISC);
  79. icache_size = r3k_cache_size(ST0_ISC|ST0_SWC);
  80. if (icache_size)
  81. icache_lsize = r3k_cache_lsize(ST0_ISC|ST0_SWC);
  82. }
  83. static void r3k_flush_icache_range(unsigned long start, unsigned long end)
  84. {
  85. unsigned long size, i, flags;
  86. volatile unsigned char *p;
  87. size = end - start;
  88. if (size > icache_size || KSEGX(start) != KSEG0) {
  89. start = KSEG0;
  90. size = icache_size;
  91. }
  92. p = (char *)start;
  93. flags = read_c0_status();
  94. /* isolate cache space */
  95. write_c0_status((ST0_ISC|ST0_SWC|flags)&~ST0_IEC);
  96. for (i = 0; i < size; i += 0x080) {
  97. asm( "sb\t$0, 0x000(%0)\n\t"
  98. "sb\t$0, 0x004(%0)\n\t"
  99. "sb\t$0, 0x008(%0)\n\t"
  100. "sb\t$0, 0x00c(%0)\n\t"
  101. "sb\t$0, 0x010(%0)\n\t"
  102. "sb\t$0, 0x014(%0)\n\t"
  103. "sb\t$0, 0x018(%0)\n\t"
  104. "sb\t$0, 0x01c(%0)\n\t"
  105. "sb\t$0, 0x020(%0)\n\t"
  106. "sb\t$0, 0x024(%0)\n\t"
  107. "sb\t$0, 0x028(%0)\n\t"
  108. "sb\t$0, 0x02c(%0)\n\t"
  109. "sb\t$0, 0x030(%0)\n\t"
  110. "sb\t$0, 0x034(%0)\n\t"
  111. "sb\t$0, 0x038(%0)\n\t"
  112. "sb\t$0, 0x03c(%0)\n\t"
  113. "sb\t$0, 0x040(%0)\n\t"
  114. "sb\t$0, 0x044(%0)\n\t"
  115. "sb\t$0, 0x048(%0)\n\t"
  116. "sb\t$0, 0x04c(%0)\n\t"
  117. "sb\t$0, 0x050(%0)\n\t"
  118. "sb\t$0, 0x054(%0)\n\t"
  119. "sb\t$0, 0x058(%0)\n\t"
  120. "sb\t$0, 0x05c(%0)\n\t"
  121. "sb\t$0, 0x060(%0)\n\t"
  122. "sb\t$0, 0x064(%0)\n\t"
  123. "sb\t$0, 0x068(%0)\n\t"
  124. "sb\t$0, 0x06c(%0)\n\t"
  125. "sb\t$0, 0x070(%0)\n\t"
  126. "sb\t$0, 0x074(%0)\n\t"
  127. "sb\t$0, 0x078(%0)\n\t"
  128. "sb\t$0, 0x07c(%0)\n\t"
  129. : : "r" (p) );
  130. p += 0x080;
  131. }
  132. write_c0_status(flags);
  133. }
  134. static void r3k_flush_dcache_range(unsigned long start, unsigned long end)
  135. {
  136. unsigned long size, i, flags;
  137. volatile unsigned char *p;
  138. size = end - start;
  139. if (size > dcache_size || KSEGX(start) != KSEG0) {
  140. start = KSEG0;
  141. size = dcache_size;
  142. }
  143. p = (char *)start;
  144. flags = read_c0_status();
  145. /* isolate cache space */
  146. write_c0_status((ST0_ISC|flags)&~ST0_IEC);
  147. for (i = 0; i < size; i += 0x080) {
  148. asm( "sb\t$0, 0x000(%0)\n\t"
  149. "sb\t$0, 0x004(%0)\n\t"
  150. "sb\t$0, 0x008(%0)\n\t"
  151. "sb\t$0, 0x00c(%0)\n\t"
  152. "sb\t$0, 0x010(%0)\n\t"
  153. "sb\t$0, 0x014(%0)\n\t"
  154. "sb\t$0, 0x018(%0)\n\t"
  155. "sb\t$0, 0x01c(%0)\n\t"
  156. "sb\t$0, 0x020(%0)\n\t"
  157. "sb\t$0, 0x024(%0)\n\t"
  158. "sb\t$0, 0x028(%0)\n\t"
  159. "sb\t$0, 0x02c(%0)\n\t"
  160. "sb\t$0, 0x030(%0)\n\t"
  161. "sb\t$0, 0x034(%0)\n\t"
  162. "sb\t$0, 0x038(%0)\n\t"
  163. "sb\t$0, 0x03c(%0)\n\t"
  164. "sb\t$0, 0x040(%0)\n\t"
  165. "sb\t$0, 0x044(%0)\n\t"
  166. "sb\t$0, 0x048(%0)\n\t"
  167. "sb\t$0, 0x04c(%0)\n\t"
  168. "sb\t$0, 0x050(%0)\n\t"
  169. "sb\t$0, 0x054(%0)\n\t"
  170. "sb\t$0, 0x058(%0)\n\t"
  171. "sb\t$0, 0x05c(%0)\n\t"
  172. "sb\t$0, 0x060(%0)\n\t"
  173. "sb\t$0, 0x064(%0)\n\t"
  174. "sb\t$0, 0x068(%0)\n\t"
  175. "sb\t$0, 0x06c(%0)\n\t"
  176. "sb\t$0, 0x070(%0)\n\t"
  177. "sb\t$0, 0x074(%0)\n\t"
  178. "sb\t$0, 0x078(%0)\n\t"
  179. "sb\t$0, 0x07c(%0)\n\t"
  180. : : "r" (p) );
  181. p += 0x080;
  182. }
  183. write_c0_status(flags);
  184. }
  185. static inline void r3k_flush_cache_all(void)
  186. {
  187. }
  188. static inline void r3k___flush_cache_all(void)
  189. {
  190. r3k_flush_dcache_range(KSEG0, KSEG0 + dcache_size);
  191. r3k_flush_icache_range(KSEG0, KSEG0 + icache_size);
  192. }
  193. static void r3k_flush_cache_mm(struct mm_struct *mm)
  194. {
  195. }
  196. static void r3k_flush_cache_range(struct vm_area_struct *vma,
  197. unsigned long start, unsigned long end)
  198. {
  199. }
  200. static void r3k_flush_cache_page(struct vm_area_struct *vma,
  201. unsigned long addr, unsigned long pfn)
  202. {
  203. unsigned long kaddr = KSEG0ADDR(pfn << PAGE_SHIFT);
  204. int exec = vma->vm_flags & VM_EXEC;
  205. struct mm_struct *mm = vma->vm_mm;
  206. pgd_t *pgdp;
  207. pud_t *pudp;
  208. pmd_t *pmdp;
  209. pte_t *ptep;
  210. pr_debug("cpage[%08lx,%08lx]\n",
  211. cpu_context(smp_processor_id(), mm), addr);
  212. /* No ASID => no such page in the cache. */
  213. if (cpu_context(smp_processor_id(), mm) == 0)
  214. return;
  215. pgdp = pgd_offset(mm, addr);
  216. pudp = pud_offset(pgdp, addr);
  217. pmdp = pmd_offset(pudp, addr);
  218. ptep = pte_offset(pmdp, addr);
  219. /* Invalid => no such page in the cache. */
  220. if (!(pte_val(*ptep) & _PAGE_PRESENT))
  221. return;
  222. r3k_flush_dcache_range(kaddr, kaddr + PAGE_SIZE);
  223. if (exec)
  224. r3k_flush_icache_range(kaddr, kaddr + PAGE_SIZE);
  225. }
  226. static void local_r3k_flush_data_cache_page(void *addr)
  227. {
  228. }
  229. static void r3k_flush_data_cache_page(unsigned long addr)
  230. {
  231. }
  232. static void r3k_flush_cache_sigtramp(unsigned long addr)
  233. {
  234. unsigned long flags;
  235. pr_debug("csigtramp[%08lx]\n", addr);
  236. flags = read_c0_status();
  237. write_c0_status(flags&~ST0_IEC);
  238. /* Fill the TLB to avoid an exception with caches isolated. */
  239. asm( "lw\t$0, 0x000(%0)\n\t"
  240. "lw\t$0, 0x004(%0)\n\t"
  241. : : "r" (addr) );
  242. write_c0_status((ST0_ISC|ST0_SWC|flags)&~ST0_IEC);
  243. asm( "sb\t$0, 0x000(%0)\n\t"
  244. "sb\t$0, 0x004(%0)\n\t"
  245. : : "r" (addr) );
  246. write_c0_status(flags);
  247. }
  248. static void r3k_flush_kernel_vmap_range(unsigned long vaddr, int size)
  249. {
  250. BUG();
  251. }
  252. static void r3k_dma_cache_wback_inv(unsigned long start, unsigned long size)
  253. {
  254. /* Catch bad driver code */
  255. BUG_ON(size == 0);
  256. iob();
  257. r3k_flush_dcache_range(start, start + size);
  258. }
  259. void r3k_cache_init(void)
  260. {
  261. extern void build_clear_page(void);
  262. extern void build_copy_page(void);
  263. r3k_probe_cache();
  264. flush_cache_all = r3k_flush_cache_all;
  265. __flush_cache_all = r3k___flush_cache_all;
  266. flush_cache_mm = r3k_flush_cache_mm;
  267. flush_cache_range = r3k_flush_cache_range;
  268. flush_cache_page = r3k_flush_cache_page;
  269. flush_icache_range = r3k_flush_icache_range;
  270. local_flush_icache_range = r3k_flush_icache_range;
  271. __flush_kernel_vmap_range = r3k_flush_kernel_vmap_range;
  272. flush_cache_sigtramp = r3k_flush_cache_sigtramp;
  273. local_flush_data_cache_page = local_r3k_flush_data_cache_page;
  274. flush_data_cache_page = r3k_flush_data_cache_page;
  275. _dma_cache_wback_inv = r3k_dma_cache_wback_inv;
  276. _dma_cache_wback = r3k_dma_cache_wback_inv;
  277. _dma_cache_inv = r3k_dma_cache_wback_inv;
  278. printk("Primary instruction cache %ldkB, linesize %ld bytes.\n",
  279. icache_size >> 10, icache_lsize);
  280. printk("Primary data cache %ldkB, linesize %ld bytes.\n",
  281. dcache_size >> 10, dcache_lsize);
  282. build_clear_page();
  283. build_copy_page();
  284. }