1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_HIGHMEM_INTERNAL_H
3 #define _LINUX_HIGHMEM_INTERNAL_H
6 * Outside of CONFIG_HIGHMEM to support X86 32bit iomap_atomic() cruft.
8 #ifdef CONFIG_KMAP_LOCAL
9 void *__kmap_local_pfn_prot(unsigned long pfn, pgprot_t prot);
10 void *__kmap_local_page_prot(struct page *page, pgprot_t prot);
11 void kunmap_local_indexed(const void *vaddr);
12 void kmap_local_fork(struct task_struct *tsk);
13 void __kmap_local_sched_out(void);
14 void __kmap_local_sched_in(void);
15 static inline void kmap_assert_nomap(void)
17 DEBUG_LOCKS_WARN_ON(current->kmap_ctrl.idx);
20 static inline void kmap_local_fork(struct task_struct *tsk) { }
21 static inline void kmap_assert_nomap(void) { }
25 #include <asm/highmem.h>
27 #ifndef ARCH_HAS_KMAP_FLUSH_TLB
28 static inline void kmap_flush_tlb(unsigned long addr) { }
32 #define kmap_prot PAGE_KERNEL
35 void *kmap_high(struct page *page);
36 void kunmap_high(struct page *page);
37 void __kmap_flush_unused(void);
38 struct page *__kmap_to_page(void *addr);
40 static inline void *kmap(struct page *page)
45 if (!PageHighMem(page))
46 addr = page_address(page);
48 addr = kmap_high(page);
49 kmap_flush_tlb((unsigned long)addr);
53 static inline void kunmap(struct page *page)
56 if (!PageHighMem(page))
61 static inline struct page *kmap_to_page(void *addr)
63 return __kmap_to_page(addr);
66 static inline void kmap_flush_unused(void)
68 __kmap_flush_unused();
71 static inline void *kmap_local_page(struct page *page)
73 return __kmap_local_page_prot(page, kmap_prot);
76 static inline void *kmap_local_folio(struct folio *folio, size_t offset)
78 struct page *page = folio_page(folio, offset / PAGE_SIZE);
79 return __kmap_local_page_prot(page, kmap_prot) + offset % PAGE_SIZE;
82 static inline void *kmap_local_page_prot(struct page *page, pgprot_t prot)
84 return __kmap_local_page_prot(page, prot);
87 static inline void *kmap_local_pfn(unsigned long pfn)
89 return __kmap_local_pfn_prot(pfn, kmap_prot);
92 static inline void __kunmap_local(const void *vaddr)
94 kunmap_local_indexed(vaddr);
97 static inline void *kmap_atomic_prot(struct page *page, pgprot_t prot)
99 if (IS_ENABLED(CONFIG_PREEMPT_RT))
105 return __kmap_local_page_prot(page, prot);
108 static inline void *kmap_atomic(struct page *page)
110 return kmap_atomic_prot(page, kmap_prot);
113 static inline void *kmap_atomic_pfn(unsigned long pfn)
115 if (IS_ENABLED(CONFIG_PREEMPT_RT))
121 return __kmap_local_pfn_prot(pfn, kmap_prot);
124 static inline void __kunmap_atomic(const void *addr)
126 kunmap_local_indexed(addr);
128 if (IS_ENABLED(CONFIG_PREEMPT_RT))
134 unsigned int __nr_free_highpages(void);
135 extern atomic_long_t _totalhigh_pages;
137 static inline unsigned int nr_free_highpages(void)
139 return __nr_free_highpages();
142 static inline unsigned long totalhigh_pages(void)
144 return (unsigned long)atomic_long_read(&_totalhigh_pages);
147 static inline void totalhigh_pages_add(long count)
149 atomic_long_add(count, &_totalhigh_pages);
152 static inline bool is_kmap_addr(const void *x)
154 unsigned long addr = (unsigned long)x;
156 return (addr >= PKMAP_ADDR(0) && addr < PKMAP_ADDR(LAST_PKMAP)) ||
157 (addr >= __fix_to_virt(FIX_KMAP_END) &&
158 addr < __fix_to_virt(FIX_KMAP_BEGIN));
160 #else /* CONFIG_HIGHMEM */
162 static inline struct page *kmap_to_page(void *addr)
164 return virt_to_page(addr);
167 static inline void *kmap(struct page *page)
170 return page_address(page);
173 static inline void kunmap_high(struct page *page) { }
174 static inline void kmap_flush_unused(void) { }
176 static inline void kunmap(struct page *page)
178 #ifdef ARCH_HAS_FLUSH_ON_KUNMAP
179 kunmap_flush_on_unmap(page_address(page));
183 static inline void *kmap_local_page(struct page *page)
185 return page_address(page);
188 static inline void *kmap_local_folio(struct folio *folio, size_t offset)
190 return page_address(&folio->page) + offset;
193 static inline void *kmap_local_page_prot(struct page *page, pgprot_t prot)
195 return kmap_local_page(page);
198 static inline void *kmap_local_pfn(unsigned long pfn)
200 return kmap_local_page(pfn_to_page(pfn));
203 static inline void __kunmap_local(const void *addr)
205 #ifdef ARCH_HAS_FLUSH_ON_KUNMAP
206 kunmap_flush_on_unmap(PTR_ALIGN_DOWN(addr, PAGE_SIZE));
210 static inline void *kmap_atomic(struct page *page)
212 if (IS_ENABLED(CONFIG_PREEMPT_RT))
217 return page_address(page);
220 static inline void *kmap_atomic_prot(struct page *page, pgprot_t prot)
222 return kmap_atomic(page);
225 static inline void *kmap_atomic_pfn(unsigned long pfn)
227 return kmap_atomic(pfn_to_page(pfn));
230 static inline void __kunmap_atomic(const void *addr)
232 #ifdef ARCH_HAS_FLUSH_ON_KUNMAP
233 kunmap_flush_on_unmap(PTR_ALIGN_DOWN(addr, PAGE_SIZE));
236 if (IS_ENABLED(CONFIG_PREEMPT_RT))
242 static inline unsigned int nr_free_highpages(void) { return 0; }
243 static inline unsigned long totalhigh_pages(void) { return 0UL; }
245 static inline bool is_kmap_addr(const void *x)
250 #endif /* CONFIG_HIGHMEM */
253 * kunmap_atomic - Unmap the virtual address mapped by kmap_atomic() - deprecated!
254 * @__addr: Virtual address to be unmapped
256 * Unmaps an address previously mapped by kmap_atomic() and re-enables
257 * pagefaults. Depending on PREEMP_RT configuration, re-enables also
258 * migration and preemption. Users should not count on these side effects.
260 * Mappings should be unmapped in the reverse order that they were mapped.
261 * See kmap_local_page() for details on nesting.
263 * @__addr can be any address within the mapped page, so there is no need
264 * to subtract any offset that has been added. In contrast to kunmap(),
265 * this function takes the address returned from kmap_atomic(), not the
266 * page passed to it. The compiler will warn you if you pass the page.
268 #define kunmap_atomic(__addr) \
270 BUILD_BUG_ON(__same_type((__addr), struct page *)); \
271 __kunmap_atomic(__addr); \
275 * kunmap_local - Unmap a page mapped via kmap_local_page().
276 * @__addr: An address within the page mapped
278 * @__addr can be any address within the mapped page. Commonly it is the
279 * address return from kmap_local_page(), but it can also include offsets.
281 * Unmapping should be done in the reverse order of the mapping. See
282 * kmap_local_page() for details.
284 #define kunmap_local(__addr) \
286 BUILD_BUG_ON(__same_type((__addr), struct page *)); \
287 __kunmap_local(__addr); \