blob: c44a5d53e464733509de6d3d6347cf9b89889c06 [file] [log] [blame]
H. Peter Anvin05e4d312008-10-23 00:01:39 -07001#ifndef _ASM_X86_XEN_PAGE_H
2#define _ASM_X86_XEN_PAGE_H
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -07003
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -08004#include <linux/kernel.h>
5#include <linux/types.h>
6#include <linux/spinlock.h>
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -07007#include <linux/pfn.h>
Ian Campbell7e775062010-09-30 12:37:26 +01008#include <linux/mm.h>
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -07009
10#include <asm/uaccess.h>
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080011#include <asm/page.h>
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070012#include <asm/pgtable.h>
13
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080014#include <xen/interface/xen.h>
Stefano Stabellini0930bba2011-09-29 11:57:56 +010015#include <xen/grant_table.h>
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070016#include <xen/features.h>
17
18/* Xen machine address */
19typedef struct xmaddr {
20 phys_addr_t maddr;
21} xmaddr_t;
22
23/* Xen pseudo-physical address */
24typedef struct xpaddr {
25 phys_addr_t paddr;
26} xpaddr_t;
27
28#define XMADDR(x) ((xmaddr_t) { .maddr = (x) })
29#define XPADDR(x) ((xpaddr_t) { .paddr = (x) })
30
31/**** MACHINE <-> PHYSICAL CONVERSION MACROS ****/
32#define INVALID_P2M_ENTRY (~0UL)
Konrad Rzeszutek Wilkf4cec352011-01-18 20:15:21 -050033#define FOREIGN_FRAME_BIT (1UL<<(BITS_PER_LONG-1))
34#define IDENTITY_FRAME_BIT (1UL<<(BITS_PER_LONG-2))
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070035#define FOREIGN_FRAME(m) ((m) | FOREIGN_FRAME_BIT)
Konrad Rzeszutek Wilkf4cec352011-01-18 20:15:21 -050036#define IDENTITY_FRAME(m) ((m) | IDENTITY_FRAME_BIT)
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070037
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +010038/* Maximum amount of memory we can handle in a domain in pages */
39#define MAX_DOMAIN_PAGES \
40 ((unsigned long)((u64)CONFIG_XEN_MAX_DOMAIN_MEMORY * 1024 * 1024 * 1024 / PAGE_SIZE))
41
Ian Campbell7e775062010-09-30 12:37:26 +010042extern unsigned long *machine_to_phys_mapping;
Jan Beulichccbcdf72011-08-16 15:07:41 +010043extern unsigned long machine_to_phys_nr;
Juergen Gross5b8e7d82014-11-28 11:53:55 +010044extern unsigned long *xen_p2m_addr;
45extern unsigned long xen_p2m_size;
46extern unsigned long xen_max_p2m_pfn;
Jeremy Fitzhardinge8006ec32008-05-26 23:31:19 +010047
Jeremy Fitzhardinged451bb72008-05-26 23:31:18 +010048extern unsigned long get_phys_to_machine(unsigned long pfn);
Jeremy Fitzhardingec3798062010-08-27 13:42:04 -070049extern bool set_phys_to_machine(unsigned long pfn, unsigned long mfn);
Konrad Rzeszutek Wilk6eaa4122011-01-18 20:09:41 -050050extern bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn);
Konrad Rzeszutek Wilkf4cec352011-01-18 20:15:21 -050051extern unsigned long set_phys_range_identity(unsigned long pfn_s,
52 unsigned long pfn_e);
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -070053
Zoltan Kiss1429d462014-02-27 15:55:30 +000054extern int set_foreign_p2m_mapping(struct gnttab_map_grant_ref *map_ops,
55 struct gnttab_map_grant_ref *kmap_ops,
56 struct page **pages, unsigned int count);
Zoltan Kiss1429d462014-02-27 15:55:30 +000057extern int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
David Vrabel853d0282015-01-05 14:13:41 +000058 struct gnttab_unmap_grant_ref *kunmap_ops,
Zoltan Kiss1429d462014-02-27 15:55:30 +000059 struct page **pages, unsigned int count);
Jeremy Fitzhardinge448f28312010-12-15 13:19:33 +000060
Juergen Gross0aad5682014-11-28 11:53:57 +010061/*
Juergen Gross90fff3e2014-12-05 13:28:04 +010062 * Helper functions to write or read unsigned long values to/from
63 * memory, when the access may fault.
64 */
65static inline int xen_safe_write_ulong(unsigned long *addr, unsigned long val)
66{
67 return __put_user(val, (unsigned long __user *)addr);
68}
69
70static inline int xen_safe_read_ulong(unsigned long *addr, unsigned long *val)
71{
72 return __get_user(*val, (unsigned long __user *)addr);
73}
74
75/*
Juergen Gross0aad5682014-11-28 11:53:57 +010076 * When to use pfn_to_mfn(), __pfn_to_mfn() or get_phys_to_machine():
77 * - pfn_to_mfn() returns either INVALID_P2M_ENTRY or the mfn. No indicator
78 * bits (identity or foreign) are set.
79 * - __pfn_to_mfn() returns the found entry of the p2m table. A possibly set
80 * identity or foreign indicator will be still set. __pfn_to_mfn() is
Juergen Gross054954e2014-11-28 11:53:58 +010081 * encapsulating get_phys_to_machine() which is called in special cases only.
82 * - get_phys_to_machine() is to be called by __pfn_to_mfn() only in special
83 * cases needing an extended handling.
Juergen Gross0aad5682014-11-28 11:53:57 +010084 */
85static inline unsigned long __pfn_to_mfn(unsigned long pfn)
86{
Juergen Gross054954e2014-11-28 11:53:58 +010087 unsigned long mfn;
88
89 if (pfn < xen_p2m_size)
90 mfn = xen_p2m_addr[pfn];
91 else if (unlikely(pfn < xen_max_p2m_pfn))
92 return get_phys_to_machine(pfn);
93 else
94 return IDENTITY_FRAME(pfn);
95
96 if (unlikely(mfn == INVALID_P2M_ENTRY))
97 return get_phys_to_machine(pfn);
98
99 return mfn;
Juergen Gross0aad5682014-11-28 11:53:57 +0100100}
101
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700102static inline unsigned long pfn_to_mfn(unsigned long pfn)
103{
Jeremy Fitzhardingecfd89512010-08-31 14:06:22 -0700104 unsigned long mfn;
105
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700106 if (xen_feature(XENFEAT_auto_translated_physmap))
107 return pfn;
108
Juergen Gross0aad5682014-11-28 11:53:57 +0100109 mfn = __pfn_to_mfn(pfn);
Jeremy Fitzhardingecfd89512010-08-31 14:06:22 -0700110
111 if (mfn != INVALID_P2M_ENTRY)
Konrad Rzeszutek Wilkf4cec352011-01-18 20:15:21 -0500112 mfn &= ~(FOREIGN_FRAME_BIT | IDENTITY_FRAME_BIT);
Jeremy Fitzhardingecfd89512010-08-31 14:06:22 -0700113
114 return mfn;
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700115}
116
117static inline int phys_to_machine_mapping_valid(unsigned long pfn)
118{
119 if (xen_feature(XENFEAT_auto_translated_physmap))
120 return 1;
121
Juergen Gross0aad5682014-11-28 11:53:57 +0100122 return __pfn_to_mfn(pfn) != INVALID_P2M_ENTRY;
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700123}
124
David Vrabel01606762013-09-13 15:13:30 +0100125static inline unsigned long mfn_to_pfn_no_overrides(unsigned long mfn)
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700126{
127 unsigned long pfn;
David Vrabel01606762013-09-13 15:13:30 +0100128 int ret;
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700129
130 if (xen_feature(XENFEAT_auto_translated_physmap))
131 return mfn;
132
David Vrabel01606762013-09-13 15:13:30 +0100133 if (unlikely(mfn >= machine_to_phys_nr))
134 return ~0;
135
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700136 /*
137 * The array access can fail (e.g., device space beyond end of RAM).
138 * In such cases it doesn't matter what we return (we return garbage),
139 * but we must handle the fault without crashing!
140 */
Juergen Gross90fff3e2014-12-05 13:28:04 +0100141 ret = xen_safe_read_ulong(&machine_to_phys_mapping[mfn], &pfn);
Stefano Stabellini706cc9d2011-02-02 18:32:59 +0000142 if (ret < 0)
David Vrabel01606762013-09-13 15:13:30 +0100143 return ~0;
144
145 return pfn;
146}
147
148static inline unsigned long mfn_to_pfn(unsigned long mfn)
149{
150 unsigned long pfn;
151
152 if (xen_feature(XENFEAT_auto_translated_physmap))
153 return mfn;
154
155 pfn = mfn_to_pfn_no_overrides(mfn);
David Vrabel0bb599f2015-01-05 17:06:01 +0000156 if (__pfn_to_mfn(pfn) != mfn)
157 pfn = ~0;
Stefano Stabellini706cc9d2011-02-02 18:32:59 +0000158
Zoltan Kiss1429d462014-02-27 15:55:30 +0000159 /*
David Vrabel0bb599f2015-01-05 17:06:01 +0000160 * pfn is ~0 if there are no entries in the m2p for mfn or the
161 * entry doesn't map back to the mfn.
Jeremy Fitzhardinge448f28312010-12-15 13:19:33 +0000162 */
Juergen Gross0aad5682014-11-28 11:53:57 +0100163 if (pfn == ~0 && __pfn_to_mfn(mfn) == IDENTITY_FRAME(mfn))
Stefano Stabellini706cc9d2011-02-02 18:32:59 +0000164 pfn = mfn;
Jeremy Fitzhardinge448f28312010-12-15 13:19:33 +0000165
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700166 return pfn;
167}
168
169static inline xmaddr_t phys_to_machine(xpaddr_t phys)
170{
171 unsigned offset = phys.paddr & ~PAGE_MASK;
Jeremy Fitzhardinge947d0492008-09-11 01:31:48 -0700172 return XMADDR(PFN_PHYS(pfn_to_mfn(PFN_DOWN(phys.paddr))) | offset);
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700173}
174
175static inline xpaddr_t machine_to_phys(xmaddr_t machine)
176{
177 unsigned offset = machine.maddr & ~PAGE_MASK;
Jeremy Fitzhardinge947d0492008-09-11 01:31:48 -0700178 return XPADDR(PFN_PHYS(mfn_to_pfn(PFN_DOWN(machine.maddr))) | offset);
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700179}
180
181/*
182 * We detect special mappings in one of two ways:
183 * 1. If the MFN is an I/O page then Xen will set the m2p entry
184 * to be outside our maximum possible pseudophys range.
185 * 2. If the MFN belongs to a different domain then we will certainly
186 * not have MFN in our p2m table. Conversely, if the page is ours,
187 * then we'll have p2m(m2p(MFN))==MFN.
188 * If we detect a special mapping then it doesn't have a 'struct page'.
189 * We force !pfn_valid() by returning an out-of-range pointer.
190 *
191 * NB. These checks require that, for any MFN that is not in our reservation,
192 * there is no PFN such that p2m(PFN) == MFN. Otherwise we can get confused if
193 * we are foreign-mapping the MFN, and the other domain as m2p(MFN) == PFN.
194 * Yikes! Various places must poke in INVALID_P2M_ENTRY for safety.
195 *
196 * NB2. When deliberately mapping foreign pages into the p2m table, you *must*
197 * use FOREIGN_FRAME(). This will cause pte_pfn() to choke on it, as we
198 * require. In all the cases we care about, the FOREIGN_FRAME bit is
199 * masked (e.g., pfn_to_mfn()) so behaviour there is correct.
200 */
201static inline unsigned long mfn_to_local_pfn(unsigned long mfn)
202{
Mukesh Rathorfc590ef2013-12-13 12:09:28 -0500203 unsigned long pfn;
204
205 if (xen_feature(XENFEAT_auto_translated_physmap))
206 return mfn;
207
208 pfn = mfn_to_pfn(mfn);
Juergen Gross0aad5682014-11-28 11:53:57 +0100209 if (__pfn_to_mfn(pfn) != mfn)
Jeremy Fitzhardingec0011db2010-02-04 14:46:34 -0800210 return -1; /* force !pfn_valid() */
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700211 return pfn;
212}
213
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700214/* VIRT <-> MACHINE conversion */
215#define virt_to_machine(v) (phys_to_machine(XPADDR(__pa(v))))
Alex Nixonb40bf532009-02-09 12:05:46 -0800216#define virt_to_pfn(v) (PFN_DOWN(__pa(v)))
217#define virt_to_mfn(v) (pfn_to_mfn(virt_to_pfn(v)))
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700218#define mfn_to_virt(m) (__va(mfn_to_pfn(m) << PAGE_SHIFT))
219
220static inline unsigned long pte_mfn(pte_t pte)
221{
Jeremy Fitzhardinge59438c92008-07-21 22:59:42 -0700222 return (pte.pte & PTE_PFN_MASK) >> PAGE_SHIFT;
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700223}
224
225static inline pte_t mfn_pte(unsigned long page_nr, pgprot_t pgprot)
226{
227 pte_t pte;
228
229 pte.pte = ((phys_addr_t)page_nr << PAGE_SHIFT) |
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800230 massage_pgprot(pgprot);
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700231
232 return pte;
233}
234
235static inline pteval_t pte_val_ma(pte_t pte)
236{
237 return pte.pte;
238}
239
240static inline pte_t __pte_ma(pteval_t x)
241{
242 return (pte_t) { .pte = x };
243}
244
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700245#define pmd_val_ma(v) ((v).pmd)
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700246#ifdef __PAGETABLE_PUD_FOLDED
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700247#define pud_val_ma(v) ((v).pgd.pgd)
Jeremy Fitzhardingef6e58732008-07-08 15:06:38 -0700248#else
249#define pud_val_ma(v) ((v).pud)
250#endif
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700251#define __pmd_ma(x) ((pmd_t) { (x) } )
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700252
253#define pgd_val_ma(x) ((x).pgd)
254
Jeremy Fitzhardingeeba3ff82009-02-09 12:05:49 -0800255void xen_set_domain_pte(pte_t *ptep, pte_t pteval, unsigned domid);
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700256
Jeremy Fitzhardingece803e72008-07-08 15:06:55 -0700257xmaddr_t arbitrary_virt_to_machine(void *address);
Jeremy Fitzhardinge9976b392009-02-27 09:19:26 -0800258unsigned long arbitrary_virt_to_mfn(void *vaddr);
Isaku Yamahata20e71f2e2008-04-02 10:53:58 -0700259void make_lowmem_page_readonly(void *vaddr);
260void make_lowmem_page_readwrite(void *vaddr);
261
Stefano Stabellini3216dce2013-02-19 13:59:19 +0000262#define xen_remap(cookie, size) ioremap((cookie), (size));
Konrad Rzeszutek Wilkefaf30a2014-01-06 10:40:36 -0500263#define xen_unmap(cookie) iounmap((cookie))
Stefano Stabellini3216dce2013-02-19 13:59:19 +0000264
Stefano Stabellinia4dba132014-11-21 11:07:39 +0000265static inline bool xen_arch_need_swiotlb(struct device *dev,
266 unsigned long pfn,
267 unsigned long mfn)
268{
269 return false;
270}
271
Stefano Stabellini87465152015-04-24 10:16:40 +0100272static inline unsigned long xen_get_swiotlb_free_pages(unsigned int order)
273{
274 return __get_free_pages(__GFP_NOWARN, order);
275}
276
H. Peter Anvin05e4d312008-10-23 00:01:39 -0700277#endif /* _ASM_X86_XEN_PAGE_H */