cache.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/highmem.h>
  9. #include <linux/unistd.h>
  10. #include <asm/cacheflush.h>
  11. #include <asm/cachectl.h>
  12. #include <asm/processor.h>
  13. #include <asm/uaccess.h>
  14. #include <asm/syscalls.h>
  15. /*
  16. * If you attempt to flush anything more than this, you need superuser
  17. * privileges. The value is completely arbitrary.
  18. */
  19. #define CACHEFLUSH_MAX_LEN 1024
  20. void invalidate_dcache_region(void *start, size_t size)
  21. {
  22. unsigned long v, begin, end, linesz, mask;
  23. linesz = boot_cpu_data.dcache.linesz;
  24. mask = linesz - 1;
  25. /* when first and/or last cachelines are shared, flush them
  26. * instead of invalidating ... never discard valid data!
  27. */
  28. begin = (unsigned long)start;
  29. end = begin + size;
  30. if (begin & mask) {
  31. flush_dcache_line(start);
  32. begin += linesz;
  33. }
  34. if (end & mask) {
  35. flush_dcache_line((void *)end);
  36. end &= ~mask;
  37. }
  38. /* remaining cachelines only need invalidation */
  39. for (v = begin; v < end; v += linesz)
  40. invalidate_dcache_line((void *)v);
  41. flush_write_buffer();
  42. }
  43. void clean_dcache_region(void *start, size_t size)
  44. {
  45. unsigned long v, begin, end, linesz;
  46. linesz = boot_cpu_data.dcache.linesz;
  47. begin = (unsigned long)start & ~(linesz - 1);
  48. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  49. for (v = begin; v < end; v += linesz)
  50. clean_dcache_line((void *)v);
  51. flush_write_buffer();
  52. }
  53. void flush_dcache_region(void *start, size_t size)
  54. {
  55. unsigned long v, begin, end, linesz;
  56. linesz = boot_cpu_data.dcache.linesz;
  57. begin = (unsigned long)start & ~(linesz - 1);
  58. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  59. for (v = begin; v < end; v += linesz)
  60. flush_dcache_line((void *)v);
  61. flush_write_buffer();
  62. }
  63. void invalidate_icache_region(void *start, size_t size)
  64. {
  65. unsigned long v, begin, end, linesz;
  66. linesz = boot_cpu_data.icache.linesz;
  67. begin = (unsigned long)start & ~(linesz - 1);
  68. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  69. for (v = begin; v < end; v += linesz)
  70. invalidate_icache_line((void *)v);
  71. }
  72. static inline void __flush_icache_range(unsigned long start, unsigned long end)
  73. {
  74. unsigned long v, linesz;
  75. linesz = boot_cpu_data.dcache.linesz;
  76. for (v = start; v < end; v += linesz) {
  77. clean_dcache_line((void *)v);
  78. invalidate_icache_line((void *)v);
  79. }
  80. flush_write_buffer();
  81. }
  82. /*
  83. * This one is called after a module has been loaded.
  84. */
  85. void flush_icache_range(unsigned long start, unsigned long end)
  86. {
  87. unsigned long linesz;
  88. linesz = boot_cpu_data.dcache.linesz;
  89. __flush_icache_range(start & ~(linesz - 1),
  90. (end + linesz - 1) & ~(linesz - 1));
  91. }
  92. EXPORT_SYMBOL(flush_icache_range);
  93. /*
  94. * This one is called from __do_fault() and do_swap_page().
  95. */
  96. void flush_icache_page(struct vm_area_struct *vma, struct page *page)
  97. {
  98. if (vma->vm_flags & VM_EXEC) {
  99. void *v = page_address(page);
  100. __flush_icache_range((unsigned long)v, (unsigned long)v + PAGE_SIZE);
  101. }
  102. }
  103. asmlinkage int sys_cacheflush(int operation, void __user *addr, size_t len)
  104. {
  105. int ret;
  106. if (len > CACHEFLUSH_MAX_LEN) {
  107. ret = -EPERM;
  108. if (!capable(CAP_SYS_ADMIN))
  109. goto out;
  110. }
  111. ret = -EFAULT;
  112. if (!access_ok(VERIFY_WRITE, addr, len))
  113. goto out;
  114. switch (operation) {
  115. case CACHE_IFLUSH:
  116. flush_icache_range((unsigned long)addr,
  117. (unsigned long)addr + len);
  118. ret = 0;
  119. break;
  120. default:
  121. ret = -EINVAL;
  122. }
  123. out:
  124. return ret;
  125. }
  126. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  127. unsigned long vaddr, void *dst, const void *src,
  128. unsigned long len)
  129. {
  130. memcpy(dst, src, len);
  131. if (vma->vm_flags & VM_EXEC)
  132. flush_icache_range((unsigned long)dst,
  133. (unsigned long)dst + len);
  134. }