Browse Source

Finalyze the sample

Suren A. Chilingaryan 8 years ago
parent
commit
2c7360501a
5 changed files with 57 additions and 40 deletions
  1. 2 1
      Makefile
  2. 47 22
      app.c
  3. 6 7
      mod.c
  4. 0 9
      rdma.h
  5. 2 1
      sysfs.c

+ 2 - 1
Makefile

@@ -2,9 +2,10 @@ obj-m := test.o
 test-objs := mod.o dev.o sysfs.o
 
 KERNELDIR ?= /lib/modules/$(shell uname -r)/build
-INSTALLDIR ?= /lib/modules/$(shell uname -r)/kernel/extra
+INSTALLDIR ?= /lib/modules/$(shell uname -r)/extra
 PWD := $(shell pwd)
 
+KBUILD_EXTRA_SYMBOLS := ${INSTALLDIR}/pciDriver.symvers
 EXTRA_CFLAGS += -I$(M)/..
 
 default: app

+ 47 - 22
app.c

@@ -9,38 +9,23 @@
 #include <time.h>
 #include <sched.h>
 #include <fcntl.h>
+#include <errno.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/stat.h>
 
 #include <pcilib.h>
-#include "pcilib/kmem.h"
+#include <pcilib/bar.h>
+#include <pcilib/kmem.h>
 
 #define DEVICE "/dev/fpga0"
+#define BAR PCILIB_BAR0
+
 
 #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);
-
+void request(void* volatile ua) {
     int fd = open("/sys/class/test/test0/test_request",  O_RDWR);
     if (fd >= 0) {
 	size_t bytes;
@@ -60,9 +45,49 @@ int main(int argc, char *argv[]) {
 	}
 
 	close(fd);
+    } else {
+        printf("Error openning /sys/class/test/test0/test_request, errno %i\n", errno);
+    }
+}
+
+int main(int argc, char *argv[]) {
+    uint32_t i, j;
+    pcilib_t *pci;
+    pcilib_kmem_handle_t *kbuf;
+
+    void* volatile bar;
+    const pcilib_bar_info_t *bar_info;
+
+    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);
+    void* volatile ua = pcilib_kmem_get_ua(pci, kbuf);
+    uintptr_t pa = pcilib_kmem_get_pa(pci, kbuf);
+
+    printf("Kmem\n-----\n");
+    printf("User: %p\n", ua);
+    printf("HW  : 0x%lx\n", pa);
+    request(ua);
+    printf("\n");
+
+    bar = pcilib_resolve_bar_address(pci, BAR, 0);
+    if (!bar) {
+        pcilib_close(pci);
+        printf("map bar\n");
+        exit(1);
     }
+    bar_info = pcilib_get_bar_info(pci, BAR);
+
+    printf("Bar\n-----\n");
+    printf("User: %p\n", bar_info[BAR].virt_addr);
+    printf("HW  : %p\n", bar_info[BAR].phys_addr);
+    request(bar_info[BAR].virt_addr);
+    printf("\n");
 
     pcilib_free_kernel_memory(pci, kbuf, 0);
-    
     pcilib_close(pci);
 }

+ 6 - 7
mod.c

@@ -5,6 +5,7 @@
 #include <linux/cdev.h>
 #include <linux/fs.h>
 #include <linux/slab.h>
+#include <linux/semaphore.h>
 
 
 #include "mod.h"
@@ -24,8 +25,8 @@ module_param(test_minors, int, S_IRUGO);
 
 
 static dev_t test_devno;			/**< major number */
-static test_dev_t **test_devs = NULL;	/**< per-device context */
-static spinlock_t test_devs_lock;	/**< lock protecting creation/destruction of devices */
+static test_dev_t **test_devs = NULL;	        /**< per-device context */
+static DEFINE_SEMAPHORE(test_devs_mtx);            /**< mutex protecting creation/destruction of devices */
 static struct class *test_class;		/**< device class */
 
 static void test_module_destroy_cdev(test_dev_t *test)
@@ -55,7 +56,7 @@ static int test_module_setup_cdev(void)
     test->cdev.owner = THIS_MODULE;
     test->cdev.ops = test_get_fops();
 
-    spin_lock(&test_devs_lock);
+    down(&test_devs_mtx);
     for (i = 0; i < test_minors; i++) {
 	if (!test_devs[i])
 	    break;
@@ -84,7 +85,7 @@ static int test_module_setup_cdev(void)
     dev_set_drvdata(test->dev, test);
 
     test_devs[i] = test;
-    spin_unlock(&test_devs_lock);
+    up(&test_devs_mtx);
 
     err = test_sysfs_init(test);
     if (err) goto fail_sysfs;
@@ -94,7 +95,7 @@ static int test_module_setup_cdev(void)
     return 0;
 
 fail:
-    spin_unlock(&test_devs_lock);
+    up(&test_devs_mtx);
 fail_sysfs:
     test_module_destroy_cdev(test);
     return err;
@@ -124,8 +125,6 @@ static int __init test_module_init(void)
 {
     int err;
 
-    spin_lock_init(&test_devs_lock);
-
     if ((err = alloc_chrdev_region(&test_devno, 0, test_minors, TEST_NODE))) {
 	mod_info("Couldn't allocate chrdev region. Module not loaded.\n");
 	goto alloc_chrdev_fail;

+ 0 - 9
rdma.h

@@ -1,9 +0,0 @@
-#ifndef _PCIDRIVER_RDMA_H
-#define _PCIDRIVER_RDMA_H
-
-#include <linux/mm.h>
-#include <linux/pagemap.h>
-
-extern unsigned long pcidriver_resolve_bar(unsigned long address);
-
-#endif /* _PCIDRIVER_RDMA_H */

+ 2 - 1
sysfs.c

@@ -7,11 +7,12 @@
 #include <linux/slab.h>
 #include <linux/sysfs.h>
 
+#include <linux/pcidriver.h>
+
 #include "mod.h"
 #include "dev.h"
 #include "debug.h"
 
-#include "rdma.h"
 
 static ssize_t test_request_show(struct device *dev, struct device_attribute *attr, char *buf) {
     test_dev_t *test = dev_get_drvdata(dev);