xref: /openbmc/linux/arch/csky/mm/highmem.c (revision 95298d63)
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 
16 void kmap_flush_tlb(unsigned long addr)
17 {
18 	flush_tlb_one(addr);
19 }
20 EXPORT_SYMBOL(kmap_flush_tlb);
21 
22 EXPORT_SYMBOL(kmap);
23 
24 void *kmap_atomic_high_prot(struct page *page, pgprot_t prot)
25 {
26 	unsigned long vaddr;
27 	int idx, type;
28 
29 	type = kmap_atomic_idx_push();
30 	idx = type + KM_TYPE_NR*smp_processor_id();
31 	vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
32 #ifdef CONFIG_DEBUG_HIGHMEM
33 	BUG_ON(!pte_none(*(kmap_pte - idx)));
34 #endif
35 	set_pte(kmap_pte-idx, mk_pte(page, prot));
36 	flush_tlb_one((unsigned long)vaddr);
37 
38 	return (void *)vaddr;
39 }
40 EXPORT_SYMBOL(kmap_atomic_high_prot);
41 
42 void kunmap_atomic_high(void *kvaddr)
43 {
44 	unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
45 	int idx;
46 
47 	if (vaddr < FIXADDR_START)
48 		return;
49 
50 #ifdef CONFIG_DEBUG_HIGHMEM
51 	idx = KM_TYPE_NR*smp_processor_id() + kmap_atomic_idx();
52 
53 	BUG_ON(vaddr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
54 
55 	pte_clear(&init_mm, vaddr, kmap_pte - idx);
56 	flush_tlb_one(vaddr);
57 #else
58 	(void) idx; /* to kill a warning */
59 #endif
60 	kmap_atomic_idx_pop();
61 }
62 EXPORT_SYMBOL(kunmap_atomic_high);
63 
64 /*
65  * This is the same as kmap_atomic() but can map memory that doesn't
66  * have a struct page associated with it.
67  */
68 void *kmap_atomic_pfn(unsigned long pfn)
69 {
70 	unsigned long vaddr;
71 	int idx, type;
72 
73 	pagefault_disable();
74 
75 	type = kmap_atomic_idx_push();
76 	idx = type + KM_TYPE_NR*smp_processor_id();
77 	vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
78 	set_pte(kmap_pte-idx, pfn_pte(pfn, PAGE_KERNEL));
79 	flush_tlb_one(vaddr);
80 
81 	return (void *) vaddr;
82 }
83 
84 static void __init kmap_pages_init(void)
85 {
86 	unsigned long vaddr;
87 	pgd_t *pgd;
88 	pmd_t *pmd;
89 	pud_t *pud;
90 	pte_t *pte;
91 
92 	vaddr = PKMAP_BASE;
93 	fixrange_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, swapper_pg_dir);
94 
95 	pgd = swapper_pg_dir + pgd_index(vaddr);
96 	pud = (pud_t *)pgd;
97 	pmd = pmd_offset(pud, vaddr);
98 	pte = pte_offset_kernel(pmd, vaddr);
99 	pkmap_page_table = pte;
100 }
101 
102 void __init kmap_init(void)
103 {
104 	unsigned long vaddr;
105 
106 	kmap_pages_init();
107 
108 	vaddr = __fix_to_virt(FIX_KMAP_BEGIN);
109 
110 	kmap_pte = pte_offset_kernel((pmd_t *)pgd_offset_k(vaddr), vaddr);
111 }
112