xref: /openbmc/linux/arch/powerpc/include/asm/highmem.h (revision b8b572e1)
1b8b572e1SStephen Rothwell /*
2b8b572e1SStephen Rothwell  * highmem.h: virtual kernel memory mappings for high memory
3b8b572e1SStephen Rothwell  *
4b8b572e1SStephen Rothwell  * PowerPC version, stolen from the i386 version.
5b8b572e1SStephen Rothwell  *
6b8b572e1SStephen Rothwell  * Used in CONFIG_HIGHMEM systems for memory pages which
7b8b572e1SStephen Rothwell  * are not addressable by direct kernel virtual addresses.
8b8b572e1SStephen Rothwell  *
9b8b572e1SStephen Rothwell  * Copyright (C) 1999 Gerhard Wichert, Siemens AG
10b8b572e1SStephen Rothwell  *		      Gerhard.Wichert@pdb.siemens.de
11b8b572e1SStephen Rothwell  *
12b8b572e1SStephen Rothwell  *
13b8b572e1SStephen Rothwell  * Redesigned the x86 32-bit VM architecture to deal with
14b8b572e1SStephen Rothwell  * up to 16 Terrabyte physical memory. With current x86 CPUs
15b8b572e1SStephen Rothwell  * we now support up to 64 Gigabytes physical RAM.
16b8b572e1SStephen Rothwell  *
17b8b572e1SStephen Rothwell  * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
18b8b572e1SStephen Rothwell  */
19b8b572e1SStephen Rothwell 
20b8b572e1SStephen Rothwell #ifndef _ASM_HIGHMEM_H
21b8b572e1SStephen Rothwell #define _ASM_HIGHMEM_H
22b8b572e1SStephen Rothwell 
23b8b572e1SStephen Rothwell #ifdef __KERNEL__
24b8b572e1SStephen Rothwell 
25b8b572e1SStephen Rothwell #include <linux/init.h>
26b8b572e1SStephen Rothwell #include <linux/interrupt.h>
27b8b572e1SStephen Rothwell #include <asm/kmap_types.h>
28b8b572e1SStephen Rothwell #include <asm/tlbflush.h>
29b8b572e1SStephen Rothwell #include <asm/page.h>
30b8b572e1SStephen Rothwell #include <asm/fixmap.h>
31b8b572e1SStephen Rothwell 
32b8b572e1SStephen Rothwell extern pte_t *kmap_pte;
33b8b572e1SStephen Rothwell extern pgprot_t kmap_prot;
34b8b572e1SStephen Rothwell extern pte_t *pkmap_page_table;
35b8b572e1SStephen Rothwell 
36b8b572e1SStephen Rothwell /*
37b8b572e1SStephen Rothwell  * Right now we initialize only a single pte table. It can be extended
38b8b572e1SStephen Rothwell  * easily, subsequent pte tables have to be allocated in one physical
39b8b572e1SStephen Rothwell  * chunk of RAM.
40b8b572e1SStephen Rothwell  */
41b8b572e1SStephen Rothwell #define LAST_PKMAP 	(1 << PTE_SHIFT)
42b8b572e1SStephen Rothwell #define LAST_PKMAP_MASK (LAST_PKMAP-1)
43b8b572e1SStephen Rothwell #define PKMAP_BASE	((FIXADDR_START - PAGE_SIZE*(LAST_PKMAP + 1)) & PMD_MASK)
44b8b572e1SStephen Rothwell #define PKMAP_NR(virt)  ((virt-PKMAP_BASE) >> PAGE_SHIFT)
45b8b572e1SStephen Rothwell #define PKMAP_ADDR(nr)  (PKMAP_BASE + ((nr) << PAGE_SHIFT))
46b8b572e1SStephen Rothwell 
47b8b572e1SStephen Rothwell extern void *kmap_high(struct page *page);
48b8b572e1SStephen Rothwell extern void kunmap_high(struct page *page);
49b8b572e1SStephen Rothwell 
50b8b572e1SStephen Rothwell static inline void *kmap(struct page *page)
51b8b572e1SStephen Rothwell {
52b8b572e1SStephen Rothwell 	might_sleep();
53b8b572e1SStephen Rothwell 	if (!PageHighMem(page))
54b8b572e1SStephen Rothwell 		return page_address(page);
55b8b572e1SStephen Rothwell 	return kmap_high(page);
56b8b572e1SStephen Rothwell }
57b8b572e1SStephen Rothwell 
58b8b572e1SStephen Rothwell static inline void kunmap(struct page *page)
59b8b572e1SStephen Rothwell {
60b8b572e1SStephen Rothwell 	BUG_ON(in_interrupt());
61b8b572e1SStephen Rothwell 	if (!PageHighMem(page))
62b8b572e1SStephen Rothwell 		return;
63b8b572e1SStephen Rothwell 	kunmap_high(page);
64b8b572e1SStephen Rothwell }
65b8b572e1SStephen Rothwell 
66b8b572e1SStephen Rothwell /*
67b8b572e1SStephen Rothwell  * The use of kmap_atomic/kunmap_atomic is discouraged - kmap/kunmap
68b8b572e1SStephen Rothwell  * gives a more generic (and caching) interface. But kmap_atomic can
69b8b572e1SStephen Rothwell  * be used in IRQ contexts, so in some (very limited) cases we need
70b8b572e1SStephen Rothwell  * it.
71b8b572e1SStephen Rothwell  */
72b8b572e1SStephen Rothwell static inline void *kmap_atomic_prot(struct page *page, enum km_type type, pgprot_t prot)
73b8b572e1SStephen Rothwell {
74b8b572e1SStephen Rothwell 	unsigned int idx;
75b8b572e1SStephen Rothwell 	unsigned long vaddr;
76b8b572e1SStephen Rothwell 
77b8b572e1SStephen Rothwell 	/* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */
78b8b572e1SStephen Rothwell 	pagefault_disable();
79b8b572e1SStephen Rothwell 	if (!PageHighMem(page))
80b8b572e1SStephen Rothwell 		return page_address(page);
81b8b572e1SStephen Rothwell 
82b8b572e1SStephen Rothwell 	idx = type + KM_TYPE_NR*smp_processor_id();
83b8b572e1SStephen Rothwell 	vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
84b8b572e1SStephen Rothwell #ifdef CONFIG_DEBUG_HIGHMEM
85b8b572e1SStephen Rothwell 	BUG_ON(!pte_none(*(kmap_pte-idx)));
86b8b572e1SStephen Rothwell #endif
87b8b572e1SStephen Rothwell 	set_pte_at(&init_mm, vaddr, kmap_pte-idx, mk_pte(page, prot));
88b8b572e1SStephen Rothwell 	flush_tlb_page(NULL, vaddr);
89b8b572e1SStephen Rothwell 
90b8b572e1SStephen Rothwell 	return (void*) vaddr;
91b8b572e1SStephen Rothwell }
92b8b572e1SStephen Rothwell 
93b8b572e1SStephen Rothwell static inline void *kmap_atomic(struct page *page, enum km_type type)
94b8b572e1SStephen Rothwell {
95b8b572e1SStephen Rothwell 	return kmap_atomic_prot(page, type, kmap_prot);
96b8b572e1SStephen Rothwell }
97b8b572e1SStephen Rothwell 
98b8b572e1SStephen Rothwell static inline void kunmap_atomic(void *kvaddr, enum km_type type)
99b8b572e1SStephen Rothwell {
100b8b572e1SStephen Rothwell #ifdef CONFIG_DEBUG_HIGHMEM
101b8b572e1SStephen Rothwell 	unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
102b8b572e1SStephen Rothwell 	enum fixed_addresses idx = type + KM_TYPE_NR*smp_processor_id();
103b8b572e1SStephen Rothwell 
104b8b572e1SStephen Rothwell 	if (vaddr < __fix_to_virt(FIX_KMAP_END)) {
105b8b572e1SStephen Rothwell 		pagefault_enable();
106b8b572e1SStephen Rothwell 		return;
107b8b572e1SStephen Rothwell 	}
108b8b572e1SStephen Rothwell 
109b8b572e1SStephen Rothwell 	BUG_ON(vaddr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
110b8b572e1SStephen Rothwell 
111b8b572e1SStephen Rothwell 	/*
112b8b572e1SStephen Rothwell 	 * force other mappings to Oops if they'll try to access
113b8b572e1SStephen Rothwell 	 * this pte without first remap it
114b8b572e1SStephen Rothwell 	 */
115b8b572e1SStephen Rothwell 	pte_clear(&init_mm, vaddr, kmap_pte-idx);
116b8b572e1SStephen Rothwell 	flush_tlb_page(NULL, vaddr);
117b8b572e1SStephen Rothwell #endif
118b8b572e1SStephen Rothwell 	pagefault_enable();
119b8b572e1SStephen Rothwell }
120b8b572e1SStephen Rothwell 
121b8b572e1SStephen Rothwell static inline struct page *kmap_atomic_to_page(void *ptr)
122b8b572e1SStephen Rothwell {
123b8b572e1SStephen Rothwell 	unsigned long idx, vaddr = (unsigned long) ptr;
124b8b572e1SStephen Rothwell 	pte_t *pte;
125b8b572e1SStephen Rothwell 
126b8b572e1SStephen Rothwell 	if (vaddr < FIXADDR_START)
127b8b572e1SStephen Rothwell 		return virt_to_page(ptr);
128b8b572e1SStephen Rothwell 
129b8b572e1SStephen Rothwell 	idx = virt_to_fix(vaddr);
130b8b572e1SStephen Rothwell 	pte = kmap_pte - (idx - FIX_KMAP_BEGIN);
131b8b572e1SStephen Rothwell 	return pte_page(*pte);
132b8b572e1SStephen Rothwell }
133b8b572e1SStephen Rothwell 
134b8b572e1SStephen Rothwell #define flush_cache_kmaps()	flush_cache_all()
135b8b572e1SStephen Rothwell 
136b8b572e1SStephen Rothwell #endif /* __KERNEL__ */
137b8b572e1SStephen Rothwell 
138b8b572e1SStephen Rothwell #endif /* _ASM_HIGHMEM_H */
139