c-tx39.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. pud_t *pudp;
  159. pmd_t *pmdp;
  160. pte_t *ptep;
  161. /*
  162. * If ownes no valid ASID yet, cannot possibly have gotten
  163. * this page into the cache.
  164. */
  165. if (cpu_context(smp_processor_id(), mm) == 0)
  166. return;
  167. page &= PAGE_MASK;
  168. pgdp = pgd_offset(mm, page);
  169. pudp = pud_offset(pgdp, page);
  170. pmdp = pmd_offset(pudp, page);
  171. ptep = pte_offset(pmdp, page);
  172. /*
  173. * If the page isn't marked valid, the page cannot possibly be
  174. * in the cache.
  175. */
  176. if (!(pte_val(*ptep) & _PAGE_PRESENT))
  177. return;
  178. /*
  179. * Doing flushes for another ASID than the current one is
  180. * too difficult since stupid R4k caches do a TLB translation
  181. * for every cache flush operation. So we do indexed flushes
  182. * in that case, which doesn't overly flush the cache too much.
  183. */
  184. if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID)) {
  185. if (cpu_has_dc_aliases || exec)
  186. tx39_blast_dcache_page(page);
  187. if (exec)
  188. tx39_blast_icache_page(page);
  189. return;
  190. }
  191. /*
  192. * Do indexed flush, too much work to get the (possible) TLB refills
  193. * to work correctly.
  194. */
  195. page = (KSEG0 + (page & (dcache_size - 1)));
  196. if (cpu_has_dc_aliases || exec)
  197. tx39_blast_dcache_page_indexed(page);
  198. if (exec)
  199. tx39_blast_icache_page_indexed(page);
  200. }
  201. static void tx39_flush_data_cache_page(unsigned long addr)
  202. {
  203. tx39_blast_dcache_page(addr);
  204. }
  205. static void tx39_flush_icache_range(unsigned long start, unsigned long end)
  206. {
  207. unsigned long dc_lsize = current_cpu_data.dcache.linesz;
  208. unsigned long addr, aend;
  209. if (end - start > dcache_size)
  210. tx39_blast_dcache();
  211. else {
  212. addr = start & ~(dc_lsize - 1);
  213. aend = (end - 1) & ~(dc_lsize - 1);
  214. while (1) {
  215. /* Hit_Writeback_Inv_D */
  216. protected_writeback_dcache_line(addr);
  217. if (addr == aend)
  218. break;
  219. addr += dc_lsize;
  220. }
  221. }
  222. if (end - start > icache_size)
  223. tx39_blast_icache();
  224. else {
  225. unsigned long flags, config;
  226. addr = start & ~(dc_lsize - 1);
  227. aend = (end - 1) & ~(dc_lsize - 1);
  228. /* disable icache (set ICE#) */
  229. local_irq_save(flags);
  230. config = read_c0_conf();
  231. write_c0_conf(config & ~TX39_CONF_ICE);
  232. TX39_STOP_STREAMING();
  233. while (1) {
  234. /* Hit_Invalidate_I */
  235. protected_flush_icache_line(addr);
  236. if (addr == aend)
  237. break;
  238. addr += dc_lsize;
  239. }
  240. write_c0_conf(config);
  241. local_irq_restore(flags);
  242. }
  243. }
  244. /*
  245. * Ok, this seriously sucks. We use them to flush a user page but don't
  246. * know the virtual address, so we have to blast away the whole icache
  247. * which is significantly more expensive than the real thing. Otoh we at
  248. * least know the kernel address of the page so we can flush it
  249. * selectivly.
  250. */
  251. static void tx39_flush_icache_page(struct vm_area_struct *vma, struct page *page)
  252. {
  253. unsigned long addr;
  254. /*
  255. * If there's no context yet, or the page isn't executable, no icache
  256. * flush is needed.
  257. */
  258. if (!(vma->vm_flags & VM_EXEC))
  259. return;
  260. addr = (unsigned long) page_address(page);
  261. tx39_blast_dcache_page(addr);
  262. /*
  263. * We're not sure of the virtual address(es) involved here, so
  264. * we have to flush the entire I-cache.
  265. */
  266. tx39_blast_icache();
  267. }
  268. static void tx39_dma_cache_wback_inv(unsigned long addr, unsigned long size)
  269. {
  270. unsigned long end, a;
  271. if (((size | addr) & (PAGE_SIZE - 1)) == 0) {
  272. end = addr + size;
  273. do {
  274. tx39_blast_dcache_page(addr);
  275. addr += PAGE_SIZE;
  276. } while(addr != end);
  277. } else if (size > dcache_size) {
  278. tx39_blast_dcache();
  279. } else {
  280. unsigned long dc_lsize = current_cpu_data.dcache.linesz;
  281. a = addr & ~(dc_lsize - 1);
  282. end = (addr + size - 1) & ~(dc_lsize - 1);
  283. while (1) {
  284. flush_dcache_line(a); /* Hit_Writeback_Inv_D */
  285. if (a == end) break;
  286. a += dc_lsize;
  287. }
  288. }
  289. }
  290. static void tx39_dma_cache_inv(unsigned long addr, unsigned long size)
  291. {
  292. unsigned long end, a;
  293. if (((size | addr) & (PAGE_SIZE - 1)) == 0) {
  294. end = addr + size;
  295. do {
  296. tx39_blast_dcache_page(addr);
  297. addr += PAGE_SIZE;
  298. } while(addr != end);
  299. } else if (size > dcache_size) {
  300. tx39_blast_dcache();
  301. } else {
  302. unsigned long dc_lsize = current_cpu_data.dcache.linesz;
  303. a = addr & ~(dc_lsize - 1);
  304. end = (addr + size - 1) & ~(dc_lsize - 1);
  305. while (1) {
  306. invalidate_dcache_line(a); /* Hit_Invalidate_D */
  307. if (a == end) break;
  308. a += dc_lsize;
  309. }
  310. }
  311. }
  312. static void tx39_flush_cache_sigtramp(unsigned long addr)
  313. {
  314. unsigned long ic_lsize = current_cpu_data.icache.linesz;
  315. unsigned long dc_lsize = current_cpu_data.dcache.linesz;
  316. unsigned long config;
  317. unsigned long flags;
  318. protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
  319. /* disable icache (set ICE#) */
  320. local_irq_save(flags);
  321. config = read_c0_conf();
  322. write_c0_conf(config & ~TX39_CONF_ICE);
  323. TX39_STOP_STREAMING();
  324. protected_flush_icache_line(addr & ~(ic_lsize - 1));
  325. write_c0_conf(config);
  326. local_irq_restore(flags);
  327. }
  328. static __init void tx39_probe_cache(void)
  329. {
  330. unsigned long config;
  331. config = read_c0_conf();
  332. icache_size = 1 << (10 + ((config & TX39_CONF_ICS_MASK) >>
  333. TX39_CONF_ICS_SHIFT));
  334. dcache_size = 1 << (10 + ((config & TX39_CONF_DCS_MASK) >>
  335. TX39_CONF_DCS_SHIFT));
  336. current_cpu_data.icache.linesz = 16;
  337. switch (current_cpu_data.cputype) {
  338. case CPU_TX3912:
  339. current_cpu_data.icache.ways = 1;
  340. current_cpu_data.dcache.ways = 1;
  341. current_cpu_data.dcache.linesz = 4;
  342. break;
  343. case CPU_TX3927:
  344. current_cpu_data.icache.ways = 2;
  345. current_cpu_data.dcache.ways = 2;
  346. current_cpu_data.dcache.linesz = 16;
  347. break;
  348. case CPU_TX3922:
  349. default:
  350. current_cpu_data.icache.ways = 1;
  351. current_cpu_data.dcache.ways = 1;
  352. current_cpu_data.dcache.linesz = 16;
  353. break;
  354. }
  355. }
  356. void __init ld_mmu_tx39(void)
  357. {
  358. extern void build_clear_page(void);
  359. extern void build_copy_page(void);
  360. unsigned long config;
  361. config = read_c0_conf();
  362. config &= ~TX39_CONF_WBON;
  363. write_c0_conf(config);
  364. tx39_probe_cache();
  365. switch (current_cpu_data.cputype) {
  366. case CPU_TX3912:
  367. /* TX39/H core (writethru direct-map cache) */
  368. flush_cache_all = tx39h_flush_icache_all;
  369. __flush_cache_all = tx39h_flush_icache_all;
  370. flush_cache_mm = (void *) tx39h_flush_icache_all;
  371. flush_cache_range = (void *) tx39h_flush_icache_all;
  372. flush_cache_page = (void *) tx39h_flush_icache_all;
  373. flush_icache_page = (void *) tx39h_flush_icache_all;
  374. flush_icache_range = (void *) tx39h_flush_icache_all;
  375. flush_cache_sigtramp = (void *) tx39h_flush_icache_all;
  376. flush_data_cache_page = (void *) tx39h_flush_icache_all;
  377. _dma_cache_wback_inv = tx39h_dma_cache_wback_inv;
  378. shm_align_mask = PAGE_SIZE - 1;
  379. break;
  380. case CPU_TX3922:
  381. case CPU_TX3927:
  382. default:
  383. /* TX39/H2,H3 core (writeback 2way-set-associative cache) */
  384. r3k_have_wired_reg = 1;
  385. write_c0_wired(0); /* set 8 on reset... */
  386. /* board-dependent init code may set WBON */
  387. flush_cache_all = tx39_flush_cache_all;
  388. __flush_cache_all = tx39___flush_cache_all;
  389. flush_cache_mm = tx39_flush_cache_mm;
  390. flush_cache_range = tx39_flush_cache_range;
  391. flush_cache_page = tx39_flush_cache_page;
  392. flush_icache_page = tx39_flush_icache_page;
  393. flush_icache_range = tx39_flush_icache_range;
  394. flush_cache_sigtramp = tx39_flush_cache_sigtramp;
  395. flush_data_cache_page = tx39_flush_data_cache_page;
  396. _dma_cache_wback_inv = tx39_dma_cache_wback_inv;
  397. _dma_cache_wback = tx39_dma_cache_wback_inv;
  398. _dma_cache_inv = tx39_dma_cache_inv;
  399. shm_align_mask = max_t(unsigned long,
  400. (dcache_size / current_cpu_data.dcache.ways) - 1,
  401. PAGE_SIZE - 1);
  402. break;
  403. }
  404. current_cpu_data.icache.waysize = icache_size / current_cpu_data.icache.ways;
  405. current_cpu_data.dcache.waysize = dcache_size / current_cpu_data.dcache.ways;
  406. current_cpu_data.icache.sets =
  407. current_cpu_data.icache.waysize / current_cpu_data.icache.linesz;
  408. current_cpu_data.dcache.sets =
  409. current_cpu_data.dcache.waysize / current_cpu_data.dcache.linesz;
  410. if (current_cpu_data.dcache.waysize > PAGE_SIZE)
  411. current_cpu_data.dcache.flags |= MIPS_CACHE_ALIASES;
  412. current_cpu_data.icache.waybit = 0;
  413. current_cpu_data.dcache.waybit = 0;
  414. printk("Primary instruction cache %ldkB, linesize %d bytes\n",
  415. icache_size >> 10, current_cpu_data.icache.linesz);
  416. printk("Primary data cache %ldkB, linesize %d bytes\n",
  417. dcache_size >> 10, current_cpu_data.dcache.linesz);
  418. build_clear_page();
  419. build_copy_page();
  420. tx39h_flush_icache_all();
  421. }