2 * Copyright (C) 2015 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/bootmem.h>
11 #include <linux/export.h>
12 #include <linux/highmem.h>
13 #include <asm/processor.h>
14 #include <asm/pgtable.h>
15 #include <asm/pgalloc.h>
16 #include <asm/tlbflush.h>
21 * kmap() API provides sleep semantics hence referred to as "permanent maps"
22 * It allows mapping LAST_PKMAP pages, using @last_pkmap_nr as the cursor
25 * kmap_atomic() can't sleep (calls pagefault_disable()), thus it provides
26 * shortlived ala "temporary mappings" which historically were implemented as
27 * fixmaps (compile time addr etc). Their book-keeping is done per cpu.
29 * Both these facts combined (preemption disabled and per-cpu allocation)
30 * means the total number of concurrent fixmaps will be limited to max
31 * such allocations in a single control path. Thus KM_TYPE_NR (another
32 * historic relic) is a small'ish number which caps max percpu fixmaps
36 * - the kernel vaddr space from 0x7z to 0x8z (currently used by vmalloc/module)
37 * is now shared between vmalloc and kmap (non overlapping though)
39 * - Both fixmap/pkmap use a dedicated page table each, hooked up to swapper PGD
40 * This means each only has 1 PGDIR_SIZE worth of kvaddr mappings, which means
41 * 2M of kvaddr space for typical config (8K page and 11:8:13 traversal split)
43 * - fixmap anyhow needs a limited number of mappings. So 2M kvaddr == 256 PTE
44 * slots across NR_CPUS would be more than sufficient (generic code defines
47 * - pkmap being preemptible, in theory could do with more than 256 concurrent
48 * mappings. However, generic pkmap code: map_new_virtual(), doesn't traverse
49 * the PGD and only works with a single page table @pkmap_page_table, hence
53 extern pte_t
* pkmap_page_table
;
54 static pte_t
* fixmap_page_table
;
56 void *kmap(struct page
*page
)
58 BUG_ON(in_interrupt());
59 if (!PageHighMem(page
))
60 return page_address(page
);
62 return kmap_high(page
);
66 void *kmap_atomic(struct page
*page
)
73 if (!PageHighMem(page
))
74 return page_address(page
);
76 cpu_idx
= kmap_atomic_idx_push();
77 idx
= cpu_idx
+ KM_TYPE_NR
* smp_processor_id();
78 vaddr
= FIXMAP_ADDR(idx
);
80 set_pte_at(&init_mm
, vaddr
, fixmap_page_table
+ idx
,
81 mk_pte(page
, kmap_prot
));
85 EXPORT_SYMBOL(kmap_atomic
);
87 void __kunmap_atomic(void *kv
)
89 unsigned long kvaddr
= (unsigned long)kv
;
91 if (kvaddr
>= FIXMAP_BASE
&& kvaddr
< (FIXMAP_BASE
+ FIXMAP_SIZE
)) {
94 * Because preemption is disabled, this vaddr can be associated
95 * with the current allocated index.
96 * But in case of multiple live kmap_atomic(), it still relies on
97 * callers to unmap in right order.
99 int cpu_idx
= kmap_atomic_idx();
100 int idx
= cpu_idx
+ KM_TYPE_NR
* smp_processor_id();
102 WARN_ON(kvaddr
!= FIXMAP_ADDR(idx
));
104 pte_clear(&init_mm
, kvaddr
, fixmap_page_table
+ idx
);
105 local_flush_tlb_kernel_range(kvaddr
, kvaddr
+ PAGE_SIZE
);
107 kmap_atomic_idx_pop();
113 EXPORT_SYMBOL(__kunmap_atomic
);
115 static noinline pte_t
* __init
alloc_kmap_pgtable(unsigned long kvaddr
)
122 pgd_k
= pgd_offset_k(kvaddr
);
123 pud_k
= pud_offset(pgd_k
, kvaddr
);
124 pmd_k
= pmd_offset(pud_k
, kvaddr
);
126 pte_k
= (pte_t
*)alloc_bootmem_low_pages(PAGE_SIZE
);
127 pmd_populate_kernel(&init_mm
, pmd_k
, pte_k
);
131 void __init
kmap_init(void)
133 /* Due to recursive include hell, we can't do this in processor.h */
134 BUILD_BUG_ON(PAGE_OFFSET
< (VMALLOC_END
+ FIXMAP_SIZE
+ PKMAP_SIZE
));
136 BUILD_BUG_ON(KM_TYPE_NR
> PTRS_PER_PTE
);
137 pkmap_page_table
= alloc_kmap_pgtable(PKMAP_BASE
);
139 BUILD_BUG_ON(LAST_PKMAP
> PTRS_PER_PTE
);
140 fixmap_page_table
= alloc_kmap_pgtable(FIXMAP_BASE
);