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/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>
43 #include <asm/bitops.h>
44 #include <asm/kmap_types.h>
45 #include <asm/atomic.h>
47 #include "include/osd.h"
53 struct osd_callback_struct
{
54 struct work_struct work
;
55 void (*callback
)(void *);
59 void* VirtualAllocExec(unsigned int size
)
62 return __vmalloc(size
, GFP_KERNEL
, PAGE_KERNEL_EXEC
);
64 return __vmalloc(size
, GFP_KERNEL
, __pgprot(__PAGE_KERNEL
& (~_PAGE_NX
)));
68 void* PageAlloc(unsigned int count
)
71 p
= (void *)__get_free_pages(GFP_KERNEL
, get_order(count
* PAGE_SIZE
));
72 if (p
) memset(p
, 0, count
* PAGE_SIZE
);
75 /* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
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); */
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);
92 void* PageMapVirtualAddress(unsigned long Pfn
)
94 return kmap_atomic(pfn_to_page(Pfn
), KM_IRQ0
);
97 void PageUnmapVirtualAddress(void* VirtAddr
)
99 kunmap_atomic(VirtAddr
, KM_IRQ0
);
102 void *MemMapIO(unsigned long phys
, unsigned long size
)
104 return (void*)GetVirtualAddress(phys
); /* return ioremap_nocache(phys, size); */
107 void MemUnmapIO(void *virt
)
112 static void TimerCallback(unsigned long data
)
114 struct osd_timer
*t
= (struct osd_timer
*) data
;
116 t
->callback(t
->context
);
119 struct osd_timer
*TimerCreate(PFN_TIMER_CALLBACK pfnTimerCB
, void* context
)
121 struct osd_timer
*t
= kmalloc(sizeof(struct osd_timer
), GFP_KERNEL
);
127 t
->callback
= pfnTimerCB
;
128 t
->context
= context
;
130 init_timer(&t
->timer
);
131 t
->timer
.data
= (unsigned long)t
;
132 t
->timer
.function
= TimerCallback
;
137 void TimerStart(struct osd_timer
*t
, u32 expirationInUs
)
139 t
->timer
.expires
= jiffies
+ usecs_to_jiffies(expirationInUs
);
140 add_timer(&t
->timer
);
143 int TimerStop(struct osd_timer
*t
)
145 return del_timer(&t
->timer
);
148 void TimerClose(struct osd_timer
*t
)
150 del_timer(&t
->timer
);
154 struct osd_waitevent
*WaitEventCreate(void)
156 struct osd_waitevent
*wait
= kmalloc(sizeof(struct osd_waitevent
), GFP_KERNEL
);
163 init_waitqueue_head(&wait
->event
);
167 void WaitEventSet(struct osd_waitevent
*waitEvent
)
169 waitEvent
->condition
= 1;
170 wake_up_interruptible(&waitEvent
->event
);
173 int WaitEventWait(struct osd_waitevent
*waitEvent
)
177 ret
= wait_event_interruptible(waitEvent
->event
,
178 waitEvent
->condition
);
179 waitEvent
->condition
= 0;
183 int WaitEventWaitEx(struct osd_waitevent
*waitEvent
, u32 TimeoutInMs
)
187 ret
= wait_event_interruptible_timeout(waitEvent
->event
,
188 waitEvent
->condition
,
189 msecs_to_jiffies(TimeoutInMs
));
190 waitEvent
->condition
= 0;
194 void* Physical2LogicalAddr(unsigned long PhysAddr
)
196 void* logicalAddr
= phys_to_virt(PhysAddr
);
197 BUG_ON(!virt_addr_valid(logicalAddr
));
201 unsigned long Logical2PhysicalAddr(void * LogicalAddr
)
203 BUG_ON(!virt_addr_valid(LogicalAddr
));
204 return virt_to_phys(LogicalAddr
);
208 unsigned long Virtual2Physical(void * VirtAddr
)
210 unsigned long pfn
= vmalloc_to_pfn(VirtAddr
);
212 return pfn
<< PAGE_SHIFT
;
215 static void osd_callback_work(struct work_struct
*work
)
217 struct osd_callback_struct
*cb
= container_of(work
,
218 struct osd_callback_struct
,
220 (cb
->callback
)(cb
->data
);
225 int osd_schedule_callback(struct workqueue_struct
*wq
,
226 void (*func
)(void *),
229 struct osd_callback_struct
*cb
;
231 cb
= kmalloc(sizeof(*cb
), GFP_KERNEL
);
234 printk(KERN_ERR
"unable to allocate memory in osd_schedule_callback");
240 INIT_WORK(&cb
->work
, osd_callback_work
);
241 return queue_work(wq
, &cb
->work
);