summaryrefslogtreecommitdiffstats
path: root/pcilib/locking.c
blob: cbe953376ae470d9303250ecce8779761a8afb8d (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
#define _XOPEN_SOURCE 700

#include "error.h"
#include "pci.h"
#include <sys/file.h>
#include <string.h>
#include "lock.h"

/*
 * this function destroys all locks without touching at kernel memory directly
 */
void pcilib_clean_all_locks(pcilib_t* ctx){
int i,j;
void* addr;
	i=0;
	/* iteration on all locks*/
	while(i<PCILIB_NUMBER_OF_LOCK_PAGES){
addr=pcilib_kmem_get_block_ua(ctx,ctx->locks_handle,i);
for(j=0;j<PCILIB_LOCKS_PER_PAGE;j++)
	  pcilib_free_lock((pcilib_lock_t*)(addr+i*PCILIB_LOCK_SIZE));
		i++;
	}

}

/*
 * this functions destroy all locks and then free the kernel memory allocated for them
 */
void pcilib_free_locking(pcilib_t* ctx){
  pcilib_clean_all_locks(ctx);
  pcilib_free_kernel_memory(ctx,ctx->locks_handle,PCILIB_KMEM_FLAG_REUSE);
}

/*
 * this function allocates the kernel memory for the locks for software registers
 */
int pcilib_init_locking(pcilib_t* ctx, ...){
/*for future possible more args
	va_list pa;
	va_start(pa,ctx);*/
	pcilib_kmem_handle_t *handle;
	int err;
	pcilib_kmem_reuse_state_t reused;
	
	/* we flock() to make sure to not have two initialization in the same time (possible long time to init)*/
	if((err=flock(ctx->handle,LOCK_EX))==-1) pcilib_warning("can't get flock on /dev/fpga0");

	handle=pcilib_alloc_kernel_memory(ctx,PCILIB_KMEM_TYPE_PAGE,PCILIB_NUMBER_OF_LOCK_PAGES,PCILIB_KMEM_PAGE_SIZE,0,PCILIB_KMEM_USE(PCILIB_KMEM_USE_MUTEXES,0),PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_PERSISTENT);

	if (!handle) {
         pcilib_error("Allocation of kernel memory for mutexes has failed");
         return 1;
    }

	ctx->locks_handle=handle;
        reused = pcilib_kmem_is_reused(ctx, handle);
//#define DEBUG_REUSE
#ifdef DEBUG_REUSE
reused=0;
#endif
/* verification about the handling memory got, first use or not, and integrity*/
	if ((reused & PCILIB_KMEM_REUSE_REUSED) == 0) {
	    pcilib_register_t i;

	    if (reused & PCILIB_KMEM_REUSE_PARTIAL) {
		pcilib_error("Inconsistent memory for locks was found (only part of required buffers is available)");
	        pcilib_clean_all_locks(ctx);
		return 1;
	    }
	    /* if we get here so this is the first initialization (after some free or new), we so set kernel pages to 0 and initialize then the first lock that will be used when we create other locks*/
            for(i=0;i<PCILIB_NUMBER_OF_LOCK_PAGES;i++){
                memset(pcilib_kmem_get_block_ua(ctx,handle,i),0,PCILIB_KMEM_PAGE_SIZE);
            }
            pcilib_init_lock(ctx,PCILIB_NO_LOCK,"%s","pcilib_first_locking");
	
	}
	    
	if((err=flock(ctx->handle,LOCK_UN))==-1) pcilib_warning("could not remove lock correctly on /dev/fpga0");

	return 0;
}