]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/hv/osd.c
Merge tag 'v2.6.38' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[mv-sheeva.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 <linux/slab.h>
44 #include "osd.h"
45
46 void *osd_virtual_alloc_exec(unsigned int size)
47 {
48 #ifdef __x86_64__
49         return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC);
50 #else
51         return __vmalloc(size, GFP_KERNEL,
52                          __pgprot(__PAGE_KERNEL & (~_PAGE_NX)));
53 #endif
54 }
55
56 /**
57  * osd_page_alloc() - Allocate pages
58  * @count:      Total number of Kernel pages you want to allocate
59  *
60  * Tries to allocate @count number of consecutive free kernel pages.
61  * And if successful, it will set the pages to 0 before returning.
62  * If successfull it will return pointer to the @count pages.
63  * Mainly used by Hyper-V drivers.
64  */
65 void *osd_page_alloc(unsigned int count)
66 {
67         void *p;
68
69         p = (void *)__get_free_pages(GFP_KERNEL, get_order(count * PAGE_SIZE));
70         if (p)
71                 memset(p, 0, count * PAGE_SIZE);
72         return p;
73
74         /* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
75         /* void *p; */
76
77         /* BUGBUG: We need to use kmap in case we are in HIMEM region */
78         /* p = page_address(page); */
79         /* if (p) memset(p, 0, PAGE_SIZE); */
80         /* return p; */
81 }
82 EXPORT_SYMBOL_GPL(osd_page_alloc);
83
84 /**
85  * osd_page_free() - Free pages
86  * @page:       Pointer to the first page to be freed
87  * @count:      Total number of Kernel pages you free
88  *
89  * Frees the pages allocated by osd_page_alloc()
90  * Mainly used by Hyper-V drivers.
91  */
92 void osd_page_free(void *page, unsigned int count)
93 {
94         free_pages((unsigned long)page, get_order(count * PAGE_SIZE));
95         /*struct page* p = virt_to_page(page);
96         __free_page(p);*/
97 }
98 EXPORT_SYMBOL_GPL(osd_page_free);
99
100 /**
101  * osd_waitevent_create() - Create the event queue
102  *
103  * Allocates memory for a &struct osd_waitevent. And then calls
104  * init_waitqueue_head to set up the wait queue for the event.
105  * This structure is usually part of a another structure that contains
106  * the actual Hyper-V device driver structure.
107  *
108  * Returns pointer to &struct osd_waitevent
109  * Mainly used by Hyper-V drivers.
110  */
111 struct osd_waitevent *osd_waitevent_create(void)
112 {
113         struct osd_waitevent *wait = kmalloc(sizeof(struct osd_waitevent),
114                                              GFP_KERNEL);
115         if (!wait)
116                 return NULL;
117
118         wait->condition = 0;
119         init_waitqueue_head(&wait->event);
120         return wait;
121 }
122 EXPORT_SYMBOL_GPL(osd_waitevent_create);
123
124
125 /**
126  * osd_waitevent_set() - Wake up the process
127  * @wait_event: Structure to event to be woken up
128  *
129  * @wait_event is of type &struct osd_waitevent
130  *
131  * Wake up the sleeping process so it can do some work.
132  * And set condition indicator in &struct osd_waitevent to indicate
133  * the process is in a woken state.
134  *
135  * Only used by Network and Storage Hyper-V drivers.
136  */
137 void osd_waitevent_set(struct osd_waitevent *wait_event)
138 {
139         wait_event->condition = 1;
140         wake_up_interruptible(&wait_event->event);
141 }
142 EXPORT_SYMBOL_GPL(osd_waitevent_set);
143
144 /**
145  * osd_waitevent_wait() - Wait for event till condition is true
146  * @wait_event: Structure to event to be put to sleep
147  *
148  * @wait_event is of type &struct osd_waitevent
149  *
150  * Set up the process to sleep until waitEvent->condition get true.
151  * And set condition indicator in &struct osd_waitevent to indicate
152  * the process is in a sleeping state.
153  *
154  * Returns the status of 'wait_event_interruptible()' system call
155  *
156  * Mainly used by Hyper-V drivers.
157  */
158 int osd_waitevent_wait(struct osd_waitevent *wait_event)
159 {
160         int ret = 0;
161
162         ret = wait_event_interruptible(wait_event->event,
163                                        wait_event->condition);
164         wait_event->condition = 0;
165         return ret;
166 }
167 EXPORT_SYMBOL_GPL(osd_waitevent_wait);
168
169 /**
170  * osd_waitevent_waitex() - Wait for event or timeout for process wakeup
171  * @wait_event: Structure to event to be put to sleep
172  * @timeout_in_ms:       Total number of Milliseconds to wait before waking up
173  *
174  * @wait_event is of type &struct osd_waitevent
175  * Set up the process to sleep until @waitEvent->condition get true or
176  * @timeout_in_ms (Time out in Milliseconds) has been reached.
177  * And set condition indicator in &struct osd_waitevent to indicate
178  * the process is in a sleeping state.
179  *
180  * Returns the status of 'wait_event_interruptible_timeout()' system call
181  *
182  * Mainly used by Hyper-V drivers.
183  */
184 int osd_waitevent_waitex(struct osd_waitevent *wait_event, u32 timeout_in_ms)
185 {
186         int ret = 0;
187
188         ret = wait_event_interruptible_timeout(wait_event->event,
189                                                wait_event->condition,
190                                                msecs_to_jiffies(timeout_in_ms));
191         wait_event->condition = 0;
192         return ret;
193 }
194 EXPORT_SYMBOL_GPL(osd_waitevent_waitex);