1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2015 Synopsys, Inc. (www.synopsys.com)
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/memblock.h>
7*4882a593Smuzhiyun #include <linux/export.h>
8*4882a593Smuzhiyun #include <linux/highmem.h>
9*4882a593Smuzhiyun #include <linux/pgtable.h>
10*4882a593Smuzhiyun #include <asm/processor.h>
11*4882a593Smuzhiyun #include <asm/pgalloc.h>
12*4882a593Smuzhiyun #include <asm/tlbflush.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun * HIGHMEM API:
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * kmap() API provides sleep semantics hence referred to as "permanent maps"
18*4882a593Smuzhiyun * It allows mapping LAST_PKMAP pages, using @last_pkmap_nr as the cursor
19*4882a593Smuzhiyun * for book-keeping
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * kmap_atomic() can't sleep (calls pagefault_disable()), thus it provides
22*4882a593Smuzhiyun * shortlived ala "temporary mappings" which historically were implemented as
23*4882a593Smuzhiyun * fixmaps (compile time addr etc). Their book-keeping is done per cpu.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * Both these facts combined (preemption disabled and per-cpu allocation)
26*4882a593Smuzhiyun * means the total number of concurrent fixmaps will be limited to max
27*4882a593Smuzhiyun * such allocations in a single control path. Thus KM_TYPE_NR (another
28*4882a593Smuzhiyun * historic relic) is a small'ish number which caps max percpu fixmaps
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * ARC HIGHMEM Details
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * - the kernel vaddr space from 0x7z to 0x8z (currently used by vmalloc/module)
33*4882a593Smuzhiyun * is now shared between vmalloc and kmap (non overlapping though)
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * - Both fixmap/pkmap use a dedicated page table each, hooked up to swapper PGD
36*4882a593Smuzhiyun * This means each only has 1 PGDIR_SIZE worth of kvaddr mappings, which means
37*4882a593Smuzhiyun * 2M of kvaddr space for typical config (8K page and 11:8:13 traversal split)
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun * - fixmap anyhow needs a limited number of mappings. So 2M kvaddr == 256 PTE
40*4882a593Smuzhiyun * slots across NR_CPUS would be more than sufficient (generic code defines
41*4882a593Smuzhiyun * KM_TYPE_NR as 20).
42*4882a593Smuzhiyun *
43*4882a593Smuzhiyun * - pkmap being preemptible, in theory could do with more than 256 concurrent
44*4882a593Smuzhiyun * mappings. However, generic pkmap code: map_new_virtual(), doesn't traverse
45*4882a593Smuzhiyun * the PGD and only works with a single page table @pkmap_page_table, hence
46*4882a593Smuzhiyun * sets the limit
47*4882a593Smuzhiyun */
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun extern pte_t * pkmap_page_table;
50*4882a593Smuzhiyun static pte_t * fixmap_page_table;
51*4882a593Smuzhiyun
kmap_atomic_high_prot(struct page * page,pgprot_t prot)52*4882a593Smuzhiyun void *kmap_atomic_high_prot(struct page *page, pgprot_t prot)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun int idx, cpu_idx;
55*4882a593Smuzhiyun unsigned long vaddr;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun cpu_idx = kmap_atomic_idx_push();
58*4882a593Smuzhiyun idx = cpu_idx + KM_TYPE_NR * smp_processor_id();
59*4882a593Smuzhiyun vaddr = FIXMAP_ADDR(idx);
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun set_pte_at(&init_mm, vaddr, fixmap_page_table + idx,
62*4882a593Smuzhiyun mk_pte(page, prot));
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun return (void *)vaddr;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun EXPORT_SYMBOL(kmap_atomic_high_prot);
67*4882a593Smuzhiyun
kunmap_atomic_high(void * kv)68*4882a593Smuzhiyun void kunmap_atomic_high(void *kv)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun unsigned long kvaddr = (unsigned long)kv;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun if (kvaddr >= FIXMAP_BASE && kvaddr < (FIXMAP_BASE + FIXMAP_SIZE)) {
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun * Because preemption is disabled, this vaddr can be associated
76*4882a593Smuzhiyun * with the current allocated index.
77*4882a593Smuzhiyun * But in case of multiple live kmap_atomic(), it still relies on
78*4882a593Smuzhiyun * callers to unmap in right order.
79*4882a593Smuzhiyun */
80*4882a593Smuzhiyun int cpu_idx = kmap_atomic_idx();
81*4882a593Smuzhiyun int idx = cpu_idx + KM_TYPE_NR * smp_processor_id();
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun WARN_ON(kvaddr != FIXMAP_ADDR(idx));
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun pte_clear(&init_mm, kvaddr, fixmap_page_table + idx);
86*4882a593Smuzhiyun local_flush_tlb_kernel_range(kvaddr, kvaddr + PAGE_SIZE);
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun kmap_atomic_idx_pop();
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun EXPORT_SYMBOL(kunmap_atomic_high);
92*4882a593Smuzhiyun
alloc_kmap_pgtable(unsigned long kvaddr)93*4882a593Smuzhiyun static noinline pte_t * __init alloc_kmap_pgtable(unsigned long kvaddr)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun pmd_t *pmd_k = pmd_off_k(kvaddr);
96*4882a593Smuzhiyun pte_t *pte_k;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun pte_k = (pte_t *)memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
99*4882a593Smuzhiyun if (!pte_k)
100*4882a593Smuzhiyun panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
101*4882a593Smuzhiyun __func__, PAGE_SIZE, PAGE_SIZE);
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun pmd_populate_kernel(&init_mm, pmd_k, pte_k);
104*4882a593Smuzhiyun return pte_k;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
kmap_init(void)107*4882a593Smuzhiyun void __init kmap_init(void)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun /* Due to recursive include hell, we can't do this in processor.h */
110*4882a593Smuzhiyun BUILD_BUG_ON(PAGE_OFFSET < (VMALLOC_END + FIXMAP_SIZE + PKMAP_SIZE));
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun BUILD_BUG_ON(KM_TYPE_NR > PTRS_PER_PTE);
113*4882a593Smuzhiyun pkmap_page_table = alloc_kmap_pgtable(PKMAP_BASE);
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun BUILD_BUG_ON(LAST_PKMAP > PTRS_PER_PTE);
116*4882a593Smuzhiyun fixmap_page_table = alloc_kmap_pgtable(FIXMAP_BASE);
117*4882a593Smuzhiyun }
118