summaryrefslogtreecommitdiffstats
path: root/pcilib/mem.c
blob: d8f63915a84dd3d8943255755c5bc6ff85069764 (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
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <errno.h>
#include <assert.h>

#include "pcilib.h"
#include "locking.h"
#include "mem.h"
#include "error.h"
#include "pci.h"



void *pcilib_map_area(pcilib_t *ctx, uintptr_t addr, size_t size) {
    void *res;
    int err, ret; 

    err = pcilib_lock_global(ctx);
    if (err) {
	pcilib_error("Error (%i) acquiring mmap lock", err);
	return NULL;
    }

    ret = ioctl( ctx->handle, PCIDRIVER_IOC_MMAP_MODE, PCIDRIVER_MMAP_AREA );
    if (ret) {
	pcilib_unlock_global(ctx);
	pcilib_error("PCIDRIVER_IOC_MMAP_MODE ioctl have failed");
	return NULL;
    }

    res = mmap( 0, size, PROT_WRITE | PROT_READ, MAP_SHARED, ctx->handle, addr );

    pcilib_unlock_global(ctx);

    if ((!res)||(res == MAP_FAILED)) {
	pcilib_error("Failed to mmap area 0x%lx of size %zu bytes", addr, size);
	return NULL;
    }

    return res;
}

void pcilib_unmap_area(pcilib_t *ctx, void *addr, size_t size) {
    munmap(addr, size);
}