c-tx39.c 12 KB

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