Staging: hv: remove wrapper functions around kmap_
[linux-2.6/mini2440.git] / drivers / staging / hv / osd.c
blobad883c93ec9cc43eb7454ab99f45eec9f1bb127c
1 /*
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
12 * more details.
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.
18 * Authors:
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>
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>
42 #include <asm/io.h>
43 #include <asm/bitops.h>
44 #include <asm/kmap_types.h>
45 #include <asm/atomic.h>
47 #include "include/osd.h"
50 /* Data types */
53 struct osd_callback_struct {
54 struct work_struct work;
55 void (*callback)(void *);
56 void *data;
59 void* VirtualAllocExec(unsigned int size)
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
68 void* PageAlloc(unsigned int count)
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;
75 /* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
76 /* void *p; */
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; */
84 void 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);
88 __free_page(p);*/
91 void *MemMapIO(unsigned long phys, unsigned long size)
93 return (void*)GetVirtualAddress(phys); /* return ioremap_nocache(phys, size); */
96 void MemUnmapIO(void *virt)
98 /* iounmap(virt); */
101 static void TimerCallback(unsigned long data)
103 struct osd_timer *t = (struct osd_timer *) data;
105 t->callback(t->context);
108 struct osd_timer *TimerCreate(PFN_TIMER_CALLBACK pfnTimerCB, void* context)
110 struct osd_timer *t = kmalloc(sizeof(struct osd_timer), GFP_KERNEL);
111 if (!t)
113 return NULL;
116 t->callback = pfnTimerCB;
117 t->context = context;
119 init_timer(&t->timer);
120 t->timer.data = (unsigned long)t;
121 t->timer.function = TimerCallback;
123 return t;
126 void TimerStart(struct osd_timer *t, u32 expirationInUs)
128 t->timer.expires = jiffies + usecs_to_jiffies(expirationInUs);
129 add_timer(&t->timer);
132 int TimerStop(struct osd_timer *t)
134 return del_timer(&t->timer);
137 void TimerClose(struct osd_timer *t)
139 del_timer(&t->timer);
140 kfree(t);
143 struct osd_waitevent *WaitEventCreate(void)
145 struct osd_waitevent *wait = kmalloc(sizeof(struct osd_waitevent), GFP_KERNEL);
146 if (!wait)
148 return NULL;
151 wait->condition = 0;
152 init_waitqueue_head(&wait->event);
153 return wait;
156 void WaitEventSet(struct osd_waitevent *waitEvent)
158 waitEvent->condition = 1;
159 wake_up_interruptible(&waitEvent->event);
162 int WaitEventWait(struct osd_waitevent *waitEvent)
164 int ret=0;
166 ret = wait_event_interruptible(waitEvent->event,
167 waitEvent->condition);
168 waitEvent->condition = 0;
169 return ret;
172 int WaitEventWaitEx(struct osd_waitevent *waitEvent, u32 TimeoutInMs)
174 int ret=0;
176 ret = wait_event_interruptible_timeout(waitEvent->event,
177 waitEvent->condition,
178 msecs_to_jiffies(TimeoutInMs));
179 waitEvent->condition = 0;
180 return ret;
183 void* Physical2LogicalAddr(unsigned long PhysAddr)
185 void* logicalAddr = phys_to_virt(PhysAddr);
186 BUG_ON(!virt_addr_valid(logicalAddr));
187 return logicalAddr;
190 unsigned long Logical2PhysicalAddr(void * LogicalAddr)
192 BUG_ON(!virt_addr_valid(LogicalAddr));
193 return virt_to_phys(LogicalAddr);
197 unsigned long Virtual2Physical(void * VirtAddr)
199 unsigned long pfn = vmalloc_to_pfn(VirtAddr);
201 return pfn << PAGE_SHIFT;
204 static void osd_callback_work(struct work_struct *work)
206 struct osd_callback_struct *cb = container_of(work,
207 struct osd_callback_struct,
208 work);
209 (cb->callback)(cb->data);
211 kfree(cb);
214 int osd_schedule_callback(struct workqueue_struct *wq,
215 void (*func)(void *),
216 void *data)
218 struct osd_callback_struct *cb;
220 cb = kmalloc(sizeof(*cb), GFP_KERNEL);
221 if (!cb)
223 printk(KERN_ERR "unable to allocate memory in osd_schedule_callback");
224 return -1;
227 cb->callback = func;
228 cb->data = data;
229 INIT_WORK(&cb->work, osd_callback_work);
230 return queue_work(wq, &cb->work);