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