]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/hv/osd.c
3a4793a0fd050996d83c2de8e07d5e1183dfd4c4
[karo-tx-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/sched.h>
34 #include <linux/wait.h>
35 #include <linux/spinlock.h>
36 #include <linux/workqueue.h>
37 #include <linux/kernel.h>
38 #include <linux/jiffies.h>
39 #include <linux/delay.h>
40 #include <linux/time.h>
41 #include <linux/io.h>
42 #include <linux/bitops.h>
43 #include "osd.h"
44
45 struct osd_callback_struct {
46         struct work_struct work;
47         void (*callback)(void *);
48         void *data;
49 };
50
51 void *osd_VirtualAllocExec(unsigned int size)
52 {
53 #ifdef __x86_64__
54         return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC);
55 #else
56         return __vmalloc(size, GFP_KERNEL,
57                          __pgprot(__PAGE_KERNEL & (~_PAGE_NX)));
58 #endif
59 }
60
61 void *osd_PageAlloc(unsigned int count)
62 {
63         void *p;
64
65         p = (void *)__get_free_pages(GFP_KERNEL, get_order(count * PAGE_SIZE));
66         if (p)
67                 memset(p, 0, count * PAGE_SIZE);
68         return p;
69
70         /* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
71         /* void *p; */
72
73         /* BUGBUG: We need to use kmap in case we are in HIMEM region */
74         /* p = page_address(page); */
75         /* if (p) memset(p, 0, PAGE_SIZE); */
76         /* return p; */
77 }
78 EXPORT_SYMBOL_GPL(osd_PageAlloc);
79
80 void osd_PageFree(void *page, unsigned int count)
81 {
82         free_pages((unsigned long)page, get_order(count * PAGE_SIZE));
83         /*struct page* p = virt_to_page(page);
84         __free_page(p);*/
85 }
86 EXPORT_SYMBOL_GPL(osd_PageFree);
87
88 struct osd_waitevent *osd_WaitEventCreate(void)
89 {
90         struct osd_waitevent *wait = kmalloc(sizeof(struct osd_waitevent),
91                                              GFP_KERNEL);
92         if (!wait)
93                 return NULL;
94
95         wait->condition = 0;
96         init_waitqueue_head(&wait->event);
97         return wait;
98 }
99 EXPORT_SYMBOL_GPL(osd_WaitEventCreate);
100
101 void osd_WaitEventSet(struct osd_waitevent *waitEvent)
102 {
103         waitEvent->condition = 1;
104         wake_up_interruptible(&waitEvent->event);
105 }
106 EXPORT_SYMBOL_GPL(osd_WaitEventSet);
107
108 int osd_WaitEventWait(struct osd_waitevent *waitEvent)
109 {
110         int ret = 0;
111
112         ret = wait_event_interruptible(waitEvent->event,
113                                        waitEvent->condition);
114         waitEvent->condition = 0;
115         return ret;
116 }
117 EXPORT_SYMBOL_GPL(osd_WaitEventWait);
118
119 int osd_WaitEventWaitEx(struct osd_waitevent *waitEvent, u32 TimeoutInMs)
120 {
121         int ret = 0;
122
123         ret = wait_event_interruptible_timeout(waitEvent->event,
124                                                waitEvent->condition,
125                                                msecs_to_jiffies(TimeoutInMs));
126         waitEvent->condition = 0;
127         return ret;
128 }
129 EXPORT_SYMBOL_GPL(osd_WaitEventWaitEx);
130
131 static void osd_callback_work(struct work_struct *work)
132 {
133         struct osd_callback_struct *cb = container_of(work,
134                                                 struct osd_callback_struct,
135                                                 work);
136         (cb->callback)(cb->data);
137         kfree(cb);
138 }
139
140 int osd_schedule_callback(struct workqueue_struct *wq,
141                           void (*func)(void *),
142                           void *data)
143 {
144         struct osd_callback_struct *cb;
145
146         cb = kmalloc(sizeof(*cb), GFP_KERNEL);
147         if (!cb) {
148                 printk(KERN_ERR "unable to allocate memory in osd_schedule_callback\n");
149                 return -1;
150         }
151
152         cb->callback = func;
153         cb->data = data;
154         INIT_WORK(&cb->work, osd_callback_work);
155         return queue_work(wq, &cb->work);
156 }
157