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. asmlinkage int sys_cacheflush(int operation, void __user *addr, size_t len)
  102. {
  103. int ret;
  104. if (len > CACHEFLUSH_MAX_LEN) {
  105. ret = -EPERM;
  106. if (!capable(CAP_SYS_ADMIN))
  107. goto out;
  108. }
  109. ret = -EFAULT;
  110. if (!access_ok(VERIFY_WRITE, addr, len))
  111. goto out;
  112. switch (operation) {
  113. case CACHE_IFLUSH:
  114. flush_icache_range((unsigned long)addr,
  115. (unsigned long)addr + len);
  116. ret = 0;
  117. break;
  118. default:
  119. ret = -EINVAL;
  120. }
  121. out:
  122. return ret;
  123. }
  124. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  125. unsigned long vaddr, void *dst, const void *src,
  126. unsigned long len)
  127. {
  128. memcpy(dst, src, len);
  129. if (vma->vm_flags & VM_EXEC)
  130. flush_icache_range((unsigned long)dst,
  131. (unsigned long)dst + len);
  132. }