aboutsummaryrefslogtreecommitdiff
path: root/drivers/staging/hv/osd.c
blob: c178f180ab278ca90bf87b6edc001c3c8da3af06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 *
 * 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>
 *
 */

#define KERNEL_2_6_27

#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/config.h>
#include <linux/ioport.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <linux/delay.h>
#include <linux/time.h>

#include <asm/io.h>
#include <asm/bitops.h>
#include <asm/kmap_types.h>
#include <asm/atomic.h>

#include "include/osd.h"

//
// Data types
//
typedef struct _TIMER {
	struct timer_list timer;
	PFN_TIMER_CALLBACK callback;
	void* context;
}TIMER;


typedef struct _WAITEVENT {
	int	condition;
	wait_queue_head_t event;
} WAITEVENT;

typedef struct _SPINLOCK {
	spinlock_t		lock;
	unsigned long	flags;
} SPINLOCK;

typedef struct _WORKQUEUE {
	struct workqueue_struct *queue;
} WORKQUEUE;

typedef struct _WORKITEM {
	struct work_struct work;
	PFN_WORKITEM_CALLBACK callback;
	void* context;
} WORKITEM;


//
// Global
//

void LogMsg(const char *fmt, ...)
{
#ifdef KERNEL_2_6_5
	char buf[1024];
#endif
	va_list args;

	va_start(args, fmt);
#ifdef KERNEL_2_6_5
	vsnprintf(buf, 1024, fmt, args);
	va_end(args);
	printk(buf);
#else
	vprintk(fmt, args);
	va_end(args);
#endif
}

void BitSet(unsigned int* addr, int bit)
{
	set_bit(bit, (unsigned long*)addr);
}

int BitTest(unsigned int* addr, int bit)
{
	return test_bit(bit, (unsigned long*)addr);
}

void BitClear(unsigned int* addr, int bit)
{
	clear_bit(bit, (unsigned long*)addr);
}

int BitTestAndClear(unsigned int* addr, int bit)
{
	return test_and_clear_bit(bit, (unsigned long*)addr);
}

int BitTestAndSet(unsigned int* addr, int bit)
{
	return test_and_set_bit(bit, (unsigned long*)addr);
}


int InterlockedIncrement(int *val)
{
#ifdef KERNEL_2_6_5
	int i;
	local_irq_disable();
	i = atomic_read((atomic_t*)val);
	atomic_set((atomic_t*)val, i+1);
	local_irq_enable();
	return i+1;
#else
	return atomic_inc_return((atomic_t*)val);
#endif
}

int InterlockedDecrement(int *val)
{
#ifdef KERNEL_2_6_5
	int i;
	local_irq_disable();
	i = atomic_read((atomic_t*)val);
	atomic_set((atomic_t*)val, i-1);
	local_irq_enable();
	return i-1;
#else
	return atomic_dec_return((atomic_t*)val);
#endif
}

#ifndef atomic_cmpxchg
#define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new))
#endif
int InterlockedCompareExchange(int *val, int new, int curr)
{
	//return ((int)cmpxchg(((atomic_t*)val), curr, new));
	return atomic_cmpxchg((atomic_t*)val, curr, new);

}

void Sleep(unsigned long usecs)
{
	udelay(usecs);
}

void* VirtualAllocExec(unsigned int size)
{
#ifdef __x86_64__
	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC);
#else
	return __vmalloc(size, GFP_KERNEL, __pgprot(__PAGE_KERNEL & (~_PAGE_NX)));
#endif
}

void VirtualFree(void* VirtAddr)
{
	return vfree(VirtAddr);
}

void* PageAlloc(unsigned int count)
{
	void *p;
	p = (void *)__get_free_pages(GFP_KERNEL, get_order(count * PAGE_SIZE));
	if (p) memset(p, 0, count * PAGE_SIZE);
	return p;

	//struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO);
	//void *p;

	////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;
}

void PageFree(void* page, unsigned int count)
{
	free_pages((unsigned long)page, get_order(count * PAGE_SIZE));
	/*struct page* p = virt_to_page(page);
	__free_page(p);*/
}


void* PageMapVirtualAddress(unsigned long Pfn)
{
	return kmap_atomic(pfn_to_page(Pfn), KM_IRQ0);
}

void PageUnmapVirtualAddress(void* VirtAddr)
{
	kunmap_atomic(VirtAddr, KM_IRQ0);
}

void* MemAlloc(unsigned int size)
{
	return kmalloc(size, GFP_KERNEL);
}

void* MemAllocZeroed(unsigned int size)
{
	void *p = kmalloc(size, GFP_KERNEL);
	if (p) memset(p, 0, size);
	return p;
}

void* MemAllocAtomic(unsigned int size)
{
	return kmalloc(size, GFP_ATOMIC);
}

void MemFree(void* buf)
{
	kfree(buf);
}

void *MemMapIO(unsigned long phys, unsigned long size)
{
#if X2V_LINUX
#ifdef __x86_64__
	return (void*)(phys + 0xFFFF83000C000000);
#else // i386
	return (void*)(phys + 0xfb000000);
#endif
#else
	return (void*)GetVirtualAddress(phys); //return ioremap_nocache(phys, size);
#endif
}

void MemUnmapIO(void *virt)
{
	//iounmap(virt);
}

void MemoryFence()
{
	mb();
}

void TimerCallback(unsigned long data)
{
	TIMER* t = (TIMER*)data;

	t->callback(t->context);
}

HANDLE TimerCreate(PFN_TIMER_CALLBACK pfnTimerCB, void* context)
{
	TIMER* t = kmalloc(sizeof(TIMER), GFP_KERNEL);
	if (!t)
	{
		return NULL;
	}

	t->callback = pfnTimerCB;
	t->context = context;

	init_timer(&t->timer);
	t->timer.data = (unsigned long)t;
	t->timer.function = TimerCallback;

	return t;
}

void TimerStart(HANDLE hTimer, u32 expirationInUs)
{
	TIMER* t  = (TIMER* )hTimer;

	t->timer.expires = jiffies + usecs_to_jiffies(expirationInUs);
	add_timer(&t->timer);
}

int TimerStop(HANDLE hTimer)
{
	TIMER* t  = (TIMER* )hTimer;

	return del_timer(&t->timer);
}

void TimerClose(HANDLE hTimer)
{
	TIMER* t  = (TIMER* )hTimer;

	del_timer(&t->timer);
	kfree(t);
}

SIZE_T GetTickCount(void)
{
	return jiffies;
}

signed long long GetTimestamp(void)
{
	struct timeval t;

	do_gettimeofday(&t);

	return  timeval_to_ns(&t);
}

HANDLE WaitEventCreate(void)
{
	WAITEVENT* wait = kmalloc(sizeof(WAITEVENT), GFP_KERNEL);
	if (!wait)
	{
		return NULL;
	}

	wait->condition = 0;
	init_waitqueue_head(&wait->event);
	return wait;
}

void WaitEventClose(HANDLE hWait)
{
	WAITEVENT* waitEvent = (WAITEVENT* )hWait;
	kfree(waitEvent);
}

void WaitEventSet(HANDLE hWait)
{
	WAITEVENT* waitEvent = (WAITEVENT* )hWait;
	waitEvent->condition = 1;
	wake_up_interruptible(&waitEvent->event);
}

int WaitEventWait(HANDLE hWait)
{
	int ret=0;
	WAITEVENT* waitEvent = (WAITEVENT* )hWait;

	ret= wait_event_interruptible(waitEvent->event,
		waitEvent->condition);
	waitEvent->condition = 0;
	return ret;
}

int WaitEventWaitEx(HANDLE hWait, u32 TimeoutInMs)
{
	int ret=0;
	WAITEVENT* waitEvent = (WAITEVENT* )hWait;

	ret= wait_event_interruptible_timeout(waitEvent->event,
											waitEvent->condition,
											msecs_to_jiffies(TimeoutInMs));
	waitEvent->condition = 0;
	return ret;
}

HANDLE SpinlockCreate(void)
{
	SPINLOCK* spin = kmalloc(sizeof(SPINLOCK), GFP_KERNEL);
	if (!spin)
	{
		return NULL;
	}
	spin_lock_init(&spin->lock);

	return spin;
}

void SpinlockAcquire(HANDLE hSpin)
{
	SPINLOCK* spin = (SPINLOCK* )hSpin;

	spin_lock_irqsave(&spin->lock, spin->flags);
}

void SpinlockRelease(HANDLE hSpin)
{
	SPINLOCK* spin = (SPINLOCK* )hSpin;

	spin_unlock_irqrestore(&spin->lock, spin->flags);
}

void SpinlockClose(HANDLE hSpin)
{
	SPINLOCK* spin = (SPINLOCK* )hSpin;
	kfree(spin);
}

void* Physical2LogicalAddr(ULONG_PTR PhysAddr)
{
	void* logicalAddr = phys_to_virt(PhysAddr);
	BUG_ON(!virt_addr_valid(logicalAddr));
	return logicalAddr;
}

ULONG_PTR Logical2PhysicalAddr(void * LogicalAddr)
{
	BUG_ON(!virt_addr_valid(LogicalAddr));
	return virt_to_phys(LogicalAddr);
}


ULONG_PTR Virtual2Physical(void * VirtAddr)
{
	ULONG_PTR pfn = vmalloc_to_pfn(VirtAddr);

	return pfn << PAGE_SHIFT;
}

#ifdef KERNEL_2_6_27
void WorkItemCallback(struct work_struct *work)
#else
void WorkItemCallback(void* work)
#endif
{
	WORKITEM* w = (WORKITEM*)work;

	w->callback(w->context);

	kfree(w);
}

HANDLE WorkQueueCreate(char* name)
{
	WORKQUEUE *wq = kmalloc(sizeof(WORKQUEUE), GFP_KERNEL);
	if (!wq)
	{
		return NULL;
	}
	wq->queue = create_workqueue(name);

	return wq;
}

void WorkQueueClose(HANDLE hWorkQueue)
{
	WORKQUEUE *wq = (WORKQUEUE *)hWorkQueue;

	destroy_workqueue(wq->queue);

	return;
}

int WorkQueueQueueWorkItem(HANDLE hWorkQueue, PFN_WORKITEM_CALLBACK workItem, void* context)
{
	WORKQUEUE *wq = (WORKQUEUE *)hWorkQueue;

	WORKITEM* w = kmalloc(sizeof(WORKITEM), GFP_ATOMIC);
	if (!w)
	{
		return -1;
	}

	w->callback = workItem,
	w->context = context;
#ifdef KERNEL_2_6_27
	INIT_WORK(&w->work, WorkItemCallback);
#else
	INIT_WORK(&w->work, WorkItemCallback, w);
#endif
	return queue_work(wq->queue, &w->work);
}

void QueueWorkItem(PFN_WORKITEM_CALLBACK workItem, void* context)
{
	WORKITEM* w = kmalloc(sizeof(WORKITEM), GFP_ATOMIC);
	if (!w)
	{
		return;
	}

	w->callback = workItem,
	w->context = context;
#ifdef KERNEL_2_6_27
	INIT_WORK(&w->work, WorkItemCallback);
#else
	INIT_WORK(&w->work, WorkItemCallback, w);
#endif
	schedule_work(&w->work);
}