cache.c 14 KB

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