c-tx39.c 11 KB

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