cache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* $Id: cache.c,v 1.4 2000/01/25 00:11:38 prumpf Exp $
  2. *
  3. * This file is subject to the terms and conditions of the GNU General Public
  4. * License. See the file "COPYING" in the main directory of this archive
  5. * for more details.
  6. *
  7. * Copyright (C) 1999-2006 Helge Deller <deller@gmx.de> (07-13-1999)
  8. * Copyright (C) 1999 SuSE GmbH Nuernberg
  9. * Copyright (C) 2000 Philipp Rumpf (prumpf@tux.org)
  10. *
  11. * Cache and TLB management
  12. *
  13. */
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/module.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/pagemap.h>
  20. #include <asm/pdc.h>
  21. #include <asm/cache.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/system.h>
  25. #include <asm/page.h>
  26. #include <asm/pgalloc.h>
  27. #include <asm/processor.h>
  28. #include <asm/sections.h>
  29. int split_tlb __read_mostly;
  30. int dcache_stride __read_mostly;
  31. int icache_stride __read_mostly;
  32. EXPORT_SYMBOL(dcache_stride);
  33. /* On some machines (e.g. ones with the Merced bus), there can be
  34. * only a single PxTLB broadcast at a time; this must be guaranteed
  35. * by software. We put a spinlock around all TLB flushes to
  36. * ensure this.
  37. */
  38. DEFINE_SPINLOCK(pa_tlb_lock);
  39. struct pdc_cache_info cache_info __read_mostly;
  40. #ifndef CONFIG_PA20
  41. static struct pdc_btlb_info btlb_info __read_mostly;
  42. #endif
  43. #ifdef CONFIG_SMP
  44. void
  45. flush_data_cache(void)
  46. {
  47. on_each_cpu(flush_data_cache_local, NULL, 1, 1);
  48. }
  49. void
  50. flush_instruction_cache(void)
  51. {
  52. on_each_cpu(flush_instruction_cache_local, NULL, 1, 1);
  53. }
  54. #endif
  55. void
  56. flush_cache_all_local(void)
  57. {
  58. flush_instruction_cache_local(NULL);
  59. flush_data_cache_local(NULL);
  60. }
  61. EXPORT_SYMBOL(flush_cache_all_local);
  62. /* flushes EVERYTHING (tlb & cache) */
  63. void
  64. flush_all_caches(void)
  65. {
  66. flush_cache_all();
  67. flush_tlb_all();
  68. }
  69. EXPORT_SYMBOL(flush_all_caches);
  70. void
  71. update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  72. {
  73. struct page *page = pte_page(pte);
  74. if (pfn_valid(page_to_pfn(page)) && page_mapping(page) &&
  75. test_bit(PG_dcache_dirty, &page->flags)) {
  76. flush_kernel_dcache_page(page);
  77. clear_bit(PG_dcache_dirty, &page->flags);
  78. } else if (parisc_requires_coherency())
  79. flush_kernel_dcache_page(page);
  80. }
  81. void
  82. show_cache_info(struct seq_file *m)
  83. {
  84. char buf[32];
  85. seq_printf(m, "I-cache\t\t: %ld KB\n",
  86. cache_info.ic_size/1024 );
  87. if (cache_info.dc_loop == 1)
  88. snprintf(buf, 32, "%lu-way associative", cache_info.dc_loop);
  89. seq_printf(m, "D-cache\t\t: %ld KB (%s%s, %s)\n",
  90. cache_info.dc_size/1024,
  91. (cache_info.dc_conf.cc_wt ? "WT":"WB"),
  92. (cache_info.dc_conf.cc_sh ? ", shared I/D":""),
  93. ((cache_info.dc_loop == 1) ? "direct mapped" : buf));
  94. seq_printf(m, "ITLB entries\t: %ld\n" "DTLB entries\t: %ld%s\n",
  95. cache_info.it_size,
  96. cache_info.dt_size,
  97. cache_info.dt_conf.tc_sh ? " - shared with ITLB":""
  98. );
  99. #ifndef CONFIG_PA20
  100. /* BTLB - Block TLB */
  101. if (btlb_info.max_size==0) {
  102. seq_printf(m, "BTLB\t\t: not supported\n" );
  103. } else {
  104. seq_printf(m,
  105. "BTLB fixed\t: max. %d pages, pagesize=%d (%dMB)\n"
  106. "BTLB fix-entr.\t: %d instruction, %d data (%d combined)\n"
  107. "BTLB var-entr.\t: %d instruction, %d data (%d combined)\n",
  108. btlb_info.max_size, (int)4096,
  109. btlb_info.max_size>>8,
  110. btlb_info.fixed_range_info.num_i,
  111. btlb_info.fixed_range_info.num_d,
  112. btlb_info.fixed_range_info.num_comb,
  113. btlb_info.variable_range_info.num_i,
  114. btlb_info.variable_range_info.num_d,
  115. btlb_info.variable_range_info.num_comb
  116. );
  117. }
  118. #endif
  119. }
  120. void __init
  121. parisc_cache_init(void)
  122. {
  123. if (pdc_cache_info(&cache_info) < 0)
  124. panic("parisc_cache_init: pdc_cache_info failed");
  125. #if 0
  126. printk("ic_size %lx dc_size %lx it_size %lx\n",
  127. cache_info.ic_size,
  128. cache_info.dc_size,
  129. cache_info.it_size);
  130. printk("DC base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx\n",
  131. cache_info.dc_base,
  132. cache_info.dc_stride,
  133. cache_info.dc_count,
  134. cache_info.dc_loop);
  135. printk("dc_conf = 0x%lx alias %d blk %d line %d shift %d\n",
  136. *(unsigned long *) (&cache_info.dc_conf),
  137. cache_info.dc_conf.cc_alias,
  138. cache_info.dc_conf.cc_block,
  139. cache_info.dc_conf.cc_line,
  140. cache_info.dc_conf.cc_shift);
  141. printk(" wt %d sh %d cst %d hv %d\n",
  142. cache_info.dc_conf.cc_wt,
  143. cache_info.dc_conf.cc_sh,
  144. cache_info.dc_conf.cc_cst,
  145. cache_info.dc_conf.cc_hv);
  146. printk("IC base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx\n",
  147. cache_info.ic_base,
  148. cache_info.ic_stride,
  149. cache_info.ic_count,
  150. cache_info.ic_loop);
  151. printk("ic_conf = 0x%lx alias %d blk %d line %d shift %d\n",
  152. *(unsigned long *) (&cache_info.ic_conf),
  153. cache_info.ic_conf.cc_alias,
  154. cache_info.ic_conf.cc_block,
  155. cache_info.ic_conf.cc_line,
  156. cache_info.ic_conf.cc_shift);
  157. printk(" wt %d sh %d cst %d hv %d\n",
  158. cache_info.ic_conf.cc_wt,
  159. cache_info.ic_conf.cc_sh,
  160. cache_info.ic_conf.cc_cst,
  161. cache_info.ic_conf.cc_hv);
  162. printk("D-TLB conf: sh %d page %d cst %d aid %d pad1 %d \n",
  163. cache_info.dt_conf.tc_sh,
  164. cache_info.dt_conf.tc_page,
  165. cache_info.dt_conf.tc_cst,
  166. cache_info.dt_conf.tc_aid,
  167. cache_info.dt_conf.tc_pad1);
  168. printk("I-TLB conf: sh %d page %d cst %d aid %d pad1 %d \n",
  169. cache_info.it_conf.tc_sh,
  170. cache_info.it_conf.tc_page,
  171. cache_info.it_conf.tc_cst,
  172. cache_info.it_conf.tc_aid,
  173. cache_info.it_conf.tc_pad1);
  174. #endif
  175. split_tlb = 0;
  176. if (cache_info.dt_conf.tc_sh == 0 || cache_info.dt_conf.tc_sh == 2) {
  177. if (cache_info.dt_conf.tc_sh == 2)
  178. printk(KERN_WARNING "Unexpected TLB configuration. "
  179. "Will flush I/D separately (could be optimized).\n");
  180. split_tlb = 1;
  181. }
  182. /* "New and Improved" version from Jim Hull
  183. * (1 << (cc_block-1)) * (cc_line << (4 + cnf.cc_shift))
  184. * The following CAFL_STRIDE is an optimized version, see
  185. * http://lists.parisc-linux.org/pipermail/parisc-linux/2004-June/023625.html
  186. * http://lists.parisc-linux.org/pipermail/parisc-linux/2004-June/023671.html
  187. */
  188. #define CAFL_STRIDE(cnf) (cnf.cc_line << (3 + cnf.cc_block + cnf.cc_shift))
  189. dcache_stride = CAFL_STRIDE(cache_info.dc_conf);
  190. icache_stride = CAFL_STRIDE(cache_info.ic_conf);
  191. #undef CAFL_STRIDE
  192. #ifndef CONFIG_PA20
  193. if (pdc_btlb_info(&btlb_info) < 0) {
  194. memset(&btlb_info, 0, sizeof btlb_info);
  195. }
  196. #endif
  197. if ((boot_cpu_data.pdc.capabilities & PDC_MODEL_NVA_MASK) ==
  198. PDC_MODEL_NVA_UNSUPPORTED) {
  199. printk(KERN_WARNING "parisc_cache_init: Only equivalent aliasing supported!\n");
  200. #if 0
  201. panic("SMP kernel required to avoid non-equivalent aliasing");
  202. #endif
  203. }
  204. }
  205. void disable_sr_hashing(void)
  206. {
  207. int srhash_type, retval;
  208. unsigned long space_bits;
  209. switch (boot_cpu_data.cpu_type) {
  210. case pcx: /* We shouldn't get this far. setup.c should prevent it. */
  211. BUG();
  212. return;
  213. case pcxs:
  214. case pcxt:
  215. case pcxt_:
  216. srhash_type = SRHASH_PCXST;
  217. break;
  218. case pcxl:
  219. srhash_type = SRHASH_PCXL;
  220. break;
  221. case pcxl2: /* pcxl2 doesn't support space register hashing */
  222. return;
  223. default: /* Currently all PA2.0 machines use the same ins. sequence */
  224. srhash_type = SRHASH_PA20;
  225. break;
  226. }
  227. disable_sr_hashing_asm(srhash_type);
  228. retval = pdc_spaceid_bits(&space_bits);
  229. /* If this procedure isn't implemented, don't panic. */
  230. if (retval < 0 && retval != PDC_BAD_OPTION)
  231. panic("pdc_spaceid_bits call failed.\n");
  232. if (space_bits != 0)
  233. panic("SpaceID hashing is still on!\n");
  234. }
  235. void flush_dcache_page(struct page *page)
  236. {
  237. struct address_space *mapping = page_mapping(page);
  238. struct vm_area_struct *mpnt;
  239. struct prio_tree_iter iter;
  240. unsigned long offset;
  241. unsigned long addr;
  242. pgoff_t pgoff;
  243. unsigned long pfn = page_to_pfn(page);
  244. if (mapping && !mapping_mapped(mapping)) {
  245. set_bit(PG_dcache_dirty, &page->flags);
  246. return;
  247. }
  248. flush_kernel_dcache_page(page);
  249. if (!mapping)
  250. return;
  251. pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  252. /* We have carefully arranged in arch_get_unmapped_area() that
  253. * *any* mappings of a file are always congruently mapped (whether
  254. * declared as MAP_PRIVATE or MAP_SHARED), so we only need
  255. * to flush one address here for them all to become coherent */
  256. flush_dcache_mmap_lock(mapping);
  257. vma_prio_tree_foreach(mpnt, &iter, &mapping->i_mmap, pgoff, pgoff) {
  258. offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
  259. addr = mpnt->vm_start + offset;
  260. /* Flush instructions produce non access tlb misses.
  261. * On PA, we nullify these instructions rather than
  262. * taking a page fault if the pte doesn't exist.
  263. * This is just for speed. If the page translation
  264. * isn't there, there's no point exciting the
  265. * nadtlb handler into a nullification frenzy.
  266. *
  267. * Make sure we really have this page: the private
  268. * mappings may cover this area but have COW'd this
  269. * particular page.
  270. */
  271. if (translation_exists(mpnt, addr, pfn)) {
  272. __flush_cache_page(mpnt, addr);
  273. break;
  274. }
  275. }
  276. flush_dcache_mmap_unlock(mapping);
  277. }
  278. EXPORT_SYMBOL(flush_dcache_page);
  279. /* Defined in arch/parisc/kernel/pacache.S */
  280. EXPORT_SYMBOL(flush_kernel_dcache_range_asm);
  281. EXPORT_SYMBOL(flush_kernel_dcache_page_asm);
  282. EXPORT_SYMBOL(flush_data_cache_local);
  283. EXPORT_SYMBOL(flush_kernel_icache_range_asm);
  284. void clear_user_page_asm(void *page, unsigned long vaddr)
  285. {
  286. /* This function is implemented in assembly in pacache.S */
  287. extern void __clear_user_page_asm(void *page, unsigned long vaddr);
  288. purge_tlb_start();
  289. __clear_user_page_asm(page, vaddr);
  290. purge_tlb_end();
  291. }
  292. #define FLUSH_THRESHOLD 0x80000 /* 0.5MB */
  293. int parisc_cache_flush_threshold __read_mostly = FLUSH_THRESHOLD;
  294. void parisc_setup_cache_timing(void)
  295. {
  296. unsigned long rangetime, alltime;
  297. unsigned long size;
  298. alltime = mfctl(16);
  299. flush_data_cache();
  300. alltime = mfctl(16) - alltime;
  301. size = (unsigned long)(_end - _text);
  302. rangetime = mfctl(16);
  303. flush_kernel_dcache_range((unsigned long)_text, size);
  304. rangetime = mfctl(16) - rangetime;
  305. printk(KERN_DEBUG "Whole cache flush %lu cycles, flushing %lu bytes %lu cycles\n",
  306. alltime, size, rangetime);
  307. /* Racy, but if we see an intermediate value, it's ok too... */
  308. parisc_cache_flush_threshold = size * alltime / rangetime;
  309. parisc_cache_flush_threshold = (parisc_cache_flush_threshold + L1_CACHE_BYTES - 1) &~ (L1_CACHE_BYTES - 1);
  310. if (!parisc_cache_flush_threshold)
  311. parisc_cache_flush_threshold = FLUSH_THRESHOLD;
  312. printk(KERN_INFO "Setting cache flush threshold to %x (%d CPUs online)\n", parisc_cache_flush_threshold, num_online_cpus());
  313. }
  314. extern void purge_kernel_dcache_page(unsigned long);
  315. extern void clear_user_page_asm(void *page, unsigned long vaddr);
  316. void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
  317. {
  318. purge_kernel_dcache_page((unsigned long)page);
  319. purge_tlb_start();
  320. pdtlb_kernel(page);
  321. purge_tlb_end();
  322. clear_user_page_asm(page, vaddr);
  323. }
  324. EXPORT_SYMBOL(clear_user_page);
  325. void flush_kernel_dcache_page_addr(void *addr)
  326. {
  327. flush_kernel_dcache_page_asm(addr);
  328. purge_tlb_start();
  329. pdtlb_kernel(addr);
  330. purge_tlb_end();
  331. }
  332. EXPORT_SYMBOL(flush_kernel_dcache_page_addr);
  333. void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
  334. struct page *pg)
  335. {
  336. /* no coherency needed (all in kmap/kunmap) */
  337. copy_user_page_asm(vto, vfrom);
  338. if (!parisc_requires_coherency())
  339. flush_kernel_dcache_page_asm(vto);
  340. }
  341. EXPORT_SYMBOL(copy_user_page);
  342. #ifdef CONFIG_PA8X00
  343. void kunmap_parisc(void *addr)
  344. {
  345. if (parisc_requires_coherency())
  346. flush_kernel_dcache_page_addr(addr);
  347. }
  348. EXPORT_SYMBOL(kunmap_parisc);
  349. #endif