- supported.conf: Added sparse_keymap (eeepc_laptop depends on it)
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / include / mach-xen / asm / maddr_32.h
1 #ifndef _I386_MADDR_H
2 #define _I386_MADDR_H
3
4 #include <asm/bug.h>
5 #include <xen/features.h>
6 #include <xen/interface/xen.h>
7
8 /**** MACHINE <-> PHYSICAL CONVERSION MACROS ****/
9 #define INVALID_P2M_ENTRY       (~0UL)
10 #define FOREIGN_FRAME_BIT       (1UL<<31)
11 #define FOREIGN_FRAME(m)        ((m) | FOREIGN_FRAME_BIT)
12
13 /* Definitions for machine and pseudophysical addresses. */
14 #ifdef CONFIG_X86_PAE
15 typedef unsigned long long paddr_t;
16 typedef unsigned long long maddr_t;
17 #else
18 typedef unsigned long paddr_t;
19 typedef unsigned long maddr_t;
20 #endif
21
22 #ifdef CONFIG_XEN
23
24 extern unsigned long *phys_to_machine_mapping;
25 extern unsigned long  max_mapnr;
26
27 #undef machine_to_phys_mapping
28 extern unsigned long *machine_to_phys_mapping;
29 extern unsigned int   machine_to_phys_order;
30
31 static inline unsigned long pfn_to_mfn(unsigned long pfn)
32 {
33         if (unlikely(xen_feature(XENFEAT_auto_translated_physmap)))
34                 return pfn;
35         if (likely(max_mapnr))
36                 BUG_ON(pfn >= max_mapnr);
37         return phys_to_machine_mapping[pfn] & ~FOREIGN_FRAME_BIT;
38 }
39
40 static inline int phys_to_machine_mapping_valid(unsigned long pfn)
41 {
42         if (unlikely(xen_feature(XENFEAT_auto_translated_physmap)))
43                 return 1;
44         if (likely(max_mapnr))
45                 BUG_ON(pfn >= max_mapnr);
46         return (phys_to_machine_mapping[pfn] != INVALID_P2M_ENTRY);
47 }
48
49 static inline unsigned long mfn_to_pfn(unsigned long mfn)
50 {
51         unsigned long pfn;
52
53         if (unlikely(xen_feature(XENFEAT_auto_translated_physmap)))
54                 return mfn;
55
56         if (unlikely((mfn >> machine_to_phys_order) != 0))
57                 return max_mapnr;
58
59         /* The array access can fail (e.g., device space beyond end of RAM). */
60         asm (
61                 "1:     movl %1,%0\n"
62                 "2:\n"
63                 ".section .fixup,\"ax\"\n"
64                 "3:     movl %2,%0\n"
65                 "       jmp  2b\n"
66                 ".previous\n"
67                 ".section __ex_table,\"a\"\n"
68                 "       .align 4\n"
69                 "       .long 1b,3b\n"
70                 ".previous"
71                 : "=r" (pfn)
72                 : "m" (machine_to_phys_mapping[mfn]), "m" (max_mapnr) );
73
74         return pfn;
75 }
76
77 /*
78  * We detect special mappings in one of two ways:
79  *  1. If the MFN is an I/O page then Xen will set the m2p entry
80  *     to be outside our maximum possible pseudophys range.
81  *  2. If the MFN belongs to a different domain then we will certainly
82  *     not have MFN in our p2m table. Conversely, if the page is ours,
83  *     then we'll have p2m(m2p(MFN))==MFN.
84  * If we detect a special mapping then it doesn't have a 'struct page'.
85  * We force !pfn_valid() by returning an out-of-range pointer.
86  *
87  * NB. These checks require that, for any MFN that is not in our reservation,
88  * there is no PFN such that p2m(PFN) == MFN. Otherwise we can get confused if
89  * we are foreign-mapping the MFN, and the other domain as m2p(MFN) == PFN.
90  * Yikes! Various places must poke in INVALID_P2M_ENTRY for safety.
91  *
92  * NB2. When deliberately mapping foreign pages into the p2m table, you *must*
93  *      use FOREIGN_FRAME(). This will cause pte_pfn() to choke on it, as we
94  *      require. In all the cases we care about, the FOREIGN_FRAME bit is
95  *      masked (e.g., pfn_to_mfn()) so behaviour there is correct.
96  */
97 static inline unsigned long mfn_to_local_pfn(unsigned long mfn)
98 {
99         unsigned long pfn = mfn_to_pfn(mfn);
100         if (likely(pfn < max_mapnr)
101             && likely(!xen_feature(XENFEAT_auto_translated_physmap))
102             && unlikely(phys_to_machine_mapping[pfn] != mfn))
103                 return max_mapnr; /* force !pfn_valid() */
104         return pfn;
105 }
106
107 static inline void set_phys_to_machine(unsigned long pfn, unsigned long mfn)
108 {
109         if (likely(max_mapnr))
110                 BUG_ON(pfn >= max_mapnr);
111         if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) {
112                 BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
113                 return;
114         }
115         phys_to_machine_mapping[pfn] = mfn;
116 }
117
118 static inline maddr_t phys_to_machine(paddr_t phys)
119 {
120         maddr_t machine = pfn_to_mfn(phys >> PAGE_SHIFT);
121         machine = (machine << PAGE_SHIFT) | (phys & ~PAGE_MASK);
122         return machine;
123 }
124
125 static inline paddr_t machine_to_phys(maddr_t machine)
126 {
127         paddr_t phys = mfn_to_pfn(machine >> PAGE_SHIFT);
128         phys = (phys << PAGE_SHIFT) | (machine & ~PAGE_MASK);
129         return phys;
130 }
131
132 #ifdef CONFIG_X86_PAE
133 static inline paddr_t pte_phys_to_machine(paddr_t phys)
134 {
135         /*
136          * In PAE mode, the NX bit needs to be dealt with in the value
137          * passed to pfn_to_mfn(). On x86_64, we need to mask it off,
138          * but for i386 the conversion to ulong for the argument will
139          * clip it off.
140          */
141         maddr_t machine = pfn_to_mfn(phys >> PAGE_SHIFT);
142         machine = (machine << PAGE_SHIFT) | (phys & ~PHYSICAL_PAGE_MASK);
143         return machine;
144 }
145
146 static inline paddr_t pte_machine_to_phys(maddr_t machine)
147 {
148         /*
149          * In PAE mode, the NX bit needs to be dealt with in the value
150          * passed to mfn_to_pfn(). On x86_64, we need to mask it off,
151          * but for i386 the conversion to ulong for the argument will
152          * clip it off.
153          */
154         paddr_t phys = mfn_to_pfn(machine >> PAGE_SHIFT);
155         phys = (phys << PAGE_SHIFT) | (machine & ~PHYSICAL_PAGE_MASK);
156         return phys;
157 }
158 #else
159 #define pte_phys_to_machine phys_to_machine
160 #define pte_machine_to_phys machine_to_phys
161 #endif
162
163 #else /* !CONFIG_XEN */
164
165 #define pfn_to_mfn(pfn) (pfn)
166 #define mfn_to_pfn(mfn) (mfn)
167 #define mfn_to_local_pfn(mfn) (mfn)
168 #define set_phys_to_machine(pfn, mfn) ((void)0)
169 #define phys_to_machine_mapping_valid(pfn) (1)
170 #define phys_to_machine(phys) ((maddr_t)(phys))
171 #define machine_to_phys(mach) ((paddr_t)(mach))
172 #define pfn_pte_ma(pfn, prot) pfn_pte(pfn, prot)
173 #define __pte_ma(x) __pte(x)
174
175 #endif /* !CONFIG_XEN */
176
177 /* VIRT <-> MACHINE conversion */
178 #define virt_to_machine(v)      (phys_to_machine(__pa(v)))
179 #define virt_to_mfn(v)          (pfn_to_mfn(__pa(v) >> PAGE_SHIFT))
180 #define mfn_to_virt(m)          (__va(mfn_to_pfn(m) << PAGE_SHIFT))
181
182 #endif /* _I386_MADDR_H */