c-tx39.c 11 KB

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