diff options
-rw-r--r-- | apps/lorenzo_ipedma_test.c | 252 | ||||
-rw-r--r-- | apps/xilinx.c | 2 | ||||
-rw-r--r-- | pcilib/CMakeLists.txt | 6 | ||||
-rw-r--r-- | pcilib/bar.c | 197 | ||||
-rw-r--r-- | pcilib/bar.h | 7 | ||||
-rw-r--r-- | pcilib/fifo.c | 55 | ||||
-rw-r--r-- | pcilib/fifo.h | 4 | ||||
-rw-r--r-- | pcilib/pci.c | 216 | ||||
-rw-r--r-- | pcilib/pci.h | 1 |
9 files changed, 354 insertions, 386 deletions
diff --git a/apps/lorenzo_ipedma_test.c b/apps/lorenzo_ipedma_test.c index c637b15..2b97057 100644 --- a/apps/lorenzo_ipedma_test.c +++ b/apps/lorenzo_ipedma_test.c @@ -13,10 +13,9 @@ #include <sched.h> #include <errno.h> -#include "pcilib.h" -#include "irq.h" -#include "kmem.h" -#include "pci.h" +#include <pcilib.h> +#include <pcilib/irq.h> +#include <pcilib/kmem.h> //#include <sys/ipc.h> //#include <sys/shm.h> @@ -35,25 +34,22 @@ // if set to 0, the update only happens when INT is received #define HUGE_PAGE 1 // number of pages per huge page -#define TLP_SIZE 64 // TLP SIZE = 64 for 256B payload, 32 for 128B payload +#define TLP_SIZE 32 // TLP SIZE = 64 for 256B payload, 32 for 128B payload #define PAGE_SIZE 4096 // other values are not supported in the kernel -#define USE_64 // Lorenzo: use 64bit addressing +#define USE_64 // Lorenzo: use 64bit addressing //#define DUAL_CORE // Lorenzo: DUAL Core //#define SHARED_MEMORY // Lorenzo: Test for fast GUI -#define CHECK_READY // Lorenzo: Check if PCI-Express is ready by reading 0x0 -#define CHECK_RESULTS // Lorenzo: Check if data received is ok (only for counter!) -//#define PRINT_RESULTS // Lorenzo: Save the received data in "data.out" +#define CHECK_READY // Lorenzo: Check if PCI-Express is ready by reading 0x0 +#define MEM_COPY // Lorenzo: CPY data +//#define CHECK_RESULTS // Lorenzo: Check if data received is ok (only for counter!) +//#define CHECK_RESULTS_LOG // Lorenzo: Check if data received is ok (only for counter!) +#define PRINT_RESULTS // Lorenzo: Save the received data in "data.out" //#define EXIT_ON_EMPTY // Lorenzo: Exit if an "empty_detected" signal is received -//#define HEB // Lorenzo: Testing HEB -//#define SWITCH_GENERATOR // Lorenzo: Testing HEB -> Turn data gen on/off - -//#define TEST_DDR // Lorenzo: Testing DDR - #define TIMEOUT 1000000 @@ -69,8 +65,6 @@ #define FPGA_CLOCK 250 // Lorenzo: in MHz ! - - //#define WR(addr, value) { val = value; pcilib_write(pci, BAR, addr, sizeof(val), &val); } //#define RD(addr, value) { pcilib_read(pci, BAR, addr, sizeof(val), &val); value = val; } #define WR(addr, value) { *(uint32_t*)(bar + addr + offset) = value; } @@ -140,14 +134,13 @@ int main() { int err; - int i, j, k; - int mem_diff; + long i, j; pcilib_t *pci; pcilib_kmem_handle_t *kdesc; pcilib_kmem_handle_t *kbuf; struct timeval start, end; size_t run_time; - size_t size_mb; + long long int size_mb; void* volatile bar; uintptr_t bus_addr[BUFFERS]; uintptr_t kdesc_bus; @@ -158,13 +151,18 @@ int main() { #ifdef SWITCH_GENERATOR int switch_generator = 0; #endif /* SWITCH_GENERATOR */ - +#if defined(CHECK_RESULTS)||defined(CHECK_RESULTS_LOG) + long k; + int mem_diff; +#endif /* CHECK_RESULTS */ + + float performance, perf_counter; pcilib_bar_t bar_tmp = BAR; uintptr_t offset = 0; unsigned int temp; - unsigned iterations_completed, buffers_filled; + int iterations_completed, buffers_filled; // int shmid; @@ -172,9 +170,10 @@ int main() { printf("\n\n**** **** **** KIT-DMA TEST **** **** ****\n\n"); + //size = ITERATIONS * BUFFERS * HUGE_PAGE * PAGE_SIZE; size_mb = ITERATIONS * BUFFERS * HUGE_PAGE * 4 / 1024; printf("Total size of memory buffer: \t %.3lf GBytes\n", (float)size_mb/1024 ); - printf("Using %u Buffers with %u iterations\n\n", BUFFERS, ITERATIONS ); + printf("Using %d Buffers with %d iterations\n\n", BUFFERS, ITERATIONS ); #ifdef ADD_DELAYS long rpt = 0, rpt2 = 0; @@ -187,7 +186,7 @@ int main() { pcilib_kmem_flags_t free_flags = PCILIB_KMEM_FLAG_HARDWARE/*|PCILIB_KMEM_FLAG_EXCLUSIVE|PCILIB_KMEM_FLAG_REUSE*/; pcilib_kmem_flags_t clean_flags = PCILIB_KMEM_FLAG_HARDWARE|PCILIB_KMEM_FLAG_PERSISTENT|PCILIB_KMEM_FLAG_EXCLUSIVE; - pci = pcilib_open(DEVICE, PCILIB_MODEL_DETECT); + pci = pcilib_open(DEVICE, "pci"); if (!pci) fail("pcilib_open"); bar = pcilib_map_bar(pci, BAR); @@ -239,7 +238,7 @@ int main() { // Pointers for Virtualized Mem for (j = 0; j < BUFFERS; j++) { ptr[j] = (volatile uint32_t*)pcilib_kmem_get_block_ua(pci, kbuf, j); - memset((void*)ptr[j], 0, HUGE_PAGE * PAGE_SIZE); + memset((void*)(ptr[j]), 0, HUGE_PAGE * PAGE_SIZE); } err = 0; @@ -271,7 +270,8 @@ int main() { FILE * Output; FILE * error_log; -#ifdef CHECK_RESULTS +#ifdef MEM_COPY + uint32_t *temp_data[ITERATIONS][BUFFERS]; for (j=0; j < ITERATIONS; j++) { @@ -314,10 +314,6 @@ int main() { error_log = fopen ("error_log.txt", "w"); fclose(error_log); - // ************************************* - Output = fopen("data.txt", "w"); - fclose(Output); - // ****************************************************************** // **** PCIe TEST ***** // ****************************************************************** @@ -345,13 +341,6 @@ int main() { // **** DMA CONFIGURATION ***** // ****************************************************************** - - printf("* DMA: Start Data Generator...\n"); - WR(0x04, 0x10) // Start data generator - - printf("* DMA: Send Data Fill Pattern 55aa55aa\n"); - WR(0x14, 0xbeef); - printf("* DMA: Send Data Amount\n"); #ifdef DUAL_CORE WR(0x10, (HUGE_PAGE * (PAGE_SIZE / (4 * TLP_SIZE)))/2); @@ -360,6 +349,7 @@ int main() { #endif printf("* DMA: Running mode: "); + #ifdef USE_64 if (TLP_SIZE == 64) { @@ -383,18 +373,18 @@ int main() { printf ("32bit - 128B Payload\n"); } #endif - + printf("* DMA: Reset Desc Memory...\n"); WR(0x5C, 0x00); // RST Desc Memory - //printf("Writing SW Read Descriptor\n"); + printf("Writing SW Read Descriptor\n"); WR(0x58, BUFFERS-1); //WR(0x58, 0x01); - //printf("Writing the Descriptor Threshold\n"); + printf("Writing the Descriptor Threshold\n"); WR(0x60, DESC_THRESHOLD); - //printf("Writing HW write Descriptor Address: %lx\n", kdesc_bus); + printf("Writing HW write Descriptor Address: %lx\n", kdesc_bus); WR(0x54, kdesc_bus); usleep(100000); @@ -403,109 +393,11 @@ int main() { bus_addr[j] = pcilib_kmem_get_block_ba(pci, kbuf, j); // LEAVE THIS DELAY???!?!?!?! usleep(1000); - printf("Writing descriptor num. %d: \t %08lx \r", j, bus_addr[j]); + //printf("Writing descriptor num. %ld: \t %08lx \n", j, bus_addr[j]); WR(0x50, bus_addr[j]); } // ****************************************************************** - // **** HEB CONFIGURATION ***** - // ****************************************************************** -#ifdef HEB - - - printf("* DDR REGISTERS: AXI_BUF_SIZE \n"); - WR(0x9130, 0x1000); - - usleep(100000); - - printf("* HEB: Control \n"); - WR(0x9040, 0x00000001); - - usleep(100000); - - printf("* HEB: Control \n"); - WR(0x9040, 0x00000004); - - usleep(100000); - - printf("* HEB: Control \n"); - WR(0x9040, 0x00000000); - - usleep(100000); - - printf("* HEB: Writing Total Orbit Num\n"); - WR(0x9020, 0x2000); - - printf("* HEB: Orbit Skip Num h9028\n"); - WR(0x9028, 0x4); - - //printf("* HEB: LVDS_DELAY h9080\n"); - //WR(0x9080, 0x10101010); - - //printf("* HEB: Delay ADCs \n"); - //WR(0x9088, 0x001); - //WR(0x9090, 0x001); - //WR(0x9094, 0x001); - //WR(0x9098, 0x001); - - //printf("* HEB: Delay TH \n"); - //WR(0x90a0, 0x005); - - //printf("* HEB: Delay_FPGA_reg \n"); - //WR(0x90a8, 0x006); - - //printf("* HEB: Control \n"); - //WR(0x9040, 0x40000000); - - //usleep(1000000); - - printf("* HEB: Control \n"); - WR(0x9040, 0x40000bf0); - - usleep(100000); - - printf("* HEB: Control \n"); - WR(0x9040, 0x400003f0); - - usleep(100000); - - printf("* HEB: Control \n"); - WR(0x9040, 0x480007F0); - - usleep(100000); - - printf("* HEB: Control \n"); - WR(0x9040, 0x48000FF0); - - -#endif - - // ****************************************************************** - // **** TEST DDR conf ***** - // ****************************************************************** -#ifdef TEST_DDR - - - printf("* DDR: AXI_BUF_SIZE_ADDR: 4k\n"); - WR(0x9010, 0x04000); - - printf("* DDR: Control \n"); - WR(0x9000, 0x000000F); - - usleep(100000); - WR(0x9000, 0x00000008); - usleep(100000); - WR(0x9000, 0x08000008); - - usleep(50000); - - printf("* DDR: Control \n"); - WR(0x9000, 0x08000208); - - -#endif - - // ****************************************************************** // **** START DMA ***** // ****************************************************************** @@ -513,7 +405,7 @@ int main() { //getchar(); printf("* DMA: Start \n"); - WR(0x04, 0x1f); + WR(0x04, 0x1); gettimeofday(&start, NULL); // ****************************************************************** @@ -528,13 +420,14 @@ int main() { while (i < ITERATIONS) { j = 0; - // printf("\ndesc0: %lx", htonl(desc[0])); - // printf("\ndesc1: %lx", htonl(desc[1])); - // printf("\ndesc2: %lx", htonl(desc[2])); - // printf("\ndesc3: %lx", htonl(desc[3])); - // printf("\ndesc4: %lx", htonl(desc[4])); + //printf("\ndesc0: %lx", desc[0]); + //printf("\ndesc1: %lx", desc[1]); + //printf("\ndesc2: %lx", desc[2]); + //printf("\ndesc3: %lx", desc[3]); + //printf("\ndesc4: %lx", desc[4]); // printf("\ndesc5: %lx", htonl(desc[5])); //printf("Iteration: %li of %li \r", i+1, ITERATIONS); + //getchar(); //loadBar(i+1, ITERATIONS, ITERATIONS, 30); // printf("\nhwptr: %zu", hwptr); // printf("\ncurptr: %zu", curptr); @@ -551,11 +444,24 @@ int main() { do { pcilib_kmem_sync_block(pci, kbuf, PCILIB_KMEM_SYNC_FROMDEVICE, curbuf); -#ifdef CHECK_RESULTS +#ifdef MEM_COPY memcpy(temp_data[i][curbuf], (void*)ptr[curbuf], 4096); #endif +#ifdef CHECK_RESULTS +for (k = 0; k < 1024 ; k++) + { + mem_diff = (ptr[curbuf][k] - ptr[curbuf][k]); + //if ((mem_diff == 1) || (mem_diff == (-7)) || (k == 1023) ) + if (mem_diff == -1) + {;} + else { + //fprintf(error_log, "Error in: \t IT %li \t BUF : %li \t OFFSET: %li \t | %08x --> %08x - DIFF: %d \n", i, j, k, temp_data[i][j][k], temp_data[i][j][k+1], mem_diff); + err++; + } + } +#endif #ifdef SHARED_MEMORY - memcpy(shared_memory, (void*)ptr[curbuf], 4096); + memcpy(shared_memory, ptr[curbuf], 4096); #endif //printf("\ncurbuf: %08x", curbuf); //printf("\nbus_addr[curbuf]\n: %08x",bus_addr[curbuf]); @@ -600,9 +506,7 @@ int main() { //printf("WR %d\n", curbuf + 1); //printf("%u (%lu)\n", curbuf, j); curptr = hwptr; - } - // ****************************************************************** @@ -611,17 +515,20 @@ int main() { gettimeofday(&end, NULL); WR(0x04, 0x00); - WR(0x01, 0x00); + usleep(100); RD(0x28, perf_counter); + usleep(100); + WR(0x00, 0x01); + iterations_completed = i; buffers_filled = curbuf; - if (empty) printf("* DMA: Empty FIFO! Last iteration: %u of %u\n", i+1, ITERATIONS); + if (empty) printf("* DMA: Empty FIFO! Last iteration: %li of %i\n", i+1, ITERATIONS); printf ("* DMA: Stop\n\n"); -#ifdef CHECK_RESULTS +#ifdef MEM_COPY printf ("First value:\t %08x\n", temp_data[0][0][0]); printf ("Last value:\t %08x\n\n", temp_data[ITERATIONS-1][BUFFERS-1][(PAGE_SIZE/4)-4]); #endif @@ -634,8 +541,9 @@ int main() { run_time = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); + //size = (long long int) (( BUFFERS * (iterations_completed) + buffers_filled) * HUGE_PAGE * PAGE_SIZE); size_mb = (long long int) (( BUFFERS * (iterations_completed) + buffers_filled) * HUGE_PAGE * 4 / 1024); - printf("Performance: transfered %zu Mbytes in %zu us using %u buffers\n", (size_mb), run_time, BUFFERS); + printf("Performance: transfered %llu Mbytes in %zu us using %d buffers\n", (size_mb), run_time, BUFFERS); //printf("Buffers: \t %d \n", BUFFERS); //printf("Buf_Size: \t %d \n", PAGE_SIZE); //printf("Perf_counter: \t %f \n", perf_counter); @@ -649,7 +557,7 @@ int main() { // ****************************************************************** -#ifdef PRINT_RESULTS + #ifdef PRINT_RESULTS printf("Writing Data to HDD... \n"); for (i=0; i < iterations_completed; i++) { for (j=0; j < BUFFERS; j++) @@ -668,9 +576,9 @@ int main() { fclose(Output); } printf("Data saved in data.out. \n"); -#endif + #endif -#ifdef CHECK_RESULTS + #ifdef CHECK_RESULTS_LOG err = 0; error_log = fopen ("error_log.txt", "a"); printf("\nChecking data ...\n"); @@ -683,7 +591,7 @@ int main() { if ((mem_diff == -1) || (k == 1023) ) {;} else { - fprintf(error_log, "Error in: \t IT %u \t BUF : %u \t OFFSET: %u \t | %08x --> %08x - DIFF: %d \n", i, j, k, temp_data[i][j][k], temp_data[i][j][k+1], mem_diff); + fprintf(error_log, "Error in: \t IT %li \t BUF : %li \t OFFSET: %li \t | %08x --> %08x - DIFF: %d \n", i, j, k, temp_data[i][j][k], temp_data[i][j][k+1], mem_diff); err++; } } @@ -693,7 +601,7 @@ int main() { if (mem_diff == (1)) {;} else { - fprintf(error_log, "Error_2 in: \t IT %u \t BUF : %u \t OFFSET: %u \t | %08x --> %08x - DIFF: %d \n", i, j, k, temp_data[i][j+1][0], temp_data[i][j][1023], mem_diff); + fprintf(error_log, "Error_2 in: \t IT %li \t BUF : %li \t OFFSET: %li \t | %08x --> %08x - DIFF: %d \n", i, j, k, temp_data[i][j+1][0], temp_data[i][j][1023], mem_diff); err++; } } @@ -702,22 +610,23 @@ int main() { loadBar(i+1, ITERATIONS, ITERATIONS, 30); } for (j = 0; j < buffers_filled; j++) { - for (k = 0; k < 1024 ; k++) { + for (k = 0; k < 1024 ; k++) + { mem_diff = ((uint32_t)temp_data[iterations_completed][j][k] - (uint32_t)temp_data[iterations_completed][j][k+1]); - if ((mem_diff == -1) || (k == 1023) ) { - ; - } else { - fprintf(error_log, "Error in: \t IT %u \t BUF : %u \t OFFSET: %u \t | %08x --> %08x - DIFF: %d \n", iterations_completed, j, k, temp_data[iterations_completed][j][k], temp_data[iterations_completed][j][k+1], mem_diff); + if ((mem_diff == -1) || (k == 1023) ) + {;} + else { + fprintf(error_log, "Error in: \t IT %li \t BUF : %li \t OFFSET: %li \t | %08x --> %08x - DIFF: %d \n", iterations_completed, j, k, temp_data[iterations_completed][j][k], temp_data[iterations_completed][j][k+1], mem_diff); err++; } } - if (j < (buffers_filled-1)) { + if (j != buffers_filled-1) { // Check first and Last mem_diff = (uint32_t)(temp_data[i][j+1][0] - temp_data[i][j][1023]); - if (mem_diff == (1)) { - ; - } else { - fprintf(error_log, "Error_2 in: \t IT %u \t BUF : %u \t OFFSET: %u \t | %08x --> %08x - DIFF: %d \n", iterations_completed, j, k, temp_data[iterations_completed][j+1][0], temp_data[iterations_completed][j][1023], mem_diff); + if (mem_diff == (1)) + {;} + else { + fprintf(error_log, "Error_2 in: \t IT %li \t BUF : %li \t OFFSET: %li \t | %08x --> %08x - DIFF: %d \n", iterations_completed, j, k, temp_data[iterations_completed][j+1][0], temp_data[iterations_completed][j][1023], mem_diff); err++; } } @@ -725,18 +634,17 @@ int main() { if (err != 0) printf("\rChecking data: \xE2\x9C\x98 %d errors found \n See \"error_log.txt\" for details \n\n", err); else printf("\rChecking data: \xE2\x9C\x93 no errors found \n\n"); fclose(error_log); -#endif - + #endif // *********** Free Memory -#ifdef CHECK_RESULTS +#ifdef MEM_COPY for (i=0; i < ITERATIONS; i++) { for (j=0; j < BUFFERS; j++) { free(temp_data[i][j]); } } -#endif /* CHECK_RESULTS */ +#endif pcilib_free_kernel_memory(pci, kbuf, free_flags); pcilib_free_kernel_memory(pci, kdesc, free_flags); diff --git a/apps/xilinx.c b/apps/xilinx.c index 4c4d3c6..0fe146b 100644 --- a/apps/xilinx.c +++ b/apps/xilinx.c @@ -12,7 +12,7 @@ #include "pcilib.h" #include "irq.h" #include "kmem.h" -#include "pci.h" +#include "bar.h" #define DEVICE "/dev/fpga0" #define BAR PCILIB_BAR0 diff --git a/pcilib/CMakeLists.txt b/pcilib/CMakeLists.txt index eb86762..bb0abfd 100644 --- a/pcilib/CMakeLists.txt +++ b/pcilib/CMakeLists.txt @@ -3,8 +3,8 @@ include_directories( ${CMAKE_SOURCE_DIR}/pcilib ) -set(HEADERS pcilib.h pci.h export.h model.h bank.h register.h kmem.h irq.h dma.h event.h plugin.h tools.h error.h debug.h version.h config.h) -add_library(pcilib SHARED pci.c export.c model.c bank.c register.c kmem.c irq.c dma.c event.c plugin.c tools.c error.c debug.c) +set(HEADERS pcilib.h pci.h export.h bar.h fifo.h model.h bank.h register.h kmem.h irq.h dma.h event.h plugin.h tools.h error.h debug.h version.h config.h) +add_library(pcilib SHARED pci.c export.c bar.c fifo.c model.c bank.c register.c kmem.c irq.c dma.c event.c plugin.c tools.c error.c debug.c) target_link_libraries(pcilib dma protocols ${CMAKE_THREAD_LIBS_INIT} ${UFODECODE_LIBRARIES} ${CMAKE_DL_LIBS}) add_dependencies(pcilib dma protocols) @@ -16,6 +16,6 @@ install(FILES pcilib.h DESTINATION include ) -install(FILES bank.h register.h dma.h event.h model.h error.h debug.h tools.h export.h version.h +install(FILES bar.h kmem.h bank.h register.h dma.h event.h model.h error.h debug.h tools.h export.h version.h DESTINATION include/pcilib ) diff --git a/pcilib/bar.c b/pcilib/bar.c new file mode 100644 index 0000000..a2670c0 --- /dev/null +++ b/pcilib/bar.c @@ -0,0 +1,197 @@ +#define _BSD_SOURCE +#define _POSIX_C_SOURCE 200809L + +#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 <arpa/inet.h> +#include <errno.h> +#include <assert.h> + +#include "pcilib.h" +#include "pci.h" +#include "tools.h" +#include "error.h" +#include "model.h" +#include "plugin.h" + +pcilib_bar_t pcilib_detect_bar(pcilib_t *ctx, uintptr_t addr, size_t size) { + int n = 0; + pcilib_bar_t i; + + const pcilib_board_info_t *board_info = pcilib_get_board_info(ctx); + if (!board_info) return PCILIB_BAR_INVALID; + + for (i = 0; i < PCILIB_MAX_BARS; i++) { + if (board_info->bar_length[i] > 0) { + if ((addr >= board_info->bar_start[i])&&((board_info->bar_start[i] + board_info->bar_length[i]) >= (addr + size))) return i; + + if (n) n = -1; + else n = i + 1; + } + } + + if (n > 0) return n - 1; + + return PCILIB_BAR_INVALID; +} + +int pcilib_detect_address(pcilib_t *ctx, pcilib_bar_t *bar, uintptr_t *addr, size_t size) { + const pcilib_board_info_t *board_info = pcilib_get_board_info(ctx); + if (!board_info) return PCILIB_ERROR_NOTFOUND; + + if (*bar == PCILIB_BAR_DETECT) { + *bar = pcilib_detect_bar(ctx, *addr, size); + if (*bar == PCILIB_BAR_INVALID) { + pcilib_error("The requested data block at address 0x%x with size %zu does not belongs to any available memory bar", *addr, size); + return PCILIB_ERROR_NOTFOUND; + } + if (*addr < board_info->bar_start[*bar]) + *addr += board_info->bar_start[*bar]; + } else { + if ((*addr < board_info->bar_start[*bar])||((board_info->bar_start[*bar] + board_info->bar_length[*bar]) < (((uintptr_t)*addr) + size))) { + if ((board_info->bar_length[*bar]) >= (((uintptr_t)*addr) + size)) { + *addr += board_info->bar_start[*bar]; + } else { + pcilib_error("The requested data block at address 0x%x with size %zu does not belong the specified memory bar (Bar %i: starting at 0x%x with size 0x%x)", *addr, size, *bar, board_info->bar_start[*bar], board_info->bar_length[*bar]); + return PCILIB_ERROR_NOTFOUND; + } + } + } + + *addr -= board_info->bar_start[*bar]; + *addr += board_info->bar_start[*bar] & ctx->page_mask; + + return 0; +} + +void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar) { + void *res; + int ret; + + const pcilib_board_info_t *board_info = pcilib_get_board_info(ctx); + if (!board_info) return NULL; + + if (ctx->bar_space[bar]) return ctx->bar_space[bar]; + + ret = ioctl( ctx->handle, PCIDRIVER_IOC_MMAP_MODE, PCIDRIVER_MMAP_PCI ); + if (ret) { + pcilib_error("PCIDRIVER_IOC_MMAP_MODE ioctl have failed", bar); + return NULL; + } + + ret = ioctl( ctx->handle, PCIDRIVER_IOC_MMAP_AREA, PCIDRIVER_BAR0 + bar ); + if (ret) { + pcilib_error("PCIDRIVER_IOC_MMAP_AREA ioctl have failed for bank %i", bar); + return NULL; + } + +#ifdef PCILIB_FILE_IO + file_io_handle = open("/root/data", O_RDWR); + res = mmap( 0, board_info->bar_length[bar], PROT_WRITE | PROT_READ, MAP_SHARED, ctx->file_io_handle, 0 ); +#else + res = mmap( 0, board_info->bar_length[bar], PROT_WRITE | PROT_READ, MAP_SHARED, ctx->handle, 0 ); +#endif + if ((!res)||(res == MAP_FAILED)) { + pcilib_error("Failed to mmap data bank %i", bar); + return NULL; + } + + + return res; +} + +void pcilib_unmap_bar(pcilib_t *ctx, pcilib_bar_t bar, void *data) { + const pcilib_board_info_t *board_info = pcilib_get_board_info(ctx); + if (!board_info) return; + + if (ctx->bar_space[bar]) return; + + munmap(data, board_info->bar_length[bar]); +#ifdef PCILIB_FILE_IO + close(ctx->file_io_handle); +#endif +} + +int pcilib_map_register_space(pcilib_t *ctx) { + int err; + pcilib_register_bank_t i; + + if (!ctx->reg_bar_mapped) { + const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); + const pcilib_register_bank_description_t *banks = model_info->banks; + + for (i = 0; ((banks)&&(banks[i].access)); i++) { +// uint32_t buf[2]; + void *reg_space; + pcilib_bar_t bar = banks[i].bar; + + if (bar == PCILIB_BAR_DETECT) { + uintptr_t addr = banks[0].read_addr; + + err = pcilib_detect_address(ctx, &bar, &addr, 1); + if (err) return err; + + if (!ctx->bar_space[bar]) { + reg_space = pcilib_map_bar(ctx, bar); +// pcilib_memcpy(&buf, reg_space, 8); + + if (reg_space) { + ctx->bar_space[bar] = reg_space; + } else { + return PCILIB_ERROR_FAILED; + } + } + } else if (!ctx->bar_space[bar]) { + reg_space = pcilib_map_bar(ctx, bar); + if (reg_space) { + ctx->bar_space[bar] = reg_space; + } else { + return PCILIB_ERROR_FAILED; + } +// pcilib_memcpy(&buf, reg_space, 8); + + } + + if (!i) ctx->reg_bar = bar; + } + + + ctx->reg_bar_mapped = 1; + } + + return 0; +} + +int pcilib_read(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf) { + void *data; + + pcilib_detect_address(ctx, &bar, &addr, size); + data = pcilib_map_bar(ctx, bar); + + pcilib_memcpy(buf, data + addr, size); + + pcilib_unmap_bar(ctx, bar, data); + + return 0; +} + +int pcilib_write(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf) { + void *data; + + pcilib_detect_address(ctx, &bar, &addr, size); + data = pcilib_map_bar(ctx, bar); + + pcilib_memcpy(data + addr, buf, size); + + pcilib_unmap_bar(ctx, bar, data); + + return 0; +} + diff --git a/pcilib/bar.h b/pcilib/bar.h new file mode 100644 index 0000000..9680fc1 --- /dev/null +++ b/pcilib/bar.h @@ -0,0 +1,7 @@ +#ifndef _PCILIB_BAR_H +#define _PCILIB_BAR_H + +pcilib_bar_t pcilib_detect_bar(pcilib_t *ctx, uintptr_t addr, size_t size); +int pcilib_detect_address(pcilib_t *ctx, pcilib_bar_t *bar, uintptr_t *addr, size_t size); + +#endif /* _PCILIB_BAR_H */ diff --git a/pcilib/fifo.c b/pcilib/fifo.c new file mode 100644 index 0000000..593400f --- /dev/null +++ b/pcilib/fifo.c @@ -0,0 +1,55 @@ +#define _BSD_SOURCE +#define _POSIX_C_SOURCE 200809L + +#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 <arpa/inet.h> +#include <errno.h> +#include <assert.h> + +#include "pcilib.h" +#include "pci.h" +#include "tools.h" +#include "error.h" +#include "model.h" +#include "plugin.h" +#include "bar.h" + +int pcilib_read_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf) { + int i; + void *data; + + pcilib_detect_address(ctx, &bar, &addr, fifo_size); + data = pcilib_map_bar(ctx, bar); + + for (i = 0; i < n; i++) { + pcilib_memcpy(buf + i * fifo_size, data + addr, fifo_size); + } + + pcilib_unmap_bar(ctx, bar, data); + + return 0; +} + +int pcilib_write_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf) { + int i; + void *data; + + pcilib_detect_address(ctx, &bar, &addr, fifo_size); + data = pcilib_map_bar(ctx, bar); + + for (i = 0; i < n; i++) { + pcilib_memcpy(data + addr, buf + i * fifo_size, fifo_size); + } + + pcilib_unmap_bar(ctx, bar, data); + + return 0; +} diff --git a/pcilib/fifo.h b/pcilib/fifo.h new file mode 100644 index 0000000..1bd5434 --- /dev/null +++ b/pcilib/fifo.h @@ -0,0 +1,4 @@ +#ifndef _PCILIB_FIFO_H +#define _PCILIB_FIFO_H + +#endif /* _PCILIB_FIFO_H */ diff --git a/pcilib/pci.c b/pcilib/pci.c index 62b3c64..a9cefda 100644 --- a/pcilib/pci.c +++ b/pcilib/pci.c @@ -21,6 +21,7 @@ #include "error.h" #include "model.h" #include "plugin.h" +#include "bar.h" static int pcilib_detect_model(pcilib_t *ctx, const char *model) { int i, j; @@ -86,8 +87,12 @@ static int pcilib_detect_model(pcilib_t *ctx, const char *model) { // how we reconcile the banks from event model and dma description? The banks specified in the DMA description should override corresponding banks of events... - if ((model)&&(!model_info)/*&&(no xml)*/) - return PCILIB_ERROR_NOTFOUND; + if (!model_info) { + if ((model)&&(strcasecmp(model, "pci"))/*&&(no xml)*/) + return PCILIB_ERROR_NOTFOUND; + + ctx->model = strdup("pci"); + } return 0; } @@ -192,155 +197,6 @@ pcilib_context_t *pcilib_get_implementation_context(pcilib_t *ctx) { return ctx->event_ctx; } - -static pcilib_bar_t pcilib_detect_bar(pcilib_t *ctx, uintptr_t addr, size_t size) { - int n = 0; - pcilib_bar_t i; - - const pcilib_board_info_t *board_info = pcilib_get_board_info(ctx); - if (!board_info) return PCILIB_BAR_INVALID; - - for (i = 0; i < PCILIB_MAX_BARS; i++) { - if (board_info->bar_length[i] > 0) { - if ((addr >= board_info->bar_start[i])&&((board_info->bar_start[i] + board_info->bar_length[i]) >= (addr + size))) return i; - - if (n) n = -1; - else n = i + 1; - } - } - - if (n > 0) return n - 1; - - return PCILIB_BAR_INVALID; -} - -int pcilib_detect_address(pcilib_t *ctx, pcilib_bar_t *bar, uintptr_t *addr, size_t size) { - const pcilib_board_info_t *board_info = pcilib_get_board_info(ctx); - if (!board_info) return PCILIB_ERROR_NOTFOUND; - - if (*bar == PCILIB_BAR_DETECT) { - *bar = pcilib_detect_bar(ctx, *addr, size); - if (*bar == PCILIB_BAR_INVALID) { - pcilib_error("The requested data block at address 0x%x with size %zu does not belongs to any available memory bank", *addr, size); - return PCILIB_ERROR_NOTFOUND; - } - if (*addr < board_info->bar_start[*bar]) - *addr += board_info->bar_start[*bar]; - } else { - if ((*addr < board_info->bar_start[*bar])||((board_info->bar_start[*bar] + board_info->bar_length[*bar]) < (((uintptr_t)*addr) + size))) { - if ((board_info->bar_length[*bar]) >= (((uintptr_t)*addr) + size)) { - *addr += board_info->bar_start[*bar]; - } else { - pcilib_error("The requested data block at address 0x%x with size %zu does not belong the specified memory bank (Bar %i: starting at 0x%x with size 0x%x)", *addr, size, *bar, board_info->bar_start[*bar], board_info->bar_length[*bar]); - return PCILIB_ERROR_NOTFOUND; - } - } - } - - *addr -= board_info->bar_start[*bar]; - *addr += board_info->bar_start[*bar] & ctx->page_mask; - - return 0; -} - -void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar) { - void *res; - int ret; - - const pcilib_board_info_t *board_info = pcilib_get_board_info(ctx); - if (!board_info) return NULL; - - if (ctx->bar_space[bar]) return ctx->bar_space[bar]; - - ret = ioctl( ctx->handle, PCIDRIVER_IOC_MMAP_MODE, PCIDRIVER_MMAP_PCI ); - if (ret) { - pcilib_error("PCIDRIVER_IOC_MMAP_MODE ioctl have failed", bar); - return NULL; - } - - ret = ioctl( ctx->handle, PCIDRIVER_IOC_MMAP_AREA, PCIDRIVER_BAR0 + bar ); - if (ret) { - pcilib_error("PCIDRIVER_IOC_MMAP_AREA ioctl have failed for bank %i", bar); - return NULL; - } - -#ifdef PCILIB_FILE_IO - file_io_handle = open("/root/data", O_RDWR); - res = mmap( 0, board_info->bar_length[bar], PROT_WRITE | PROT_READ, MAP_SHARED, ctx->file_io_handle, 0 ); -#else - res = mmap( 0, board_info->bar_length[bar], PROT_WRITE | PROT_READ, MAP_SHARED, ctx->handle, 0 ); -#endif - if ((!res)||(res == MAP_FAILED)) { - pcilib_error("Failed to mmap data bank %i", bar); - return NULL; - } - - - return res; -} - -void pcilib_unmap_bar(pcilib_t *ctx, pcilib_bar_t bar, void *data) { - const pcilib_board_info_t *board_info = pcilib_get_board_info(ctx); - if (!board_info) return; - - if (ctx->bar_space[bar]) return; - - munmap(data, board_info->bar_length[bar]); -#ifdef PCILIB_FILE_IO - close(ctx->file_io_handle); -#endif -} - -int pcilib_map_register_space(pcilib_t *ctx) { - int err; - pcilib_register_bank_t i; - - if (!ctx->reg_bar_mapped) { - const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); - const pcilib_register_bank_description_t *banks = model_info->banks; - - for (i = 0; ((banks)&&(banks[i].access)); i++) { -// uint32_t buf[2]; - void *reg_space; - pcilib_bar_t bar = banks[i].bar; - - if (bar == PCILIB_BAR_DETECT) { - uintptr_t addr = banks[0].read_addr; - - err = pcilib_detect_address(ctx, &bar, &addr, 1); - if (err) return err; - - if (!ctx->bar_space[bar]) { - reg_space = pcilib_map_bar(ctx, bar); -// pcilib_memcpy(&buf, reg_space, 8); - - if (reg_space) { - ctx->bar_space[bar] = reg_space; - } else { - return PCILIB_ERROR_FAILED; - } - } - } else if (!ctx->bar_space[bar]) { - reg_space = pcilib_map_bar(ctx, bar); - if (reg_space) { - ctx->bar_space[bar] = reg_space; - } else { - return PCILIB_ERROR_FAILED; - } -// pcilib_memcpy(&buf, reg_space, 8); - - } - - if (!i) ctx->reg_bar = bar; - } - - - ctx->reg_bar_mapped = 1; - } - - return 0; -} - int pcilib_map_data_space(pcilib_t *ctx, uintptr_t addr) { int err; pcilib_bar_t i; @@ -505,61 +361,3 @@ void pcilib_close(pcilib_t *ctx) { } } -int pcilib_read(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf) { - void *data; - - pcilib_detect_address(ctx, &bar, &addr, size); - data = pcilib_map_bar(ctx, bar); - - pcilib_memcpy(buf, data + addr, size); - - pcilib_unmap_bar(ctx, bar, data); - - return 0; -} - -int pcilib_write(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, size_t size, void *buf) { - void *data; - - pcilib_detect_address(ctx, &bar, &addr, size); - data = pcilib_map_bar(ctx, bar); - - pcilib_memcpy(data + addr, buf, size); - - pcilib_unmap_bar(ctx, bar, data); - - return 0; -} - - -int pcilib_read_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf) { - int i; - void *data; - - pcilib_detect_address(ctx, &bar, &addr, fifo_size); - data = pcilib_map_bar(ctx, bar); - - for (i = 0; i < n; i++) { - pcilib_memcpy(buf + i * fifo_size, data + addr, fifo_size); - } - - pcilib_unmap_bar(ctx, bar, data); - - return 0; -} - -int pcilib_write_fifo(pcilib_t *ctx, pcilib_bar_t bar, uintptr_t addr, uint8_t fifo_size, size_t n, void *buf) { - int i; - void *data; - - pcilib_detect_address(ctx, &bar, &addr, fifo_size); - data = pcilib_map_bar(ctx, bar); - - for (i = 0; i < n; i++) { - pcilib_memcpy(data + addr, buf + i * fifo_size, fifo_size); - } - - pcilib_unmap_bar(ctx, bar, data); - - return 0; -} diff --git a/pcilib/pci.h b/pcilib/pci.h index 0775663..a914e84 100644 --- a/pcilib/pci.h +++ b/pcilib/pci.h @@ -70,6 +70,5 @@ const pcilib_board_info_t *pcilib_get_board_info(pcilib_t *ctx); int pcilib_map_register_space(pcilib_t *ctx); int pcilib_map_data_space(pcilib_t *ctx, uintptr_t addr); -int pcilib_detect_address(pcilib_t *ctx, pcilib_bar_t *bar, uintptr_t *addr, size_t size); #endif /* _PCITOOL_PCI_H */ |