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
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.
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>
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>
42 #include <linux/bitops.h>
43 #include <linux/slab.h>
46 void *osd_virtual_alloc_exec(unsigned int size
)
49 return __vmalloc(size
, GFP_KERNEL
, PAGE_KERNEL_EXEC
);
51 return __vmalloc(size
, GFP_KERNEL
,
52 __pgprot(__PAGE_KERNEL
& (~_PAGE_NX
)));
57 * osd_page_alloc() - Allocate pages
58 * @count: Total number of Kernel pages you want to allocate
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.
65 void *osd_page_alloc(unsigned int count
)
69 p
= (void *)__get_free_pages(GFP_KERNEL
, get_order(count
* PAGE_SIZE
));
71 memset(p
, 0, count
* PAGE_SIZE
);
74 /* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
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); */
82 EXPORT_SYMBOL_GPL(osd_page_alloc
);
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
89 * Frees the pages allocated by osd_page_alloc()
90 * Mainly used by Hyper-V drivers.
92 void osd_page_free(void *page
, unsigned int count
)
94 free_pages((unsigned long)page
, get_order(count
* PAGE_SIZE
));
95 /*struct page* p = virt_to_page(page);
98 EXPORT_SYMBOL_GPL(osd_page_free
);
101 * osd_waitevent_create() - Create the event queue
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.
108 * Returns pointer to &struct osd_waitevent
109 * Mainly used by Hyper-V drivers.
111 struct osd_waitevent
*osd_waitevent_create(void)
113 struct osd_waitevent
*wait
= kmalloc(sizeof(struct osd_waitevent
),
119 init_waitqueue_head(&wait
->event
);
122 EXPORT_SYMBOL_GPL(osd_waitevent_create
);
126 * osd_waitevent_set() - Wake up the process
127 * @wait_event: Structure to event to be woken up
129 * @wait_event is of type &struct osd_waitevent
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.
135 * Only used by Network and Storage Hyper-V drivers.
137 void osd_waitevent_set(struct osd_waitevent
*wait_event
)
139 wait_event
->condition
= 1;
140 wake_up_interruptible(&wait_event
->event
);
142 EXPORT_SYMBOL_GPL(osd_waitevent_set
);
145 * osd_waitevent_wait() - Wait for event till condition is true
146 * @wait_event: Structure to event to be put to sleep
148 * @wait_event is of type &struct osd_waitevent
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.
154 * Returns the status of 'wait_event_interruptible()' system call
156 * Mainly used by Hyper-V drivers.
158 int osd_waitevent_wait(struct osd_waitevent
*wait_event
)
162 ret
= wait_event_interruptible(wait_event
->event
,
163 wait_event
->condition
);
164 wait_event
->condition
= 0;
167 EXPORT_SYMBOL_GPL(osd_waitevent_wait
);
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
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.
180 * Returns the status of 'wait_event_interruptible_timeout()' system call
182 * Mainly used by Hyper-V drivers.
184 int osd_waitevent_waitex(struct osd_waitevent
*wait_event
, u32 timeout_in_ms
)
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;
194 EXPORT_SYMBOL_GPL(osd_waitevent_waitex
);