Staging: hv: remove wrapper function VirtualFree
[deliverable/linux.git] / 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* 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* 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 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
92 void* PageMapVirtualAddress(unsigned long Pfn)
93 {
94 return kmap_atomic(pfn_to_page(Pfn), KM_IRQ0);
95 }
96
97 void PageUnmapVirtualAddress(void* VirtAddr)
98 {
99 kunmap_atomic(VirtAddr, KM_IRQ0);
100 }
101
102 void *MemMapIO(unsigned long phys, unsigned long size)
103 {
104 return (void*)GetVirtualAddress(phys); /* return ioremap_nocache(phys, size); */
105 }
106
107 void MemUnmapIO(void *virt)
108 {
109 /* iounmap(virt); */
110 }
111
112 static void TimerCallback(unsigned long data)
113 {
114 struct osd_timer *t = (struct osd_timer *) data;
115
116 t->callback(t->context);
117 }
118
119 struct osd_timer *TimerCreate(PFN_TIMER_CALLBACK pfnTimerCB, void* context)
120 {
121 struct osd_timer *t = kmalloc(sizeof(struct osd_timer), GFP_KERNEL);
122 if (!t)
123 {
124 return NULL;
125 }
126
127 t->callback = pfnTimerCB;
128 t->context = context;
129
130 init_timer(&t->timer);
131 t->timer.data = (unsigned long)t;
132 t->timer.function = TimerCallback;
133
134 return t;
135 }
136
137 void TimerStart(struct osd_timer *t, u32 expirationInUs)
138 {
139 t->timer.expires = jiffies + usecs_to_jiffies(expirationInUs);
140 add_timer(&t->timer);
141 }
142
143 int TimerStop(struct osd_timer *t)
144 {
145 return del_timer(&t->timer);
146 }
147
148 void TimerClose(struct osd_timer *t)
149 {
150 del_timer(&t->timer);
151 kfree(t);
152 }
153
154 struct osd_waitevent *WaitEventCreate(void)
155 {
156 struct osd_waitevent *wait = kmalloc(sizeof(struct osd_waitevent), GFP_KERNEL);
157 if (!wait)
158 {
159 return NULL;
160 }
161
162 wait->condition = 0;
163 init_waitqueue_head(&wait->event);
164 return wait;
165 }
166
167 void WaitEventSet(struct osd_waitevent *waitEvent)
168 {
169 waitEvent->condition = 1;
170 wake_up_interruptible(&waitEvent->event);
171 }
172
173 int WaitEventWait(struct osd_waitevent *waitEvent)
174 {
175 int ret=0;
176
177 ret = wait_event_interruptible(waitEvent->event,
178 waitEvent->condition);
179 waitEvent->condition = 0;
180 return ret;
181 }
182
183 int WaitEventWaitEx(struct osd_waitevent *waitEvent, u32 TimeoutInMs)
184 {
185 int ret=0;
186
187 ret = wait_event_interruptible_timeout(waitEvent->event,
188 waitEvent->condition,
189 msecs_to_jiffies(TimeoutInMs));
190 waitEvent->condition = 0;
191 return ret;
192 }
193
194 void* Physical2LogicalAddr(unsigned long PhysAddr)
195 {
196 void* logicalAddr = phys_to_virt(PhysAddr);
197 BUG_ON(!virt_addr_valid(logicalAddr));
198 return logicalAddr;
199 }
200
201 unsigned long Logical2PhysicalAddr(void * LogicalAddr)
202 {
203 BUG_ON(!virt_addr_valid(LogicalAddr));
204 return virt_to_phys(LogicalAddr);
205 }
206
207
208 unsigned long Virtual2Physical(void * VirtAddr)
209 {
210 unsigned long pfn = vmalloc_to_pfn(VirtAddr);
211
212 return pfn << PAGE_SHIFT;
213 }
214
215 static void osd_callback_work(struct work_struct *work)
216 {
217 struct osd_callback_struct *cb = container_of(work,
218 struct osd_callback_struct,
219 work);
220 (cb->callback)(cb->data);
221
222 kfree(cb);
223 }
224
225 int osd_schedule_callback(struct workqueue_struct *wq,
226 void (*func)(void *),
227 void *data)
228 {
229 struct osd_callback_struct *cb;
230
231 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
232 if (!cb)
233 {
234 printk(KERN_ERR "unable to allocate memory in osd_schedule_callback");
235 return -1;
236 }
237
238 cb->callback = func;
239 cb->data = data;
240 INIT_WORK(&cb->work, osd_callback_work);
241 return queue_work(wq, &cb->work);
242 }
243
This page took 0.044253 seconds and 5 git commands to generate.