cache.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * arch/sh/mm/pg-mmu.c
  3. *
  4. * Copyright (C) 1999, 2000, 2002 Niibe Yutaka
  5. * Copyright (C) 2002 - 2009 Paul Mundt
  6. *
  7. * Released under the terms of the GNU GPL v2.0.
  8. */
  9. #include <linux/mm.h>
  10. #include <linux/init.h>
  11. #include <linux/mutex.h>
  12. #include <linux/fs.h>
  13. #include <linux/highmem.h>
  14. #include <linux/module.h>
  15. #include <asm/mmu_context.h>
  16. #include <asm/cacheflush.h>
  17. void (*flush_cache_all)(void);
  18. void (*flush_cache_mm)(struct mm_struct *mm);
  19. void (*flush_cache_dup_mm)(struct mm_struct *mm);
  20. void (*flush_cache_page)(struct vm_area_struct *vma,
  21. unsigned long addr, unsigned long pfn);
  22. void (*flush_cache_range)(struct vm_area_struct *vma,
  23. unsigned long start, unsigned long end);
  24. void (*flush_dcache_page)(struct page *page);
  25. void (*flush_icache_range)(unsigned long start, unsigned long end);
  26. void (*flush_icache_page)(struct vm_area_struct *vma,
  27. struct page *page);
  28. void (*flush_cache_sigtramp)(unsigned long address);
  29. void (*__flush_wback_region)(void *start, int size);
  30. void (*__flush_purge_region)(void *start, int size);
  31. void (*__flush_invalidate_region)(void *start, int size);
  32. static inline void noop_flush_cache_all(void)
  33. {
  34. }
  35. static inline void noop_flush_cache_mm(struct mm_struct *mm)
  36. {
  37. }
  38. static inline void noop_flush_cache_page(struct vm_area_struct *vma,
  39. unsigned long addr, unsigned long pfn)
  40. {
  41. }
  42. static inline void noop_flush_cache_range(struct vm_area_struct *vma,
  43. unsigned long start, unsigned long end)
  44. {
  45. }
  46. static inline void noop_flush_dcache_page(struct page *page)
  47. {
  48. }
  49. static inline void noop_flush_icache_range(unsigned long start,
  50. unsigned long end)
  51. {
  52. }
  53. static inline void noop_flush_icache_page(struct vm_area_struct *vma,
  54. struct page *page)
  55. {
  56. }
  57. static inline void noop_flush_cache_sigtramp(unsigned long address)
  58. {
  59. }
  60. static inline void noop__flush_region(void *start, int size)
  61. {
  62. }
  63. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  64. unsigned long vaddr, void *dst, const void *src,
  65. unsigned long len)
  66. {
  67. if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
  68. !test_bit(PG_dcache_dirty, &page->flags)) {
  69. void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
  70. memcpy(vto, src, len);
  71. kunmap_coherent();
  72. } else {
  73. memcpy(dst, src, len);
  74. if (boot_cpu_data.dcache.n_aliases)
  75. set_bit(PG_dcache_dirty, &page->flags);
  76. }
  77. if (vma->vm_flags & VM_EXEC)
  78. flush_cache_page(vma, vaddr, page_to_pfn(page));
  79. }
  80. void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
  81. unsigned long vaddr, void *dst, const void *src,
  82. unsigned long len)
  83. {
  84. if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
  85. !test_bit(PG_dcache_dirty, &page->flags)) {
  86. void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
  87. memcpy(dst, vfrom, len);
  88. kunmap_coherent();
  89. } else {
  90. memcpy(dst, src, len);
  91. if (boot_cpu_data.dcache.n_aliases)
  92. set_bit(PG_dcache_dirty, &page->flags);
  93. }
  94. }
  95. void copy_user_highpage(struct page *to, struct page *from,
  96. unsigned long vaddr, struct vm_area_struct *vma)
  97. {
  98. void *vfrom, *vto;
  99. vto = kmap_atomic(to, KM_USER1);
  100. if (boot_cpu_data.dcache.n_aliases && page_mapped(from) &&
  101. !test_bit(PG_dcache_dirty, &from->flags)) {
  102. vfrom = kmap_coherent(from, vaddr);
  103. copy_page(vto, vfrom);
  104. kunmap_coherent();
  105. } else {
  106. vfrom = kmap_atomic(from, KM_USER0);
  107. copy_page(vto, vfrom);
  108. kunmap_atomic(vfrom, KM_USER0);
  109. }
  110. if (pages_do_alias((unsigned long)vto, vaddr & PAGE_MASK))
  111. __flush_wback_region(vto, PAGE_SIZE);
  112. kunmap_atomic(vto, KM_USER1);
  113. /* Make sure this page is cleared on other CPU's too before using it */
  114. smp_wmb();
  115. }
  116. EXPORT_SYMBOL(copy_user_highpage);
  117. void clear_user_highpage(struct page *page, unsigned long vaddr)
  118. {
  119. void *kaddr = kmap_atomic(page, KM_USER0);
  120. clear_page(kaddr);
  121. if (pages_do_alias((unsigned long)kaddr, vaddr & PAGE_MASK))
  122. __flush_wback_region(kaddr, PAGE_SIZE);
  123. kunmap_atomic(kaddr, KM_USER0);
  124. }
  125. EXPORT_SYMBOL(clear_user_highpage);
  126. void __update_cache(struct vm_area_struct *vma,
  127. unsigned long address, pte_t pte)
  128. {
  129. struct page *page;
  130. unsigned long pfn = pte_pfn(pte);
  131. if (!boot_cpu_data.dcache.n_aliases)
  132. return;
  133. page = pfn_to_page(pfn);
  134. if (pfn_valid(pfn) && page_mapping(page)) {
  135. int dirty = test_and_clear_bit(PG_dcache_dirty, &page->flags);
  136. if (dirty) {
  137. unsigned long addr = (unsigned long)page_address(page);
  138. if (pages_do_alias(addr, address & PAGE_MASK))
  139. __flush_wback_region((void *)addr, PAGE_SIZE);
  140. }
  141. }
  142. }
  143. void __flush_anon_page(struct page *page, unsigned long vmaddr)
  144. {
  145. unsigned long addr = (unsigned long) page_address(page);
  146. if (pages_do_alias(addr, vmaddr)) {
  147. if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
  148. !test_bit(PG_dcache_dirty, &page->flags)) {
  149. void *kaddr;
  150. kaddr = kmap_coherent(page, vmaddr);
  151. __flush_wback_region((void *)kaddr, PAGE_SIZE);
  152. kunmap_coherent();
  153. } else
  154. __flush_wback_region((void *)addr, PAGE_SIZE);
  155. }
  156. }
  157. static void compute_alias(struct cache_info *c)
  158. {
  159. c->alias_mask = ((c->sets - 1) << c->entry_shift) & ~(PAGE_SIZE - 1);
  160. c->n_aliases = c->alias_mask ? (c->alias_mask >> PAGE_SHIFT) + 1 : 0;
  161. }
  162. static void __init emit_cache_params(void)
  163. {
  164. printk(KERN_NOTICE "I-cache : n_ways=%d n_sets=%d way_incr=%d\n",
  165. boot_cpu_data.icache.ways,
  166. boot_cpu_data.icache.sets,
  167. boot_cpu_data.icache.way_incr);
  168. printk(KERN_NOTICE "I-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
  169. boot_cpu_data.icache.entry_mask,
  170. boot_cpu_data.icache.alias_mask,
  171. boot_cpu_data.icache.n_aliases);
  172. printk(KERN_NOTICE "D-cache : n_ways=%d n_sets=%d way_incr=%d\n",
  173. boot_cpu_data.dcache.ways,
  174. boot_cpu_data.dcache.sets,
  175. boot_cpu_data.dcache.way_incr);
  176. printk(KERN_NOTICE "D-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
  177. boot_cpu_data.dcache.entry_mask,
  178. boot_cpu_data.dcache.alias_mask,
  179. boot_cpu_data.dcache.n_aliases);
  180. /*
  181. * Emit Secondary Cache parameters if the CPU has a probed L2.
  182. */
  183. if (boot_cpu_data.flags & CPU_HAS_L2_CACHE) {
  184. printk(KERN_NOTICE "S-cache : n_ways=%d n_sets=%d way_incr=%d\n",
  185. boot_cpu_data.scache.ways,
  186. boot_cpu_data.scache.sets,
  187. boot_cpu_data.scache.way_incr);
  188. printk(KERN_NOTICE "S-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
  189. boot_cpu_data.scache.entry_mask,
  190. boot_cpu_data.scache.alias_mask,
  191. boot_cpu_data.scache.n_aliases);
  192. }
  193. }
  194. void __init cpu_cache_init(void)
  195. {
  196. compute_alias(&boot_cpu_data.icache);
  197. compute_alias(&boot_cpu_data.dcache);
  198. compute_alias(&boot_cpu_data.scache);
  199. flush_cache_all = noop_flush_cache_all;
  200. flush_cache_mm = noop_flush_cache_mm;
  201. flush_cache_dup_mm = noop_flush_cache_mm;
  202. flush_cache_page = noop_flush_cache_page;
  203. flush_cache_range = noop_flush_cache_range;
  204. flush_dcache_page = noop_flush_dcache_page;
  205. flush_icache_range = noop_flush_icache_range;
  206. flush_icache_page = noop_flush_icache_page;
  207. flush_cache_sigtramp = noop_flush_cache_sigtramp;
  208. __flush_wback_region = noop__flush_region;
  209. __flush_purge_region = noop__flush_region;
  210. __flush_invalidate_region = noop__flush_region;
  211. if (boot_cpu_data.family == CPU_FAMILY_SH2) {
  212. extern void __weak sh2_cache_init(void);
  213. sh2_cache_init();
  214. }
  215. if (boot_cpu_data.family == CPU_FAMILY_SH2A) {
  216. extern void __weak sh2a_cache_init(void);
  217. sh2a_cache_init();
  218. }
  219. if (boot_cpu_data.family == CPU_FAMILY_SH3) {
  220. extern void __weak sh3_cache_init(void);
  221. sh3_cache_init();
  222. }
  223. if ((boot_cpu_data.family == CPU_FAMILY_SH4) ||
  224. (boot_cpu_data.family == CPU_FAMILY_SH4A) ||
  225. (boot_cpu_data.family == CPU_FAMILY_SH4AL_DSP)) {
  226. extern void __weak sh4_cache_init(void);
  227. sh4_cache_init();
  228. }
  229. emit_cache_params();
  230. }