]> Pileus Git - ~andy/linux/blob - drivers/staging/hv/include/osd.h
Staging: hv: remove PAGE_SIZE and PAGE_SHIFT and __builtin functions
[~andy/linux] / drivers / staging / hv / include / osd.h
1 /*
2  *
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * Authors:
19  *   Haiyang Zhang <haiyangz@microsoft.com>
20  *   Hank Janssen  <hjanssen@microsoft.com>
21  *
22  */
23
24
25 #ifndef _OSD_H_
26 #define _OSD_H_
27
28 //
29 // Defines
30 //
31
32
33 #define STRUCT_PACKED           __attribute__((__packed__))
34 #define STRUCT_ALIGNED(x)       __attribute__((__aligned__(x)))
35
36 #define UNUSED_VAR(v)           v  __attribute__((__unused__))
37
38 #define ALIGN_UP(value, align)                  ( ((value) & (align-1))? ( ((value) + (align-1)) & ~(align-1) ): (value) )
39 #define ALIGN_DOWN(value, align)                ( (value) & ~(align-1) )
40 #define NUM_PAGES_SPANNED(addr, len)    ( (ALIGN_UP(addr+len, PAGE_SIZE) - ALIGN_DOWN(addr, PAGE_SIZE)) >> PAGE_SHIFT )
41
42 #define LOWORD(dw)              ((unsigned short) (dw))
43 #define HIWORD(dw)              ((unsigned short) (((unsigned int) (dw) >> 16) & 0xFFFF))
44
45 #define FIELD_OFFSET(t, f)    ((unsigned int)(unsigned long)&(((t *)0)->f))
46
47 #ifdef FALSE
48 #undef FALSE
49 #endif
50 #define FALSE 0
51
52 #ifdef TRUE
53 #undef TRUE
54 #endif
55 #define TRUE  1
56
57 #ifndef NULL
58 #define NULL  (void *)0
59 #endif
60
61 typedef struct _DLIST_ENTRY {
62    struct _DLIST_ENTRY *Flink;
63    struct _DLIST_ENTRY *Blink;
64 } DLIST_ENTRY;
65
66 //
67 // Other types
68 //
69 //typedef unsigned char         GUID[16];
70 typedef void*                           HANDLE;
71
72 typedef struct {
73         unsigned char   Data[16];
74 } GUID;
75
76 typedef void (*PFN_WORKITEM_CALLBACK)(void* context);
77 typedef void (*PFN_TIMER_CALLBACK)(void* context);
78
79
80 #ifdef __x86_64__
81
82 #define RDMSR(reg, v) {                                                        \
83     u32 h, l;                                                                 \
84      __asm__ __volatile__("rdmsr"                                                               \
85     : "=a" (l), "=d" (h)                                                       \
86     : "c" (reg));                                                              \
87     v = (((u64)h) << 32) | l;                                                         \
88 }
89
90 #define WRMSR(reg, v) {                                                        \
91     u32 h, l;                                                               \
92     l = (u32)(((u64)(v)) & 0xFFFFFFFF);                                  \
93     h = (u32)((((u64)(v)) >> 32) & 0xFFFFFFFF);                          \
94      __asm__ __volatile__("wrmsr"                                              \
95     : /* no outputs */                                                         \
96     : "c" (reg), "a" (l), "d" (h));                                            \
97 }
98
99 #else
100
101 #define RDMSR(reg, v)                                                                  \
102      __asm__ __volatile__("rdmsr"                                                  \
103     : "=A" (v)                                                                         \
104     : "c" (reg))
105
106 #define WRMSR(reg, v)                                                                  \
107      __asm__ __volatile__("wrmsr"                                                  \
108     : /* no outputs */                                                                     \
109     : "c" (reg), "A" ((u64)v))
110
111 #endif
112
113
114 static inline void do_cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx)
115 {
116         __asm__ __volatile__("cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "0" (op), "c" (ecx));
117 }
118
119 //
120 // Osd routines
121 //
122 extern void LogMsg(const char *fmt, ...);
123
124 extern void BitSet(unsigned int* addr, int value);
125 extern void BitClear(unsigned int* addr, int value);
126 extern int BitTest(unsigned int* addr, int value);
127 extern int BitTestAndClear(unsigned int* addr, int value);
128 extern int BitTestAndSet(unsigned int* addr, int value);
129
130 extern int InterlockedIncrement(int *val);
131 extern int InterlockedDecrement(int *val);
132 extern int InterlockedCompareExchange(int *val, int new, int curr);
133
134 extern void Sleep(unsigned long usecs);
135
136 extern void* VirtualAllocExec(unsigned int size);
137 extern void VirtualFree(void* VirtAddr);
138
139 extern void* PageAlloc(unsigned int count);
140 extern void PageFree(void* page, unsigned int count);
141
142 extern void* MemMapIO(unsigned long phys, unsigned long size);
143 extern void MemUnmapIO(void* virt);
144
145 extern void* MemAlloc(unsigned int size);
146 extern void* MemAllocZeroed(unsigned int size);
147 extern void* MemAllocAtomic(unsigned int size);
148 extern void MemFree(void* buf);
149 extern void MemoryFence(void);
150
151 extern HANDLE TimerCreate(PFN_TIMER_CALLBACK pfnTimerCB, void* context);
152 extern void TimerClose(HANDLE hTimer);
153 extern int TimerStop(HANDLE hTimer);
154 extern void TimerStart(HANDLE hTimer, u32 expirationInUs);
155 extern size_t GetTickCount(void);
156
157 extern HANDLE WaitEventCreate(void);
158 extern void WaitEventClose(HANDLE hWait);
159 extern void WaitEventSet(HANDLE hWait);
160 extern int      WaitEventWait(HANDLE hWait);
161
162 // If >0, hWait got signaled. If ==0, timeout. If < 0, error
163 extern int      WaitEventWaitEx(HANDLE hWait, u32 TimeoutInMs);
164
165 extern HANDLE SpinlockCreate(void);
166 extern void SpinlockClose(HANDLE hSpin);
167 extern void SpinlockAcquire(HANDLE hSpin);
168 extern void     SpinlockRelease(HANDLE hSpin);
169
170
171 #define GetVirtualAddress Physical2LogicalAddr
172 void* Physical2LogicalAddr(unsigned long PhysAddr);
173
174 #define GetPhysicalAddress Logical2PhysicalAddr
175 unsigned long Logical2PhysicalAddr(void * LogicalAddr);
176
177 unsigned long Virtual2Physical(void * VirtAddr);
178
179 void* PageMapVirtualAddress(unsigned long Pfn);
180 void PageUnmapVirtualAddress(void* VirtAddr);
181
182
183 extern HANDLE WorkQueueCreate(char* name);
184 extern void WorkQueueClose(HANDLE hWorkQueue);
185 extern int WorkQueueQueueWorkItem(HANDLE hWorkQueue, PFN_WORKITEM_CALLBACK workItem, void* context);
186
187 extern void QueueWorkItem(PFN_WORKITEM_CALLBACK workItem, void* context);
188
189 #endif // _OSD_H_