cache.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1999-2006 Helge Deller <deller@gmx.de> (07-13-1999)
  7. * Copyright (C) 1999 SuSE GmbH Nuernberg
  8. * Copyright (C) 2000 Philipp Rumpf (prumpf@tux.org)
  9. *
  10. * Cache and TLB management
  11. *
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/sched.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. #include <asm/shmparam.h>
  30. int split_tlb __read_mostly;
  31. int dcache_stride __read_mostly;
  32. int icache_stride __read_mostly;
  33. EXPORT_SYMBOL(dcache_stride);
  34. void flush_dcache_page_asm(unsigned long phys_addr, unsigned long vaddr);
  35. EXPORT_SYMBOL(flush_dcache_page_asm);
  36. void flush_icache_page_asm(unsigned long phys_addr, unsigned long vaddr);
  37. /* On some machines (e.g. ones with the Merced bus), there can be
  38. * only a single PxTLB broadcast at a time; this must be guaranteed
  39. * by software. We put a spinlock around all TLB flushes to
  40. * ensure this.
  41. */
  42. DEFINE_SPINLOCK(pa_tlb_lock);
  43. struct pdc_cache_info cache_info __read_mostly;
  44. #ifndef CONFIG_PA20
  45. static struct pdc_btlb_info btlb_info __read_mostly;
  46. #endif
  47. #ifdef CONFIG_SMP
  48. void
  49. flush_data_cache(void)
  50. {
  51. on_each_cpu(flush_data_cache_local, NULL, 1);
  52. }
  53. void
  54. flush_instruction_cache(void)
  55. {
  56. on_each_cpu(flush_instruction_cache_local, NULL, 1);
  57. }
  58. #endif
  59. void
  60. flush_cache_all_local(void)
  61. {
  62. flush_instruction_cache_local(NULL);
  63. flush_data_cache_local(NULL);
  64. }
  65. EXPORT_SYMBOL(flush_cache_all_local);
  66. void
  67. update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
  68. {
  69. struct page *page = pte_page(*ptep);
  70. if (pfn_valid(page_to_pfn(page)) && page_mapping(page) &&
  71. test_bit(PG_dcache_dirty, &page->flags)) {
  72. flush_kernel_dcache_page(page);
  73. clear_bit(PG_dcache_dirty, &page->flags);
  74. } else if (parisc_requires_coherency())
  75. flush_kernel_dcache_page(page);
  76. }
  77. void
  78. show_cache_info(struct seq_file *m)
  79. {
  80. char buf[32];
  81. seq_printf(m, "I-cache\t\t: %ld KB\n",
  82. cache_info.ic_size/1024 );
  83. if (cache_info.dc_loop != 1)
  84. snprintf(buf, 32, "%lu-way associative", cache_info.dc_loop);
  85. seq_printf(m, "D-cache\t\t: %ld KB (%s%s, %s)\n",
  86. cache_info.dc_size/1024,
  87. (cache_info.dc_conf.cc_wt ? "WT":"WB"),
  88. (cache_info.dc_conf.cc_sh ? ", shared I/D":""),
  89. ((cache_info.dc_loop == 1) ? "direct mapped" : buf));
  90. seq_printf(m, "ITLB entries\t: %ld\n" "DTLB entries\t: %ld%s\n",
  91. cache_info.it_size,
  92. cache_info.dt_size,
  93. cache_info.dt_conf.tc_sh ? " - shared with ITLB":""
  94. );
  95. #ifndef CONFIG_PA20
  96. /* BTLB - Block TLB */
  97. if (btlb_info.max_size==0) {
  98. seq_printf(m, "BTLB\t\t: not supported\n" );
  99. } else {
  100. seq_printf(m,
  101. "BTLB fixed\t: max. %d pages, pagesize=%d (%dMB)\n"
  102. "BTLB fix-entr.\t: %d instruction, %d data (%d combined)\n"
  103. "BTLB var-entr.\t: %d instruction, %d data (%d combined)\n",
  104. btlb_info.max_size, (int)4096,
  105. btlb_info.max_size>>8,
  106. btlb_info.fixed_range_info.num_i,
  107. btlb_info.fixed_range_info.num_d,
  108. btlb_info.fixed_range_info.num_comb,
  109. btlb_info.variable_range_info.num_i,
  110. btlb_info.variable_range_info.num_d,
  111. btlb_info.variable_range_info.num_comb
  112. );
  113. }
  114. #endif
  115. }
  116. void __init
  117. parisc_cache_init(void)
  118. {
  119. if (pdc_cache_info(&cache_info) < 0)
  120. panic("parisc_cache_init: pdc_cache_info failed");
  121. #if 0
  122. printk("ic_size %lx dc_size %lx it_size %lx\n",
  123. cache_info.ic_size,
  124. cache_info.dc_size,
  125. cache_info.it_size);
  126. printk("DC base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx\n",
  127. cache_info.dc_base,
  128. cache_info.dc_stride,
  129. cache_info.dc_count,
  130. cache_info.dc_loop);
  131. printk("dc_conf = 0x%lx alias %d blk %d line %d shift %d\n",
  132. *(unsigned long *) (&cache_info.dc_conf),
  133. cache_info.dc_conf.cc_alias,
  134. cache_info.dc_conf.cc_block,
  135. cache_info.dc_conf.cc_line,
  136. cache_info.dc_conf.cc_shift);
  137. printk(" wt %d sh %d cst %d hv %d\n",
  138. cache_info.dc_conf.cc_wt,
  139. cache_info.dc_conf.cc_sh,
  140. cache_info.dc_conf.cc_cst,
  141. cache_info.dc_conf.cc_hv);
  142. printk("IC base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx\n",
  143. cache_info.ic_base,
  144. cache_info.ic_stride,
  145. cache_info.ic_count,
  146. cache_info.ic_loop);
  147. printk("ic_conf = 0x%lx alias %d blk %d line %d shift %d\n",
  148. *(unsigned long *) (&cache_info.ic_conf),
  149. cache_info.ic_conf.cc_alias,
  150. cache_info.ic_conf.cc_block,
  151. cache_info.ic_conf.cc_line,
  152. cache_info.ic_conf.cc_shift);
  153. printk(" wt %d sh %d cst %d hv %d\n",
  154. cache_info.ic_conf.cc_wt,
  155. cache_info.ic_conf.cc_sh,
  156. cache_info.ic_conf.cc_cst,
  157. cache_info.ic_conf.cc_hv);
  158. printk("D-TLB conf: sh %d page %d cst %d aid %d pad1 %d\n",
  159. cache_info.dt_conf.tc_sh,
  160. cache_info.dt_conf.tc_page,
  161. cache_info.dt_conf.tc_cst,
  162. cache_info.dt_conf.tc_aid,
  163. cache_info.dt_conf.tc_pad1);
  164. printk("I-TLB conf: sh %d page %d cst %d aid %d pad1 %d\n",
  165. cache_info.it_conf.tc_sh,
  166. cache_info.it_conf.tc_page,
  167. cache_info.it_conf.tc_cst,
  168. cache_info.it_conf.tc_aid,
  169. cache_info.it_conf.tc_pad1);
  170. #endif
  171. split_tlb = 0;
  172. if (cache_info.dt_conf.tc_sh == 0 || cache_info.dt_conf.tc_sh == 2) {
  173. if (cache_info.dt_conf.tc_sh == 2)
  174. printk(KERN_WARNING "Unexpected TLB configuration. "
  175. "Will flush I/D separately (could be optimized).\n");
  176. split_tlb = 1;
  177. }
  178. /* "New and Improved" version from Jim Hull
  179. * (1 << (cc_block-1)) * (cc_line << (4 + cnf.cc_shift))
  180. * The following CAFL_STRIDE is an optimized version, see
  181. * http://lists.parisc-linux.org/pipermail/parisc-linux/2004-June/023625.html
  182. * http://lists.parisc-linux.org/pipermail/parisc-linux/2004-June/023671.html
  183. */
  184. #define CAFL_STRIDE(cnf) (cnf.cc_line << (3 + cnf.cc_block + cnf.cc_shift))
  185. dcache_stride = CAFL_STRIDE(cache_info.dc_conf);
  186. icache_stride = CAFL_STRIDE(cache_info.ic_conf);
  187. #undef CAFL_STRIDE
  188. #ifndef CONFIG_PA20
  189. if (pdc_btlb_info(&btlb_info) < 0) {
  190. memset(&btlb_info, 0, sizeof btlb_info);
  191. }
  192. #endif
  193. if ((boot_cpu_data.pdc.capabilities & PDC_MODEL_NVA_MASK) ==
  194. PDC_MODEL_NVA_UNSUPPORTED) {
  195. printk(KERN_WARNING "parisc_cache_init: Only equivalent aliasing supported!\n");
  196. #if 0
  197. panic("SMP kernel required to avoid non-equivalent aliasing");
  198. #endif
  199. }
  200. }
  201. void disable_sr_hashing(void)
  202. {
  203. int srhash_type, retval;
  204. unsigned long space_bits;
  205. switch (boot_cpu_data.cpu_type) {
  206. case pcx: /* We shouldn't get this far. setup.c should prevent it. */
  207. BUG();
  208. return;
  209. case pcxs:
  210. case pcxt:
  211. case pcxt_:
  212. srhash_type = SRHASH_PCXST;
  213. break;
  214. case pcxl:
  215. srhash_type = SRHASH_PCXL;
  216. break;
  217. case pcxl2: /* pcxl2 doesn't support space register hashing */
  218. return;
  219. default: /* Currently all PA2.0 machines use the same ins. sequence */
  220. srhash_type = SRHASH_PA20;
  221. break;
  222. }
  223. disable_sr_hashing_asm(srhash_type);
  224. retval = pdc_spaceid_bits(&space_bits);
  225. /* If this procedure isn't implemented, don't panic. */
  226. if (retval < 0 && retval != PDC_BAD_OPTION)
  227. panic("pdc_spaceid_bits call failed.\n");
  228. if (space_bits != 0)
  229. panic("SpaceID hashing is still on!\n");
  230. }
  231. static inline void
  232. __flush_cache_page(struct vm_area_struct *vma, unsigned long vmaddr,
  233. unsigned long physaddr)
  234. {
  235. flush_dcache_page_asm(physaddr, vmaddr);
  236. if (vma->vm_flags & VM_EXEC)
  237. flush_icache_page_asm(physaddr, vmaddr);
  238. }
  239. void flush_dcache_page(struct page *page)
  240. {
  241. struct address_space *mapping = page_mapping(page);
  242. struct vm_area_struct *mpnt;
  243. struct prio_tree_iter iter;
  244. unsigned long offset;
  245. unsigned long addr, old_addr = 0;
  246. pgoff_t pgoff;
  247. if (mapping && !mapping_mapped(mapping)) {
  248. set_bit(PG_dcache_dirty, &page->flags);
  249. return;
  250. }
  251. flush_kernel_dcache_page(page);
  252. if (!mapping)
  253. return;
  254. pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  255. /* We have carefully arranged in arch_get_unmapped_area() that
  256. * *any* mappings of a file are always congruently mapped (whether
  257. * declared as MAP_PRIVATE or MAP_SHARED), so we only need
  258. * to flush one address here for them all to become coherent */
  259. flush_dcache_mmap_lock(mapping);
  260. vma_prio_tree_foreach(mpnt, &iter, &mapping->i_mmap, pgoff, pgoff) {
  261. offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
  262. addr = mpnt->vm_start + offset;
  263. /* The TLB is the engine of coherence on parisc: The
  264. * CPU is entitled to speculate any page with a TLB
  265. * mapping, so here we kill the mapping then flush the
  266. * page along a special flush only alias mapping.
  267. * This guarantees that the page is no-longer in the
  268. * cache for any process and nor may it be
  269. * speculatively read in (until the user or kernel
  270. * specifically accesses it, of course) */
  271. flush_tlb_page(mpnt, addr);
  272. if (old_addr == 0 || (old_addr & (SHMLBA - 1)) != (addr & (SHMLBA - 1))) {
  273. __flush_cache_page(mpnt, addr, page_to_phys(page));
  274. if (old_addr)
  275. printk(KERN_ERR "INEQUIVALENT ALIASES 0x%lx and 0x%lx in file %s\n", old_addr, addr, mpnt->vm_file ? (char *)mpnt->vm_file->f_path.dentry->d_name.name : "(null)");
  276. old_addr = addr;
  277. }
  278. }
  279. flush_dcache_mmap_unlock(mapping);
  280. }
  281. EXPORT_SYMBOL(flush_dcache_page);
  282. /* Defined in arch/parisc/kernel/pacache.S */
  283. EXPORT_SYMBOL(flush_kernel_dcache_range_asm);
  284. EXPORT_SYMBOL(flush_kernel_dcache_page_asm);
  285. EXPORT_SYMBOL(flush_data_cache_local);
  286. EXPORT_SYMBOL(flush_kernel_icache_range_asm);
  287. void clear_user_page_asm(void *page, unsigned long vaddr)
  288. {
  289. unsigned long flags;
  290. /* This function is implemented in assembly in pacache.S */
  291. extern void __clear_user_page_asm(void *page, unsigned long vaddr);
  292. purge_tlb_start(flags);
  293. __clear_user_page_asm(page, vaddr);
  294. purge_tlb_end(flags);
  295. }
  296. #define FLUSH_THRESHOLD 0x80000 /* 0.5MB */
  297. int parisc_cache_flush_threshold __read_mostly = FLUSH_THRESHOLD;
  298. void __init parisc_setup_cache_timing(void)
  299. {
  300. unsigned long rangetime, alltime;
  301. unsigned long size;
  302. alltime = mfctl(16);
  303. flush_data_cache();
  304. alltime = mfctl(16) - alltime;
  305. size = (unsigned long)(_end - _text);
  306. rangetime = mfctl(16);
  307. flush_kernel_dcache_range((unsigned long)_text, size);
  308. rangetime = mfctl(16) - rangetime;
  309. printk(KERN_DEBUG "Whole cache flush %lu cycles, flushing %lu bytes %lu cycles\n",
  310. alltime, size, rangetime);
  311. /* Racy, but if we see an intermediate value, it's ok too... */
  312. parisc_cache_flush_threshold = size * alltime / rangetime;
  313. parisc_cache_flush_threshold = (parisc_cache_flush_threshold + L1_CACHE_BYTES - 1) &~ (L1_CACHE_BYTES - 1);
  314. if (!parisc_cache_flush_threshold)
  315. parisc_cache_flush_threshold = FLUSH_THRESHOLD;
  316. if (parisc_cache_flush_threshold > cache_info.dc_size)
  317. parisc_cache_flush_threshold = cache_info.dc_size;
  318. printk(KERN_INFO "Setting cache flush threshold to %x (%d CPUs online)\n", parisc_cache_flush_threshold, num_online_cpus());
  319. }
  320. extern void purge_kernel_dcache_page(unsigned long);
  321. extern void clear_user_page_asm(void *page, unsigned long vaddr);
  322. void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
  323. {
  324. unsigned long flags;
  325. purge_kernel_dcache_page((unsigned long)page);
  326. purge_tlb_start(flags);
  327. pdtlb_kernel(page);
  328. purge_tlb_end(flags);
  329. clear_user_page_asm(page, vaddr);
  330. }
  331. EXPORT_SYMBOL(clear_user_page);
  332. void flush_kernel_dcache_page_addr(void *addr)
  333. {
  334. unsigned long flags;
  335. flush_kernel_dcache_page_asm(addr);
  336. purge_tlb_start(flags);
  337. pdtlb_kernel(addr);
  338. purge_tlb_end(flags);
  339. }
  340. EXPORT_SYMBOL(flush_kernel_dcache_page_addr);
  341. void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
  342. struct page *pg)
  343. {
  344. /* no coherency needed (all in kmap/kunmap) */
  345. copy_user_page_asm(vto, vfrom);
  346. if (!parisc_requires_coherency())
  347. flush_kernel_dcache_page_asm(vto);
  348. }
  349. EXPORT_SYMBOL(copy_user_page);
  350. #ifdef CONFIG_PA8X00
  351. void kunmap_parisc(void *addr)
  352. {
  353. if (parisc_requires_coherency())
  354. flush_kernel_dcache_page_addr(addr);
  355. }
  356. EXPORT_SYMBOL(kunmap_parisc);
  357. #endif
  358. void __flush_tlb_range(unsigned long sid, unsigned long start,
  359. unsigned long end)
  360. {
  361. unsigned long npages;
  362. npages = ((end - (start & PAGE_MASK)) + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  363. if (npages >= 512) /* 2MB of space: arbitrary, should be tuned */
  364. flush_tlb_all();
  365. else {
  366. unsigned long flags;
  367. mtsp(sid, 1);
  368. purge_tlb_start(flags);
  369. if (split_tlb) {
  370. while (npages--) {
  371. pdtlb(start);
  372. pitlb(start);
  373. start += PAGE_SIZE;
  374. }
  375. } else {
  376. while (npages--) {
  377. pdtlb(start);
  378. start += PAGE_SIZE;
  379. }
  380. }
  381. purge_tlb_end(flags);
  382. }
  383. }
  384. static void cacheflush_h_tmp_function(void *dummy)
  385. {
  386. flush_cache_all_local();
  387. }
  388. void flush_cache_all(void)
  389. {
  390. on_each_cpu(cacheflush_h_tmp_function, NULL, 1);
  391. }
  392. void flush_cache_mm(struct mm_struct *mm)
  393. {
  394. #ifdef CONFIG_SMP
  395. flush_cache_all();
  396. #else
  397. flush_cache_all_local();
  398. #endif
  399. }
  400. void
  401. flush_user_dcache_range(unsigned long start, unsigned long end)
  402. {
  403. if ((end - start) < parisc_cache_flush_threshold)
  404. flush_user_dcache_range_asm(start,end);
  405. else
  406. flush_data_cache();
  407. }
  408. void
  409. flush_user_icache_range(unsigned long start, unsigned long end)
  410. {
  411. if ((end - start) < parisc_cache_flush_threshold)
  412. flush_user_icache_range_asm(start,end);
  413. else
  414. flush_instruction_cache();
  415. }
  416. void flush_cache_range(struct vm_area_struct *vma,
  417. unsigned long start, unsigned long end)
  418. {
  419. int sr3;
  420. BUG_ON(!vma->vm_mm->context);
  421. sr3 = mfsp(3);
  422. if (vma->vm_mm->context == sr3) {
  423. flush_user_dcache_range(start,end);
  424. flush_user_icache_range(start,end);
  425. } else {
  426. flush_cache_all();
  427. }
  428. }
  429. void
  430. flush_cache_page(struct vm_area_struct *vma, unsigned long vmaddr, unsigned long pfn)
  431. {
  432. BUG_ON(!vma->vm_mm->context);
  433. flush_tlb_page(vma, vmaddr);
  434. __flush_cache_page(vma, vmaddr, page_to_phys(pfn_to_page(pfn)));
  435. }