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 *);
59 void *osd_VirtualAllocExec(unsigned int size)
62 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC);
64 return __vmalloc(size, GFP_KERNEL, __pgprot(__PAGE_KERNEL & (~_PAGE_NX)));
68 void *osd_PageAlloc(unsigned int count)
71 p = (void *)__get_free_pages(GFP_KERNEL, get_order(count * PAGE_SIZE));
72 if (p) memset(p, 0, count * PAGE_SIZE);
75 /* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
78 /* BUGBUG: We need to use kmap in case we are in HIMEM region */
79 /* p = page_address(page); */
80 /* if (p) memset(p, 0, PAGE_SIZE); */
84 void osd_PageFree(void* page, unsigned int count)
86 free_pages((unsigned long)page, get_order(count * PAGE_SIZE));
87 /*struct page* p = virt_to_page(page);
91 static void TimerCallback(unsigned long data)
93 struct osd_timer *t = (struct osd_timer *) data;
95 t->callback(t->context);
98 struct osd_timer *osd_TimerCreate(PFN_TIMER_CALLBACK pfnTimerCB, void* context)
100 struct osd_timer *t = kmalloc(sizeof(struct osd_timer), GFP_KERNEL);
106 t->callback = pfnTimerCB;
107 t->context = context;
109 init_timer(&t->timer);
110 t->timer.data = (unsigned long)t;
111 t->timer.function = TimerCallback;
116 void osd_TimerStart(struct osd_timer *t, u32 expirationInUs)
118 t->timer.expires = jiffies + usecs_to_jiffies(expirationInUs);
119 add_timer(&t->timer);
122 int osd_TimerStop(struct osd_timer *t)
124 return del_timer(&t->timer);
127 void osd_TimerClose(struct osd_timer *t)
129 del_timer(&t->timer);
133 struct osd_waitevent *osd_WaitEventCreate(void)
135 struct osd_waitevent *wait = kmalloc(sizeof(struct osd_waitevent), GFP_KERNEL);
142 init_waitqueue_head(&wait->event);
146 void osd_WaitEventSet(struct osd_waitevent *waitEvent)
148 waitEvent->condition = 1;
149 wake_up_interruptible(&waitEvent->event);
152 int osd_WaitEventWait(struct osd_waitevent *waitEvent)
156 ret = wait_event_interruptible(waitEvent->event,
157 waitEvent->condition);
158 waitEvent->condition = 0;
162 int osd_WaitEventWaitEx(struct osd_waitevent *waitEvent, u32 TimeoutInMs)
166 ret = wait_event_interruptible_timeout(waitEvent->event,
167 waitEvent->condition,
168 msecs_to_jiffies(TimeoutInMs));
169 waitEvent->condition = 0;
173 static void osd_callback_work(struct work_struct *work)
175 struct osd_callback_struct *cb = container_of(work,
176 struct osd_callback_struct,
178 (cb->callback)(cb->data);
183 int osd_schedule_callback(struct workqueue_struct *wq,
184 void (*func)(void *),
187 struct osd_callback_struct *cb;
189 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
192 printk(KERN_ERR "unable to allocate memory in osd_schedule_callback");
198 INIT_WORK(&cb->work, osd_callback_work);
199 return queue_work(wq, &cb->work);