1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * OpenRISC cache.c
4 *
5 * Linux architectural port borrowing liberally from similar works of
6 * others. All original copyrights apply as per the original source
7 * declaration.
8 *
9 * Modifications for the OpenRISC architecture:
10 * Copyright (C) 2015 Jan Henrik Weinstock <jan.weinstock@rwth-aachen.de>
11 */
12
13 #include <asm/spr.h>
14 #include <asm/spr_defs.h>
15 #include <asm/cache.h>
16 #include <asm/cacheflush.h>
17 #include <asm/cpuinfo.h>
18 #include <asm/tlbflush.h>
19
20 /*
21 * Check if the cache component exists.
22 */
cpu_cache_is_present(const unsigned int cache_type)23 bool cpu_cache_is_present(const unsigned int cache_type)
24 {
25 unsigned long upr = mfspr(SPR_UPR);
26 unsigned long mask = SPR_UPR_UP | cache_type;
27
28 return !((upr & mask) ^ mask);
29 }
30
cache_loop(unsigned long paddr,unsigned long end,const unsigned short reg,const unsigned int cache_type)31 static __always_inline void cache_loop(unsigned long paddr, unsigned long end,
32 const unsigned short reg, const unsigned int cache_type)
33 {
34 if (!cpu_cache_is_present(cache_type))
35 return;
36
37 while (paddr < end) {
38 mtspr(reg, paddr);
39 paddr += L1_CACHE_BYTES;
40 }
41 }
42
cache_loop_page(struct page * page,const unsigned short reg,const unsigned int cache_type)43 static __always_inline void cache_loop_page(struct page *page, const unsigned short reg,
44 const unsigned int cache_type)
45 {
46 unsigned long paddr = page_to_pfn(page) << PAGE_SHIFT;
47 unsigned long end = paddr + PAGE_SIZE;
48
49 paddr &= ~(L1_CACHE_BYTES - 1);
50
51 cache_loop(paddr, end, reg, cache_type);
52 }
53
local_dcache_page_flush(struct page * page)54 void local_dcache_page_flush(struct page *page)
55 {
56 cache_loop_page(page, SPR_DCBFR, SPR_UPR_DCP);
57 }
58 EXPORT_SYMBOL(local_dcache_page_flush);
59
local_icache_page_inv(struct page * page)60 void local_icache_page_inv(struct page *page)
61 {
62 cache_loop_page(page, SPR_ICBIR, SPR_UPR_ICP);
63 }
64 EXPORT_SYMBOL(local_icache_page_inv);
65
local_dcache_range_flush(unsigned long start,unsigned long end)66 void local_dcache_range_flush(unsigned long start, unsigned long end)
67 {
68 cache_loop(start, end, SPR_DCBFR, SPR_UPR_DCP);
69 }
70
local_dcache_range_inv(unsigned long start,unsigned long end)71 void local_dcache_range_inv(unsigned long start, unsigned long end)
72 {
73 cache_loop(start, end, SPR_DCBIR, SPR_UPR_DCP);
74 }
75
local_icache_range_inv(unsigned long start,unsigned long end)76 void local_icache_range_inv(unsigned long start, unsigned long end)
77 {
78 cache_loop(start, end, SPR_ICBIR, SPR_UPR_ICP);
79 }
80
update_cache(struct vm_area_struct * vma,unsigned long address,pte_t * pte)81 void update_cache(struct vm_area_struct *vma, unsigned long address,
82 pte_t *pte)
83 {
84 unsigned long pfn = pte_val(*pte) >> PAGE_SHIFT;
85 struct folio *folio = page_folio(pfn_to_page(pfn));
86 int dirty = !test_and_set_bit(PG_dc_clean, &folio->flags);
87
88 /*
89 * Since icaches do not snoop for updated data on OpenRISC, we
90 * must write back and invalidate any dirty pages manually. We
91 * can skip data pages, since they will not end up in icaches.
92 */
93 if ((vma->vm_flags & VM_EXEC) && dirty) {
94 unsigned int nr = folio_nr_pages(folio);
95
96 while (nr--)
97 sync_icache_dcache(folio_page(folio, nr));
98 }
99 }
100