]> Pileus Git - ~andy/linux/blob - drivers/staging/hv/include/osd.h
dfc20c3d3f9b71010b0aff9f1488c677a9ab13b1
[~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 #ifndef PAGE_SIZE
33 #define PAGE_SIZE 0x1000
34 #endif
35
36 #ifndef PAGE_SHIFT
37 #define PAGE_SHIFT 12
38 #endif
39
40 #ifndef memcpy
41 #define memcpy          __builtin_memcpy
42 #endif
43
44 #ifndef memset
45 #define memset          __builtin_memset
46 #endif
47
48 #ifndef memcmp
49 #define memcmp          __builtin_memcmp
50 #endif
51
52 #ifndef strcpy
53 #define strcpy          __builtin_strcpy
54 #endif
55
56 //
57 //#ifndef sprintf
58 //#define sprintf                               __builtin_sprintf
59 //#endif
60
61 #define STRUCT_PACKED           __attribute__((__packed__))
62 #define STRUCT_ALIGNED(x)       __attribute__((__aligned__(x)))
63
64 #define UNUSED_VAR(v)           v  __attribute__((__unused__))
65
66 #define ALIGN_UP(value, align)                  ( ((value) & (align-1))? ( ((value) + (align-1)) & ~(align-1) ): (value) )
67 #define ALIGN_DOWN(value, align)                ( (value) & ~(align-1) )
68 #define NUM_PAGES_SPANNED(addr, len)    ( (ALIGN_UP(addr+len, PAGE_SIZE) - ALIGN_DOWN(addr, PAGE_SIZE)) >> PAGE_SHIFT )
69
70 #define LOWORD(dw)              ((unsigned short) (dw))
71 #define HIWORD(dw)              ((unsigned short) (((unsigned int) (dw) >> 16) & 0xFFFF))
72
73 #define FIELD_OFFSET(t, f)    ((unsigned int)(unsigned long)&(((t *)0)->f))
74
75 #ifdef FALSE
76 #undef FALSE
77 #endif
78 #define FALSE 0
79
80 #ifdef TRUE
81 #undef TRUE
82 #endif
83 #define TRUE  1
84
85 #ifndef NULL
86 #define NULL  (void *)0
87 #endif
88
89 typedef struct _DLIST_ENTRY {
90    struct _DLIST_ENTRY *Flink;
91    struct _DLIST_ENTRY *Blink;
92 } DLIST_ENTRY;
93
94 //
95 // Other types
96 //
97 //typedef unsigned char         GUID[16];
98 typedef void*                           HANDLE;
99
100 typedef struct {
101         unsigned char   Data[16];
102 } GUID;
103
104 typedef void (*PFN_WORKITEM_CALLBACK)(void* context);
105 typedef void (*PFN_TIMER_CALLBACK)(void* context);
106
107
108 #ifdef __x86_64__
109
110 #define RDMSR(reg, v) {                                                        \
111     u32 h, l;                                                                 \
112      __asm__ __volatile__("rdmsr"                                                               \
113     : "=a" (l), "=d" (h)                                                       \
114     : "c" (reg));                                                              \
115     v = (((u64)h) << 32) | l;                                                         \
116 }
117
118 #define WRMSR(reg, v) {                                                        \
119     u32 h, l;                                                               \
120     l = (u32)(((u64)(v)) & 0xFFFFFFFF);                                  \
121     h = (u32)((((u64)(v)) >> 32) & 0xFFFFFFFF);                          \
122      __asm__ __volatile__("wrmsr"                                              \
123     : /* no outputs */                                                         \
124     : "c" (reg), "a" (l), "d" (h));                                            \
125 }
126
127 #else
128
129 #define RDMSR(reg, v)                                                                  \
130      __asm__ __volatile__("rdmsr"                                                  \
131     : "=A" (v)                                                                         \
132     : "c" (reg))
133
134 #define WRMSR(reg, v)                                                                  \
135      __asm__ __volatile__("wrmsr"                                                  \
136     : /* no outputs */                                                                     \
137     : "c" (reg), "A" ((u64)v))
138
139 #endif
140
141
142 static inline void do_cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx)
143 {
144         __asm__ __volatile__("cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "0" (op), "c" (ecx));
145 }
146
147 //
148 // Osd routines
149 //
150 extern void LogMsg(const char *fmt, ...);
151
152 extern void BitSet(unsigned int* addr, int value);
153 extern void BitClear(unsigned int* addr, int value);
154 extern int BitTest(unsigned int* addr, int value);
155 extern int BitTestAndClear(unsigned int* addr, int value);
156 extern int BitTestAndSet(unsigned int* addr, int value);
157
158 extern int InterlockedIncrement(int *val);
159 extern int InterlockedDecrement(int *val);
160 extern int InterlockedCompareExchange(int *val, int new, int curr);
161
162 extern void Sleep(unsigned long usecs);
163
164 extern void* VirtualAllocExec(unsigned int size);
165 extern void VirtualFree(void* VirtAddr);
166
167 extern void* PageAlloc(unsigned int count);
168 extern void PageFree(void* page, unsigned int count);
169
170 extern void* MemMapIO(unsigned long phys, unsigned long size);
171 extern void MemUnmapIO(void* virt);
172
173 extern void* MemAlloc(unsigned int size);
174 extern void* MemAllocZeroed(unsigned int size);
175 extern void* MemAllocAtomic(unsigned int size);
176 extern void MemFree(void* buf);
177 extern void MemoryFence(void);
178
179 extern HANDLE TimerCreate(PFN_TIMER_CALLBACK pfnTimerCB, void* context);
180 extern void TimerClose(HANDLE hTimer);
181 extern int TimerStop(HANDLE hTimer);
182 extern void TimerStart(HANDLE hTimer, u32 expirationInUs);
183 extern size_t GetTickCount(void);
184
185 extern HANDLE WaitEventCreate(void);
186 extern void WaitEventClose(HANDLE hWait);
187 extern void WaitEventSet(HANDLE hWait);
188 extern int      WaitEventWait(HANDLE hWait);
189
190 // If >0, hWait got signaled. If ==0, timeout. If < 0, error
191 extern int      WaitEventWaitEx(HANDLE hWait, u32 TimeoutInMs);
192
193 extern HANDLE SpinlockCreate(void);
194 extern void SpinlockClose(HANDLE hSpin);
195 extern void SpinlockAcquire(HANDLE hSpin);
196 extern void     SpinlockRelease(HANDLE hSpin);
197
198
199 #define GetVirtualAddress Physical2LogicalAddr
200 void* Physical2LogicalAddr(unsigned long PhysAddr);
201
202 #define GetPhysicalAddress Logical2PhysicalAddr
203 unsigned long Logical2PhysicalAddr(void * LogicalAddr);
204
205 unsigned long Virtual2Physical(void * VirtAddr);
206
207 void* PageMapVirtualAddress(unsigned long Pfn);
208 void PageUnmapVirtualAddress(void* VirtAddr);
209
210
211 extern HANDLE WorkQueueCreate(char* name);
212 extern void WorkQueueClose(HANDLE hWorkQueue);
213 extern int WorkQueueQueueWorkItem(HANDLE hWorkQueue, PFN_WORKITEM_CALLBACK workItem, void* context);
214
215 extern void QueueWorkItem(PFN_WORKITEM_CALLBACK workItem, void* context);
216
217 #endif // _OSD_H_