blob: 8f7b1c3cd0f94658fec53cea41f81c856a90aa24 [file] [log] [blame]
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001/*
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
23void invalidate_dcache_region(void *start, size_t size)
24{
David Brownell212868d2007-01-28 12:56:42 -080025 unsigned long v, begin, end, linesz, mask;
26 int flush = 0;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070027
28 linesz = boot_cpu_data.dcache.linesz;
David Brownell212868d2007-01-28 12:56:42 -080029 mask = linesz - 1;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070030
David Brownell212868d2007-01-28 12:56:42 -080031 /* 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 Skinnemoen5f97f7f2006-09-25 23:32:13 -070036
David Brownell212868d2007-01-28 12:56:42 -080037 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 Skinnemoen5f97f7f2006-09-25 23:32:13 -070047
David Brownell212868d2007-01-28 12:56:42 -080048 /* remaining cachelines only need invalidation */
49 for (v = begin; v <= end; v += linesz)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070050 invalidate_dcache_line((void *)v);
David Brownell212868d2007-01-28 12:56:42 -080051 if (flush)
52 flush_write_buffer();
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070053}
54
55void 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
68void 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
81void 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
93static 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 */
109void 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 */
121void flush_icache_page(struct vm_area_struct *vma, struct page *page)
122{
123 if (vma->vm_flags & VM_EXEC) {
Haavard Skinnemoen28c1d392007-03-01 16:32:31 +0100124 void *v = page_address(page);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700125 __flush_icache_range((unsigned long)v, (unsigned long)v + PAGE_SIZE);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700126 }
127}
128
129/*
130 * This one is used by copy_to_user_page()
131 */
132void 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
139asmlinkage 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
163out:
164 return ret;
165}