summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 7fdacd0)
raw | patch | inline | side by side (parent: 7fdacd0)
author | David Lide <a0216552@gtudci01.(none)> | |
Mon, 2 Apr 2012 20:05:12 +0000 (16:05 -0400) | ||
committer | David Lide <a0216552@gtudci01.(none)> | |
Mon, 2 Apr 2012 20:05:12 +0000 (16:05 -0400) |
ti/runtime/netapi/tools/netapimod_test.c | [new file with mode: 0644] | patch | blob |
diff --git a/ti/runtime/netapi/tools/netapimod_test.c b/ti/runtime/netapi/tools/netapimod_test.c
--- /dev/null
@@ -0,0 +1,310 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
+
+#include "module/netapimod.h"
+#include "../src/netapi_util.h"
+
+#define AVOID_MMAP
+#ifdef AVOID_MMAP
+static int fd;
+#endif
+//#define __DEBUG
+
+#ifdef __DEBUG
+#define __D(fmt, args...) fprintf(stdout, "NETAPIMODTEST Debug: " fmt, ## args)
+#else
+#define __D(fmt, args...)
+#endif
+
+#define __E(fmt, args...) fprintf(stderr, "NETAPIMODTEST Error: " fmt, ## args)
+
+static int netapi_fd;
+
+int netapimod_init(void)
+{
+ netapi_fd = open("/dev/netapi", O_RDWR);
+
+ if (netapi_fd == -1) {
+ __E("init: failed to open /dev/netapi: '%s'\n", strerror(errno));
+ return -1;
+ }
+
+ __D("init: successfully open /dev/netapi.\n");
+ return 0;
+}
+
+void netapimod_close(void)
+{
+ close(netapi_fd);
+
+#ifdef AVOID_MMAP
+ close(fd);
+#endif
+}
+
+static inline unsigned long netapimod_getPhys(void)
+{
+ unsigned long physp;
+
+ if (ioctl(netapi_fd, NETAPIMOD_IOCGETPHYS | NETAPIMOD_IOCMAGIC, &physp) == -1) {
+ __E("getPhys: Failed to get physical address.\n");
+ return 0;
+ }
+
+ __D("getPhys: exiting, ioctl NETAPIMOD_IOCGETPHYS succeeded, returning %#lx\n",
+ physp);
+
+ return physp;
+}
+
+static inline unsigned long netapimod_getSize(void)
+{
+ unsigned long size;
+
+ if (ioctl(netapi_fd, NETAPIMOD_IOCGETSIZE | NETAPIMOD_IOCMAGIC, &size) == -1) {
+ __E("getSize: Failed to get size.\n");
+ return 0;
+ }
+
+ __D("getSize: exiting, ioctl NETAPIMOD_IOCGETSIZE succeeded, returning 0x%x\n", size);
+
+ return size;
+}
+
+static inline int netapimod_cacheWb(void *ptr, size_t size)
+{
+ struct netapimod_block block;
+
+ __D("cacheWb: entered w/ addr %p, size %p\n", ptr, size);
+
+ block.addr = (unsigned long)ptr;
+ block.size = size;
+
+ if (ioctl(netapi_fd, NETAPIMOD_IOCCACHEWB | NETAPIMOD_IOCMAGIC, &block) == -1) {
+ __E("cacheWb: Failed to writeback %p\n", ptr);
+
+ return -1;
+ }
+
+ __D("cacheWb: exiting, ioctl NETAPIMOD_IOCCACHEWB succeeded, returning 0\n");
+
+ return 0;
+}
+
+static inline int netapimod_cacheWbInv(void *ptr, size_t size)
+{
+ struct netapimod_block block;
+
+ __D("cacheWbInv: entered w/ addr %p, size %p\n", ptr, size);
+
+ block.addr = (unsigned long)ptr;
+ block.size = size;
+
+ if (ioctl(netapi_fd, NETAPIMOD_IOCCACHEWBINV | NETAPIMOD_IOCMAGIC, &block) == -1) {
+ __E("cacheWbInv: Failed to writeback & invalidate %p\n", ptr);
+
+ return -1;
+ }
+
+ __D("cacheWbInv: exiting, ioctl NETAPIMOD_IOCCACHEWBINV succeeded, returning 0\n");
+
+ return 0;
+}
+
+static inline int netapimod_cacheInv(void *ptr, size_t size)
+{
+ struct netapimod_block block;
+
+ __D("cacheInv: entered w/ addr %p, size %p\n", ptr, size);
+
+ block.addr = (unsigned long)ptr;
+ block.size = size;
+
+ if (ioctl(netapi_fd, NETAPIMOD_IOCCACHEINV | NETAPIMOD_IOCMAGIC, &block) == -1) {
+ __E("cacheInv: Failed to invalidate %p\n", ptr);
+
+ return -1;
+ }
+
+ __D("cacheInv: exiting, ioctl NETAPIMOD_IOCCACHEINV succeeded, returning 0\n");
+
+ return 0;
+}
+
+static inline unsigned long netapimod_mmap(void * phys, int sz)
+{
+ void *userp;
+#ifdef AVOID_MMAP
+ if( (fd = open("/dev/mem", O_RDWR )) == -1) return (unsigned long) NULL;
+ printf("/dev/mem opened.\n");
+
+ /* Map sz bytes` */
+ userp = mmap(0,sz , PROT_READ | PROT_WRITE, MAP_SHARED, fd, phys);
+ if(userp == (void *) -1) exit(1);
+ printf("Memory mapped at address %p. (cached=%d)\n", userp,1);
+
+#else
+ /* Map the physical address to user space */
+ userp = mmap(0, // Preferred start address
+ sz, // Length to be mapped
+ PROT_WRITE | PROT_READ, // Read and write access
+ MAP_SHARED, // Shared memory
+ netapi_fd, // File descriptor
+ 0); // The byte offset from fd
+#endif
+ if (userp == MAP_FAILED) {
+ __E("allocHeap: Failed to mmap.\n");
+ return 0;
+ }
+
+ __D("mmap succeeded, returning virt buffer %p\n", userp);
+
+ return (unsigned long)userp;
+}
+
+long buffer[10000];
+
+int main()
+{
+ unsigned long physp, virtp, size;
+ int i,j;
+ unsigned long v1;
+ unsigned long v2;
+ register long sum=0;
+ register long *p;
+ int c;
+ v1 = netapi_timing_start();
+ sleep(1);
+ v2 = netapi_timing_stop();
+ printf("PMU time calibrate : 1 sec = %d tics\n", v2-v1);
+
+
+ if (netapimod_init()) return -1;
+ if (!(physp = netapimod_getPhys())) return -1;
+ if (!(size = netapimod_getSize())) return -1;
+ v1 = netapi_timing_start();
+ for(i=0;i<10000;i++)
+ {
+ if (!(size = netapimod_getSize())) return -1;
+ }
+ v2 = netapi_timing_stop();
+ printf("getSize: (pure ioctl) %d cycles \n", (v2-v1)/10000 );
+ sleep(1);
+
+ if (!(virtp = netapimod_mmap(physp,size))) return 1;
+ printf("virtp = %x phys=%x sz=%d\n",virtp, physp, size) ;
+ sleep(1);
+
+ p = (long*) virtp;
+ v1 = netapi_timing_start();
+ for(i=0;i<10000;i++)
+ {
+ sum += p[i];
+ }
+ v2 = netapi_timing_stop();
+ printf("read mmap'd ddr buffer: %d cycles for size = 10000 words sum=%d\n", (v2-v1)/10000,sum);
+ sleep(1);
+
+//repeat with regular memory
+ p = (long*) &buffer[0];
+ v1 = netapi_timing_start();
+ for(i=0;i<10000;i++)
+ {
+ sum += p[i];
+ }
+ v2 = netapi_timing_stop();
+ printf("static ddr buffer: %d cycles for size = 10000 words sum=%d\n", (v2-v1)/10000,sum);
+ sleep(1);
+
+
+
+#ifdef AVOID_MMAP
+for(j=1;j<50;j++)
+{
+ v1 = netapi_timing_start();
+ for(i=0;i<10000;i++)
+ {
+ if (netapimod_cacheWb((void *)physp, 128*j)) return -1;
+ }
+ v2 = netapi_timing_stop();
+ printf("wb: %d cycles for size = %d\n", (v2-v1)/10000, 128*j);
+ sleep(1);
+ v1 = netapi_timing_start();
+ for(i=0;i<10000;i++)
+ {
+ if (netapimod_cacheInv((void *)physp, 128*j)) return -1;
+ }
+ v2 = netapi_timing_stop();
+ printf("Inv: %d cycles for size = %d\n", (v2-v1)/10000, 128*j);
+ sleep(1);
+ v1 = netapi_timing_start();
+ for(i=0;i<10000;i++)
+ {
+ if (netapimod_cacheWbInv((void *)physp, 128*j)) return -1;
+ }
+ v2 = netapi_timing_stop();
+ printf("wbInv: %d cycles for size = %d\n", (v2-v1)/10000, 128*j);
+}
+
+//stress test
+printf("stress test..\n");
+{
+volatile char *pp = (char*) physp;
+volatile char *vp = (char*) virtp;
+v1 = netapi_timing_start();
+for(i=0,c=0;c<1000000000;c++)
+{
+ memcpy(vp,(char *)&i,4);
+ memcpy(vp+64,(char *) &c,4);
+ if (netapimod_cacheWbInv((void *)pp, 128)) return -1;
+ if (!(c%0x4000)) {
+ v2 = netapi_timing_start();
+ printf("%d ; ok %d cycles per wbinv call (128 bytes) %x %x\n",c, (v2-v1)/10000, pp,vp);
+ v1 = netapi_timing_start();
+ }
+ if (i*128 > size-128){ i=0; pp = (char*) physp; vp = (char *) virtp;}
+ else {i+=128; pp+=128; vp+=128;}
+}
+}
+#else
+for(j=1;j<50;j++)
+{
+ v1 = netapi_timing_start();
+ for(i=0;i<1000;i++)
+ {
+ if (netapimod_cacheWb((void *)virtp, 128*j)) return -1;
+ }
+ v2 = netapi_timing_stop();
+ printf("wb: %d cycles for size = %d\n", (v2-v1)/1000, 128*j);
+ sleep(1);
+ v1 = netapi_timing_start();
+ for(i=0;i<1000;i++)
+ {
+ if (netapimod_cacheInv((void *)virtp, 128*j)) return -1;
+ }
+ v2 = netapi_timing_stop();
+ printf("Inv: %d cycles for size = %d\n", (v2-v1)/1000, 128*j);
+ sleep(1);
+
+ v1 = netapi_timing_start();
+ for(i=0;i<1000;i++)
+ {
+ if (netapimod_cacheWbInv((void *)virtp, 128*j)) return -1;
+ }
+ v2 = netapi_timing_stop();
+ printf("wbInv: %d cycles for size = %d\n", (v2-v1)/1000, 128*j);
+}
+#endif
+ netapimod_close();
+
+ return 0;
+}
+
+