cache.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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;
  22. linesz = boot_cpu_data.dcache.linesz;
  23. //printk("invalidate dcache: %p + %u\n", start, size);
  24. /* You asked for it, you got it */
  25. begin = (unsigned long)start & ~(linesz - 1);
  26. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  27. for (v = begin; v < end; v += linesz)
  28. invalidate_dcache_line((void *)v);
  29. }
  30. void clean_dcache_region(void *start, size_t size)
  31. {
  32. unsigned long v, begin, end, linesz;
  33. linesz = boot_cpu_data.dcache.linesz;
  34. begin = (unsigned long)start & ~(linesz - 1);
  35. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  36. for (v = begin; v < end; v += linesz)
  37. clean_dcache_line((void *)v);
  38. flush_write_buffer();
  39. }
  40. void flush_dcache_region(void *start, size_t size)
  41. {
  42. unsigned long v, begin, end, linesz;
  43. linesz = boot_cpu_data.dcache.linesz;
  44. begin = (unsigned long)start & ~(linesz - 1);
  45. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  46. for (v = begin; v < end; v += linesz)
  47. flush_dcache_line((void *)v);
  48. flush_write_buffer();
  49. }
  50. void invalidate_icache_region(void *start, size_t size)
  51. {
  52. unsigned long v, begin, end, linesz;
  53. linesz = boot_cpu_data.icache.linesz;
  54. begin = (unsigned long)start & ~(linesz - 1);
  55. end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);
  56. for (v = begin; v < end; v += linesz)
  57. invalidate_icache_line((void *)v);
  58. }
  59. static inline void __flush_icache_range(unsigned long start, unsigned long end)
  60. {
  61. unsigned long v, linesz;
  62. linesz = boot_cpu_data.dcache.linesz;
  63. for (v = start; v < end; v += linesz) {
  64. clean_dcache_line((void *)v);
  65. invalidate_icache_line((void *)v);
  66. }
  67. flush_write_buffer();
  68. }
  69. /*
  70. * This one is called after a module has been loaded.
  71. */
  72. void flush_icache_range(unsigned long start, unsigned long end)
  73. {
  74. unsigned long linesz;
  75. linesz = boot_cpu_data.dcache.linesz;
  76. __flush_icache_range(start & ~(linesz - 1),
  77. (end + linesz - 1) & ~(linesz - 1));
  78. }
  79. /*
  80. * This one is called from do_no_page(), do_swap_page() and install_page().
  81. */
  82. void flush_icache_page(struct vm_area_struct *vma, struct page *page)
  83. {
  84. if (vma->vm_flags & VM_EXEC) {
  85. void *v = kmap(page);
  86. __flush_icache_range((unsigned long)v, (unsigned long)v + PAGE_SIZE);
  87. kunmap(v);
  88. }
  89. }
  90. /*
  91. * This one is used by copy_to_user_page()
  92. */
  93. void flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
  94. unsigned long addr, int len)
  95. {
  96. if (vma->vm_flags & VM_EXEC)
  97. flush_icache_range(addr, addr + len);
  98. }
  99. asmlinkage int sys_cacheflush(int operation, void __user *addr, size_t len)
  100. {
  101. int ret;
  102. if (len > CACHEFLUSH_MAX_LEN) {
  103. ret = -EPERM;
  104. if (!capable(CAP_SYS_ADMIN))
  105. goto out;
  106. }
  107. ret = -EFAULT;
  108. if (!access_ok(VERIFY_WRITE, addr, len))
  109. goto out;
  110. switch (operation) {
  111. case CACHE_IFLUSH:
  112. flush_icache_range((unsigned long)addr,
  113. (unsigned long)addr + len);
  114. ret = 0;
  115. break;
  116. default:
  117. ret = -EINVAL;
  118. }
  119. out:
  120. return ret;
  121. }