cache.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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 <linux/sched/mm.h>
  21. #include <linux/syscalls.h>
  22. #include <linux/vmalloc.h>
  23. #include <asm/pdc.h>
  24. #include <asm/cache.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm/page.h>
  28. #include <asm/processor.h>
  29. #include <asm/sections.h>
  30. #include <asm/shmparam.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/cachectl.h>
  33. #define PTR_PAGE_ALIGN_DOWN(addr) PTR_ALIGN_DOWN(addr, PAGE_SIZE)
  34. /*
  35. * When nonzero, use _PAGE_ACCESSED bit to try to reduce the number
  36. * of page flushes done flush_cache_page_if_present. There are some
  37. * pros and cons in using this option. It may increase the risk of
  38. * random segmentation faults.
  39. */
  40. #define CONFIG_FLUSH_PAGE_ACCESSED 0
  41. int split_tlb __ro_after_init;
  42. int dcache_stride __ro_after_init;
  43. int icache_stride __ro_after_init;
  44. EXPORT_SYMBOL(dcache_stride);
  45. /* Internal implementation in arch/parisc/kernel/pacache.S */
  46. void flush_dcache_page_asm(unsigned long phys_addr, unsigned long vaddr);
  47. EXPORT_SYMBOL(flush_dcache_page_asm);
  48. void purge_dcache_page_asm(unsigned long phys_addr, unsigned long vaddr);
  49. void flush_icache_page_asm(unsigned long phys_addr, unsigned long vaddr);
  50. void flush_data_cache_local(void *); /* flushes local data-cache only */
  51. void flush_instruction_cache_local(void); /* flushes local code-cache only */
  52. static void flush_kernel_dcache_page_addr(const void *addr);
  53. /* On some machines (i.e., ones with the Merced bus), there can be
  54. * only a single PxTLB broadcast at a time; this must be guaranteed
  55. * by software. We need a spinlock around all TLB flushes to ensure
  56. * this.
  57. */
  58. DEFINE_SPINLOCK(pa_tlb_flush_lock);
  59. #if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
  60. int pa_serialize_tlb_flushes __ro_after_init;
  61. #endif
  62. struct pdc_cache_info cache_info __ro_after_init;
  63. #ifndef CONFIG_PA20
  64. struct pdc_btlb_info btlb_info;
  65. #endif
  66. DEFINE_STATIC_KEY_TRUE(parisc_has_cache);
  67. DEFINE_STATIC_KEY_TRUE(parisc_has_dcache);
  68. DEFINE_STATIC_KEY_TRUE(parisc_has_icache);
  69. static void cache_flush_local_cpu(void *dummy)
  70. {
  71. if (static_branch_likely(&parisc_has_icache))
  72. flush_instruction_cache_local();
  73. if (static_branch_likely(&parisc_has_dcache))
  74. flush_data_cache_local(NULL);
  75. }
  76. void flush_cache_all_local(void)
  77. {
  78. cache_flush_local_cpu(NULL);
  79. }
  80. void flush_cache_all(void)
  81. {
  82. if (static_branch_likely(&parisc_has_cache))
  83. on_each_cpu(cache_flush_local_cpu, NULL, 1);
  84. }
  85. static inline void flush_data_cache(void)
  86. {
  87. if (static_branch_likely(&parisc_has_dcache))
  88. on_each_cpu(flush_data_cache_local, NULL, 1);
  89. }
  90. /* Kernel virtual address of pfn. */
  91. #define pfn_va(pfn) __va(PFN_PHYS(pfn))
  92. void __update_cache(pte_t pte)
  93. {
  94. unsigned long pfn = pte_pfn(pte);
  95. struct folio *folio;
  96. unsigned int nr;
  97. /* We don't have pte special. As a result, we can be called with
  98. an invalid pfn and we don't need to flush the kernel dcache page.
  99. This occurs with FireGL card in C8000. */
  100. if (!pfn_valid(pfn))
  101. return;
  102. folio = page_folio(pfn_to_page(pfn));
  103. pfn = folio_pfn(folio);
  104. nr = folio_nr_pages(folio);
  105. if (folio_flush_mapping(folio) &&
  106. test_bit(PG_dcache_dirty, &folio->flags)) {
  107. while (nr--)
  108. flush_kernel_dcache_page_addr(pfn_va(pfn + nr));
  109. clear_bit(PG_dcache_dirty, &folio->flags);
  110. } else if (parisc_requires_coherency())
  111. while (nr--)
  112. flush_kernel_dcache_page_addr(pfn_va(pfn + nr));
  113. }
  114. void
  115. show_cache_info(struct seq_file *m)
  116. {
  117. char buf[32];
  118. seq_printf(m, "I-cache\t\t: %ld KB\n",
  119. cache_info.ic_size/1024 );
  120. if (cache_info.dc_loop != 1)
  121. snprintf(buf, 32, "%lu-way associative", cache_info.dc_loop);
  122. seq_printf(m, "D-cache\t\t: %ld KB (%s%s, %s, alias=%d)\n",
  123. cache_info.dc_size/1024,
  124. (cache_info.dc_conf.cc_wt ? "WT":"WB"),
  125. (cache_info.dc_conf.cc_sh ? ", shared I/D":""),
  126. ((cache_info.dc_loop == 1) ? "direct mapped" : buf),
  127. cache_info.dc_conf.cc_alias
  128. );
  129. seq_printf(m, "ITLB entries\t: %ld\n" "DTLB entries\t: %ld%s\n",
  130. cache_info.it_size,
  131. cache_info.dt_size,
  132. cache_info.dt_conf.tc_sh ? " - shared with ITLB":""
  133. );
  134. #ifndef CONFIG_PA20
  135. /* BTLB - Block TLB */
  136. if (btlb_info.max_size==0) {
  137. seq_printf(m, "BTLB\t\t: not supported\n" );
  138. } else {
  139. seq_printf(m,
  140. "BTLB fixed\t: max. %d pages, pagesize=%d (%dMB)\n"
  141. "BTLB fix-entr.\t: %d instruction, %d data (%d combined)\n"
  142. "BTLB var-entr.\t: %d instruction, %d data (%d combined)\n",
  143. btlb_info.max_size, (int)4096,
  144. btlb_info.max_size>>8,
  145. btlb_info.fixed_range_info.num_i,
  146. btlb_info.fixed_range_info.num_d,
  147. btlb_info.fixed_range_info.num_comb,
  148. btlb_info.variable_range_info.num_i,
  149. btlb_info.variable_range_info.num_d,
  150. btlb_info.variable_range_info.num_comb
  151. );
  152. }
  153. #endif
  154. }
  155. void __init
  156. parisc_cache_init(void)
  157. {
  158. if (pdc_cache_info(&cache_info) < 0)
  159. panic("parisc_cache_init: pdc_cache_info failed");
  160. #if 0
  161. printk("ic_size %lx dc_size %lx it_size %lx\n",
  162. cache_info.ic_size,
  163. cache_info.dc_size,
  164. cache_info.it_size);
  165. printk("DC base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx\n",
  166. cache_info.dc_base,
  167. cache_info.dc_stride,
  168. cache_info.dc_count,
  169. cache_info.dc_loop);
  170. printk("dc_conf = 0x%lx alias %d blk %d line %d shift %d\n",
  171. *(unsigned long *) (&cache_info.dc_conf),
  172. cache_info.dc_conf.cc_alias,
  173. cache_info.dc_conf.cc_block,
  174. cache_info.dc_conf.cc_line,
  175. cache_info.dc_conf.cc_shift);
  176. printk(" wt %d sh %d cst %d hv %d\n",
  177. cache_info.dc_conf.cc_wt,
  178. cache_info.dc_conf.cc_sh,
  179. cache_info.dc_conf.cc_cst,
  180. cache_info.dc_conf.cc_hv);
  181. printk("IC base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx\n",
  182. cache_info.ic_base,
  183. cache_info.ic_stride,
  184. cache_info.ic_count,
  185. cache_info.ic_loop);
  186. printk("IT base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx off_base 0x%lx off_stride 0x%lx off_count 0x%lx\n",
  187. cache_info.it_sp_base,
  188. cache_info.it_sp_stride,
  189. cache_info.it_sp_count,
  190. cache_info.it_loop,
  191. cache_info.it_off_base,
  192. cache_info.it_off_stride,
  193. cache_info.it_off_count);
  194. printk("DT base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx off_base 0x%lx off_stride 0x%lx off_count 0x%lx\n",
  195. cache_info.dt_sp_base,
  196. cache_info.dt_sp_stride,
  197. cache_info.dt_sp_count,
  198. cache_info.dt_loop,
  199. cache_info.dt_off_base,
  200. cache_info.dt_off_stride,
  201. cache_info.dt_off_count);
  202. printk("ic_conf = 0x%lx alias %d blk %d line %d shift %d\n",
  203. *(unsigned long *) (&cache_info.ic_conf),
  204. cache_info.ic_conf.cc_alias,
  205. cache_info.ic_conf.cc_block,
  206. cache_info.ic_conf.cc_line,
  207. cache_info.ic_conf.cc_shift);
  208. printk(" wt %d sh %d cst %d hv %d\n",
  209. cache_info.ic_conf.cc_wt,
  210. cache_info.ic_conf.cc_sh,
  211. cache_info.ic_conf.cc_cst,
  212. cache_info.ic_conf.cc_hv);
  213. printk("D-TLB conf: sh %d page %d cst %d aid %d sr %d\n",
  214. cache_info.dt_conf.tc_sh,
  215. cache_info.dt_conf.tc_page,
  216. cache_info.dt_conf.tc_cst,
  217. cache_info.dt_conf.tc_aid,
  218. cache_info.dt_conf.tc_sr);
  219. printk("I-TLB conf: sh %d page %d cst %d aid %d sr %d\n",
  220. cache_info.it_conf.tc_sh,
  221. cache_info.it_conf.tc_page,
  222. cache_info.it_conf.tc_cst,
  223. cache_info.it_conf.tc_aid,
  224. cache_info.it_conf.tc_sr);
  225. #endif
  226. split_tlb = 0;
  227. if (cache_info.dt_conf.tc_sh == 0 || cache_info.dt_conf.tc_sh == 2) {
  228. if (cache_info.dt_conf.tc_sh == 2)
  229. printk(KERN_WARNING "Unexpected TLB configuration. "
  230. "Will flush I/D separately (could be optimized).\n");
  231. split_tlb = 1;
  232. }
  233. /* "New and Improved" version from Jim Hull
  234. * (1 << (cc_block-1)) * (cc_line << (4 + cnf.cc_shift))
  235. * The following CAFL_STRIDE is an optimized version, see
  236. * http://lists.parisc-linux.org/pipermail/parisc-linux/2004-June/023625.html
  237. * http://lists.parisc-linux.org/pipermail/parisc-linux/2004-June/023671.html
  238. */
  239. #define CAFL_STRIDE(cnf) (cnf.cc_line << (3 + cnf.cc_block + cnf.cc_shift))
  240. dcache_stride = CAFL_STRIDE(cache_info.dc_conf);
  241. icache_stride = CAFL_STRIDE(cache_info.ic_conf);
  242. #undef CAFL_STRIDE
  243. /* stride needs to be non-zero, otherwise cache flushes will not work */
  244. WARN_ON(cache_info.dc_size && dcache_stride == 0);
  245. WARN_ON(cache_info.ic_size && icache_stride == 0);
  246. if ((boot_cpu_data.pdc.capabilities & PDC_MODEL_NVA_MASK) ==
  247. PDC_MODEL_NVA_UNSUPPORTED) {
  248. printk(KERN_WARNING "parisc_cache_init: Only equivalent aliasing supported!\n");
  249. #if 0
  250. panic("SMP kernel required to avoid non-equivalent aliasing");
  251. #endif
  252. }
  253. }
  254. void disable_sr_hashing(void)
  255. {
  256. int srhash_type, retval;
  257. unsigned long space_bits;
  258. switch (boot_cpu_data.cpu_type) {
  259. case pcx: /* We shouldn't get this far. setup.c should prevent it. */
  260. BUG();
  261. return;
  262. case pcxs:
  263. case pcxt:
  264. case pcxt_:
  265. srhash_type = SRHASH_PCXST;
  266. break;
  267. case pcxl:
  268. srhash_type = SRHASH_PCXL;
  269. break;
  270. case pcxl2: /* pcxl2 doesn't support space register hashing */
  271. return;
  272. default: /* Currently all PA2.0 machines use the same ins. sequence */
  273. srhash_type = SRHASH_PA20;
  274. break;
  275. }
  276. disable_sr_hashing_asm(srhash_type);
  277. retval = pdc_spaceid_bits(&space_bits);
  278. /* If this procedure isn't implemented, don't panic. */
  279. if (retval < 0 && retval != PDC_BAD_OPTION)
  280. panic("pdc_spaceid_bits call failed.\n");
  281. if (space_bits != 0)
  282. panic("SpaceID hashing is still on!\n");
  283. }
  284. static inline void
  285. __flush_cache_page(struct vm_area_struct *vma, unsigned long vmaddr,
  286. unsigned long physaddr)
  287. {
  288. if (!static_branch_likely(&parisc_has_cache))
  289. return;
  290. /*
  291. * The TLB is the engine of coherence on parisc. The CPU is
  292. * entitled to speculate any page with a TLB mapping, so here
  293. * we kill the mapping then flush the page along a special flush
  294. * only alias mapping. This guarantees that the page is no-longer
  295. * in the cache for any process and nor may it be speculatively
  296. * read in (until the user or kernel specifically accesses it,
  297. * of course).
  298. */
  299. flush_tlb_page(vma, vmaddr);
  300. preempt_disable();
  301. flush_dcache_page_asm(physaddr, vmaddr);
  302. if (vma->vm_flags & VM_EXEC)
  303. flush_icache_page_asm(physaddr, vmaddr);
  304. preempt_enable();
  305. }
  306. static void flush_kernel_dcache_page_addr(const void *addr)
  307. {
  308. unsigned long vaddr = (unsigned long)addr;
  309. unsigned long flags;
  310. /* Purge TLB entry to remove translation on all CPUs */
  311. purge_tlb_start(flags);
  312. pdtlb(SR_KERNEL, addr);
  313. purge_tlb_end(flags);
  314. /* Use tmpalias flush to prevent data cache move-in */
  315. preempt_disable();
  316. flush_dcache_page_asm(__pa(vaddr), vaddr);
  317. preempt_enable();
  318. }
  319. static void flush_kernel_icache_page_addr(const void *addr)
  320. {
  321. unsigned long vaddr = (unsigned long)addr;
  322. unsigned long flags;
  323. /* Purge TLB entry to remove translation on all CPUs */
  324. purge_tlb_start(flags);
  325. pdtlb(SR_KERNEL, addr);
  326. purge_tlb_end(flags);
  327. /* Use tmpalias flush to prevent instruction cache move-in */
  328. preempt_disable();
  329. flush_icache_page_asm(__pa(vaddr), vaddr);
  330. preempt_enable();
  331. }
  332. void kunmap_flush_on_unmap(const void *addr)
  333. {
  334. flush_kernel_dcache_page_addr(addr);
  335. }
  336. EXPORT_SYMBOL(kunmap_flush_on_unmap);
  337. void flush_icache_pages(struct vm_area_struct *vma, struct page *page,
  338. unsigned int nr)
  339. {
  340. void *kaddr = page_address(page);
  341. for (;;) {
  342. flush_kernel_dcache_page_addr(kaddr);
  343. flush_kernel_icache_page_addr(kaddr);
  344. if (--nr == 0)
  345. break;
  346. kaddr += PAGE_SIZE;
  347. }
  348. }
  349. /*
  350. * Walk page directory for MM to find PTEP pointer for address ADDR.
  351. */
  352. static inline pte_t *get_ptep(struct mm_struct *mm, unsigned long addr)
  353. {
  354. pte_t *ptep = NULL;
  355. pgd_t *pgd = mm->pgd;
  356. p4d_t *p4d;
  357. pud_t *pud;
  358. pmd_t *pmd;
  359. if (!pgd_none(*pgd)) {
  360. p4d = p4d_offset(pgd, addr);
  361. if (!p4d_none(*p4d)) {
  362. pud = pud_offset(p4d, addr);
  363. if (!pud_none(*pud)) {
  364. pmd = pmd_offset(pud, addr);
  365. if (!pmd_none(*pmd))
  366. ptep = pte_offset_map(pmd, addr);
  367. }
  368. }
  369. }
  370. return ptep;
  371. }
  372. static inline bool pte_needs_cache_flush(pte_t pte)
  373. {
  374. return (pte_val(pte) & (_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_NO_CACHE))
  375. == (_PAGE_PRESENT | _PAGE_ACCESSED);
  376. }
  377. /*
  378. * Return user physical address. Returns 0 if page is not present.
  379. */
  380. static inline unsigned long get_upa(struct mm_struct *mm, unsigned long addr)
  381. {
  382. unsigned long flags, space, pgd, prot, pa;
  383. #ifdef CONFIG_TLB_PTLOCK
  384. unsigned long pgd_lock;
  385. #endif
  386. /* Save context */
  387. local_irq_save(flags);
  388. prot = mfctl(8);
  389. space = mfsp(SR_USER);
  390. pgd = mfctl(25);
  391. #ifdef CONFIG_TLB_PTLOCK
  392. pgd_lock = mfctl(28);
  393. #endif
  394. /* Set context for lpa_user */
  395. switch_mm_irqs_off(NULL, mm, NULL);
  396. pa = lpa_user(addr);
  397. /* Restore previous context */
  398. #ifdef CONFIG_TLB_PTLOCK
  399. mtctl(pgd_lock, 28);
  400. #endif
  401. mtctl(pgd, 25);
  402. mtsp(space, SR_USER);
  403. mtctl(prot, 8);
  404. local_irq_restore(flags);
  405. return pa;
  406. }
  407. void flush_dcache_folio(struct folio *folio)
  408. {
  409. struct address_space *mapping = folio_flush_mapping(folio);
  410. struct vm_area_struct *vma;
  411. unsigned long addr, old_addr = 0;
  412. void *kaddr;
  413. unsigned long count = 0;
  414. unsigned long i, nr, flags;
  415. pgoff_t pgoff;
  416. if (mapping && !mapping_mapped(mapping)) {
  417. set_bit(PG_dcache_dirty, &folio->flags);
  418. return;
  419. }
  420. nr = folio_nr_pages(folio);
  421. kaddr = folio_address(folio);
  422. for (i = 0; i < nr; i++)
  423. flush_kernel_dcache_page_addr(kaddr + i * PAGE_SIZE);
  424. if (!mapping)
  425. return;
  426. pgoff = folio->index;
  427. /*
  428. * We have carefully arranged in arch_get_unmapped_area() that
  429. * *any* mappings of a file are always congruently mapped (whether
  430. * declared as MAP_PRIVATE or MAP_SHARED), so we only need
  431. * to flush one address here for them all to become coherent
  432. * on machines that support equivalent aliasing
  433. */
  434. flush_dcache_mmap_lock_irqsave(mapping, flags);
  435. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff + nr - 1) {
  436. unsigned long offset = pgoff - vma->vm_pgoff;
  437. unsigned long pfn = folio_pfn(folio);
  438. addr = vma->vm_start;
  439. nr = folio_nr_pages(folio);
  440. if (offset > -nr) {
  441. pfn -= offset;
  442. nr += offset;
  443. } else {
  444. addr += offset * PAGE_SIZE;
  445. }
  446. if (addr + nr * PAGE_SIZE > vma->vm_end)
  447. nr = (vma->vm_end - addr) / PAGE_SIZE;
  448. if (old_addr == 0 || (old_addr & (SHM_COLOUR - 1))
  449. != (addr & (SHM_COLOUR - 1))) {
  450. for (i = 0; i < nr; i++)
  451. __flush_cache_page(vma,
  452. addr + i * PAGE_SIZE,
  453. (pfn + i) * PAGE_SIZE);
  454. /*
  455. * Software is allowed to have any number
  456. * of private mappings to a page.
  457. */
  458. if (!(vma->vm_flags & VM_SHARED))
  459. continue;
  460. if (old_addr)
  461. pr_err("INEQUIVALENT ALIASES 0x%lx and 0x%lx in file %pD\n",
  462. old_addr, addr, vma->vm_file);
  463. if (nr == folio_nr_pages(folio))
  464. old_addr = addr;
  465. }
  466. WARN_ON(++count == 4096);
  467. }
  468. flush_dcache_mmap_unlock_irqrestore(mapping, flags);
  469. }
  470. EXPORT_SYMBOL(flush_dcache_folio);
  471. /* Defined in arch/parisc/kernel/pacache.S */
  472. EXPORT_SYMBOL(flush_kernel_dcache_range_asm);
  473. EXPORT_SYMBOL(flush_kernel_icache_range_asm);
  474. #define FLUSH_THRESHOLD 0x80000 /* 0.5MB */
  475. static unsigned long parisc_cache_flush_threshold __ro_after_init = FLUSH_THRESHOLD;
  476. #define FLUSH_TLB_THRESHOLD (16*1024) /* 16 KiB minimum TLB threshold */
  477. static unsigned long parisc_tlb_flush_threshold __ro_after_init = ~0UL;
  478. void __init parisc_setup_cache_timing(void)
  479. {
  480. unsigned long rangetime, alltime;
  481. unsigned long size;
  482. unsigned long threshold, threshold2;
  483. alltime = mfctl(16);
  484. flush_data_cache();
  485. alltime = mfctl(16) - alltime;
  486. size = (unsigned long)(_end - _text);
  487. rangetime = mfctl(16);
  488. flush_kernel_dcache_range((unsigned long)_text, size);
  489. rangetime = mfctl(16) - rangetime;
  490. printk(KERN_DEBUG "Whole cache flush %lu cycles, flushing %lu bytes %lu cycles\n",
  491. alltime, size, rangetime);
  492. threshold = L1_CACHE_ALIGN((unsigned long)((uint64_t)size * alltime / rangetime));
  493. pr_info("Calculated flush threshold is %lu KiB\n",
  494. threshold/1024);
  495. /*
  496. * The threshold computed above isn't very reliable. The following
  497. * heuristic works reasonably well on c8000/rp3440.
  498. */
  499. threshold2 = cache_info.dc_size * num_online_cpus();
  500. parisc_cache_flush_threshold = threshold2;
  501. printk(KERN_INFO "Cache flush threshold set to %lu KiB\n",
  502. parisc_cache_flush_threshold/1024);
  503. /* calculate TLB flush threshold */
  504. /* On SMP machines, skip the TLB measure of kernel text which
  505. * has been mapped as huge pages. */
  506. if (num_online_cpus() > 1 && !parisc_requires_coherency()) {
  507. threshold = max(cache_info.it_size, cache_info.dt_size);
  508. threshold *= PAGE_SIZE;
  509. threshold /= num_online_cpus();
  510. goto set_tlb_threshold;
  511. }
  512. size = (unsigned long)_end - (unsigned long)_text;
  513. rangetime = mfctl(16);
  514. flush_tlb_kernel_range((unsigned long)_text, (unsigned long)_end);
  515. rangetime = mfctl(16) - rangetime;
  516. alltime = mfctl(16);
  517. flush_tlb_all();
  518. alltime = mfctl(16) - alltime;
  519. printk(KERN_INFO "Whole TLB flush %lu cycles, Range flush %lu bytes %lu cycles\n",
  520. alltime, size, rangetime);
  521. threshold = PAGE_ALIGN((num_online_cpus() * size * alltime) / rangetime);
  522. printk(KERN_INFO "Calculated TLB flush threshold %lu KiB\n",
  523. threshold/1024);
  524. set_tlb_threshold:
  525. parisc_tlb_flush_threshold = max(threshold, FLUSH_TLB_THRESHOLD);
  526. printk(KERN_INFO "TLB flush threshold set to %lu KiB\n",
  527. parisc_tlb_flush_threshold/1024);
  528. }
  529. extern void purge_kernel_dcache_page_asm(unsigned long);
  530. extern void clear_user_page_asm(void *, unsigned long);
  531. extern void copy_user_page_asm(void *, void *, unsigned long);
  532. static void flush_cache_page_if_present(struct vm_area_struct *vma,
  533. unsigned long vmaddr)
  534. {
  535. #if CONFIG_FLUSH_PAGE_ACCESSED
  536. bool needs_flush = false;
  537. pte_t *ptep, pte;
  538. ptep = get_ptep(vma->vm_mm, vmaddr);
  539. if (ptep) {
  540. pte = ptep_get(ptep);
  541. needs_flush = pte_needs_cache_flush(pte);
  542. pte_unmap(ptep);
  543. }
  544. if (needs_flush)
  545. __flush_cache_page(vma, vmaddr, PFN_PHYS(pte_pfn(pte)));
  546. #else
  547. struct mm_struct *mm = vma->vm_mm;
  548. unsigned long physaddr = get_upa(mm, vmaddr);
  549. if (physaddr)
  550. __flush_cache_page(vma, vmaddr, PAGE_ALIGN_DOWN(physaddr));
  551. #endif
  552. }
  553. void copy_user_highpage(struct page *to, struct page *from,
  554. unsigned long vaddr, struct vm_area_struct *vma)
  555. {
  556. void *kto, *kfrom;
  557. kfrom = kmap_local_page(from);
  558. kto = kmap_local_page(to);
  559. __flush_cache_page(vma, vaddr, PFN_PHYS(page_to_pfn(from)));
  560. copy_page_asm(kto, kfrom);
  561. kunmap_local(kto);
  562. kunmap_local(kfrom);
  563. }
  564. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  565. unsigned long user_vaddr, void *dst, void *src, int len)
  566. {
  567. __flush_cache_page(vma, user_vaddr, PFN_PHYS(page_to_pfn(page)));
  568. memcpy(dst, src, len);
  569. flush_kernel_dcache_page_addr(PTR_PAGE_ALIGN_DOWN(dst));
  570. }
  571. void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
  572. unsigned long user_vaddr, void *dst, void *src, int len)
  573. {
  574. __flush_cache_page(vma, user_vaddr, PFN_PHYS(page_to_pfn(page)));
  575. memcpy(dst, src, len);
  576. flush_kernel_dcache_page_addr(PTR_PAGE_ALIGN_DOWN(src));
  577. }
  578. /* __flush_tlb_range()
  579. *
  580. * returns 1 if all TLBs were flushed.
  581. */
  582. int __flush_tlb_range(unsigned long sid, unsigned long start,
  583. unsigned long end)
  584. {
  585. unsigned long flags;
  586. if ((!IS_ENABLED(CONFIG_SMP) || !arch_irqs_disabled()) &&
  587. end - start >= parisc_tlb_flush_threshold) {
  588. flush_tlb_all();
  589. return 1;
  590. }
  591. /* Purge TLB entries for small ranges using the pdtlb and
  592. pitlb instructions. These instructions execute locally
  593. but cause a purge request to be broadcast to other TLBs. */
  594. while (start < end) {
  595. purge_tlb_start(flags);
  596. mtsp(sid, SR_TEMP1);
  597. pdtlb(SR_TEMP1, start);
  598. pitlb(SR_TEMP1, start);
  599. purge_tlb_end(flags);
  600. start += PAGE_SIZE;
  601. }
  602. return 0;
  603. }
  604. static void flush_cache_pages(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  605. {
  606. unsigned long addr;
  607. for (addr = start; addr < end; addr += PAGE_SIZE)
  608. flush_cache_page_if_present(vma, addr);
  609. }
  610. static inline unsigned long mm_total_size(struct mm_struct *mm)
  611. {
  612. struct vm_area_struct *vma;
  613. unsigned long usize = 0;
  614. VMA_ITERATOR(vmi, mm, 0);
  615. for_each_vma(vmi, vma) {
  616. if (usize >= parisc_cache_flush_threshold)
  617. break;
  618. usize += vma->vm_end - vma->vm_start;
  619. }
  620. return usize;
  621. }
  622. void flush_cache_mm(struct mm_struct *mm)
  623. {
  624. struct vm_area_struct *vma;
  625. VMA_ITERATOR(vmi, mm, 0);
  626. /*
  627. * Flushing the whole cache on each cpu takes forever on
  628. * rp3440, etc. So, avoid it if the mm isn't too big.
  629. *
  630. * Note that we must flush the entire cache on machines
  631. * with aliasing caches to prevent random segmentation
  632. * faults.
  633. */
  634. if (!parisc_requires_coherency()
  635. || mm_total_size(mm) >= parisc_cache_flush_threshold) {
  636. if (WARN_ON(IS_ENABLED(CONFIG_SMP) && arch_irqs_disabled()))
  637. return;
  638. flush_tlb_all();
  639. flush_cache_all();
  640. return;
  641. }
  642. /* Flush mm */
  643. for_each_vma(vmi, vma)
  644. flush_cache_pages(vma, vma->vm_start, vma->vm_end);
  645. }
  646. void flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  647. {
  648. if (!parisc_requires_coherency()
  649. || end - start >= parisc_cache_flush_threshold) {
  650. if (WARN_ON(IS_ENABLED(CONFIG_SMP) && arch_irqs_disabled()))
  651. return;
  652. flush_tlb_range(vma, start, end);
  653. if (vma->vm_flags & VM_EXEC)
  654. flush_cache_all();
  655. else
  656. flush_data_cache();
  657. return;
  658. }
  659. flush_cache_pages(vma, start & PAGE_MASK, end);
  660. }
  661. void flush_cache_page(struct vm_area_struct *vma, unsigned long vmaddr, unsigned long pfn)
  662. {
  663. __flush_cache_page(vma, vmaddr, PFN_PHYS(pfn));
  664. }
  665. void flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr)
  666. {
  667. if (!PageAnon(page))
  668. return;
  669. __flush_cache_page(vma, vmaddr, PFN_PHYS(page_to_pfn(page)));
  670. }
  671. int ptep_clear_flush_young(struct vm_area_struct *vma, unsigned long addr,
  672. pte_t *ptep)
  673. {
  674. pte_t pte = ptep_get(ptep);
  675. if (!pte_young(pte))
  676. return 0;
  677. set_pte(ptep, pte_mkold(pte));
  678. #if CONFIG_FLUSH_PAGE_ACCESSED
  679. __flush_cache_page(vma, addr, PFN_PHYS(pte_pfn(pte)));
  680. #endif
  681. return 1;
  682. }
  683. /*
  684. * After a PTE is cleared, we have no way to flush the cache for
  685. * the physical page. On PA8800 and PA8900 processors, these lines
  686. * can cause random cache corruption. Thus, we must flush the cache
  687. * as well as the TLB when clearing a PTE that's valid.
  688. */
  689. pte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr,
  690. pte_t *ptep)
  691. {
  692. struct mm_struct *mm = (vma)->vm_mm;
  693. pte_t pte = ptep_get_and_clear(mm, addr, ptep);
  694. unsigned long pfn = pte_pfn(pte);
  695. if (pfn_valid(pfn))
  696. __flush_cache_page(vma, addr, PFN_PHYS(pfn));
  697. else if (pte_accessible(mm, pte))
  698. flush_tlb_page(vma, addr);
  699. return pte;
  700. }
  701. /*
  702. * The physical address for pages in the ioremap case can be obtained
  703. * from the vm_struct struct. I wasn't able to successfully handle the
  704. * vmalloc and vmap cases. We have an array of struct page pointers in
  705. * the uninitialized vmalloc case but the flush failed using page_to_pfn.
  706. */
  707. void flush_cache_vmap(unsigned long start, unsigned long end)
  708. {
  709. unsigned long addr, physaddr;
  710. struct vm_struct *vm;
  711. /* Prevent cache move-in */
  712. flush_tlb_kernel_range(start, end);
  713. if (end - start >= parisc_cache_flush_threshold) {
  714. flush_cache_all();
  715. return;
  716. }
  717. if (WARN_ON_ONCE(!is_vmalloc_addr((void *)start))) {
  718. flush_cache_all();
  719. return;
  720. }
  721. vm = find_vm_area((void *)start);
  722. if (!vm) {
  723. flush_cache_all();
  724. return;
  725. }
  726. /* The physical addresses of IOREMAP regions are contiguous */
  727. if (vm->flags & VM_IOREMAP) {
  728. physaddr = vm->phys_addr;
  729. for (addr = start; addr < end; addr += PAGE_SIZE) {
  730. preempt_disable();
  731. flush_dcache_page_asm(physaddr, start);
  732. flush_icache_page_asm(physaddr, start);
  733. preempt_enable();
  734. physaddr += PAGE_SIZE;
  735. }
  736. return;
  737. }
  738. flush_cache_all();
  739. }
  740. EXPORT_SYMBOL(flush_cache_vmap);
  741. /*
  742. * The vm_struct has been retired and the page table is set up. The
  743. * last page in the range is a guard page. Its physical address can't
  744. * be determined using lpa, so there is no way to flush the range
  745. * using flush_dcache_page_asm.
  746. */
  747. void flush_cache_vunmap(unsigned long start, unsigned long end)
  748. {
  749. /* Prevent cache move-in */
  750. flush_tlb_kernel_range(start, end);
  751. flush_data_cache();
  752. }
  753. EXPORT_SYMBOL(flush_cache_vunmap);
  754. /*
  755. * On systems with PA8800/PA8900 processors, there is no way to flush
  756. * a vmap range other than using the architected loop to flush the
  757. * entire cache. The page directory is not set up, so we can't use
  758. * fdc, etc. FDCE/FICE don't work to flush a portion of the cache.
  759. * L2 is physically indexed but FDCE/FICE instructions in virtual
  760. * mode output their virtual address on the core bus, not their
  761. * real address. As a result, the L2 cache index formed from the
  762. * virtual address will most likely not be the same as the L2 index
  763. * formed from the real address.
  764. */
  765. void flush_kernel_vmap_range(void *vaddr, int size)
  766. {
  767. unsigned long start = (unsigned long)vaddr;
  768. unsigned long end = start + size;
  769. flush_tlb_kernel_range(start, end);
  770. if (!static_branch_likely(&parisc_has_dcache))
  771. return;
  772. /* If interrupts are disabled, we can only do local flush */
  773. if (WARN_ON(IS_ENABLED(CONFIG_SMP) && arch_irqs_disabled())) {
  774. flush_data_cache_local(NULL);
  775. return;
  776. }
  777. flush_data_cache();
  778. }
  779. EXPORT_SYMBOL(flush_kernel_vmap_range);
  780. void invalidate_kernel_vmap_range(void *vaddr, int size)
  781. {
  782. unsigned long start = (unsigned long)vaddr;
  783. unsigned long end = start + size;
  784. /* Ensure DMA is complete */
  785. asm_syncdma();
  786. flush_tlb_kernel_range(start, end);
  787. if (!static_branch_likely(&parisc_has_dcache))
  788. return;
  789. /* If interrupts are disabled, we can only do local flush */
  790. if (WARN_ON(IS_ENABLED(CONFIG_SMP) && arch_irqs_disabled())) {
  791. flush_data_cache_local(NULL);
  792. return;
  793. }
  794. flush_data_cache();
  795. }
  796. EXPORT_SYMBOL(invalidate_kernel_vmap_range);
  797. SYSCALL_DEFINE3(cacheflush, unsigned long, addr, unsigned long, bytes,
  798. unsigned int, cache)
  799. {
  800. unsigned long start, end;
  801. ASM_EXCEPTIONTABLE_VAR(error);
  802. if (bytes == 0)
  803. return 0;
  804. if (!access_ok((void __user *) addr, bytes))
  805. return -EFAULT;
  806. end = addr + bytes;
  807. if (cache & DCACHE) {
  808. start = addr;
  809. __asm__ __volatile__ (
  810. #ifdef CONFIG_64BIT
  811. "1: cmpb,*<<,n %0,%2,1b\n"
  812. #else
  813. "1: cmpb,<<,n %0,%2,1b\n"
  814. #endif
  815. " fic,m %3(%4,%0)\n"
  816. "2: sync\n"
  817. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 2b, "%1")
  818. : "+r" (start), "+r" (error)
  819. : "r" (end), "r" (dcache_stride), "i" (SR_USER));
  820. }
  821. if (cache & ICACHE && error == 0) {
  822. start = addr;
  823. __asm__ __volatile__ (
  824. #ifdef CONFIG_64BIT
  825. "1: cmpb,*<<,n %0,%2,1b\n"
  826. #else
  827. "1: cmpb,<<,n %0,%2,1b\n"
  828. #endif
  829. " fdc,m %3(%4,%0)\n"
  830. "2: sync\n"
  831. ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 2b, "%1")
  832. : "+r" (start), "+r" (error)
  833. : "r" (end), "r" (icache_stride), "i" (SR_USER));
  834. }
  835. return error;
  836. }