c-tx39.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/smp.h>
  15. #include <linux/mm.h>
  16. #include <asm/cacheops.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. /* For R3000 cores with R4000 style caches */
  25. static unsigned long icache_size, dcache_size; /* Size in bytes */
  26. #include <asm/r4kcache.h>
  27. /* This sequence is required to ensure icache is disabled immediately */
  28. #define TX39_STOP_STREAMING() \
  29. __asm__ __volatile__( \
  30. ".set push\n\t" \
  31. ".set noreorder\n\t" \
  32. "b 1f\n\t" \
  33. "nop\n\t" \
  34. "1:\n\t" \
  35. ".set pop" \
  36. )
  37. /* TX39H-style cache flush routines. */
  38. static void tx39h_flush_icache_all(void)
  39. {
  40. unsigned long flags, config;
  41. /* disable icache (set ICE#) */
  42. local_irq_save(flags);
  43. config = read_c0_conf();
  44. write_c0_conf(config & ~TX39_CONF_ICE);
  45. TX39_STOP_STREAMING();
  46. blast_icache16();
  47. write_c0_conf(config);
  48. local_irq_restore(flags);
  49. }
  50. static void tx39h_dma_cache_wback_inv(unsigned long addr, unsigned long size)
  51. {
  52. /* Catch bad driver code */
  53. BUG_ON(size == 0);
  54. iob();
  55. blast_inv_dcache_range(addr, addr + size);
  56. }
  57. /* TX39H2,TX39H3 */
  58. static inline void tx39_blast_dcache_page(unsigned long addr)
  59. {
  60. if (current_cpu_type() != CPU_TX3912)
  61. blast_dcache16_page(addr);
  62. }
  63. static inline void tx39_blast_dcache_page_indexed(unsigned long addr)
  64. {
  65. blast_dcache16_page_indexed(addr);
  66. }
  67. static inline void tx39_blast_dcache(void)
  68. {
  69. blast_dcache16();
  70. }
  71. static inline void tx39_blast_icache_page(unsigned long addr)
  72. {
  73. unsigned long flags, config;
  74. /* disable icache (set ICE#) */
  75. local_irq_save(flags);
  76. config = read_c0_conf();
  77. write_c0_conf(config & ~TX39_CONF_ICE);
  78. TX39_STOP_STREAMING();
  79. blast_icache16_page(addr);
  80. write_c0_conf(config);
  81. local_irq_restore(flags);
  82. }
  83. static inline void tx39_blast_icache_page_indexed(unsigned long addr)
  84. {
  85. unsigned long flags, config;
  86. /* disable icache (set ICE#) */
  87. local_irq_save(flags);
  88. config = read_c0_conf();
  89. write_c0_conf(config & ~TX39_CONF_ICE);
  90. TX39_STOP_STREAMING();
  91. blast_icache16_page_indexed(addr);
  92. write_c0_conf(config);
  93. local_irq_restore(flags);
  94. }
  95. static inline void tx39_blast_icache(void)
  96. {
  97. unsigned long flags, config;
  98. /* disable icache (set ICE#) */
  99. local_irq_save(flags);
  100. config = read_c0_conf();
  101. write_c0_conf(config & ~TX39_CONF_ICE);
  102. TX39_STOP_STREAMING();
  103. blast_icache16();
  104. write_c0_conf(config);
  105. local_irq_restore(flags);
  106. }
  107. static void tx39__flush_cache_vmap(void)
  108. {
  109. tx39_blast_dcache();
  110. }
  111. static void tx39__flush_cache_vunmap(void)
  112. {
  113. tx39_blast_dcache();
  114. }
  115. static inline void tx39_flush_cache_all(void)
  116. {
  117. if (!cpu_has_dc_aliases)
  118. return;
  119. tx39_blast_dcache();
  120. }
  121. static inline void tx39___flush_cache_all(void)
  122. {
  123. tx39_blast_dcache();
  124. tx39_blast_icache();
  125. }
  126. static void tx39_flush_cache_mm(struct mm_struct *mm)
  127. {
  128. if (!cpu_has_dc_aliases)
  129. return;
  130. if (cpu_context(smp_processor_id(), mm) != 0)
  131. tx39_blast_dcache();
  132. }
  133. static void tx39_flush_cache_range(struct vm_area_struct *vma,
  134. unsigned long start, unsigned long end)
  135. {
  136. if (!cpu_has_dc_aliases)
  137. return;
  138. if (!(cpu_context(smp_processor_id(), vma->vm_mm)))
  139. return;
  140. tx39_blast_dcache();
  141. }
  142. static void tx39_flush_cache_page(struct vm_area_struct *vma, unsigned long page, unsigned long pfn)
  143. {
  144. int exec = vma->vm_flags & VM_EXEC;
  145. struct mm_struct *mm = vma->vm_mm;
  146. pgd_t *pgdp;
  147. pud_t *pudp;
  148. pmd_t *pmdp;
  149. pte_t *ptep;
  150. /*
  151. * If ownes no valid ASID yet, cannot possibly have gotten
  152. * this page into the cache.
  153. */
  154. if (cpu_context(smp_processor_id(), mm) == 0)
  155. return;
  156. page &= PAGE_MASK;
  157. pgdp = pgd_offset(mm, page);
  158. pudp = pud_offset(pgdp, page);
  159. pmdp = pmd_offset(pudp, page);
  160. ptep = pte_offset(pmdp, page);
  161. /*
  162. * If the page isn't marked valid, the page cannot possibly be
  163. * in the cache.
  164. */
  165. if (!(pte_val(*ptep) & _PAGE_PRESENT))
  166. return;
  167. /*
  168. * Doing flushes for another ASID than the current one is
  169. * too difficult since stupid R4k caches do a TLB translation
  170. * for every cache flush operation. So we do indexed flushes
  171. * in that case, which doesn't overly flush the cache too much.
  172. */
  173. if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID)) {
  174. if (cpu_has_dc_aliases || exec)
  175. tx39_blast_dcache_page(page);
  176. if (exec)
  177. tx39_blast_icache_page(page);
  178. return;
  179. }
  180. /*
  181. * Do indexed flush, too much work to get the (possible) TLB refills
  182. * to work correctly.
  183. */
  184. if (cpu_has_dc_aliases || exec)
  185. tx39_blast_dcache_page_indexed(page);
  186. if (exec)
  187. tx39_blast_icache_page_indexed(page);
  188. }
  189. static void local_tx39_flush_data_cache_page(void * addr)
  190. {
  191. tx39_blast_dcache_page((unsigned long)addr);
  192. }
  193. static void tx39_flush_data_cache_page(unsigned long addr)
  194. {
  195. tx39_blast_dcache_page(addr);
  196. }
  197. static void tx39_flush_icache_range(unsigned long start, unsigned long end)
  198. {
  199. if (end - start > dcache_size)
  200. tx39_blast_dcache();
  201. else
  202. protected_blast_dcache_range(start, end);
  203. if (end - start > icache_size)
  204. tx39_blast_icache();
  205. else {
  206. unsigned long flags, config;
  207. /* disable icache (set ICE#) */
  208. local_irq_save(flags);
  209. config = read_c0_conf();
  210. write_c0_conf(config & ~TX39_CONF_ICE);
  211. TX39_STOP_STREAMING();
  212. protected_blast_icache_range(start, end);
  213. write_c0_conf(config);
  214. local_irq_restore(flags);
  215. }
  216. }
  217. static void tx39_flush_kernel_vmap_range(unsigned long vaddr, int size)
  218. {
  219. BUG();
  220. }
  221. static void tx39_dma_cache_wback_inv(unsigned long addr, unsigned long size)
  222. {
  223. unsigned long end;
  224. if (((size | addr) & (PAGE_SIZE - 1)) == 0) {
  225. end = addr + size;
  226. do {
  227. tx39_blast_dcache_page(addr);
  228. addr += PAGE_SIZE;
  229. } while(addr != end);
  230. } else if (size > dcache_size) {
  231. tx39_blast_dcache();
  232. } else {
  233. blast_dcache_range(addr, addr + size);
  234. }
  235. }
  236. static void tx39_dma_cache_inv(unsigned long addr, unsigned long size)
  237. {
  238. unsigned long end;
  239. if (((size | addr) & (PAGE_SIZE - 1)) == 0) {
  240. end = addr + size;
  241. do {
  242. tx39_blast_dcache_page(addr);
  243. addr += PAGE_SIZE;
  244. } while(addr != end);
  245. } else if (size > dcache_size) {
  246. tx39_blast_dcache();
  247. } else {
  248. blast_inv_dcache_range(addr, addr + size);
  249. }
  250. }
  251. static void tx39_flush_cache_sigtramp(unsigned long addr)
  252. {
  253. unsigned long ic_lsize = current_cpu_data.icache.linesz;
  254. unsigned long dc_lsize = current_cpu_data.dcache.linesz;
  255. unsigned long config;
  256. unsigned long flags;
  257. protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
  258. /* disable icache (set ICE#) */
  259. local_irq_save(flags);
  260. config = read_c0_conf();
  261. write_c0_conf(config & ~TX39_CONF_ICE);
  262. TX39_STOP_STREAMING();
  263. protected_flush_icache_line(addr & ~(ic_lsize - 1));
  264. write_c0_conf(config);
  265. local_irq_restore(flags);
  266. }
  267. static __init void tx39_probe_cache(void)
  268. {
  269. unsigned long config;
  270. config = read_c0_conf();
  271. icache_size = 1 << (10 + ((config & TX39_CONF_ICS_MASK) >>
  272. TX39_CONF_ICS_SHIFT));
  273. dcache_size = 1 << (10 + ((config & TX39_CONF_DCS_MASK) >>
  274. TX39_CONF_DCS_SHIFT));
  275. current_cpu_data.icache.linesz = 16;
  276. switch (current_cpu_type()) {
  277. case CPU_TX3912:
  278. current_cpu_data.icache.ways = 1;
  279. current_cpu_data.dcache.ways = 1;
  280. current_cpu_data.dcache.linesz = 4;
  281. break;
  282. case CPU_TX3927:
  283. current_cpu_data.icache.ways = 2;
  284. current_cpu_data.dcache.ways = 2;
  285. current_cpu_data.dcache.linesz = 16;
  286. break;
  287. case CPU_TX3922:
  288. default:
  289. current_cpu_data.icache.ways = 1;
  290. current_cpu_data.dcache.ways = 1;
  291. current_cpu_data.dcache.linesz = 16;
  292. break;
  293. }
  294. }
  295. void tx39_cache_init(void)
  296. {
  297. extern void build_clear_page(void);
  298. extern void build_copy_page(void);
  299. unsigned long config;
  300. config = read_c0_conf();
  301. config &= ~TX39_CONF_WBON;
  302. write_c0_conf(config);
  303. tx39_probe_cache();
  304. switch (current_cpu_type()) {
  305. case CPU_TX3912:
  306. /* TX39/H core (writethru direct-map cache) */
  307. __flush_cache_vmap = tx39__flush_cache_vmap;
  308. __flush_cache_vunmap = tx39__flush_cache_vunmap;
  309. flush_cache_all = tx39h_flush_icache_all;
  310. __flush_cache_all = tx39h_flush_icache_all;
  311. flush_cache_mm = (void *) tx39h_flush_icache_all;
  312. flush_cache_range = (void *) tx39h_flush_icache_all;
  313. flush_cache_page = (void *) tx39h_flush_icache_all;
  314. flush_icache_range = (void *) tx39h_flush_icache_all;
  315. local_flush_icache_range = (void *) tx39h_flush_icache_all;
  316. flush_cache_sigtramp = (void *) tx39h_flush_icache_all;
  317. local_flush_data_cache_page = (void *) tx39h_flush_icache_all;
  318. flush_data_cache_page = (void *) tx39h_flush_icache_all;
  319. _dma_cache_wback_inv = tx39h_dma_cache_wback_inv;
  320. shm_align_mask = PAGE_SIZE - 1;
  321. break;
  322. case CPU_TX3922:
  323. case CPU_TX3927:
  324. default:
  325. /* TX39/H2,H3 core (writeback 2way-set-associative cache) */
  326. /* board-dependent init code may set WBON */
  327. __flush_cache_vmap = tx39__flush_cache_vmap;
  328. __flush_cache_vunmap = tx39__flush_cache_vunmap;
  329. flush_cache_all = tx39_flush_cache_all;
  330. __flush_cache_all = tx39___flush_cache_all;
  331. flush_cache_mm = tx39_flush_cache_mm;
  332. flush_cache_range = tx39_flush_cache_range;
  333. flush_cache_page = tx39_flush_cache_page;
  334. flush_icache_range = tx39_flush_icache_range;
  335. local_flush_icache_range = tx39_flush_icache_range;
  336. __flush_kernel_vmap_range = tx39_flush_kernel_vmap_range;
  337. flush_cache_sigtramp = tx39_flush_cache_sigtramp;
  338. local_flush_data_cache_page = local_tx39_flush_data_cache_page;
  339. flush_data_cache_page = tx39_flush_data_cache_page;
  340. _dma_cache_wback_inv = tx39_dma_cache_wback_inv;
  341. _dma_cache_wback = tx39_dma_cache_wback_inv;
  342. _dma_cache_inv = tx39_dma_cache_inv;
  343. shm_align_mask = max_t(unsigned long,
  344. (dcache_size / current_cpu_data.dcache.ways) - 1,
  345. PAGE_SIZE - 1);
  346. break;
  347. }
  348. current_cpu_data.icache.waysize = icache_size / current_cpu_data.icache.ways;
  349. current_cpu_data.dcache.waysize = dcache_size / current_cpu_data.dcache.ways;
  350. current_cpu_data.icache.sets =
  351. current_cpu_data.icache.waysize / current_cpu_data.icache.linesz;
  352. current_cpu_data.dcache.sets =
  353. current_cpu_data.dcache.waysize / current_cpu_data.dcache.linesz;
  354. if (current_cpu_data.dcache.waysize > PAGE_SIZE)
  355. current_cpu_data.dcache.flags |= MIPS_CACHE_ALIASES;
  356. current_cpu_data.icache.waybit = 0;
  357. current_cpu_data.dcache.waybit = 0;
  358. printk("Primary instruction cache %ldkB, linesize %d bytes\n",
  359. icache_size >> 10, current_cpu_data.icache.linesz);
  360. printk("Primary data cache %ldkB, linesize %d bytes\n",
  361. dcache_size >> 10, current_cpu_data.dcache.linesz);
  362. build_clear_page();
  363. build_copy_page();
  364. tx39h_flush_icache_all();
  365. }