cache.c 10.0 KB

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