cache.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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) 1994 - 2003, 06, 07 by Ralf Baechle (ralf@linux-mips.org)
  7. * Copyright (C) 2007 MIPS Technologies, Inc.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/fcntl.h>
  11. #include <linux/kernel.h>
  12. #include <linux/linkage.h>
  13. #include <linux/export.h>
  14. #include <linux/sched.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/mm.h>
  17. #include <linux/highmem.h>
  18. #include <linux/pagemap.h>
  19. #include <asm/bcache.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/processor.h>
  22. #include <asm/cpu.h>
  23. #include <asm/cpu-features.h>
  24. #include <asm/setup.h>
  25. #include <asm/pgtable.h>
  26. /* Cache operations. */
  27. void (*flush_cache_all)(void);
  28. void (*__flush_cache_all)(void);
  29. EXPORT_SYMBOL_GPL(__flush_cache_all);
  30. void (*flush_cache_mm)(struct mm_struct *mm);
  31. void (*flush_cache_range)(struct vm_area_struct *vma, unsigned long start,
  32. unsigned long end);
  33. void (*flush_cache_page)(struct vm_area_struct *vma, unsigned long page,
  34. unsigned long pfn);
  35. void (*flush_icache_range)(unsigned long start, unsigned long end);
  36. EXPORT_SYMBOL_GPL(flush_icache_range);
  37. void (*local_flush_icache_range)(unsigned long start, unsigned long end);
  38. EXPORT_SYMBOL_GPL(local_flush_icache_range);
  39. void (*__flush_icache_user_range)(unsigned long start, unsigned long end);
  40. void (*__local_flush_icache_user_range)(unsigned long start, unsigned long end);
  41. EXPORT_SYMBOL_GPL(__local_flush_icache_user_range);
  42. void (*__flush_cache_vmap)(void);
  43. void (*__flush_cache_vunmap)(void);
  44. void (*__flush_kernel_vmap_range)(unsigned long vaddr, int size);
  45. EXPORT_SYMBOL_GPL(__flush_kernel_vmap_range);
  46. /* MIPS specific cache operations */
  47. void (*flush_data_cache_page)(unsigned long addr);
  48. void (*flush_icache_all)(void);
  49. EXPORT_SYMBOL(flush_data_cache_page);
  50. EXPORT_SYMBOL(flush_icache_all);
  51. /*
  52. * Dummy cache handling routine
  53. */
  54. void cache_noop(void) {}
  55. #ifdef CONFIG_BOARD_SCACHE
  56. static struct bcache_ops no_sc_ops = {
  57. .bc_enable = (void *)cache_noop,
  58. .bc_disable = (void *)cache_noop,
  59. .bc_wback_inv = (void *)cache_noop,
  60. .bc_inv = (void *)cache_noop
  61. };
  62. struct bcache_ops *bcops = &no_sc_ops;
  63. #endif
  64. #ifdef CONFIG_DMA_NONCOHERENT
  65. /* DMA cache operations. */
  66. void (*_dma_cache_wback_inv)(unsigned long start, unsigned long size);
  67. void (*_dma_cache_wback)(unsigned long start, unsigned long size);
  68. void (*_dma_cache_inv)(unsigned long start, unsigned long size);
  69. #endif /* CONFIG_DMA_NONCOHERENT */
  70. /*
  71. * We could optimize the case where the cache argument is not BCACHE but
  72. * that seems very atypical use ...
  73. */
  74. SYSCALL_DEFINE3(cacheflush, unsigned long, addr, unsigned long, bytes,
  75. unsigned int, cache)
  76. {
  77. if (bytes == 0)
  78. return 0;
  79. if (!access_ok((void __user *) addr, bytes))
  80. return -EFAULT;
  81. __flush_icache_user_range(addr, addr + bytes);
  82. return 0;
  83. }
  84. void __flush_dcache_pages(struct page *page, unsigned int nr)
  85. {
  86. struct folio *folio = page_folio(page);
  87. struct address_space *mapping = folio_flush_mapping(folio);
  88. unsigned long addr;
  89. unsigned int i;
  90. if (mapping && !mapping_mapped(mapping)) {
  91. folio_set_dcache_dirty(folio);
  92. return;
  93. }
  94. /*
  95. * We could delay the flush for the !folio_mapping case too. But that
  96. * case is for exec env/arg pages and those are %99 certainly going to
  97. * get faulted into the tlb (and thus flushed) anyways.
  98. */
  99. for (i = 0; i < nr; i++) {
  100. addr = (unsigned long)kmap_local_page(nth_page(page, i));
  101. flush_data_cache_page(addr);
  102. kunmap_local((void *)addr);
  103. }
  104. }
  105. EXPORT_SYMBOL(__flush_dcache_pages);
  106. void __flush_anon_page(struct page *page, unsigned long vmaddr)
  107. {
  108. unsigned long addr = (unsigned long) page_address(page);
  109. struct folio *folio = page_folio(page);
  110. if (pages_do_alias(addr, vmaddr)) {
  111. if (folio_mapped(folio) && !folio_test_dcache_dirty(folio)) {
  112. void *kaddr;
  113. kaddr = kmap_coherent(page, vmaddr);
  114. flush_data_cache_page((unsigned long)kaddr);
  115. kunmap_coherent();
  116. } else
  117. flush_data_cache_page(addr);
  118. }
  119. }
  120. EXPORT_SYMBOL(__flush_anon_page);
  121. void __update_cache(unsigned long address, pte_t pte)
  122. {
  123. struct folio *folio;
  124. unsigned long pfn, addr;
  125. int exec = !pte_no_exec(pte) && !cpu_has_ic_fills_f_dc;
  126. unsigned int i;
  127. pfn = pte_pfn(pte);
  128. if (unlikely(!pfn_valid(pfn)))
  129. return;
  130. folio = page_folio(pfn_to_page(pfn));
  131. address &= PAGE_MASK;
  132. address -= offset_in_folio(folio, pfn << PAGE_SHIFT);
  133. if (folio_test_dcache_dirty(folio)) {
  134. for (i = 0; i < folio_nr_pages(folio); i++) {
  135. addr = (unsigned long)kmap_local_folio(folio, i);
  136. if (exec || pages_do_alias(addr, address))
  137. flush_data_cache_page(addr);
  138. kunmap_local((void *)addr);
  139. address += PAGE_SIZE;
  140. }
  141. folio_clear_dcache_dirty(folio);
  142. }
  143. }
  144. unsigned long _page_cachable_default;
  145. EXPORT_SYMBOL(_page_cachable_default);
  146. #define PM(p) __pgprot(_page_cachable_default | (p))
  147. static pgprot_t protection_map[16] __ro_after_init;
  148. DECLARE_VM_GET_PAGE_PROT
  149. static inline void setup_protection_map(void)
  150. {
  151. protection_map[0] = PM(_PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
  152. protection_map[1] = PM(_PAGE_PRESENT | _PAGE_NO_EXEC);
  153. protection_map[2] = PM(_PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
  154. protection_map[3] = PM(_PAGE_PRESENT | _PAGE_NO_EXEC);
  155. protection_map[4] = PM(_PAGE_PRESENT);
  156. protection_map[5] = PM(_PAGE_PRESENT);
  157. protection_map[6] = PM(_PAGE_PRESENT);
  158. protection_map[7] = PM(_PAGE_PRESENT);
  159. protection_map[8] = PM(_PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
  160. protection_map[9] = PM(_PAGE_PRESENT | _PAGE_NO_EXEC);
  161. protection_map[10] = PM(_PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_WRITE |
  162. _PAGE_NO_READ);
  163. protection_map[11] = PM(_PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_WRITE);
  164. protection_map[12] = PM(_PAGE_PRESENT);
  165. protection_map[13] = PM(_PAGE_PRESENT);
  166. protection_map[14] = PM(_PAGE_PRESENT | _PAGE_WRITE);
  167. protection_map[15] = PM(_PAGE_PRESENT | _PAGE_WRITE);
  168. }
  169. #undef PM
  170. void cpu_cache_init(void)
  171. {
  172. if (IS_ENABLED(CONFIG_CPU_R3000) && cpu_has_3k_cache)
  173. r3k_cache_init();
  174. if (IS_ENABLED(CONFIG_CPU_R4K_CACHE_TLB) && cpu_has_4k_cache)
  175. r4k_cache_init();
  176. if (IS_ENABLED(CONFIG_CPU_CAVIUM_OCTEON) && cpu_has_octeon_cache)
  177. octeon_cache_init();
  178. setup_protection_map();
  179. }