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
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.
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>
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>
43 #include <asm/bitops.h>
44 #include <asm/kmap_types.h>
45 #include <asm/atomic.h>
47 #include "include/osd.h"
52 typedef struct _TIMER
{
53 struct timer_list timer
;
54 PFN_TIMER_CALLBACK callback
;
59 typedef struct _WAITEVENT
{
61 wait_queue_head_t event
;
64 typedef struct _WORKQUEUE
{
65 struct workqueue_struct
*queue
;
68 typedef struct _WORKITEM
{
69 struct work_struct work
;
70 PFN_WORKITEM_CALLBACK callback
;
79 void LogMsg(const char *fmt
, ...)
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))
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
)
137 return __vmalloc(size
, GFP_KERNEL
, PAGE_KERNEL_EXEC
);
139 return __vmalloc(size
, GFP_KERNEL
, __pgprot(__PAGE_KERNEL
& (~_PAGE_NX
)));
143 void VirtualFree(void* VirtAddr
)
145 return vfree(VirtAddr
);
148 void* PageAlloc(unsigned int count
)
151 p
= (void *)__get_free_pages(GFP_KERNEL
, get_order(count
* PAGE_SIZE
));
152 if (p
) memset(p
, 0, count
* PAGE_SIZE
);
155 //struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO);
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);
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);
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
)
186 return (void*)(phys
+ 0xFFFF83000C000000);
188 return (void*)(phys
+ 0xfb000000);
191 return (void*)GetVirtualAddress(phys
); //return ioremap_nocache(phys, size);
195 void MemUnmapIO(void *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
);
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
;
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
);
248 size_t GetTickCount(void)
253 signed long long GetTimestamp(void)
259 return timeval_to_ns(&t
);
262 HANDLE
WaitEventCreate(void)
264 WAITEVENT
* wait
= kmalloc(sizeof(WAITEVENT
), GFP_KERNEL
);
271 init_waitqueue_head(&wait
->event
);
275 void WaitEventClose(HANDLE hWait
)
277 WAITEVENT
* waitEvent
= (WAITEVENT
* )hWait
;
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
)
291 WAITEVENT
* waitEvent
= (WAITEVENT
* )hWait
;
293 ret
= wait_event_interruptible(waitEvent
->event
,
294 waitEvent
->condition
);
295 waitEvent
->condition
= 0;
299 int WaitEventWaitEx(HANDLE hWait
, u32 TimeoutInMs
)
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;
311 void* Physical2LogicalAddr(unsigned long PhysAddr
)
313 void* logicalAddr
= phys_to_virt(PhysAddr
);
314 BUG_ON(!virt_addr_valid(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
);
341 HANDLE
WorkQueueCreate(char* name
)
343 WORKQUEUE
*wq
= kmalloc(sizeof(WORKQUEUE
), GFP_KERNEL
);
348 wq
->queue
= create_workqueue(name
);
353 void WorkQueueClose(HANDLE hWorkQueue
)
355 WORKQUEUE
*wq
= (WORKQUEUE
*)hWorkQueue
;
357 destroy_workqueue(wq
->queue
);
362 int WorkQueueQueueWorkItem(HANDLE hWorkQueue
, PFN_WORKITEM_CALLBACK workItem
, void* context
)
364 WORKQUEUE
*wq
= (WORKQUEUE
*)hWorkQueue
;
366 WORKITEM
* w
= kmalloc(sizeof(WORKITEM
), GFP_ATOMIC
);
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
);
386 w
->callback
= workItem
,
387 w
->context
= context
;
388 INIT_WORK(&w
->work
, WorkItemCallback
);
389 schedule_work(&w
->work
);