cache.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /*
  15. * If you attempt to flush anything more than this, you need superuser
  16. * privileges. The value is completely arbitrary.
  17. */
  18. #define CACHEFLUSH_MAX_LEN 1024
  19. void invalidate_dcache_region(void *start, size_t size)
  20. {
  21. unsigned long v, begin, end, linesz, mask;
  22. int flush = 0;
  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 - 1;
  30. if (begin & mask) {
  31. flush_dcache_line(start);
  32. begin += linesz;
  33. flush = 1;
  34. }
  35. if ((end & mask) != mask) {
  36. flush_dcache_line((void *)end);
  37. end -= linesz;
  38. flush = 1;
  39. }
  40. /* remaining cachelines only need invalidation */
  41. for (v = begin; v <= end; v += linesz)
  42. invalidate_dcache_line((void *)v);
  43. if (flush)
  44. flush_write_buffer();
  45. }
  46. void clean_dcache_region(void *start, size_t size)
  47. {
  48. unsigned long v, begin, end, linesz;
  49. linesz = boot_cpu_data.dcache.linesz;
  50. begin = (unsigned long)start & ~(linesz - 1);
  51. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  52. for (v = begin; v < end; v += linesz)
  53. clean_dcache_line((void *)v);
  54. flush_write_buffer();
  55. }
  56. void flush_dcache_region(void *start, size_t size)
  57. {
  58. unsigned long v, begin, end, linesz;
  59. linesz = boot_cpu_data.dcache.linesz;
  60. begin = (unsigned long)start & ~(linesz - 1);
  61. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  62. for (v = begin; v < end; v += linesz)
  63. flush_dcache_line((void *)v);
  64. flush_write_buffer();
  65. }
  66. void invalidate_icache_region(void *start, size_t size)
  67. {
  68. unsigned long v, begin, end, linesz;
  69. linesz = boot_cpu_data.icache.linesz;
  70. begin = (unsigned long)start & ~(linesz - 1);
  71. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  72. for (v = begin; v < end; v += linesz)
  73. invalidate_icache_line((void *)v);
  74. }
  75. static inline void __flush_icache_range(unsigned long start, unsigned long end)
  76. {
  77. unsigned long v, linesz;
  78. linesz = boot_cpu_data.dcache.linesz;
  79. for (v = start; v < end; v += linesz) {
  80. clean_dcache_line((void *)v);
  81. invalidate_icache_line((void *)v);
  82. }
  83. flush_write_buffer();
  84. }
  85. /*
  86. * This one is called after a module has been loaded.
  87. */
  88. void flush_icache_range(unsigned long start, unsigned long end)
  89. {
  90. unsigned long linesz;
  91. linesz = boot_cpu_data.dcache.linesz;
  92. __flush_icache_range(start & ~(linesz - 1),
  93. (end + linesz - 1) & ~(linesz - 1));
  94. }
  95. /*
  96. * This one is called from do_no_page(), do_swap_page() and install_page().
  97. */
  98. void flush_icache_page(struct vm_area_struct *vma, struct page *page)
  99. {
  100. if (vma->vm_flags & VM_EXEC) {
  101. void *v = kmap(page);
  102. __flush_icache_range((unsigned long)v, (unsigned long)v + PAGE_SIZE);
  103. kunmap(v);
  104. }
  105. }
  106. /*
  107. * This one is used by copy_to_user_page()
  108. */
  109. void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
  110. unsigned long addr, int len)
  111. {
  112. if (vma->vm_flags & VM_EXEC)
  113. flush_icache_range(addr, addr + len);
  114. }
  115. asmlinkage int sys_cacheflush(int operation, void __user *addr, size_t len)
  116. {
  117. int ret;
  118. if (len > CACHEFLUSH_MAX_LEN) {
  119. ret = -EPERM;
  120. if (!capable(CAP_SYS_ADMIN))
  121. goto out;
  122. }
  123. ret = -EFAULT;
  124. if (!access_ok(VERIFY_WRITE, addr, len))
  125. goto out;
  126. switch (operation) {
  127. case CACHE_IFLUSH:
  128. flush_icache_range((unsigned long)addr,
  129. (unsigned long)addr + len);
  130. ret = 0;
  131. break;
  132. default:
  133. ret = -EINVAL;
  134. }
  135. out:
  136. return ret;
  137. }