2009-07-13 23:02:34 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009, Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
|
|
* Place - Suite 330, Boston, MA 02111-1307 USA.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Haiyang Zhang <haiyangz@microsoft.com>
|
|
|
|
* Hank Janssen <hjanssen@microsoft.com>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/highmem.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/ioport.h>
|
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/interrupt.h>
|
2009-10-12 21:10:34 +00:00
|
|
|
#include <linux/sched.h>
|
2009-07-13 23:02:34 +00:00
|
|
|
#include <linux/wait.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/workqueue.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/time.h>
|
2009-08-19 23:19:23 +00:00
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/bitops.h>
|
2009-08-19 23:14:47 +00:00
|
|
|
#include "osd.h"
|
2009-07-13 23:02:34 +00:00
|
|
|
|
2009-07-29 21:00:09 +00:00
|
|
|
struct osd_callback_struct {
|
2009-07-13 23:02:34 +00:00
|
|
|
struct work_struct work;
|
2009-07-29 21:00:09 +00:00
|
|
|
void (*callback)(void *);
|
|
|
|
void *data;
|
|
|
|
};
|
2009-07-13 23:02:34 +00:00
|
|
|
|
2009-07-29 22:40:18 +00:00
|
|
|
void *osd_VirtualAllocExec(unsigned int size)
|
2009-07-13 23:02:34 +00:00
|
|
|
{
|
|
|
|
#ifdef __x86_64__
|
|
|
|
return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC);
|
|
|
|
#else
|
2009-08-19 23:19:23 +00:00
|
|
|
return __vmalloc(size, GFP_KERNEL,
|
|
|
|
__pgprot(__PAGE_KERNEL & (~_PAGE_NX)));
|
2009-07-13 23:02:34 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-07-29 22:40:18 +00:00
|
|
|
void *osd_PageAlloc(unsigned int count)
|
2009-07-13 23:02:34 +00:00
|
|
|
{
|
|
|
|
void *p;
|
2009-08-19 23:19:23 +00:00
|
|
|
|
2009-07-13 23:02:34 +00:00
|
|
|
p = (void *)__get_free_pages(GFP_KERNEL, get_order(count * PAGE_SIZE));
|
2009-08-19 23:19:23 +00:00
|
|
|
if (p)
|
|
|
|
memset(p, 0, count * PAGE_SIZE);
|
2009-07-13 23:02:34 +00:00
|
|
|
return p;
|
|
|
|
|
2009-07-27 20:47:24 +00:00
|
|
|
/* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
|
|
|
|
/* void *p; */
|
2009-07-13 23:02:34 +00:00
|
|
|
|
2009-07-27 20:47:24 +00:00
|
|
|
/* BUGBUG: We need to use kmap in case we are in HIMEM region */
|
|
|
|
/* p = page_address(page); */
|
|
|
|
/* if (p) memset(p, 0, PAGE_SIZE); */
|
|
|
|
/* return p; */
|
2009-07-13 23:02:34 +00:00
|
|
|
}
|
2009-07-29 22:42:27 +00:00
|
|
|
EXPORT_SYMBOL_GPL(osd_PageAlloc);
|
2009-07-13 23:02:34 +00:00
|
|
|
|
2009-08-19 23:19:23 +00:00
|
|
|
void osd_PageFree(void *page, unsigned int count)
|
2009-07-13 23:02:34 +00:00
|
|
|
{
|
|
|
|
free_pages((unsigned long)page, get_order(count * PAGE_SIZE));
|
|
|
|
/*struct page* p = virt_to_page(page);
|
|
|
|
__free_page(p);*/
|
|
|
|
}
|
2009-07-29 22:42:27 +00:00
|
|
|
EXPORT_SYMBOL_GPL(osd_PageFree);
|
2009-07-13 23:02:34 +00:00
|
|
|
|
2009-07-29 22:40:18 +00:00
|
|
|
struct osd_waitevent *osd_WaitEventCreate(void)
|
2009-07-13 23:02:34 +00:00
|
|
|
{
|
2009-08-19 23:19:23 +00:00
|
|
|
struct osd_waitevent *wait = kmalloc(sizeof(struct osd_waitevent),
|
|
|
|
GFP_KERNEL);
|
2009-07-13 23:02:34 +00:00
|
|
|
if (!wait)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
wait->condition = 0;
|
|
|
|
init_waitqueue_head(&wait->event);
|
|
|
|
return wait;
|
|
|
|
}
|
2009-07-29 22:42:27 +00:00
|
|
|
EXPORT_SYMBOL_GPL(osd_WaitEventCreate);
|
2009-07-13 23:02:34 +00:00
|
|
|
|
2009-07-29 22:40:18 +00:00
|
|
|
void osd_WaitEventSet(struct osd_waitevent *waitEvent)
|
2009-07-13 23:02:34 +00:00
|
|
|
{
|
|
|
|
waitEvent->condition = 1;
|
|
|
|
wake_up_interruptible(&waitEvent->event);
|
|
|
|
}
|
2009-07-29 22:42:27 +00:00
|
|
|
EXPORT_SYMBOL_GPL(osd_WaitEventSet);
|
2009-07-13 23:02:34 +00:00
|
|
|
|
2009-07-29 22:40:18 +00:00
|
|
|
int osd_WaitEventWait(struct osd_waitevent *waitEvent)
|
2009-07-13 23:02:34 +00:00
|
|
|
{
|
2009-08-19 23:19:23 +00:00
|
|
|
int ret = 0;
|
2009-07-13 23:02:34 +00:00
|
|
|
|
2009-07-28 17:46:24 +00:00
|
|
|
ret = wait_event_interruptible(waitEvent->event,
|
|
|
|
waitEvent->condition);
|
2009-07-13 23:02:34 +00:00
|
|
|
waitEvent->condition = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
2009-07-29 22:42:27 +00:00
|
|
|
EXPORT_SYMBOL_GPL(osd_WaitEventWait);
|
2009-07-13 23:02:34 +00:00
|
|
|
|
2009-07-29 22:40:18 +00:00
|
|
|
int osd_WaitEventWaitEx(struct osd_waitevent *waitEvent, u32 TimeoutInMs)
|
2009-07-13 23:02:34 +00:00
|
|
|
{
|
2009-08-19 23:19:23 +00:00
|
|
|
int ret = 0;
|
2009-07-13 23:02:34 +00:00
|
|
|
|
2009-07-28 17:46:24 +00:00
|
|
|
ret = wait_event_interruptible_timeout(waitEvent->event,
|
|
|
|
waitEvent->condition,
|
|
|
|
msecs_to_jiffies(TimeoutInMs));
|
2009-07-13 23:02:34 +00:00
|
|
|
waitEvent->condition = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
2009-07-29 22:42:27 +00:00
|
|
|
EXPORT_SYMBOL_GPL(osd_WaitEventWaitEx);
|
2009-07-13 23:02:34 +00:00
|
|
|
|
2009-07-29 21:00:09 +00:00
|
|
|
static void osd_callback_work(struct work_struct *work)
|
2009-07-13 23:02:34 +00:00
|
|
|
{
|
2009-07-29 21:00:09 +00:00
|
|
|
struct osd_callback_struct *cb = container_of(work,
|
2009-08-19 23:19:23 +00:00
|
|
|
struct osd_callback_struct,
|
|
|
|
work);
|
2009-07-29 21:00:09 +00:00
|
|
|
(cb->callback)(cb->data);
|
|
|
|
kfree(cb);
|
2009-07-13 23:02:34 +00:00
|
|
|
}
|
|
|
|
|
2009-07-29 21:00:09 +00:00
|
|
|
int osd_schedule_callback(struct workqueue_struct *wq,
|
|
|
|
void (*func)(void *),
|
|
|
|
void *data)
|
2009-07-13 23:02:34 +00:00
|
|
|
{
|
2009-07-29 21:00:09 +00:00
|
|
|
struct osd_callback_struct *cb;
|
2009-07-13 23:02:34 +00:00
|
|
|
|
2009-07-29 21:00:09 +00:00
|
|
|
cb = kmalloc(sizeof(*cb), GFP_KERNEL);
|
2009-08-19 23:19:23 +00:00
|
|
|
if (!cb) {
|
|
|
|
printk(KERN_ERR "unable to allocate memory in osd_schedule_callback\n");
|
2009-07-13 23:02:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-07-29 21:00:09 +00:00
|
|
|
cb->callback = func;
|
|
|
|
cb->data = data;
|
|
|
|
INIT_WORK(&cb->work, osd_callback_work);
|
|
|
|
return queue_work(wq, &cb->work);
|
2009-07-13 23:02:34 +00:00
|
|
|
}
|
|
|
|
|