c-r3k.c 8.0 KB

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