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"
53 struct osd_callback_struct {
54 struct work_struct work;
55 void (*callback)(void *);
60 void BitSet(unsigned int* addr, int bit)
62 set_bit(bit, (unsigned long*)addr);
65 int BitTest(unsigned int* addr, int bit)
67 return test_bit(bit, (unsigned long*)addr);
70 void BitClear(unsigned int* addr, int bit)
72 clear_bit(bit, (unsigned long*)addr);
75 int BitTestAndClear(unsigned int* addr, int bit)
77 return test_and_clear_bit(bit, (unsigned long*)addr);
80 int BitTestAndSet(unsigned int* addr, int bit)
82 return test_and_set_bit(bit, (unsigned long*)addr);
86 int InterlockedIncrement(int *val)
88 return atomic_inc_return((atomic_t*)val);
91 int InterlockedDecrement(int *val)
93 return atomic_dec_return((atomic_t*)val);
96 #ifndef atomic_cmpxchg
97 #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
99 int InterlockedCompareExchange(int *val, int new, int curr)
101 /* return ((int)cmpxchg(((atomic_t*)val), curr, new)); */
102 return atomic_cmpxchg((atomic_t*)val, curr, new);
106 void* VirtualAllocExec(unsigned int size)
109 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC);
111 return __vmalloc(size, GFP_KERNEL, __pgprot(__PAGE_KERNEL & (~_PAGE_NX)));
115 void VirtualFree(void* VirtAddr)
117 return vfree(VirtAddr);
120 void* PageAlloc(unsigned int count)
123 p = (void *)__get_free_pages(GFP_KERNEL, get_order(count * PAGE_SIZE));
124 if (p) memset(p, 0, count * PAGE_SIZE);
127 /* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
130 /* BUGBUG: We need to use kmap in case we are in HIMEM region */
131 /* p = page_address(page); */
132 /* if (p) memset(p, 0, PAGE_SIZE); */
136 void PageFree(void* page, unsigned int count)
138 free_pages((unsigned long)page, get_order(count * PAGE_SIZE));
139 /*struct page* p = virt_to_page(page);
144 void* PageMapVirtualAddress(unsigned long Pfn)
146 return kmap_atomic(pfn_to_page(Pfn), KM_IRQ0);
149 void PageUnmapVirtualAddress(void* VirtAddr)
151 kunmap_atomic(VirtAddr, KM_IRQ0);
154 void *MemMapIO(unsigned long phys, unsigned long size)
156 return (void*)GetVirtualAddress(phys); /* return ioremap_nocache(phys, size); */
159 void MemUnmapIO(void *virt)
164 static void TimerCallback(unsigned long data)
166 struct osd_timer *t = (struct osd_timer *) data;
168 t->callback(t->context);
171 struct osd_timer *TimerCreate(PFN_TIMER_CALLBACK pfnTimerCB, void* context)
173 struct osd_timer *t = kmalloc(sizeof(struct osd_timer), GFP_KERNEL);
179 t->callback = pfnTimerCB;
180 t->context = context;
182 init_timer(&t->timer);
183 t->timer.data = (unsigned long)t;
184 t->timer.function = TimerCallback;
189 void TimerStart(struct osd_timer *t, u32 expirationInUs)
191 t->timer.expires = jiffies + usecs_to_jiffies(expirationInUs);
192 add_timer(&t->timer);
195 int TimerStop(struct osd_timer *t)
197 return del_timer(&t->timer);
200 void TimerClose(struct osd_timer *t)
202 del_timer(&t->timer);
206 struct osd_waitevent *WaitEventCreate(void)
208 struct osd_waitevent *wait = kmalloc(sizeof(struct osd_waitevent), GFP_KERNEL);
215 init_waitqueue_head(&wait->event);
219 void WaitEventSet(struct osd_waitevent *waitEvent)
221 waitEvent->condition = 1;
222 wake_up_interruptible(&waitEvent->event);
225 int WaitEventWait(struct osd_waitevent *waitEvent)
229 ret = wait_event_interruptible(waitEvent->event,
230 waitEvent->condition);
231 waitEvent->condition = 0;
235 int WaitEventWaitEx(struct osd_waitevent *waitEvent, u32 TimeoutInMs)
239 ret = wait_event_interruptible_timeout(waitEvent->event,
240 waitEvent->condition,
241 msecs_to_jiffies(TimeoutInMs));
242 waitEvent->condition = 0;
246 void* Physical2LogicalAddr(unsigned long PhysAddr)
248 void* logicalAddr = phys_to_virt(PhysAddr);
249 BUG_ON(!virt_addr_valid(logicalAddr));
253 unsigned long Logical2PhysicalAddr(void * LogicalAddr)
255 BUG_ON(!virt_addr_valid(LogicalAddr));
256 return virt_to_phys(LogicalAddr);
260 unsigned long Virtual2Physical(void * VirtAddr)
262 unsigned long pfn = vmalloc_to_pfn(VirtAddr);
264 return pfn << PAGE_SHIFT;
267 static void osd_callback_work(struct work_struct *work)
269 struct osd_callback_struct *cb = container_of(work,
270 struct osd_callback_struct,
272 (cb->callback)(cb->data);
277 int osd_schedule_callback(struct workqueue_struct *wq,
278 void (*func)(void *),
281 struct osd_callback_struct *cb;
283 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
286 printk(KERN_ERR "unable to allocate memory in osd_schedule_callback");
292 INIT_WORK(&cb->work, osd_callback_work);
293 return queue_work(wq, &cb->work);