Skip to content

Commit 7de9cf4

Browse files
author
Greentime Hu
committed
nds32: Cache and TLB routines
This patch contains cache and TLB maintenance functions. Signed-off-by: Vincent Chen <vincentc@andestech.com> Signed-off-by: Greentime Hu <greentime@andestech.com> Acked-by: Arnd Bergmann <arnd@arndb.de>
1 parent 664eec4 commit 7de9cf4

12 files changed

Lines changed: 1224 additions & 0 deletions

File tree

‎arch/nds32/include/asm/cache.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2005-2017 Andes Technology Corporation
3+
4+
#ifndef __NDS32_CACHE_H__
5+
#define __NDS32_CACHE_H__
6+
7+
#define L1_CACHE_BYTES 32
8+
#define L1_CACHE_SHIFT 5
9+
10+
#define ARCH_DMA_MINALIGN L1_CACHE_BYTES
11+
12+
#endif /* __NDS32_CACHE_H__ */
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2005-2017 Andes Technology Corporation
3+
4+
struct cache_info {
5+
unsigned char ways;
6+
unsigned char line_size;
7+
unsigned short sets;
8+
unsigned short size;
9+
#if defined(CONFIG_CPU_CACHE_ALIASING)
10+
unsigned short aliasing_num;
11+
unsigned int aliasing_mask;
12+
#endif
13+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2005-2017 Andes Technology Corporation
3+
4+
#ifndef __NDS32_CACHEFLUSH_H__
5+
#define __NDS32_CACHEFLUSH_H__
6+
7+
#include <linux/mm.h>
8+
9+
#define PG_dcache_dirty PG_arch_1
10+
11+
#ifdef CONFIG_CPU_CACHE_ALIASING
12+
void flush_cache_mm(struct mm_struct *mm);
13+
void flush_cache_dup_mm(struct mm_struct *mm);
14+
void flush_cache_range(struct vm_area_struct *vma,
15+
unsigned long start, unsigned long end);
16+
void flush_cache_page(struct vm_area_struct *vma,
17+
unsigned long addr, unsigned long pfn);
18+
void flush_cache_kmaps(void);
19+
void flush_cache_vmap(unsigned long start, unsigned long end);
20+
void flush_cache_vunmap(unsigned long start, unsigned long end);
21+
22+
#define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
23+
void flush_dcache_page(struct page *page);
24+
void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
25+
unsigned long vaddr, void *dst, void *src, int len);
26+
void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
27+
unsigned long vaddr, void *dst, void *src, int len);
28+
29+
#define ARCH_HAS_FLUSH_ANON_PAGE
30+
void flush_anon_page(struct vm_area_struct *vma,
31+
struct page *page, unsigned long vaddr);
32+
33+
#define ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
34+
void flush_kernel_dcache_page(struct page *page);
35+
void flush_icache_range(unsigned long start, unsigned long end);
36+
void flush_icache_page(struct vm_area_struct *vma, struct page *page);
37+
#define flush_dcache_mmap_lock(mapping) spin_lock_irq(&(mapping)->tree_lock)
38+
#define flush_dcache_mmap_unlock(mapping) spin_unlock_irq(&(mapping)->tree_lock)
39+
40+
#else
41+
#include <asm-generic/cacheflush.h>
42+
#endif
43+
44+
#endif /* __NDS32_CACHEFLUSH_H__ */
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2005-2017 Andes Technology Corporation
3+
4+
#ifndef __ASM_NDS32_MMU_CONTEXT_H
5+
#define __ASM_NDS32_MMU_CONTEXT_H
6+
7+
#include <linux/spinlock.h>
8+
#include <asm/tlbflush.h>
9+
#include <asm/proc-fns.h>
10+
#include <asm-generic/mm_hooks.h>
11+
12+
static inline int
13+
init_new_context(struct task_struct *tsk, struct mm_struct *mm)
14+
{
15+
mm->context.id = 0;
16+
return 0;
17+
}
18+
19+
#define destroy_context(mm) do { } while(0)
20+
21+
#define CID_BITS 9
22+
extern spinlock_t cid_lock;
23+
extern unsigned int cpu_last_cid;
24+
25+
static inline void __new_context(struct mm_struct *mm)
26+
{
27+
unsigned int cid;
28+
unsigned long flags;
29+
30+
spin_lock_irqsave(&cid_lock, flags);
31+
cid = cpu_last_cid;
32+
cpu_last_cid += 1 << TLB_MISC_offCID;
33+
if (cpu_last_cid == 0)
34+
cpu_last_cid = 1 << TLB_MISC_offCID << CID_BITS;
35+
36+
if ((cid & TLB_MISC_mskCID) == 0)
37+
flush_tlb_all();
38+
spin_unlock_irqrestore(&cid_lock, flags);
39+
40+
mm->context.id = cid;
41+
}
42+
43+
static inline void check_context(struct mm_struct *mm)
44+
{
45+
if (unlikely
46+
((mm->context.id ^ cpu_last_cid) >> TLB_MISC_offCID >> CID_BITS))
47+
__new_context(mm);
48+
}
49+
50+
static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
51+
{
52+
}
53+
54+
static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
55+
struct task_struct *tsk)
56+
{
57+
unsigned int cpu = smp_processor_id();
58+
59+
if (!cpumask_test_and_set_cpu(cpu, mm_cpumask(next)) || prev != next) {
60+
check_context(next);
61+
cpu_switch_mm(next);
62+
}
63+
}
64+
65+
#define deactivate_mm(tsk,mm) do { } while (0)
66+
#define activate_mm(prev,next) switch_mm(prev, next, NULL)
67+
68+
#endif

‎arch/nds32/include/asm/proc-fns.h‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2005-2017 Andes Technology Corporation
3+
4+
#ifndef __NDS32_PROCFNS_H__
5+
#define __NDS32_PROCFNS_H__
6+
7+
#ifdef __KERNEL__
8+
#include <asm/page.h>
9+
10+
struct mm_struct;
11+
struct vm_area_struct;
12+
extern void cpu_proc_init(void);
13+
extern void cpu_proc_fin(void);
14+
extern void cpu_do_idle(void);
15+
extern void cpu_reset(unsigned long reset);
16+
extern void cpu_switch_mm(struct mm_struct *mm);
17+
18+
extern void cpu_dcache_inval_all(void);
19+
extern void cpu_dcache_wbinval_all(void);
20+
extern void cpu_dcache_inval_page(unsigned long page);
21+
extern void cpu_dcache_wb_page(unsigned long page);
22+
extern void cpu_dcache_wbinval_page(unsigned long page);
23+
extern void cpu_dcache_inval_range(unsigned long start, unsigned long end);
24+
extern void cpu_dcache_wb_range(unsigned long start, unsigned long end);
25+
extern void cpu_dcache_wbinval_range(unsigned long start, unsigned long end);
26+
27+
extern void cpu_icache_inval_all(void);
28+
extern void cpu_icache_inval_page(unsigned long page);
29+
extern void cpu_icache_inval_range(unsigned long start, unsigned long end);
30+
31+
extern void cpu_cache_wbinval_page(unsigned long page, int flushi);
32+
extern void cpu_cache_wbinval_range(unsigned long start,
33+
unsigned long end, int flushi);
34+
extern void cpu_cache_wbinval_range_check(struct vm_area_struct *vma,
35+
unsigned long start,
36+
unsigned long end, bool flushi,
37+
bool wbd);
38+
39+
extern void cpu_dma_wb_range(unsigned long start, unsigned long end);
40+
extern void cpu_dma_inval_range(unsigned long start, unsigned long end);
41+
extern void cpu_dma_wbinval_range(unsigned long start, unsigned long end);
42+
43+
#endif /* __KERNEL__ */
44+
#endif /* __NDS32_PROCFNS_H__ */

‎arch/nds32/include/asm/tlb.h‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2005-2017 Andes Technology Corporation
3+
4+
#ifndef __ASMNDS32_TLB_H
5+
#define __ASMNDS32_TLB_H
6+
7+
#define tlb_start_vma(tlb,vma) \
8+
do { \
9+
if (!tlb->fullmm) \
10+
flush_cache_range(vma, vma->vm_start, vma->vm_end); \
11+
} while (0)
12+
13+
#define tlb_end_vma(tlb,vma) \
14+
do { \
15+
if(!tlb->fullmm) \
16+
flush_tlb_range(vma, vma->vm_start, vma->vm_end); \
17+
} while (0)
18+
19+
#define __tlb_remove_tlb_entry(tlb, pte, addr) do { } while (0)
20+
21+
#define tlb_flush(tlb) flush_tlb_mm((tlb)->mm)
22+
23+
#include <asm-generic/tlb.h>
24+
25+
#define __pte_free_tlb(tlb, pte, addr) pte_free((tlb)->mm, pte)
26+
#define __pmd_free_tlb(tlb, pmd, addr) pmd_free((tln)->mm, pmd)
27+
28+
#endif

‎arch/nds32/include/asm/tlbflush.h‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2005-2017 Andes Technology Corporation
3+
4+
#ifndef _ASMNDS32_TLBFLUSH_H
5+
#define _ASMNDS32_TLBFLUSH_H
6+
7+
#include <linux/spinlock.h>
8+
#include <linux/mm.h>
9+
#include <nds32_intrinsic.h>
10+
11+
static inline void local_flush_tlb_all(void)
12+
{
13+
__nds32__tlbop_flua();
14+
__nds32__isb();
15+
}
16+
17+
static inline void local_flush_tlb_mm(struct mm_struct *mm)
18+
{
19+
__nds32__tlbop_flua();
20+
__nds32__isb();
21+
}
22+
23+
static inline void local_flush_tlb_kernel_range(unsigned long start,
24+
unsigned long end)
25+
{
26+
while (start < end) {
27+
__nds32__tlbop_inv(start);
28+
__nds32__isb();
29+
start += PAGE_SIZE;
30+
}
31+
}
32+
33+
void local_flush_tlb_range(struct vm_area_struct *vma,
34+
unsigned long start, unsigned long end);
35+
void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long addr);
36+
37+
#define flush_tlb_all local_flush_tlb_all
38+
#define flush_tlb_mm local_flush_tlb_mm
39+
#define flush_tlb_range local_flush_tlb_range
40+
#define flush_tlb_page local_flush_tlb_page
41+
#define flush_tlb_kernel_range local_flush_tlb_kernel_range
42+
43+
void update_mmu_cache(struct vm_area_struct *vma,
44+
unsigned long address, pte_t * pte);
45+
void tlb_migrate_finish(struct mm_struct *mm);
46+
47+
#endif
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 1994, 1995, 1996 by Ralf Baechle
3+
// Copyright (C) 2005-2017 Andes Technology Corporation
4+
#ifndef _ASM_CACHECTL
5+
#define _ASM_CACHECTL
6+
7+
/*
8+
* Options for cacheflush system call
9+
*/
10+
#define ICACHE 0 /* flush instruction cache */
11+
#define DCACHE 1 /* writeback and flush data cache */
12+
#define BCACHE 2 /* flush instruction cache + writeback and flush data cache */
13+
14+
#endif /* _ASM_CACHECTL */

‎arch/nds32/kernel/cacheinfo.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (C) 2005-2017 Andes Technology Corporation
3+
4+
#include <linux/bitops.h>
5+
#include <linux/cacheinfo.h>
6+
#include <linux/cpu.h>
7+
8+
static void ci_leaf_init(struct cacheinfo *this_leaf,
9+
enum cache_type type, unsigned int level)
10+
{
11+
char cache_type = (type & CACHE_TYPE_INST ? ICACHE : DCACHE);
12+
13+
this_leaf->level = level;
14+
this_leaf->type = type;
15+
this_leaf->coherency_line_size = CACHE_LINE_SIZE(cache_type);
16+
this_leaf->number_of_sets = CACHE_SET(cache_type);;
17+
this_leaf->ways_of_associativity = CACHE_WAY(cache_type);
18+
this_leaf->size = this_leaf->number_of_sets *
19+
this_leaf->coherency_line_size * this_leaf->ways_of_associativity;
20+
#if defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
21+
this_leaf->attributes = CACHE_WRITE_THROUGH;
22+
#else
23+
this_leaf->attributes = CACHE_WRITE_BACK;
24+
#endif
25+
}
26+
27+
int init_cache_level(unsigned int cpu)
28+
{
29+
struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
30+
31+
/* Only 1 level and I/D cache seperate. */
32+
this_cpu_ci->num_levels = 1;
33+
this_cpu_ci->num_leaves = 2;
34+
return 0;
35+
}
36+
37+
int populate_cache_leaves(unsigned int cpu)
38+
{
39+
unsigned int level, idx;
40+
struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
41+
struct cacheinfo *this_leaf = this_cpu_ci->info_list;
42+
43+
for (idx = 0, level = 1; level <= this_cpu_ci->num_levels &&
44+
idx < this_cpu_ci->num_leaves; idx++, level++) {
45+
ci_leaf_init(this_leaf++, CACHE_TYPE_DATA, level);
46+
ci_leaf_init(this_leaf++, CACHE_TYPE_INST, level);
47+
}
48+
return 0;
49+
}

0 commit comments

Comments
 (0)