summaryrefslogtreecommitdiffstats
path: root/driver/kmem.c
blob: b3232720c8cce803e2677c0c0075a050664ea0f1 (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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/**
 *
 * @file kmem.c
 * @brief This file contains all functions dealing with kernel memory.
 * @author Guillermo Marcus
 * @date 2009-04-05
 *
 */
#include <linux/version.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/cdev.h>
#include <linux/wait.h>
#include <linux/mm.h>
#include <linux/pagemap.h>

#include "config.h"			/* compile-time configuration */
#include "compat.h"			/* compatibility definitions for older linux */
#include "pciDriver.h"			/* external interface for the driver */
#include "common.h"			/* internal definitions for all parts */
#include "kmem.h"			/* prototypes for kernel memory */
#include "sysfs.h"			/* prototypes for sysfs */

/* VM_RESERVED is removed in 3.7-rc1 */
#ifndef VM_RESERVED
# define  VM_RESERVED   (VM_DONTEXPAND | VM_DONTDUMP)
#endif

/**
 *
 * Allocates new kernel memory including the corresponding management structure, makes
 * it available via sysfs if possible.
 *
 */
int pcidriver_kmem_alloc(pcidriver_privdata_t *privdata, kmem_handle_t *kmem_handle)
{
	int flags;
	pcidriver_kmem_entry_t *kmem_entry;
	void *retptr;

	if (kmem_handle->flags&KMEM_FLAG_REUSE) {
	    kmem_entry = pcidriver_kmem_find_entry_use(privdata, kmem_handle->use, kmem_handle->item);
	    if (kmem_entry) {
		unsigned long flags = kmem_handle->flags;
		
		if (flags&KMEM_FLAG_TRY) {
		    kmem_handle->type = kmem_entry->type;
		    kmem_handle->size = kmem_entry->size;
		    kmem_handle->align = kmem_entry->align;
		} else {
		    if (kmem_handle->type != kmem_entry->type) {
		    	mod_info("Invalid type of reusable kmem_entry, currently: %lu, but requested: %lu\n", kmem_entry->type, kmem_handle->type);
			return -EINVAL;
		    }

		    if ((kmem_handle->type&PCILIB_KMEM_TYPE_MASK) == PCILIB_KMEM_TYPE_PAGE) {
			    kmem_handle->size = kmem_entry->size;
		    } else if (kmem_handle->size != kmem_entry->size) {
			mod_info("Invalid size of reusable kmem_entry, currently: %lu, but requested: %lu\n", kmem_entry->size, kmem_handle->size);
			return -EINVAL;
		    }
		    
		    if (kmem_handle->align != kmem_entry->align) {
			mod_info("Invalid alignment of reusable kmem_entry, currently: %lu, but requested: %lu\n", kmem_entry->align, kmem_handle->align);
			return -EINVAL;
		    }

		    if (((kmem_entry->mode&KMEM_MODE_EXCLUSIVE)?1:0) != ((flags&KMEM_FLAG_EXCLUSIVE)?1:0)) {
			mod_info("Invalid mode of reusable kmem_entry\n");
			return -EINVAL;
		    }
		}
		

		if ((kmem_entry->mode&KMEM_MODE_COUNT)==KMEM_MODE_COUNT) {
			mod_info("Reuse counter of kmem_entry is overflown");
			return -EBUSY;
		}
		
		
		kmem_handle->handle_id = kmem_entry->id;
		kmem_handle->pa = (unsigned long)(kmem_entry->dma_handle);

		kmem_handle->flags = KMEM_FLAG_REUSED;
		if (kmem_entry->refs&KMEM_REF_HW) kmem_handle->flags |= KMEM_FLAG_REUSED_HW;
		if (kmem_entry->mode&KMEM_MODE_PERSISTENT) kmem_handle->flags |= KMEM_FLAG_REUSED_PERSISTENT;

		kmem_entry->mode += 1;
		if (flags&KMEM_FLAG_HW) {
		    if ((kmem_entry->refs&KMEM_REF_HW)==0)
			pcidriver_module_get(privdata);
			
		    kmem_entry->refs |= KMEM_REF_HW;
		}
		if (flags&KMEM_FLAG_PERSISTENT) kmem_entry->mode |= KMEM_MODE_PERSISTENT;

		privdata->kmem_cur_id = kmem_entry->id;
		
		return 0;
	    }
	    
	    if (kmem_handle->flags&KMEM_FLAG_TRY) return -ENOENT;
	}

	/* First, allocate zeroed memory for the kmem_entry */
	if ((kmem_entry = kcalloc(1, sizeof(pcidriver_kmem_entry_t), GFP_KERNEL)) == NULL)
		goto kmem_alloc_entry_fail;

	/* Initialize the kmem_entry */
	kmem_entry->id = atomic_inc_return(&privdata->kmem_count) - 1;
	privdata->kmem_cur_id = kmem_entry->id;
	kmem_handle->handle_id = kmem_entry->id;

	kmem_entry->use = kmem_handle->use;
	kmem_entry->item = kmem_handle->item;
	kmem_entry->type = kmem_handle->type;
	kmem_entry->align = kmem_handle->align;
	kmem_entry->direction = PCI_DMA_NONE;

	/* Initialize sysfs if possible */
	if (pcidriver_sysfs_initialize_kmem(privdata, kmem_entry->id, &(kmem_entry->sysfs_attr)) != 0)
		goto kmem_alloc_mem_fail;

	/* ...and allocate the DMA memory */
	/* note this is a memory pair, referencing the same area: the cpu address (cpua)
	 * and the PCI bus address (pa). The CPU and PCI addresses may not be the same.
	 * The CPU sees only CPU addresses, while the device sees only PCI addresses.
	 * CPU address is used for the mmap (internal to the driver), and
	 * PCI address is the address passed to the DMA Controller in the device.
	 */
	switch (kmem_entry->type&PCILIB_KMEM_TYPE_MASK) {
	 case PCILIB_KMEM_TYPE_CONSISTENT:
	    retptr = pci_alloc_consistent( privdata->pdev, kmem_handle->size, &(kmem_entry->dma_handle) );
	    break;
	 case PCILIB_KMEM_TYPE_REGION:
	    retptr = ioremap(kmem_handle->pa,  kmem_handle->size);
	    kmem_entry->dma_handle = kmem_handle->pa;
	    if (kmem_entry->type == PCILIB_KMEM_TYPE_REGION_S2C) {
		kmem_entry->direction = PCI_DMA_TODEVICE;
	    } else if (kmem_entry->type == PCILIB_KMEM_TYPE_REGION_C2S) {
		kmem_entry->direction = PCI_DMA_FROMDEVICE;
	    }
	    break;
	 case PCILIB_KMEM_TYPE_PAGE:
	    flags = GFP_KERNEL;

	    if (kmem_handle->size == 0)
		kmem_handle->size = PAGE_SIZE;
	    else if (kmem_handle->size%PAGE_SIZE)
		goto kmem_alloc_mem_fail;
	
	    retptr = (void*)__get_free_pages(flags, get_order(kmem_handle->size));
	    kmem_entry->dma_handle = 0;
	    
	    if (retptr) {
	        if (kmem_entry->type == PCILIB_KMEM_TYPE_DMA_S2C_PAGE) {
		    kmem_entry->direction = PCI_DMA_TODEVICE;
    		    kmem_entry->dma_handle = pci_map_single(privdata->pdev, retptr, kmem_handle->size, PCI_DMA_TODEVICE);
		    if (pci_dma_mapping_error(privdata->pdev, kmem_entry->dma_handle)) {
			free_pages((unsigned long)retptr, get_order(kmem_handle->size));
			goto kmem_alloc_mem_fail;
		    }
		} else if (kmem_entry->type == PCILIB_KMEM_TYPE_DMA_C2S_PAGE) {
		    kmem_entry->direction = PCI_DMA_FROMDEVICE;
    		    kmem_entry->dma_handle = pci_map_single(privdata->pdev, retptr, kmem_handle->size, PCI_DMA_FROMDEVICE);
		    if (pci_dma_mapping_error(privdata->pdev, kmem_entry->dma_handle)) {
			free_pages((unsigned long)retptr, get_order(kmem_handle->size));
			goto kmem_alloc_mem_fail;
		    
		    }
		}
	    }
	    
	    break;
	 default:
	    goto kmem_alloc_mem_fail;
	}
	
	
	if (retptr == NULL)
		goto kmem_alloc_mem_fail;

	kmem_entry->size = kmem_handle->size;
	kmem_entry->cpua = (unsigned long)retptr;
	kmem_handle->pa = (unsigned long)(kmem_entry->dma_handle);

	kmem_entry->mode = 1;
	if (kmem_handle->flags&KMEM_FLAG_REUSE) {
	    kmem_entry->mode |= KMEM_MODE_REUSABLE;
	    if (kmem_handle->flags&KMEM_FLAG_EXCLUSIVE) kmem_entry->mode |= KMEM_MODE_EXCLUSIVE;
	    if (kmem_handle->flags&KMEM_FLAG_PERSISTENT) kmem_entry->mode |= KMEM_MODE_PERSISTENT;
	}
	
	kmem_entry->refs = 0;
	if (kmem_handle->flags&KMEM_FLAG_HW) {
	    pcidriver_module_get(privdata);

	    kmem_entry->refs |= KMEM_REF_HW;
	}

        kmem_handle->flags = 0;
	
	set_pages_reserved_compat(kmem_entry->cpua, kmem_entry->size);

	/* Add the kmem_entry to the list of the device */
	spin_lock( &(privdata->kmemlist_lock) );
	list_add_tail( &(kmem_entry->list), &(privdata->kmem_list) );
	spin_unlock( &(privdata->kmemlist_lock) );

	return 0;

kmem_alloc_mem_fail:
		kfree(kmem_entry);
kmem_alloc_entry_fail:
		return -ENOMEM;
}

static int pcidriver_kmem_free_check(pcidriver_privdata_t *privdata, kmem_handle_t *kmem_handle, pcidriver_kmem_entry_t *kmem_entry) {
	if ((kmem_handle->flags & KMEM_FLAG_FORCE) == 0) {
	    if (kmem_entry->mode&KMEM_MODE_COUNT)
		kmem_entry->mode -= 1;

	    if (kmem_handle->flags&KMEM_FLAG_HW) {
		if (kmem_entry->refs&KMEM_REF_HW) 
		    pcidriver_module_put(privdata);

		kmem_entry->refs &= ~KMEM_REF_HW;
	    }
	
	    if (kmem_handle->flags&KMEM_FLAG_PERSISTENT)
		kmem_entry->mode &= ~KMEM_MODE_PERSISTENT;
	
	    if (kmem_handle->flags&KMEM_FLAG_REUSE) 
		return 0;

	    if (kmem_entry->refs) {
		kmem_entry->mode += 1;
		mod_info("can't free referenced kmem_entry\n");
		return -EBUSY;
	    }
	
	    if (kmem_entry->mode & KMEM_MODE_PERSISTENT) {
		kmem_entry->mode += 1;
		mod_info("can't free persistent kmem_entry\n");
		return -EBUSY;
	    }

	    if (((kmem_entry->mode&KMEM_MODE_EXCLUSIVE)==0)&&(kmem_entry->mode&KMEM_MODE_COUNT)&&((kmem_handle->flags&KMEM_FLAG_EXCLUSIVE)==0)) 
		return 0;
	} else {
	    if (kmem_entry->refs&KMEM_REF_HW)
		    pcidriver_module_put(privdata);
		
	    while (!atomic_add_negative(-1, &(privdata->refs))) pcidriver_module_put(privdata);
	    atomic_inc(&(privdata->refs));
		
	}
	
	return 1;
}

static int pcidriver_kmem_free_use(pcidriver_privdata_t *privdata, kmem_handle_t *kmem_handle)
{
	int err;
	int failed = 0;
	struct list_head *ptr, *next;
	pcidriver_kmem_entry_t *kmem_entry;

	/* iterate safely over the entries and delete them */
	list_for_each_safe(ptr, next, &(privdata->kmem_list)) {
		kmem_entry = list_entry(ptr, pcidriver_kmem_entry_t, list);
		if (kmem_entry->use == kmem_handle->use) {
		    err = pcidriver_kmem_free_check(privdata, kmem_handle, kmem_entry);
		    if (err > 0)
			pcidriver_kmem_free_entry(privdata, kmem_entry); 		/* spin lock inside! */
		    else
			failed = 1;
		}
	}
	
	if (failed) {
		mod_info("Some kmem_entries for use %lx are still referenced\n", kmem_handle->use);
		return -EBUSY;
	}	

	return 0;
}

/**
 *
 * Called via sysfs, frees kernel memory and the corresponding management structure
 *
 */
int pcidriver_kmem_free( pcidriver_privdata_t *privdata, kmem_handle_t *kmem_handle )
{
	int err;
	pcidriver_kmem_entry_t *kmem_entry;

	if (kmem_handle->flags&KMEM_FLAG_MASS) {
	    kmem_handle->flags &= ~KMEM_FLAG_MASS;
	    return pcidriver_kmem_free_use(privdata, kmem_handle);
	}
	
	/* Find the associated kmem_entry for this buffer */
	if ((kmem_entry = pcidriver_kmem_find_entry(privdata, kmem_handle)) == NULL)
		return -EINVAL;					/* kmem_handle is not valid */

	err = pcidriver_kmem_free_check(privdata, kmem_handle, kmem_entry);
	
	if (err > 0)
		return pcidriver_kmem_free_entry(privdata, kmem_entry);
	
	return err;
}

/**
 *
 * Called when cleaning up, frees all kernel memory and their corresponding management structure
 *
 */
int pcidriver_kmem_free_all(pcidriver_privdata_t *privdata)
{
//	int failed = 0;
	struct list_head *ptr, *next;
	pcidriver_kmem_entry_t *kmem_entry;

	/* iterate safely over the entries and delete them */
	list_for_each_safe(ptr, next, &(privdata->kmem_list)) {
		kmem_entry = list_entry(ptr, pcidriver_kmem_entry_t, list);
		/*if (kmem_entry->refs)
			failed = 1;
		else*/
			pcidriver_kmem_free_entry(privdata, kmem_entry); 		/* spin lock inside! */
	}
/*	
	if (failed) {
		mod_info("Some kmem_entries are still referenced\n");
		return -EBUSY;
	}	
*/
	return 0;
}


/**
 *
 * Synchronize memory to/from the device (or in both directions).
 *
 */
int pcidriver_kmem_sync_entry( pcidriver_privdata_t *privdata, pcidriver_kmem_entry_t *kmem_entry, int direction)
{
	if (kmem_entry->direction == PCI_DMA_NONE)
		return -EINVAL;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
	switch (direction) {
		case PCILIB_KMEM_SYNC_TODEVICE:
			pci_dma_sync_single_for_device( privdata->pdev, kmem_entry->dma_handle, kmem_entry->size, kmem_entry->direction );
			break;
		case PCILIB_KMEM_SYNC_FROMDEVICE:
			pci_dma_sync_single_for_cpu( privdata->pdev, kmem_entry->dma_handle, kmem_entry->size, kmem_entry->direction );
			break;
		case PCILIB_KMEM_SYNC_BIDIRECTIONAL:
			pci_dma_sync_single_for_device( privdata->pdev, kmem_entry->dma_handle, kmem_entry->size, kmem_entry->direction );
			pci_dma_sync_single_for_cpu( privdata->pdev, kmem_entry->dma_handle, kmem_entry->size, kmem_entry->direction );
			break;
		default:
			return -EINVAL;				/* wrong direction parameter */
	}
#else
	switch (direction) {
		case PCILIB_KMEM_SYNC_TODEVICE:
			pci_dma_sync_single( privdata->pdev, kmem_entry->dma_handle, kmem_entry->size, kmem_entry->direction );
			break;
		case PCILIB_KMEM_SYNC_FROMDEVICE:
			pci_dma_sync_single( privdata->pdev, kmem_entry->dma_handle, kmem_entry->size, kmem_entry->direction );
			break;
		case PCILIB_KMEM_SYNC_BIDIRECTIONAL:
			pci_dma_sync_single( privdata->pdev, kmem_entry->dma_handle, kmem_entry->size, kmem_entry->direction );
			break;
		default:
			return -EINVAL;				/* wrong direction parameter */
	}
#endif

	return 0;	/* success */
}

/**
 *
 * Synchronize memory to/from the device (or in both directions).
 *
 */
int pcidriver_kmem_sync( pcidriver_privdata_t *privdata, kmem_sync_t *kmem_sync )
{
	pcidriver_kmem_entry_t *kmem_entry;

	/* Find the associated kmem_entry for this buffer */
	if ((kmem_entry = pcidriver_kmem_find_entry(privdata, &(kmem_sync->handle))) == NULL)
		return -EINVAL;					/* kmem_handle is not valid */

	return pcidriver_kmem_sync_entry(privdata, kmem_entry, kmem_sync->dir);
}

/**
 *
 * Free the given kmem_entry and its memory.
 *
 */
int pcidriver_kmem_free_entry(pcidriver_privdata_t *privdata, pcidriver_kmem_entry_t *kmem_entry)
{
	pcidriver_sysfs_remove(privdata, &(kmem_entry->sysfs_attr));

	/* Go over the pages of the kmem buffer, and mark them as not reserved */
#if 0
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
	/*
	 * This code is DISABLED.
	 * Apparently, it is not needed to unreserve them. Doing so here
	 * hangs the machine. Why?
	 *
	 * Uhm.. see links:
	 *
	 * http://lwn.net/Articles/161204/
	 * http://lists.openfabrics.org/pipermail/general/2007-March/034101.html
	 *
	 * I insist, this should be enabled, but doing so hangs the machine.
	 * Literature supports the point, and there is even a similar problem (see link)
	 * But this is not the case. It seems right to me. but obviously is not.
	 *
	 * Anyway, this goes away in kernel >=2.6.15.
	 */
	unsigned long start = __pa(kmem_entry->cpua) >> PAGE_SHIFT;
	unsigned long end = __pa(kmem_entry->cpua + kmem_entry->size) >> PAGE_SHIFT;
	unsigned long i;
	for(i=start;i<end;i++) {
		struct page *kpage = pfn_to_page(i);
		ClearPageReserved(kpage);
	}
#endif
#endif

	/* Release DMA memory */
	switch (kmem_entry->type&PCILIB_KMEM_TYPE_MASK) {
	 case PCILIB_KMEM_TYPE_CONSISTENT:
	    pci_free_consistent( privdata->pdev, kmem_entry->size, (void *)(kmem_entry->cpua), kmem_entry->dma_handle );
	    break;
	 case PCILIB_KMEM_TYPE_REGION:
	    iounmap((void *)(kmem_entry->cpua));
	    break;
	 case PCILIB_KMEM_TYPE_PAGE:
	    if (kmem_entry->dma_handle) {
		if (kmem_entry->type == PCILIB_KMEM_TYPE_DMA_S2C_PAGE) {
		    pci_unmap_single(privdata->pdev, kmem_entry->dma_handle, kmem_entry->size, PCI_DMA_TODEVICE);
		} else if (kmem_entry->type == PCILIB_KMEM_TYPE_DMA_C2S_PAGE) {
		    pci_unmap_single(privdata->pdev, kmem_entry->dma_handle, kmem_entry->size, PCI_DMA_FROMDEVICE);
		}
	    }
	    free_pages((unsigned long)kmem_entry->cpua, get_order(kmem_entry->size));
	    break;
	}


	/* Remove the kmem list entry */
	spin_lock( &(privdata->kmemlist_lock) );
	list_del( &(kmem_entry->list) );
	spin_unlock( &(privdata->kmemlist_lock) );

	/* Release kmem_entry memory */
	kfree(kmem_entry);

	return 0;
}

/**
 *
 * Find the corresponding kmem_entry for the given kmem_handle.
 *
 */
pcidriver_kmem_entry_t *pcidriver_kmem_find_entry(pcidriver_privdata_t *privdata, kmem_handle_t *kmem_handle)
{
	struct list_head *ptr;
	pcidriver_kmem_entry_t *entry, *result = NULL;

	/* should I implement it better using the handle_id? */

	spin_lock(&(privdata->kmemlist_lock));
	list_for_each(ptr, &(privdata->kmem_list)) {
		entry = list_entry(ptr, pcidriver_kmem_entry_t, list);

		if (entry->id == kmem_handle->handle_id) {
			result = entry;
			break;
		}
	}

	spin_unlock(&(privdata->kmemlist_lock));
	return result;
}

/**
 *
 * find the corresponding kmem_entry for the given id.
 *
 */
pcidriver_kmem_entry_t *pcidriver_kmem_find_entry_id(pcidriver_privdata_t *privdata, int id)
{
	struct list_head *ptr;
	pcidriver_kmem_entry_t *entry, *result = NULL;

	spin_lock(&(privdata->kmemlist_lock));
	list_for_each(ptr, &(privdata->kmem_list)) {
		entry = list_entry(ptr, pcidriver_kmem_entry_t, list);

		if (entry->id == id) {
			result = entry;
			break;
		}
	}

	spin_unlock(&(privdata->kmemlist_lock));
	return result;
}

/**
 *
 * find the corresponding kmem_entry for the given use and item.
 *
 */
pcidriver_kmem_entry_t *pcidriver_kmem_find_entry_use(pcidriver_privdata_t *privdata, unsigned long use, unsigned long item)
{
	struct list_head *ptr;
	pcidriver_kmem_entry_t *entry, *result = NULL;

	spin_lock(&(privdata->kmemlist_lock));
	list_for_each(ptr, &(privdata->kmem_list)) {
		entry = list_entry(ptr, pcidriver_kmem_entry_t, list);

		if ((entry->use == use)&&(entry->item == item)&&(entry->mode&KMEM_MODE_REUSABLE)) {
			result = entry;
			break;
		}
	}

	spin_unlock(&(privdata->kmemlist_lock));
	return result;
}


void pcidriver_kmem_mmap_close(struct vm_area_struct *vma) {
    unsigned long vma_size;
    pcidriver_kmem_entry_t *kmem_entry = (pcidriver_kmem_entry_t*)vma->vm_private_data;
    if (kmem_entry) {
/*
	if (kmem_entry->id == 0) {
	    mod_info("refs: %p %p %lx\n", vma, vma->vm_private_data, kmem_entry->refs);
	    mod_info("kmem_size: %lu vma_size: %lu, s: %lx, e: %lx\n", kmem_entry->size, (vma->vm_end - vma->vm_start), vma->vm_start, vma->vm_end);
	}
*/

	vma_size = (vma->vm_end - vma->vm_start);
	
	if (kmem_entry->refs&KMEM_REF_COUNT) {
	    kmem_entry->refs -= vma_size / PAGE_SIZE;
	}
    }
}

static struct vm_operations_struct pcidriver_kmem_mmap_ops = {
    .close = pcidriver_kmem_mmap_close
};

/**
 *
 * mmap() kernel memory to userspace.
 *
 */
int pcidriver_mmap_kmem(pcidriver_privdata_t *privdata, struct vm_area_struct *vma)
{
	unsigned long vma_size;
	pcidriver_kmem_entry_t *kmem_entry;
	int ret;

	mod_info_dbg("Entering mmap_kmem\n");

	/* FIXME: Is this really right? Always just the latest one? Can't we identify one? */
	/* Get latest entry on the kmem_list */
	kmem_entry = pcidriver_kmem_find_entry_id(privdata, privdata->kmem_cur_id);
	if (!kmem_entry) {
		mod_info("Trying to mmap a kernel memory buffer without creating it first!\n");
		return -EFAULT;
	}

	mod_info_dbg("Got kmem_entry with id: %d\n", kmem_entry->id);

	/* Check sizes */
	vma_size = (vma->vm_end - vma->vm_start);
	
	if ((vma_size > kmem_entry->size) &&
		((kmem_entry->size < PAGE_SIZE) && (vma_size != PAGE_SIZE))) {
		mod_info("kem_entry size(%lu) and vma size do not match(%lu)\n", kmem_entry->size, vma_size);
		return -EINVAL;
	}

	/* reference counting */
	if ((kmem_entry->mode&KMEM_MODE_EXCLUSIVE)&&(kmem_entry->refs&KMEM_REF_COUNT)) {
		mod_info("can't make second mmaping for exclusive kmem_entry\n");
		return -EBUSY;
	}
	if (((kmem_entry->refs&KMEM_REF_COUNT) + (vma_size / PAGE_SIZE)) > KMEM_REF_COUNT) {
		mod_info("maximal amount of references is reached by kmem_entry\n");
		return -EBUSY;
	}
	
	kmem_entry->refs += vma_size / PAGE_SIZE;

	vma->vm_flags |= (VM_RESERVED);

#ifdef pgprot_noncached
	// This is coherent memory, so it must not be cached.
	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
#endif

	mod_info_dbg("Mapping address %08lx / PFN %08lx\n",
			virt_to_phys((void*)kmem_entry->cpua),
			page_to_pfn(virt_to_page((void*)kmem_entry->cpua)));

	 if ((kmem_entry->type&PCILIB_KMEM_TYPE_MASK) == PCILIB_KMEM_TYPE_REGION) {
		ret = remap_pfn_range_compat(
					vma,
					vma->vm_start,
					kmem_entry->dma_handle,
					(vma_size < kmem_entry->size)?vma_size:kmem_entry->size,
					vma->vm_page_prot);
	 } else {
		ret = remap_pfn_range_cpua_compat(
					vma,
					vma->vm_start,
					kmem_entry->cpua,
					(vma_size < kmem_entry->size)?vma_size:kmem_entry->size,
					vma->vm_page_prot );
	}

	if (ret) {
		mod_info("kmem remap failed: %d (%lx)\n", ret,kmem_entry->cpua);
		kmem_entry->refs -= 1;
		return -EAGAIN;
	}

	vma->vm_ops = &pcidriver_kmem_mmap_ops;
	vma->vm_private_data = (void*)kmem_entry;
	
	return ret;
}