1 /*
2  * Copyright (c) 2021 EPAM Systems
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef __XEN_GENERIC_H__
7 #define __XEN_GENERIC_H__
8 
9 #include <zephyr/xen/public/xen.h>
10 
11 #define XEN_PAGE_SIZE		4096
12 #define XEN_PAGE_SHIFT		12
13 
14 #define XEN_PFN_UP(x)		(unsigned long)(((x) + XEN_PAGE_SIZE-1) >> XEN_PAGE_SHIFT)
15 #define XEN_PFN_DOWN(x)		(unsigned long)((x) >> XEN_PAGE_SHIFT)
16 #define XEN_PFN_PHYS(x)		((unsigned long)(x) << XEN_PAGE_SHIFT)
17 #define XEN_PHYS_PFN(x)		(unsigned long)((x) >> XEN_PAGE_SHIFT)
18 
19 #define xen_to_phys(x)		((unsigned long) (x))
20 #define xen_to_virt(x)		((void *) (x))
21 
22 #define xen_virt_to_gfn(_virt)	(XEN_PFN_DOWN(xen_to_phys(_virt)))
23 #define xen_gfn_to_virt(_gfn)	(xen_to_virt(XEN_PFN_PHYS(_gfn)))
24 
25 /*
26  * Atomically exchange value on "ptr" position. If value on "ptr" contains
27  * "old", then store and return "new". Otherwise, return the "old" value.
28  */
29 #define synch_cmpxchg(ptr, old, new) \
30 ({ __typeof__(*ptr) stored = old; \
31 	__atomic_compare_exchange_n(ptr, &stored, new, 0, __ATOMIC_SEQ_CST, \
32 				__ATOMIC_SEQ_CST) ? new : old; \
33 })
34 
35 #endif /* __XEN_GENERIC_H__ */
36