summaryrefslogtreecommitdiffstats
path: root/arch/nds32/mm/cacheflush.c
blob: ce8fd34497bf045beafa845d2df1500b00281a4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2005-2017 Andes Technology Corporation

#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <linux/module.h>
#include <asm/cacheflush.h>
#include <asm/proc-fns.h>
#include <asm/shmparam.h>
#include <asm/cache_info.h>

extern struct cache_info L1_cache_info[2];

#ifndef CONFIG_CPU_CACHE_ALIASING
void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr,
		      pte_t * pte)
{
	struct page *page;
	unsigned long pfn = pte_pfn(*pte);
	unsigned long flags;

	if (!pfn_valid(pfn))
		return;

	if (vma->vm_mm == current->active_mm) {
		local_irq_save(flags);
		__nds32__mtsr_dsb(addr, NDS32_SR_TLB_VPN);
		__nds32__tlbop_rwr(*pte);
		__nds32__isb();
		local_irq_restore(flags);
	}
	page = pfn_to_page(pfn);

	if ((test_and_clear_bit(PG_dcache_dirty, &page->flags)) ||
	    (vma->vm_flags & VM_EXEC)) {

		if (!PageHighMem(page)) {
			cpu_cache_wbinval_page((unsigned long)
					       page_address(page),
					       vma->vm_flags & VM_EXEC);
		} else {
			unsigned long kaddr = (unsigned long)kmap_atomic(page);
			cpu_cache_wbinval_page(kaddr, vma->vm_flags & VM_EXEC);
			kunmap_atomic((void *)kaddr);
		}
	}
}
#else
extern pte_t va_present(struct mm_struct *mm, unsigned long addr);

static inline unsigned long aliasing(unsigned long addr, unsigned long page)
{
	return ((addr & PAGE_MASK) ^ page) & (SHMLBA - 1);
}

static inline unsigned long kremap0(unsigned long uaddr, unsigned long pa)
{
	unsigned long kaddr, pte;

#define BASE_ADDR0 0xffffc000
	kaddr = BASE_ADDR0 | (uaddr & L1_cache_info[DCACHE].aliasing_mask);
	pte = (pa | PAGE_KERNEL);
	__nds32__mtsr_dsb(kaddr, NDS32_SR_TLB_VPN);
	__nds32__tlbop_rwlk(pte);
	__nds32__isb();
	return kaddr;
}

static inline void kunmap01(unsigned long kaddr)
{
	__nds32__tlbop_unlk(kaddr);
	__nds32__tlbop_inv(kaddr);
	__nds32__isb();
}

static inline unsigned long kremap1(unsigned long uaddr, unsigned long pa)
{
	unsigned long kaddr, pte;

#define BASE_ADDR1 0xffff8000
	kaddr = BASE_ADDR1 | (uaddr & L1_cache_info[DCACHE].aliasing_mask);
	pte = (pa | PAGE_KERNEL);
	__nds32__mtsr_dsb(kaddr, NDS32_SR_TLB_VPN);
	__nds32__tlbop_rwlk(pte);
	__nds32__isb();
	return kaddr;
}

void flush_cache_mm(struct mm_struct *mm)
{
	unsigned long flags;

	local_irq_save(flags);
	cpu_dcache_wbinval_all();
	cpu_icache_inval_all();
	local_irq_restore(flags);
}

void flush_cache_dup_mm(struct mm_struct *mm)
{
}

void flush_cache_range(struct vm_area_struct *vma,
		       unsigned long start, unsigned long end)
{
	unsigned long flags;

	if ((end - start) > 8 * PAGE_SIZE) {
		cpu_dcache_wbinval_all();
		if (vma->vm_flags & VM_EXEC)
			cpu_icache_inval_all();
		return;
	}
	local_irq_save(flags);
	while (start < end) {
		if (va_present(vma->vm_mm, start))
			cpu_cache_wbinval_page(start, vma->vm_flags & VM_EXEC);
		start += PAGE_SIZE;
	}
	local_irq_restore(flags);
	return;
}

void flush_cache_page(struct vm_area_struct *vma,
		      unsigned long addr, unsigned long pfn)
{
	unsigned long vto, flags;

	local_irq_save(flags);
	vto = kremap0(addr, pfn << PAGE_SHIFT);
	cpu_cache_wbinval_page(vto, vma->vm_flags & VM_EXEC);
	kunmap01(vto);
	local_irq_restore(flags);
}

void flush_cache_vmap(unsigned long start, unsigned long end)
{
	cpu_dcache_wbinval_all();
	cpu_icache_inval_all();
}

void flush_cache_vunmap(unsigned long start, unsigned long end)
{
	cpu_dcache_wbinval_all();
	cpu_icache_inval_all();
}

void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
		    struct page *to)
{
	cpu_dcache_wbinval_page((unsigned long)vaddr);
	cpu_icache_inval_page((unsigned long)vaddr);
	copy_page(vto, vfrom);
	cpu_dcache_wbinval_page((unsigned long)vto);
	cpu_icache_inval_page((unsigned long)vto);
}

void clear_user_page(void *addr, unsigned long vaddr, struct page *page)
{
	cpu_dcache_wbinval_page((unsigned long)vaddr);
	cpu_icache_inval_page((unsigned long)vaddr);
	clear_page(addr);
	cpu_dcache_wbinval_page((unsigned long)addr);
	cpu_icache_inval_page((unsigned long)addr);
}

void copy_user_highpage(struct page *to, struct page *from,
			unsigned long vaddr, struct vm_area_struct *vma)
{
	unsigned long vto, vfrom, flags, kto, kfrom, pfrom, pto;
	kto = ((unsigned long)page_address(to) & PAGE_MASK);
	kfrom = ((unsigned long)page_address(from) & PAGE_MASK);
	pto = page_to_phys(to);
	pfrom = page_to_phys(from);

	local_irq_save(flags);
	if (aliasing(vaddr, (unsigned long)kfrom))
		cpu_dcache_wb_page((unsigned long)kfrom);
	vto = kremap0(vaddr, pto);
	vfrom = kremap1(vaddr, pfrom);
	copy_page((void *)vto, (void *)vfrom);
	kunmap01(vfrom);
	kunmap01(vto);
	local_irq_restore(flags);
}

EXPORT_SYMBOL(copy_user_highpage);

void clear_user_highpage(struct page *page, unsigned long vaddr)
{
	unsigned long vto, flags, kto;

	kto = ((unsigned long)page_address(page) & PAGE_MASK);

	local_irq_save(flags);
	if (aliasing(kto, vaddr) && kto != 0) {
		cpu_dcache_inval_page(kto);
		cpu_icache_inval_page(kto);
	}
	vto = kremap0(vaddr, page_to_phys(page));
	clear_page((void *)vto);
	kunmap01(vto);
	local_irq_restore(flags);
}

EXPORT_SYMBOL(clear_user_highpage);

void flush_dcache_page(struct page *page)
{
	struct address_space *mapping;

	mapping = page_mapping(page);
	if (mapping && !mapping_mapped(mapping))
		set_bit(PG_dcache_dirty, &page->flags);
	else {
		unsigned long kaddr, flags;

		kaddr = (unsigned long)page_address(page);
		local_irq_save(flags);
		cpu_dcache_wbinval_page(kaddr);
		if (mapping) {
			unsigned long vaddr, kto;

			vaddr = page->index << PAGE_SHIFT;
			if (aliasing(vaddr, kaddr)) {
				kto = kremap0(vaddr, page_to_phys(page));
				cpu_dcache_wbinval_page(kto);
				kunmap01(kto);
			}
		}
		local_irq_restore(flags);
	}
}
EXPORT_SYMBOL(flush_dcache_page);

void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
		       unsigned long vaddr, void *dst, void *src, int len)
{
	unsigned long line_size, start, end, vto, flags;

	local_irq_save(flags);
	vto = kremap0(vaddr, page_to_phys(page));
	dst = (void *)(vto | (vaddr & (PAGE_SIZE - 1)));
	memcpy(dst, src, len);
	if (vma->vm_flags & VM_EXEC) {
		line_size = L1_cache_info[DCACHE].line_size;
		start = (unsigned long)dst & ~(line_size - 1);
		end =
		    ((unsigned long)dst + len + line_size - 1) & ~(line_size -
								   1);
		cpu_cache_wbinval_range(start, end, 1);
	}
	kunmap01(vto);
	local_irq_restore(flags);
}

void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
			 unsigned long vaddr, void *dst, void *src, int len)
{
	unsigned long vto, flags;

	local_irq_save(flags);
	vto = kremap0(vaddr, page_to_phys(page));
	src = (void *)(vto | (vaddr & (PAGE_SIZE - 1)));
	memcpy(dst, src, len);
	kunmap01(vto);
	local_irq_restore(flags);
}

void flush_anon_page(struct vm_area_struct *vma,
		     struct page *page, unsigned long vaddr)
{
	unsigned long kaddr, flags, ktmp;
	if (!PageAnon(page))
		return;

	if (vma->vm_mm != current->active_mm)
		return;

	local_irq_save(flags);
	if (vma->vm_flags & VM_EXEC)
		cpu_icache_inval_page(vaddr & PAGE_MASK);
	kaddr = (unsigned long)page_address(page);
	if (aliasing(vaddr, kaddr)) {
		ktmp = kremap0(vaddr, page_to_phys(page));
		cpu_dcache_wbinval_page(ktmp);
		kunmap01(ktmp);
	}
	local_irq_restore(flags);
}

void flush_kernel_dcache_page(struct page *page)
{
	unsigned long flags;
	local_irq_save(flags);
	cpu_dcache_wbinval_page((unsigned long)page_address(page));
	local_irq_restore(flags);
}
EXPORT_SYMBOL(flush_kernel_dcache_page);

void flush_kernel_vmap_range(void *addr, int size)
{
	unsigned long flags;
	local_irq_save(flags);
	cpu_dcache_wb_range((unsigned long)addr, (unsigned long)addr +  size);
	local_irq_restore(flags);
}
EXPORT_SYMBOL(flush_kernel_vmap_range);

void invalidate_kernel_vmap_range(void *addr, int size)
{
	unsigned long flags;
	local_irq_save(flags);
	cpu_dcache_inval_range((unsigned long)addr, (unsigned long)addr + size);
	local_irq_restore(flags);
}
EXPORT_SYMBOL(invalidate_kernel_vmap_range);

void flush_icache_range(unsigned long start, unsigned long end)
{
	unsigned long line_size, flags;
	line_size = L1_cache_info[DCACHE].line_size;
	start = start & ~(line_size - 1);
	end = (end + line_size - 1) & ~(line_size - 1);
	local_irq_save(flags);
	cpu_cache_wbinval_range(start, end, 1);
	local_irq_restore(flags);
}
EXPORT_SYMBOL(flush_icache_range);

void flush_icache_page(struct vm_area_struct *vma, struct page *page)
{
	unsigned long flags;
	local_irq_save(flags);
	cpu_cache_wbinval_page((unsigned long)page_address(page),
			       vma->vm_flags & VM_EXEC);
	local_irq_restore(flags);
}

void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr,
		      pte_t * pte)
{
	struct page *page;
	unsigned long flags;
	unsigned long pfn = pte_pfn(*pte);

	if (!pfn_valid(pfn))
		return;

	if (vma->vm_mm == current->active_mm) {
		local_irq_save(flags);
		__nds32__mtsr_dsb(addr, NDS32_SR_TLB_VPN);
		__nds32__tlbop_rwr(*pte);
		__nds32__isb();
		local_irq_restore(flags);
	}

	page = pfn_to_page(pfn);
	if (test_and_clear_bit(PG_dcache_dirty, &page->flags) ||
	    (vma->vm_flags & VM_EXEC)) {
		local_irq_save(flags);
		cpu_dcache_wbinval_page((unsigned long)page_address(page));
		local_irq_restore(flags);
	}
}
#endif