Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 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 | |
| 9 | #include <linux/highmem.h> |
| 10 | #include <linux/unistd.h> |
| 11 | |
| 12 | #include <asm/cacheflush.h> |
| 13 | #include <asm/cachectl.h> |
| 14 | #include <asm/processor.h> |
| 15 | #include <asm/uaccess.h> |
| 16 | |
| 17 | /* |
| 18 | * If you attempt to flush anything more than this, you need superuser |
| 19 | * privileges. The value is completely arbitrary. |
| 20 | */ |
| 21 | #define CACHEFLUSH_MAX_LEN 1024 |
| 22 | |
| 23 | void invalidate_dcache_region(void *start, size_t size) |
| 24 | { |
David Brownell | 212868d | 2007-01-28 12:56:42 -0800 | [diff] [blame] | 25 | unsigned long v, begin, end, linesz, mask; |
| 26 | int flush = 0; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 27 | |
| 28 | linesz = boot_cpu_data.dcache.linesz; |
David Brownell | 212868d | 2007-01-28 12:56:42 -0800 | [diff] [blame] | 29 | mask = linesz - 1; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 30 | |
David Brownell | 212868d | 2007-01-28 12:56:42 -0800 | [diff] [blame] | 31 | /* when first and/or last cachelines are shared, flush them |
| 32 | * instead of invalidating ... never discard valid data! |
| 33 | */ |
| 34 | begin = (unsigned long)start; |
| 35 | end = begin + size - 1; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 36 | |
David Brownell | 212868d | 2007-01-28 12:56:42 -0800 | [diff] [blame] | 37 | if (begin & mask) { |
| 38 | flush_dcache_line(start); |
| 39 | begin += linesz; |
| 40 | flush = 1; |
| 41 | } |
| 42 | if ((end & mask) != mask) { |
| 43 | flush_dcache_line((void *)end); |
| 44 | end -= linesz; |
| 45 | flush = 1; |
| 46 | } |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 47 | |
David Brownell | 212868d | 2007-01-28 12:56:42 -0800 | [diff] [blame] | 48 | /* remaining cachelines only need invalidation */ |
| 49 | for (v = begin; v <= end; v += linesz) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 50 | invalidate_dcache_line((void *)v); |
David Brownell | 212868d | 2007-01-28 12:56:42 -0800 | [diff] [blame] | 51 | if (flush) |
| 52 | flush_write_buffer(); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void clean_dcache_region(void *start, size_t size) |
| 56 | { |
| 57 | unsigned long v, begin, end, linesz; |
| 58 | |
| 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 | |
| 63 | for (v = begin; v < end; v += linesz) |
| 64 | clean_dcache_line((void *)v); |
| 65 | flush_write_buffer(); |
| 66 | } |
| 67 | |
| 68 | void flush_dcache_region(void *start, size_t size) |
| 69 | { |
| 70 | unsigned long v, begin, end, linesz; |
| 71 | |
| 72 | linesz = boot_cpu_data.dcache.linesz; |
| 73 | begin = (unsigned long)start & ~(linesz - 1); |
| 74 | end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1); |
| 75 | |
| 76 | for (v = begin; v < end; v += linesz) |
| 77 | flush_dcache_line((void *)v); |
| 78 | flush_write_buffer(); |
| 79 | } |
| 80 | |
| 81 | void invalidate_icache_region(void *start, size_t size) |
| 82 | { |
| 83 | unsigned long v, begin, end, linesz; |
| 84 | |
| 85 | linesz = boot_cpu_data.icache.linesz; |
| 86 | begin = (unsigned long)start & ~(linesz - 1); |
| 87 | end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1); |
| 88 | |
| 89 | for (v = begin; v < end; v += linesz) |
| 90 | invalidate_icache_line((void *)v); |
| 91 | } |
| 92 | |
| 93 | static inline void __flush_icache_range(unsigned long start, unsigned long end) |
| 94 | { |
| 95 | unsigned long v, linesz; |
| 96 | |
| 97 | linesz = boot_cpu_data.dcache.linesz; |
| 98 | for (v = start; v < end; v += linesz) { |
| 99 | clean_dcache_line((void *)v); |
| 100 | invalidate_icache_line((void *)v); |
| 101 | } |
| 102 | |
| 103 | flush_write_buffer(); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * This one is called after a module has been loaded. |
| 108 | */ |
| 109 | void flush_icache_range(unsigned long start, unsigned long end) |
| 110 | { |
| 111 | unsigned long linesz; |
| 112 | |
| 113 | linesz = boot_cpu_data.dcache.linesz; |
| 114 | __flush_icache_range(start & ~(linesz - 1), |
| 115 | (end + linesz - 1) & ~(linesz - 1)); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * This one is called from do_no_page(), do_swap_page() and install_page(). |
| 120 | */ |
| 121 | void flush_icache_page(struct vm_area_struct *vma, struct page *page) |
| 122 | { |
| 123 | if (vma->vm_flags & VM_EXEC) { |
Haavard Skinnemoen | 28c1d39 | 2007-03-01 16:32:31 +0100 | [diff] [blame^] | 124 | void *v = page_address(page); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 125 | __flush_icache_range((unsigned long)v, (unsigned long)v + PAGE_SIZE); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * This one is used by copy_to_user_page() |
| 131 | */ |
| 132 | void flush_icache_user_range(struct vm_area_struct *vma, struct page *page, |
| 133 | unsigned long addr, int len) |
| 134 | { |
| 135 | if (vma->vm_flags & VM_EXEC) |
| 136 | flush_icache_range(addr, addr + len); |
| 137 | } |
| 138 | |
| 139 | asmlinkage int sys_cacheflush(int operation, void __user *addr, size_t len) |
| 140 | { |
| 141 | int ret; |
| 142 | |
| 143 | if (len > CACHEFLUSH_MAX_LEN) { |
| 144 | ret = -EPERM; |
| 145 | if (!capable(CAP_SYS_ADMIN)) |
| 146 | goto out; |
| 147 | } |
| 148 | |
| 149 | ret = -EFAULT; |
| 150 | if (!access_ok(VERIFY_WRITE, addr, len)) |
| 151 | goto out; |
| 152 | |
| 153 | switch (operation) { |
| 154 | case CACHE_IFLUSH: |
| 155 | flush_icache_range((unsigned long)addr, |
| 156 | (unsigned long)addr + len); |
| 157 | ret = 0; |
| 158 | break; |
| 159 | default: |
| 160 | ret = -EINVAL; |
| 161 | } |
| 162 | |
| 163 | out: |
| 164 | return ret; |
| 165 | } |