/alps/bar_resolve

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/bar_resolve
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
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <time.h>
#include <sched.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>

#include <pcilib.h>
#include "pcilib/kmem.h"

#define DEVICE "/dev/fpga0"

#define WR(addr, value) { *(uint32_t*)(bar + addr) = value; }
#define RD(addr, value) { value = *(uint32_t*)(bar + addr); }

int main(int argc, char *argv[]) {
    uint32_t i, j;
    pcilib_t *pci;
    pcilib_kmem_handle_t *kbuf;

    void* volatile bar;
    
    pci = pcilib_open(DEVICE, PCILIB_MODEL_DETECT);
    if (!pci) {
	printf("pcilib_open\n");
	exit(1);
    }

    kbuf = pcilib_alloc_kernel_memory(pci, PCILIB_KMEM_TYPE_PAGE, 1, 4096, 4096, 0, 0);
    volatile uint32_t *ua = pcilib_kmem_get_ua(pci, kbuf);
    uintptr_t pa = pcilib_kmem_get_pa(pci, kbuf);

    printf("User: %p\n", ua);
    printf("HW  : 0x%lx\n", pa);

    int fd = open("/sys/class/test/test0/test_request",  O_RDWR);
    if (fd >= 0) {
	size_t bytes;
	char res[64];

	sprintf(res, "%p", ua);
	write(fd, res, strlen(res));
	lseek(fd, SEEK_SET, 0);
	bytes = read(fd, res, sizeof(res) - 1); 

	printf("Res : ");
	if (bytes < 0) {
	    puts("Error");
	} else {
	    res[bytes] = 0;
	    puts(res);
	}

	close(fd);
    }

    pcilib_free_kernel_memory(pci, kbuf, 0);
    
    pcilib_close(pci);
}