1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3
4 #include <linux/module.h>
5 #include <linux/highmem.h>
6 #include <linux/smp.h>
7 #include <linux/memblock.h>
8 #include <asm/fixmap.h>
9 #include <asm/tlbflush.h>
10 #include <asm/cacheflush.h>
11
12 static pte_t *kmap_pte;
13
14 unsigned long highstart_pfn, highend_pfn;
15
kmap_flush_tlb(unsigned long addr)16 void kmap_flush_tlb(unsigned long addr)
17 {
18 flush_tlb_one(addr);
19 }
20 EXPORT_SYMBOL(kmap_flush_tlb);
21
kmap_atomic_high_prot(struct page * page,pgprot_t prot)22 void *kmap_atomic_high_prot(struct page *page, pgprot_t prot)
23 {
24 unsigned long vaddr;
25 int idx, type;
26
27 type = kmap_atomic_idx_push();
28 idx = type + KM_TYPE_NR*smp_processor_id();
29 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
30 #ifdef CONFIG_DEBUG_HIGHMEM
31 BUG_ON(!pte_none(*(kmap_pte - idx)));
32 #endif
33 set_pte(kmap_pte-idx, mk_pte(page, prot));
34 flush_tlb_one((unsigned long)vaddr);
35
36 return (void *)vaddr;
37 }
38 EXPORT_SYMBOL(kmap_atomic_high_prot);
39
kunmap_atomic_high(void * kvaddr)40 void kunmap_atomic_high(void *kvaddr)
41 {
42 unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
43 int idx;
44
45 if (vaddr < FIXADDR_START)
46 return;
47
48 #ifdef CONFIG_DEBUG_HIGHMEM
49 idx = KM_TYPE_NR*smp_processor_id() + kmap_atomic_idx();
50
51 BUG_ON(vaddr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
52
53 pte_clear(&init_mm, vaddr, kmap_pte - idx);
54 flush_tlb_one(vaddr);
55 #else
56 (void) idx; /* to kill a warning */
57 #endif
58 kmap_atomic_idx_pop();
59 }
60 EXPORT_SYMBOL(kunmap_atomic_high);
61
62 /*
63 * This is the same as kmap_atomic() but can map memory that doesn't
64 * have a struct page associated with it.
65 */
kmap_atomic_pfn(unsigned long pfn)66 void *kmap_atomic_pfn(unsigned long pfn)
67 {
68 unsigned long vaddr;
69 int idx, type;
70
71 pagefault_disable();
72
73 type = kmap_atomic_idx_push();
74 idx = type + KM_TYPE_NR*smp_processor_id();
75 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
76 set_pte(kmap_pte-idx, pfn_pte(pfn, PAGE_KERNEL));
77 flush_tlb_one(vaddr);
78
79 return (void *) vaddr;
80 }
81
kmap_pages_init(void)82 static void __init kmap_pages_init(void)
83 {
84 unsigned long vaddr;
85 pgd_t *pgd;
86 pmd_t *pmd;
87 pud_t *pud;
88 pte_t *pte;
89
90 vaddr = PKMAP_BASE;
91 fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, swapper_pg_dir);
92
93 pgd = swapper_pg_dir + pgd_index(vaddr);
94 pud = (pud_t *)pgd;
95 pmd = pmd_offset(pud, vaddr);
96 pte = pte_offset_kernel(pmd, vaddr);
97 pkmap_page_table = pte;
98 }
99
kmap_init(void)100 void __init kmap_init(void)
101 {
102 unsigned long vaddr;
103
104 kmap_pages_init();
105
106 vaddr = __fix_to_virt(FIX_KMAP_BEGIN);
107
108 kmap_pte = pte_offset_kernel((pmd_t *)pgd_offset_k(vaddr), vaddr);
109 }
110