]> Pileus Git - ~andy/linux/blob - drivers/staging/hv/osd.c
c2d3094502e560e0356a85cc2e4ad42bd8fefb8a
[~andy/linux] / drivers / staging / hv / osd.c
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 #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>
41
42 #include <asm/io.h>
43 #include <asm/bitops.h>
44 #include <asm/kmap_types.h>
45 #include <asm/atomic.h>
46
47 #include "include/osd.h"
48
49
50 /* Data types */
51
52
53 struct osd_callback_struct {
54         struct work_struct work;
55         void (*callback)(void *);
56         void *data;
57 };
58
59 void *osd_VirtualAllocExec(unsigned int size)
60 {
61 #ifdef __x86_64__
62         return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC);
63 #else
64         return __vmalloc(size, GFP_KERNEL, __pgprot(__PAGE_KERNEL & (~_PAGE_NX)));
65 #endif
66 }
67
68 void *osd_PageAlloc(unsigned int count)
69 {
70         void *p;
71         p = (void *)__get_free_pages(GFP_KERNEL, get_order(count * PAGE_SIZE));
72         if (p) memset(p, 0, count * PAGE_SIZE);
73         return p;
74
75         /* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
76         /* void *p; */
77
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); */
81         /* return p; */
82 }
83
84 void osd_PageFree(void* page, unsigned int count)
85 {
86         free_pages((unsigned long)page, get_order(count * PAGE_SIZE));
87         /*struct page* p = virt_to_page(page);
88         __free_page(p);*/
89 }
90
91 static void TimerCallback(unsigned long data)
92 {
93         struct osd_timer *t = (struct osd_timer *) data;
94
95         t->callback(t->context);
96 }
97
98 struct osd_timer *osd_TimerCreate(PFN_TIMER_CALLBACK pfnTimerCB, void* context)
99 {
100         struct osd_timer *t = kmalloc(sizeof(struct osd_timer), GFP_KERNEL);
101         if (!t)
102         {
103                 return NULL;
104         }
105
106         t->callback = pfnTimerCB;
107         t->context = context;
108
109         init_timer(&t->timer);
110         t->timer.data = (unsigned long)t;
111         t->timer.function = TimerCallback;
112
113         return t;
114 }
115
116 void osd_TimerStart(struct osd_timer *t, u32 expirationInUs)
117 {
118         t->timer.expires = jiffies + usecs_to_jiffies(expirationInUs);
119         add_timer(&t->timer);
120 }
121
122 int osd_TimerStop(struct osd_timer *t)
123 {
124         return del_timer(&t->timer);
125 }
126
127 void osd_TimerClose(struct osd_timer *t)
128 {
129         del_timer(&t->timer);
130         kfree(t);
131 }
132
133 struct osd_waitevent *osd_WaitEventCreate(void)
134 {
135         struct osd_waitevent *wait = kmalloc(sizeof(struct osd_waitevent), GFP_KERNEL);
136         if (!wait)
137         {
138                 return NULL;
139         }
140
141         wait->condition = 0;
142         init_waitqueue_head(&wait->event);
143         return wait;
144 }
145
146 void osd_WaitEventSet(struct osd_waitevent *waitEvent)
147 {
148         waitEvent->condition = 1;
149         wake_up_interruptible(&waitEvent->event);
150 }
151
152 int osd_WaitEventWait(struct osd_waitevent *waitEvent)
153 {
154         int ret=0;
155
156         ret = wait_event_interruptible(waitEvent->event,
157                                        waitEvent->condition);
158         waitEvent->condition = 0;
159         return ret;
160 }
161
162 int osd_WaitEventWaitEx(struct osd_waitevent *waitEvent, u32 TimeoutInMs)
163 {
164         int ret=0;
165
166         ret = wait_event_interruptible_timeout(waitEvent->event,
167                                                waitEvent->condition,
168                                                msecs_to_jiffies(TimeoutInMs));
169         waitEvent->condition = 0;
170         return ret;
171 }
172
173 static void osd_callback_work(struct work_struct *work)
174 {
175         struct osd_callback_struct *cb = container_of(work,
176                                                       struct osd_callback_struct,
177                                                       work);
178         (cb->callback)(cb->data);
179
180         kfree(cb);
181 }
182
183 int osd_schedule_callback(struct workqueue_struct *wq,
184                           void (*func)(void *),
185                           void *data)
186 {
187         struct osd_callback_struct *cb;
188
189         cb = kmalloc(sizeof(*cb), GFP_KERNEL);
190         if (!cb)
191         {
192                 printk(KERN_ERR "unable to allocate memory in osd_schedule_callback");
193                 return -1;
194         }
195
196         cb->callback = func;
197         cb->data = data;
198         INIT_WORK(&cb->work, osd_callback_work);
199         return queue_work(wq, &cb->work);
200 }
201