Staging: hv: osd: remove MemoryFence wrapper
[linux-2.6/libata-dev.git] / drivers / staging / hv / osd.c
blob7a4c4381fd4dbc0be4bfa95d6fd647cce7c51a37
1 /*
3 * Copyright (c) 2009, Microsoft Corporation.
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.
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.
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.
18 * Authors:
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/mm.h>
28 #include <linux/highmem.h>
29 #include <linux/vmalloc.h>
30 #include <linux/ioport.h>
31 #include <linux/irq.h>
32 #include <linux/interrupt.h>
33 #include <linux/wait.h>
34 #include <linux/spinlock.h>
35 #include <linux/workqueue.h>
36 #include <linux/kernel.h>
37 #include <linux/timer.h>
38 #include <linux/jiffies.h>
39 #include <linux/delay.h>
40 #include <linux/time.h>
42 #include <asm/io.h>
43 #include <asm/bitops.h>
44 #include <asm/kmap_types.h>
45 #include <asm/atomic.h>
47 #include "include/osd.h"
50 // Data types
52 typedef struct _TIMER {
53 struct timer_list timer;
54 PFN_TIMER_CALLBACK callback;
55 void* context;
56 }TIMER;
59 typedef struct _WAITEVENT {
60 int condition;
61 wait_queue_head_t event;
62 } WAITEVENT;
64 typedef struct _WORKQUEUE {
65 struct workqueue_struct *queue;
66 } WORKQUEUE;
68 typedef struct _WORKITEM {
69 struct work_struct work;
70 PFN_WORKITEM_CALLBACK callback;
71 void* context;
72 } WORKITEM;
76 // Global
79 void LogMsg(const char *fmt, ...)
81 va_list args;
83 va_start(args, fmt);
84 vprintk(fmt, args);
85 va_end(args);
88 void BitSet(unsigned int* addr, int bit)
90 set_bit(bit, (unsigned long*)addr);
93 int BitTest(unsigned int* addr, int bit)
95 return test_bit(bit, (unsigned long*)addr);
98 void BitClear(unsigned int* addr, int bit)
100 clear_bit(bit, (unsigned long*)addr);
103 int BitTestAndClear(unsigned int* addr, int bit)
105 return test_and_clear_bit(bit, (unsigned long*)addr);
108 int BitTestAndSet(unsigned int* addr, int bit)
110 return test_and_set_bit(bit, (unsigned long*)addr);
114 int InterlockedIncrement(int *val)
116 return atomic_inc_return((atomic_t*)val);
119 int InterlockedDecrement(int *val)
121 return atomic_dec_return((atomic_t*)val);
124 #ifndef atomic_cmpxchg
125 #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
126 #endif
127 int InterlockedCompareExchange(int *val, int new, int curr)
129 //return ((int)cmpxchg(((atomic_t*)val), curr, new));
130 return atomic_cmpxchg((atomic_t*)val, curr, new);
134 void* VirtualAllocExec(unsigned int size)
136 #ifdef __x86_64__
137 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC);
138 #else
139 return __vmalloc(size, GFP_KERNEL, __pgprot(__PAGE_KERNEL & (~_PAGE_NX)));
140 #endif
143 void VirtualFree(void* VirtAddr)
145 return vfree(VirtAddr);
148 void* PageAlloc(unsigned int count)
150 void *p;
151 p = (void *)__get_free_pages(GFP_KERNEL, get_order(count * PAGE_SIZE));
152 if (p) memset(p, 0, count * PAGE_SIZE);
153 return p;
155 //struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO);
156 //void *p;
158 ////BUGBUG: We need to use kmap in case we are in HIMEM region
159 //p = page_address(page);
160 //if (p) memset(p, 0, PAGE_SIZE);
161 //return p;
164 void PageFree(void* page, unsigned int count)
166 free_pages((unsigned long)page, get_order(count * PAGE_SIZE));
167 /*struct page* p = virt_to_page(page);
168 __free_page(p);*/
172 void* PageMapVirtualAddress(unsigned long Pfn)
174 return kmap_atomic(pfn_to_page(Pfn), KM_IRQ0);
177 void PageUnmapVirtualAddress(void* VirtAddr)
179 kunmap_atomic(VirtAddr, KM_IRQ0);
182 void *MemMapIO(unsigned long phys, unsigned long size)
184 #if X2V_LINUX
185 #ifdef __x86_64__
186 return (void*)(phys + 0xFFFF83000C000000);
187 #else // i386
188 return (void*)(phys + 0xfb000000);
189 #endif
190 #else
191 return (void*)GetVirtualAddress(phys); //return ioremap_nocache(phys, size);
192 #endif
195 void MemUnmapIO(void *virt)
197 //iounmap(virt);
200 void TimerCallback(unsigned long data)
202 TIMER* t = (TIMER*)data;
204 t->callback(t->context);
207 HANDLE TimerCreate(PFN_TIMER_CALLBACK pfnTimerCB, void* context)
209 TIMER* t = kmalloc(sizeof(TIMER), GFP_KERNEL);
210 if (!t)
212 return NULL;
215 t->callback = pfnTimerCB;
216 t->context = context;
218 init_timer(&t->timer);
219 t->timer.data = (unsigned long)t;
220 t->timer.function = TimerCallback;
222 return t;
225 void TimerStart(HANDLE hTimer, u32 expirationInUs)
227 TIMER* t = (TIMER* )hTimer;
229 t->timer.expires = jiffies + usecs_to_jiffies(expirationInUs);
230 add_timer(&t->timer);
233 int TimerStop(HANDLE hTimer)
235 TIMER* t = (TIMER* )hTimer;
237 return del_timer(&t->timer);
240 void TimerClose(HANDLE hTimer)
242 TIMER* t = (TIMER* )hTimer;
244 del_timer(&t->timer);
245 kfree(t);
248 size_t GetTickCount(void)
250 return jiffies;
253 signed long long GetTimestamp(void)
255 struct timeval t;
257 do_gettimeofday(&t);
259 return timeval_to_ns(&t);
262 HANDLE WaitEventCreate(void)
264 WAITEVENT* wait = kmalloc(sizeof(WAITEVENT), GFP_KERNEL);
265 if (!wait)
267 return NULL;
270 wait->condition = 0;
271 init_waitqueue_head(&wait->event);
272 return wait;
275 void WaitEventClose(HANDLE hWait)
277 WAITEVENT* waitEvent = (WAITEVENT* )hWait;
278 kfree(waitEvent);
281 void WaitEventSet(HANDLE hWait)
283 WAITEVENT* waitEvent = (WAITEVENT* )hWait;
284 waitEvent->condition = 1;
285 wake_up_interruptible(&waitEvent->event);
288 int WaitEventWait(HANDLE hWait)
290 int ret=0;
291 WAITEVENT* waitEvent = (WAITEVENT* )hWait;
293 ret= wait_event_interruptible(waitEvent->event,
294 waitEvent->condition);
295 waitEvent->condition = 0;
296 return ret;
299 int WaitEventWaitEx(HANDLE hWait, u32 TimeoutInMs)
301 int ret=0;
302 WAITEVENT* waitEvent = (WAITEVENT* )hWait;
304 ret= wait_event_interruptible_timeout(waitEvent->event,
305 waitEvent->condition,
306 msecs_to_jiffies(TimeoutInMs));
307 waitEvent->condition = 0;
308 return ret;
311 void* Physical2LogicalAddr(unsigned long PhysAddr)
313 void* logicalAddr = phys_to_virt(PhysAddr);
314 BUG_ON(!virt_addr_valid(logicalAddr));
315 return logicalAddr;
318 unsigned long Logical2PhysicalAddr(void * LogicalAddr)
320 BUG_ON(!virt_addr_valid(LogicalAddr));
321 return virt_to_phys(LogicalAddr);
325 unsigned long Virtual2Physical(void * VirtAddr)
327 unsigned long pfn = vmalloc_to_pfn(VirtAddr);
329 return pfn << PAGE_SHIFT;
332 void WorkItemCallback(struct work_struct *work)
334 WORKITEM* w = (WORKITEM*)work;
336 w->callback(w->context);
338 kfree(w);
341 HANDLE WorkQueueCreate(char* name)
343 WORKQUEUE *wq = kmalloc(sizeof(WORKQUEUE), GFP_KERNEL);
344 if (!wq)
346 return NULL;
348 wq->queue = create_workqueue(name);
350 return wq;
353 void WorkQueueClose(HANDLE hWorkQueue)
355 WORKQUEUE *wq = (WORKQUEUE *)hWorkQueue;
357 destroy_workqueue(wq->queue);
359 return;
362 int WorkQueueQueueWorkItem(HANDLE hWorkQueue, PFN_WORKITEM_CALLBACK workItem, void* context)
364 WORKQUEUE *wq = (WORKQUEUE *)hWorkQueue;
366 WORKITEM* w = kmalloc(sizeof(WORKITEM), GFP_ATOMIC);
367 if (!w)
369 return -1;
372 w->callback = workItem,
373 w->context = context;
374 INIT_WORK(&w->work, WorkItemCallback);
375 return queue_work(wq->queue, &w->work);
378 void QueueWorkItem(PFN_WORKITEM_CALLBACK workItem, void* context)
380 WORKITEM* w = kmalloc(sizeof(WORKITEM), GFP_ATOMIC);
381 if (!w)
383 return;
386 w->callback = workItem,
387 w->context = context;
388 INIT_WORK(&w->work, WorkItemCallback);
389 schedule_work(&w->work);