cache.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. linesz = boot_cpu_data.dcache.linesz;
  23. mask = linesz - 1;
  24. /* when first and/or last cachelines are shared, flush them
  25. * instead of invalidating ... never discard valid data!
  26. */
  27. begin = (unsigned long)start;
  28. end = begin + size;
  29. if (begin & mask) {
  30. flush_dcache_line(start);
  31. begin += linesz;
  32. }
  33. if (end & mask) {
  34. flush_dcache_line((void *)end);
  35. end &= ~mask;
  36. }
  37. /* remaining cachelines only need invalidation */
  38. for (v = begin; v < end; v += linesz)
  39. invalidate_dcache_line((void *)v);
  40. flush_write_buffer();
  41. }
  42. void clean_dcache_region(void *start, size_t size)
  43. {
  44. unsigned long v, begin, end, linesz;
  45. linesz = boot_cpu_data.dcache.linesz;
  46. begin = (unsigned long)start & ~(linesz - 1);
  47. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  48. for (v = begin; v < end; v += linesz)
  49. clean_dcache_line((void *)v);
  50. flush_write_buffer();
  51. }
  52. void flush_dcache_region(void *start, size_t size)
  53. {
  54. unsigned long v, begin, end, linesz;
  55. linesz = boot_cpu_data.dcache.linesz;
  56. begin = (unsigned long)start & ~(linesz - 1);
  57. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  58. for (v = begin; v < end; v += linesz)
  59. flush_dcache_line((void *)v);
  60. flush_write_buffer();
  61. }
  62. void invalidate_icache_region(void *start, size_t size)
  63. {
  64. unsigned long v, begin, end, linesz;
  65. linesz = boot_cpu_data.icache.linesz;
  66. begin = (unsigned long)start & ~(linesz - 1);
  67. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  68. for (v = begin; v < end; v += linesz)
  69. invalidate_icache_line((void *)v);
  70. }
  71. static inline void __flush_icache_range(unsigned long start, unsigned long end)
  72. {
  73. unsigned long v, linesz;
  74. linesz = boot_cpu_data.dcache.linesz;
  75. for (v = start; v < end; v += linesz) {
  76. clean_dcache_line((void *)v);
  77. invalidate_icache_line((void *)v);
  78. }
  79. flush_write_buffer();
  80. }
  81. /*
  82. * This one is called after a module has been loaded.
  83. */
  84. void flush_icache_range(unsigned long start, unsigned long end)
  85. {
  86. unsigned long linesz;
  87. linesz = boot_cpu_data.dcache.linesz;
  88. __flush_icache_range(start & ~(linesz - 1),
  89. (end + linesz - 1) & ~(linesz - 1));
  90. }
  91. /*
  92. * This one is called from do_no_page(), do_swap_page() and install_page().
  93. */
  94. void flush_icache_page(struct vm_area_struct *vma, struct page *page)
  95. {
  96. if (vma->vm_flags & VM_EXEC) {
  97. void *v = page_address(page);
  98. __flush_icache_range((unsigned long)v, (unsigned long)v + PAGE_SIZE);
  99. }
  100. }
  101. /*
  102. * This one is used by copy_to_user_page()
  103. */
  104. void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
  105. unsigned long addr, int len)
  106. {
  107. if (vma->vm_flags & VM_EXEC)
  108. flush_icache_range(addr, addr + len);
  109. }
  110. asmlinkage int sys_cacheflush(int operation, void __user *addr, size_t len)
  111. {
  112. int ret;
  113. if (len > CACHEFLUSH_MAX_LEN) {
  114. ret = -EPERM;
  115. if (!capable(CAP_SYS_ADMIN))
  116. goto out;
  117. }
  118. ret = -EFAULT;
  119. if (!access_ok(VERIFY_WRITE, addr, len))
  120. goto out;
  121. switch (operation) {
  122. case CACHE_IFLUSH:
  123. flush_icache_range((unsigned long)addr,
  124. (unsigned long)addr + len);
  125. ret = 0;
  126. break;
  127. default:
  128. ret = -EINVAL;
  129. }
  130. out:
  131. return ret;
  132. }